AIToolbox
A library that offers tools for AI problem solving.
GraphUtils.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_MDP_GRAPH_UTILS_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_MDP_GRAPH_UTILS_HEADER_FILE
3 
7 
8 namespace AIToolbox::Factored::MDP {
9  // The "main" code to make graph is implemented in the Bandit namespace.
10  // This file contains an equivalent hierarchy of classes, but within the
11  // MDP namespace.
12  //
13  // The main reason why we need to reimplement MakeGraphImpl,
14  // UpdateGraphImpl, MakeGraph and UpdateGraph in this namespace is that the
15  // functors for *updating* factored MDPs have necessarily different
16  // arguments (in particular, they need the size of the state space and a
17  // specific state).
18  //
19  // This means that any code that wants to use the Make/UpdateGraph
20  // mechanism to write generic code for MDPs will necessarily pass arguments
21  // that are not compatible with the classes written in the Bandit
22  // namespace, so there is no reason to try to keep that code easily
23  // reachable from Factored::MDP.
24  //
25  // For MakeGraph we could have just kept extending the Bandit class (as we
26  // only generally need data + size of action space), but then it would be
27  // wierd to only duplicate UpdateGraph stuff -- and also because any direct
28  // extension would have needed to still be written in the Bandit namespace
29  // since specializations are only allowed in the original namespace. Thus,
30  // we duplicate MakeGraph[Impl] as well for consistency.
31  //
32  // At the same time, we try to reuse everything we can, be it with
33  // inheritance or directly calling the original function with the required
34  // subset of input parameters.
35 
39  template <typename Maximizer, typename Data>
40  struct MakeGraphImpl;
41 
45  template <typename Maximizer, typename Data>
47 
63  template <typename Maximizer>
64  struct MakeGraph {
65  template <typename Data, typename... Args>
66  auto operator()(const Data & d, Args && ...args) {
67  return MakeGraphImpl<Maximizer, Data>()(d, std::forward<Args>(args)...);
68  }
69  };
70 
84  template <typename Maximizer>
85  struct UpdateGraph {
86  template <typename Data, typename... Args>
87  void operator()(typename Maximizer::Graph & graph, const Data & d, Args && ...args) {
88  UpdateGraphImpl<Maximizer, Data>()(graph, d, std::forward<Args>(args)...);
89  }
90  };
91 
92  // ############################
93  // ### VARIABLE ELIMINATION ###
94  // ############################
95 
96  template <typename Data>
97  struct MakeGraphImpl<Bandit::VariableElimination, Data> : public Bandit::MakeGraphImpl<Bandit::VariableElimination, Data> {};
98 
99  template <MDP::QFRuleRange Iterable>
100  struct UpdateGraphImpl<Bandit::VariableElimination, Iterable> {
102 
103  void operator()(VE::Graph & graph, const Iterable & inputRules, const State &, const Action & A, const State &) {
104  Bandit::UpdateGraph<VE>()(graph, inputRules, A);
105  }
106  };
107 
108  template <>
109  struct UpdateGraphImpl<Bandit::VariableElimination, MDP::QFunction> {
111 
112  void operator()(VE::Graph & graph, const MDP::QFunction & qf, const State & S, const Action & A, const State & s) {
113  graph.reset(A.size());
114 
115  for (const auto & basis : qf.bases) {
116  const auto Ai = static_cast<size_t>(basis.values.cols());
117 
118  auto & factorNode = graph.getFactor(basis.actionTag)->getData();
119 
120  if (factorNode.empty()) {
121  factorNode.reserve(Ai);
122  for (size_t ai = 0; ai < Ai; ++ai)
123  factorNode.emplace_back(ai, VE::Factor{0.0, {}});
124  }
125 
126  const size_t si = toIndexPartial(basis.tag, S, s);
127  for (size_t ai = 0; ai < Ai; ++ai)
128  factorNode[ai].second.first += basis.values(si, ai);
129  }
130  }
131  };
132 
133  // ###################################
134  // ## LOCAL SEARCH / MAXPLUS / RILS ##
135  // ###################################
136 
137  template <MDP::QFRuleRange Iterable>
138  struct MakeGraphImpl<Bandit::LocalSearch, Iterable> : public Bandit::MakeGraphImpl<Bandit::LocalSearch, Iterable> {};
139 
140  template <>
141  struct MakeGraphImpl<Bandit::LocalSearch, MDP::QFunction> {
143  Bandit::LocalSearch::Graph graph(A.size());
144 
145  for (const auto & basis : qf.bases) {
146  auto & factorNode = graph.getFactor(basis.actionTag)->getData();
147 
148  if (!factorNode.size())
149  factorNode.resize(basis.values.cols());
150  }
151 
152  return graph;
153  }
154  };
155 
156  template <MDP::QFRuleRange Iterable>
157  struct UpdateGraphImpl<Bandit::LocalSearch, Iterable> {
159 
160  void operator()(LS::Graph & graph, const Iterable & inputRules, const State &, const Action & A, const State &) {
161  Bandit::UpdateGraph<LS>()(graph, inputRules, A);
162  }
163  };
164 
165  template <>
166  struct UpdateGraphImpl<Bandit::LocalSearch, MDP::QFunction> {
167  void operator()(Bandit::LocalSearch::Graph & graph, const MDP::QFunction & qf, const State & S, const Action &, const State & s) {
168  for (auto & f : graph)
169  f.getData().setZero();
170 
171  for (const auto & basis : qf.bases) {
172  const size_t si = toIndexPartial(basis.tag, S, s);
173  graph.getFactor(basis.actionTag)->getData() += basis.values.row(si);
174  }
175  }
176  };
177 
178  // MaxPlus and RILS both use the same graph type, so we don't need to implement anything more.
179  template <> struct MakeGraph<Bandit::MaxPlus> : MakeGraph<Bandit::LocalSearch> {};
180  template <> struct UpdateGraph<Bandit::MaxPlus> : UpdateGraph<Bandit::LocalSearch> {};
181 
182  template <> struct MakeGraph<Bandit::ReusingIterativeLocalSearch> : MakeGraph<Bandit::LocalSearch> {};
183  template <> struct UpdateGraph<Bandit::ReusingIterativeLocalSearch> : UpdateGraph<Bandit::LocalSearch> {};
184 }
185 
186 #endif
AIToolbox::Factored::FactorGraph::reset
void reset(size_t variables)
This function re-initializes the graph from scratch as if it was just being built.
Definition: FactorGraph.hpp:317
AIToolbox::Factored::MDP::MakeGraphImpl< Bandit::LocalSearch, MDP::QFunction >::operator()
Bandit::LocalSearch::Graph operator()(const MDP::QFunction &qf, const Action &A)
Definition: GraphUtils.hpp:142
AIToolbox::Factored::MDP::UpdateGraphImpl
This class clumps all implementations that update graphs with data for certain Maximizers.
Definition: GraphUtils.hpp:46
AIToolbox::Factored::Bandit::MakeGraphImpl
This class clumps all implementations that create graphs for data for certain Maximizers.
Definition: GraphUtils.hpp:40
AIToolbox::Factored::MDP::MakeGraph::operator()
auto operator()(const Data &d, Args &&...args)
Definition: GraphUtils.hpp:66
AIToolbox::Factored::MDP::MakeGraphImpl
This class clumps all implementations that create graphs for data for certain Maximizers.
Definition: GraphUtils.hpp:40
AIToolbox::Factored::MDP::UpdateGraph::operator()
void operator()(typename Maximizer::Graph &graph, const Data &d, Args &&...args)
Definition: GraphUtils.hpp:87
AIToolbox::Factored::MDP::MakeGraph
This class is the public interface for initializing the graph in generic code that uses the maximizer...
Definition: GraphUtils.hpp:64
AIToolbox::Factored::MDP
Definition: CooperativePrioritizedSweeping.hpp:13
AIToolbox::Factored::State
Factors State
Definition: Types.hpp:67
AIToolbox::Factored::FactoredMatrix2D
This class represents a factored 2D matrix.
Definition: FactoredMatrix.hpp:140
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::Factored::MDP::UpdateGraphImpl< Bandit::VariableElimination, Iterable >::operator()
void operator()(VE::Graph &graph, const Iterable &inputRules, const State &, const Action &A, const State &)
Definition: GraphUtils.hpp:103
AIToolbox::Factored::MDP::UpdateGraphImpl< Bandit::VariableElimination, MDP::QFunction >::operator()
void operator()(VE::Graph &graph, const MDP::QFunction &qf, const State &S, const Action &A, const State &s)
Definition: GraphUtils.hpp:112
AIToolbox::Factored::FactorGraph
This class offers a minimal interface to manager a factor graph.
Definition: FactorGraph.hpp:31
AIToolbox::Factored::Bandit::VariableElimination::Factor
std::pair< double, std::vector< std::pair< size_t, size_t > >> Factor
Definition: VariableElimination.hpp:46
AIToolbox::Factored::MDP::UpdateGraphImpl< Bandit::LocalSearch, MDP::QFunction >::operator()
void operator()(Bandit::LocalSearch::Graph &graph, const MDP::QFunction &qf, const State &S, const Action &, const State &s)
Definition: GraphUtils.hpp:167
AIToolbox::Factored::MDP::UpdateGraphImpl< Bandit::LocalSearch, Iterable >::operator()
void operator()(LS::Graph &graph, const Iterable &inputRules, const State &, const Action &A, const State &)
Definition: GraphUtils.hpp:160
AIToolbox::Factored::Bandit::VariableElimination
This class represents the Variable Elimination algorithm.
Definition: VariableElimination.hpp:41
GraphUtils.hpp
AIToolbox::Factored::FactorGraph::getFactor
FactorIt getFactor(const Variables &variables)
This function returns an iterator to a factor adjacent to the given variables.
Definition: FactorGraph.hpp:355
AIToolbox::Factored::Bandit::LocalSearch
This class approximately finds the best joint action using Local Search.
Definition: LocalSearch.hpp:24
AIToolbox::Factored::Action
Factors Action
Definition: Types.hpp:69
AIToolbox::Factored::MDP::UpdateGraph
This class is the public interface for updating the input graph with the input data in generic code t...
Definition: GraphUtils.hpp:85
AIToolbox::Factored::FactoredMatrix2D::bases
std::vector< BasisMatrix > bases
Definition: FactoredMatrix.hpp:195
Types.hpp
TypeTraits.hpp
AIToolbox::Factored::Bandit::UpdateGraph
This class is the public interface for updating the input graph with the input data in generic code t...
Definition: GraphUtils.hpp:85