PBerger
12 years ago
2 changed files with 104 additions and 0 deletions
@ -0,0 +1,81 @@ |
|||
/* |
|||
* reward_model.h |
|||
* |
|||
* Created on: 25.10.2012 |
|||
* Author: Philipp Berger |
|||
*/ |
|||
|
|||
#ifndef MRMC_REWARD_REWARD_MODEL_H_ |
|||
#define MRMC_REWARD_REWARD_MODEL_H_ |
|||
|
|||
#include <stdexcept> |
|||
|
|||
#include "boost/integer/integer_mask.hpp" |
|||
#include <pantheios/pantheios.hpp> |
|||
#include <pantheios/inserters/integer.hpp> |
|||
|
|||
namespace mrmc { |
|||
|
|||
namespace reward { |
|||
|
|||
/*! This class represents a single reward model with a type DataClass value for each state contained in a Vector of type VectorImpl |
|||
*/ |
|||
template<template<class, class> class VectorImpl, class DataClass> |
|||
class RewardModel { |
|||
|
|||
//! Shorthand for a constant reference to the DataClass type |
|||
typedef const DataClass& crDataClass; |
|||
|
|||
public: |
|||
RewardModel(const uint_fast32_t state_count, const DataClass& null_value) : null_value(null_value), state_count(state_count) { |
|||
|
|||
this->reward_vector = new VectorImpl<DataClass, std::allocator<DataClass>>(this->state_count); |
|||
// init everything to zero |
|||
for (uint_fast32_t i = 0; i < this->state_count; ++i) { |
|||
this->setReward(i, this->null_value); |
|||
} |
|||
} |
|||
|
|||
virtual ~RewardModel() { |
|||
delete reward_vector; |
|||
} |
|||
|
|||
bool setReward(const uint_fast32_t state_index, crDataClass reward) { |
|||
if (state_index < this->state_count) { |
|||
this->reward_vector->at(state_index) = reward; |
|||
// [state_index] = reward; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
crDataClass getReward(const uint_fast32_t state_index) { |
|||
if (state_index < this->state_count) { |
|||
return this->reward_vector->at(state_index); |
|||
} |
|||
return this->null_value; |
|||
} |
|||
|
|||
bool resetReward(const uint_fast32_t state_index) { |
|||
if (state_index < this->state_count) { |
|||
this->reward_vector[state_index] = this->null_value; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
uint_fast32_t getSize() const { |
|||
return this->state_count; |
|||
} |
|||
private: |
|||
uint_fast32_t state_count; |
|||
VectorImpl<DataClass, std::allocator<DataClass>>* reward_vector; |
|||
const DataClass& null_value; |
|||
|
|||
}; |
|||
|
|||
} //namespace reward |
|||
|
|||
} //namespace mrmc |
|||
|
|||
#endif /* MRMC_REWARD_REWARD_MODEL_H_ */ |
@ -0,0 +1,23 @@ |
|||
#include "gtest/gtest.h"
|
|||
|
|||
#include "Eigen/Sparse"
|
|||
#include "src/exceptions/invalid_argument.h"
|
|||
#include "boost/integer/integer_mask.hpp"
|
|||
#include <vector>
|
|||
|
|||
#include "reward/reward_model.h"
|
|||
|
|||
TEST(RewardModelTest, ReadWriteTest) { |
|||
// 50 entries
|
|||
mrmc::reward::RewardModel<std::vector, double> rm(50, 0.0); |
|||
|
|||
double values[50]; |
|||
for (int i = 0; i < 50; ++i) { |
|||
values[i] = 1.0 + i; |
|||
ASSERT_TRUE(rm.setReward(i, values[i])); |
|||
|
|||
ASSERT_EQ(rm.getReward(i), values[i]); |
|||
} |
|||
|
|||
} |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue