From cf4cb5dd8cf7748060907f38dc51b8f21ee26e2e Mon Sep 17 00:00:00 2001 From: PBerger Date: Mon, 19 Nov 2012 02:31:55 +0100 Subject: [PATCH] Added reward_model with test --- src/reward/reward_model.h | 81 +++++++++++++++++++++++++++++++ test/reward/reward_model_test.cpp | 23 +++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/reward/reward_model.h create mode 100644 test/reward/reward_model_test.cpp diff --git a/src/reward/reward_model.h b/src/reward/reward_model.h new file mode 100644 index 000000000..52b2039b0 --- /dev/null +++ b/src/reward/reward_model.h @@ -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 + +#include "boost/integer/integer_mask.hpp" +#include +#include + +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 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>(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>* reward_vector; + const DataClass& null_value; + +}; + +} //namespace reward + +} //namespace mrmc + +#endif /* MRMC_REWARD_REWARD_MODEL_H_ */ diff --git a/test/reward/reward_model_test.cpp b/test/reward/reward_model_test.cpp new file mode 100644 index 000000000..58a77fa1c --- /dev/null +++ b/test/reward/reward_model_test.cpp @@ -0,0 +1,23 @@ +#include "gtest/gtest.h" + +#include "Eigen/Sparse" +#include "src/exceptions/invalid_argument.h" +#include "boost/integer/integer_mask.hpp" +#include + +#include "reward/reward_model.h" + +TEST(RewardModelTest, ReadWriteTest) { + // 50 entries + mrmc::reward::RewardModel 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]); + } + +} +