AIToolbox
A library that offers tools for AI problem solving.
|
Go to the documentation of this file. 1 #ifndef AI_TOOLBOX_MDP_DYNAQ_HEADER_FILE
2 #define AI_TOOLBOX_MDP_DYNAQ_HEADER_FILE
9 #include <boost/functional/hash.hpp>
11 #include <unordered_set>
33 template <IsGenerativeModel M>
43 explicit DynaQ(
const M & m,
double alpha = 0.5,
unsigned n = 50);
64 void stepUpdateQ(
size_t s,
size_t a,
size_t s1,
double rew);
102 void setN(
unsigned n);
109 unsigned getN()
const;
132 std::unordered_set<std::pair<size_t,size_t>, boost::hash<std::pair<size_t, size_t>>> visitedStatesActionsInserter_;
133 std::vector<std::pair<size_t,size_t>> visitedStatesActionsSampler_;
139 template <IsGenerativeModel M>
141 N(n), model_(m), qLearning_(model_, alpha), rand_(
Seeder::getSeed())
143 visitedStatesActionsInserter_.reserve(model_.getS()*model_.getA());
144 visitedStatesActionsSampler_.reserve(model_.getS()*model_.getA());
147 template <IsGenerativeModel M>
149 qLearning_.stepUpdateQ(s, a, s1, rew);
151 const auto result = visitedStatesActionsInserter_.insert(std::make_pair(s,a));
152 if ( std::get<1>(result) )
153 visitedStatesActionsSampler_.push_back(*std::get<0>(result));
156 template <IsGenerativeModel M>
158 if ( ! visitedStatesActionsSampler_.size() )
return;
159 std::uniform_int_distribution<size_t> sampleDistribution_(0, visitedStatesActionsSampler_.size()-1);
161 for (
unsigned i = 0; i < N; ++i ) {
163 const auto [s,a] = visitedStatesActionsSampler_[sampleDistribution_(rand_)];
164 const auto [s1, rew] = model_.sample(s, a);
166 qLearning_.stepUpdateQ(s, a, s1, rew);
170 template <IsGenerativeModel M>
175 template <IsGenerativeModel M>
177 return qLearning_.getQFunction();
179 template <IsGenerativeModel M>
184 template <IsGenerativeModel M>
186 qLearning_.setLearningRate(a);
189 template <IsGenerativeModel M>
191 return qLearning_.getLearningRate();