AIToolbox
A library that offers tools for AI problem solving.
Model.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_BANDIT_MODEL_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_BANDIT_MODEL_HEADER_FILE
3 
7 
28  template <typename Dist>
29  class Model {
30  public:
46  template <typename... TupleArgs>
47  Model(Action A, std::vector<PartialKeys> deps, std::vector<AIToolbox::Bandit::Model<Dist>> arms);
48 
56  const Rewards & sampleR(const Action & a) const;
57 
61  const Action & getA() const;
62 
66  const std::vector<PartialKeys> & getGroups() const;
67 
71  const std::vector<AIToolbox::Bandit::Model<Dist>> & getArms() const;
72 
73  private:
74  Action A;
75  std::vector<PartialKeys> groups_;
76 
77  mutable std::vector<AIToolbox::Bandit::Model<Dist>> arms_;
78  mutable Rewards rews_;
79  };
80 
81  template <typename Dist>
82  template <typename... TupleArgs>
83  Model<Dist>::Model(Action a, std::vector<PartialKeys> deps, std::vector<AIToolbox::Bandit::Model<Dist>> arms) :
84  A(std::move(a)), groups_(std::move(deps)), arms_(std::move(arms)),
85  rews_(groups_.size())
86  {
87  // Sanity checks
88  //
89  // - The number of groups is equal to the number of local arms.
90  // - Each local arm has an action space equal to the product
91  // of its participating agents.
92  assert(groups_.size() == arms_.size());
93 
94  for (size_t i = 0; i < groups_.size(); ++i) {
95  const auto bSize = factorSpacePartial(groups_[i], A);
96  (void)bSize;
97 
98  assert(bSize == arms_[i].getA());
99  }
100  }
101 
102  template <typename Dist>
103  const Rewards & Model<Dist>::sampleR(const Action & a) const {
104  for (size_t i = 0; i < groups_.size(); ++i) {
105  const auto aid = toIndexPartial(groups_[i], A, a);
106 
107  rews_[i] = arms_[i].sampleR(aid);
108  }
109 
110  return rews_;
111  }
112 
113  template <typename Dist>
114  const Action & Model<Dist>::getA() const { return A; }
115  template <typename Dist>
116  const std::vector<PartialKeys> & Model<Dist>::getGroups() const { return groups_; }
117  template <typename Dist>
118  const std::vector<AIToolbox::Bandit::Model<Dist>> & Model<Dist>::getArms() const { return arms_; }
119 }
120 
121 #endif
AIToolbox::Factored::Bandit::Model
This class represents a factored multi-armed bandit.
Definition: Model.hpp:29
Types.hpp
AIToolbox::Factored::Rewards
Vector Rewards
Definition: Types.hpp:71
AIToolbox::Factored::toIndexPartial
size_t toIndexPartial(const PartialKeys &ids, const Factors &space, const Factors &f)
This function converts the input factor in the input space to an unique index.
AIToolbox::Bandit::Model
This class represent a multi-armed bandit.
Definition: Model.hpp:26
AIToolbox::Factored::Bandit::Model::getGroups
const std::vector< PartialKeys > & getGroups() const
This function returns a reference to the agent groupings.
Definition: Model.hpp:116
AIToolbox::Factored::Bandit::Model::Model
Model(Action A, std::vector< PartialKeys > deps, std::vector< AIToolbox::Bandit::Model< Dist >> arms)
Basic constructor.
Definition: Model.hpp:83
Model.hpp
AIToolbox::Factored::factorSpacePartial
size_t factorSpacePartial(const PartialKeys &ids, const Factors &space)
This function returns the multiplication of all elements of the input factor.
Core.hpp
AIToolbox::Factored::Bandit::Model::sampleR
const Rewards & sampleR(const Action &a) const
This function samples the specified joint bandit arm.
Definition: Model.hpp:103
AIToolbox::Factored::Bandit::Model::getA
const Action & getA() const
This function returns the joint action space.
Definition: Model.hpp:114
AIToolbox::Factored::Action
Factors Action
Definition: Types.hpp:69
AIToolbox::Factored::Bandit::Model::getArms
const std::vector< AIToolbox::Bandit::Model< Dist > > & getArms() const
This function returns a reference to the internal local arms.
Definition: Model.hpp:118
AIToolbox::Factored::Bandit
Definition: GraphUtils.hpp:12