AIToolbox
A library that offers tools for AI problem solving.
Model.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_BANDIT_MODEL_HEADER_FILE
2 #define AI_TOOLBOX_BANDIT_MODEL_HEADER_FILE
3 
4 #include <AIToolbox/Types.hpp>
5 #include <AIToolbox/Seeder.hpp>
6 
7 namespace AIToolbox::Bandit {
25  template <typename Dist>
26  class Model {
27  public:
40  template <typename... TupleArgs>
41  Model(TupleArgs... tupleArgs);
42 
54  template <typename... Args>
55  Model(std::vector<std::tuple<Args...>> args);
56 
64  decltype(auto) sampleR(size_t a) const;
65 
71  size_t getA() const;
72 
78  const std::vector<Dist> & getArms() const;
79 
80  private:
81  mutable std::vector<Dist> arms_;
82  mutable AIToolbox::RandomEngine rand_;
83  };
84 
85  template <typename Dist>
86  template <typename... TupleArgs>
87  Model<Dist>::Model(TupleArgs... tupleArgs) :
88  arms_({std::make_from_tuple<Dist>(std::move(tupleArgs))...}), rand_(AIToolbox::Seeder::getSeed())
89  {}
90 
91  template <typename Dist>
92  template <typename... Args>
93  Model<Dist>::Model(std::vector<std::tuple<Args...>> args) :
94  rand_(AIToolbox::Seeder::getSeed())
95  {
96  arms_.reserve(args.size());
97 
98  // Here we use a lambda to avoid having to static_cast the correct
99  // emplace_back method on the vector.
100  for (auto && t : args)
101  std::apply([this](auto&&... params){arms_.emplace_back(std::move(params)...);}, std::move(t));
102  }
103 
104  template <typename Dist>
105  decltype(auto) Model<Dist>::sampleR(const size_t a) const {
106  return arms_[a](rand_);
107  }
108 
109  template <typename Dist>
110  size_t Model<Dist>::getA() const { return arms_.size(); }
111 
112  template <typename Dist>
113  const std::vector<Dist> & Model<Dist>::getArms() const { return arms_; }
114 }
115 
116 #endif
AIToolbox::Bandit::Model::Model
Model(TupleArgs... tupleArgs)
Basic constructor.
Definition: Model.hpp:87
AIToolbox::Seeder::getSeed
static unsigned getSeed()
This function gets a random number to seed generators.
AIToolbox::Bandit::Model::sampleR
decltype(auto) sampleR(size_t a) const
This function samples the specified bandit arm.
AIToolbox::Seeder
This class is an internal class used to seed all random engines in the library.
Definition: Seeder.hpp:15
AIToolbox::Bandit::Model
This class represent a multi-armed bandit.
Definition: Model.hpp:26
AIToolbox::Bandit::Model::getA
size_t getA() const
This function returns the number of arms of the bandit.
Definition: Model.hpp:110
AIToolbox::Bandit
Definition: Experience.hpp:6
AIToolbox
Definition: Experience.hpp:6
Seeder.hpp
AIToolbox::RandomEngine
std::mt19937 RandomEngine
Definition: Types.hpp:14
Types.hpp
AIToolbox::Bandit::Model::getArms
const std::vector< Dist > & getArms() const
This function returns a reference to the underlying arms.
Definition: Model.hpp:113