AIToolbox
A library that offers tools for AI problem solving.
GraphUtils.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_BANDIT_GRAPH_UTILS_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_BANDIT_GRAPH_UTILS_HEADER_FILE
3 
6 
11 
13  // This file was designed to let users easily swap between different
14  // Maximizers when dealing with factored Bandit functions. With Maximizers
15  // we mean the algorithms designed to maximize over a factored function via
16  // factored graphs (VariableElimination, MaxPlus, LocalSearch, etc). A
17  // common example of this is with the QGreedyPolicy.
18  //
19  // The functors in this file implement the swapping mechanism, by providing
20  // a way to initialize the appropriate FactorGraph from a set of data
21  // structures in a somewhat generic manner.
22  //
23  // The system has been designed to be easily extensible, in the event that
24  // either this library or a user introduces a new type that needs to be
25  // maximized over.
26  //
27  // If that is the case, one only needs to specialize the
28  // Make/UpdateGraphImpl classes for the needed Maximizers and data types,
29  // and no other code will need to change. The specializations will need to
30  // provide an operator() with the appropriate signature.
31  //
32  // Note that this system, being templatized, only allows swapping Maximizer
33  // at compile time. At the same time, it should at least allow to not
34  // having to rewrite lots of code every single time a change is desired.
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  // Since VE deletes its graph at each update, its associated MakeGraph
97  // simply doesn't do any work, and the UpdateGraphs just reconstruct it
98  // from scratch every time.
99 
100  template <typename Data>
102  typename VariableElimination::Graph operator()(const Data &, const Action &) {
103  return VariableElimination::Graph(0);
104  }
105  };
106 
107  template <QFRuleRange Iterable>
110 
111  void operator()(VE::Graph & graph, const Iterable & inputRules, const Action & A) {
112  graph.reset(A.size());
113 
114  for (const auto & rule : inputRules) {
115  auto & factorNode = graph.getFactor(rule.action.first)->getData();
116  const auto id = toIndexPartial(A, rule.action);
117 
118  const auto it = std::lower_bound(
119  std::begin(factorNode), std::end(factorNode), id,
120  [](const auto & rule, size_t rhs) {return rule.first < rhs;}
121  );
122 
123  if (it != std::end(factorNode) && it->first == id)
124  it->second.first += rule.value;
125  else
126  factorNode.emplace(it, id, VE::Factor{rule.value, {}});
127  }
128  }
129  };
130 
131  template <>
134 
135  void operator()(VE::Graph & graph, const QFunction & qf, const Action & A) {
136  graph.reset(A.size());
137 
138  for (const auto & basis : qf.bases) {
139  const auto Ai = static_cast<size_t>(basis.values.size());
140 
141  auto & factorNode = graph.getFactor(basis.tag)->getData();
142 
143  if (factorNode.empty()) {
144  factorNode.reserve(Ai);
145  for (size_t ai = 0; ai < Ai; ++ai)
146  factorNode.emplace_back(ai, VE::Factor{0.0, {}});
147  }
148 
149  for (size_t ai = 0; ai < Ai; ++ai)
150  factorNode[ai].second.first += basis.values(ai);
151  }
152  }
153  };
154 
155  // ###################################
156  // ## LOCAL SEARCH / MAXPLUS / RILS ##
157  // ###################################
158 
159  template <QFRuleRange Iterable>
160  struct MakeGraphImpl<LocalSearch, Iterable> {
161  typename LocalSearch::Graph operator()(const Iterable & inputRules, const Action & A) {
162  LocalSearch::Graph graph(A.size());
163 
164  for (const auto & rule : inputRules) {
165  auto & factorNode = graph.getFactor(rule.action.first)->getData();
166 
167  if (!factorNode.size())
168  factorNode.resize(factorSpacePartial(rule.action.first, A));
169  }
170  return graph;
171  }
172  };
173 
174  template <>
176  typename LocalSearch::Graph operator()(const QFunction & qf, const Action & A) {
177  LocalSearch::Graph graph(A.size());
178 
179  for (const auto & basis : qf.bases) {
180  auto & factorNode = graph.getFactor(basis.tag)->getData();
181 
182  if (!factorNode.size())
183  factorNode.resize(basis.values.size());
184  }
185 
186  return graph;
187  }
188  };
189 
190  template <QFRuleRange Iterable>
191  struct UpdateGraphImpl<LocalSearch, Iterable> {
192  void operator()(LocalSearch::Graph & graph, const Iterable & inputRules, const Action & A) {
193  for (auto & f : graph)
194  f.getData().setZero();
195 
196  for (const auto & rule : inputRules) {
197  auto & factorNode = graph.getFactor(rule.action.first)->getData();
198 
199  const auto id = toIndexPartial(A, rule.action);
200  factorNode[id] += rule.value;
201  }
202  }
203  };
204 
205  template <>
208  for (auto & f : graph)
209  f.getData().setZero();
210 
211  for (const auto & basis : qf.bases)
212  graph.getFactor(basis.tag)->getData() += basis.values;
213 
214  }
215  };
216 
217  // MaxPlus and RILS both use the same graph type, so we don't need to implement anything more.
218  template <> struct MakeGraph<MaxPlus> : MakeGraph<LocalSearch> {};
219  template <> struct UpdateGraph<MaxPlus> : UpdateGraph<LocalSearch> {};
220 
221  template <> struct MakeGraph<ReusingIterativeLocalSearch> : MakeGraph<LocalSearch> {};
222  template <> struct UpdateGraph<ReusingIterativeLocalSearch> : UpdateGraph<LocalSearch> {};
223 }
224 
225 #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
LocalSearch.hpp
AIToolbox::Factored::Bandit::VariableElimination::Graph
GVE::Graph Graph
Definition: VariableElimination.hpp:48
AIToolbox::Factored::Bandit::UpdateGraphImpl< VariableElimination, Iterable >::operator()
void operator()(VE::Graph &graph, const Iterable &inputRules, const Action &A)
Definition: GraphUtils.hpp:111
AIToolbox::Factored::Bandit::MakeGraphImpl< LocalSearch, QFunction >::operator()
LocalSearch::Graph operator()(const QFunction &qf, const Action &A)
Definition: GraphUtils.hpp:176
AIToolbox::Factored::FactoredVector
This class represents a factored vector.
Definition: FactoredMatrix.hpp:60
AIToolbox::Factored::Bandit::UpdateGraphImpl< LocalSearch, QFunction >::operator()
void operator()(LocalSearch::Graph &graph, const Factored::Bandit::QFunction &qf, const Action &)
Definition: GraphUtils.hpp:207
AIToolbox::Factored::Bandit::MakeGraphImpl
This class clumps all implementations that create graphs for data for certain Maximizers.
Definition: GraphUtils.hpp:40
AIToolbox::Factored::Bandit::MaxPlus
This class represents the Max-Plus optimization algorithm for loopy FactorGraphs.
Definition: MaxPlus.hpp:45
AIToolbox::Factored::Bandit::MakeGraph::operator()
auto operator()(const Data &d, Args &&...args)
Definition: GraphUtils.hpp:66
VariableElimination.hpp
Types.hpp
AIToolbox::Factored::Bandit::UpdateGraphImpl
This class clumps all implementations that update graphs with data for certain Maximizers.
Definition: GraphUtils.hpp:46
AIToolbox::Factored::Bandit::MakeGraphImpl< VariableElimination, Data >::operator()
VariableElimination::Graph operator()(const Data &, const Action &)
Definition: GraphUtils.hpp:102
AIToolbox::Factored::Bandit::MakeGraph
This class is the public interface for initializing the graph in generic code that uses the maximizer...
Definition: GraphUtils.hpp:64
AIToolbox::Factored::Bandit::UpdateGraph::operator()
void operator()(typename Maximizer::Graph &graph, const Data &d, Args &&...args)
Definition: GraphUtils.hpp:87
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::FactorGraph
This class offers a minimal interface to manager a factor graph.
Definition: FactorGraph.hpp:31
TypeTraits.hpp
AIToolbox::Factored::Bandit::VariableElimination::Factor
std::pair< double, std::vector< std::pair< size_t, size_t > >> Factor
Definition: VariableElimination.hpp:46
AIToolbox::Factored::factorSpacePartial
size_t factorSpacePartial(const PartialKeys &ids, const Factors &space)
This function returns the multiplication of all elements of the input factor.
AIToolbox::Factored::Bandit::VariableElimination
This class represents the Variable Elimination algorithm.
Definition: VariableElimination.hpp:41
AIToolbox::Factored::Bandit::UpdateGraphImpl< LocalSearch, Iterable >::operator()
void operator()(LocalSearch::Graph &graph, const Iterable &inputRules, const Action &A)
Definition: GraphUtils.hpp:192
AIToolbox::Factored::Bandit::ReusingIterativeLocalSearch
This class approximately finds the best joint action with Reusing Iterative Local Search.
Definition: ReusingIterativeLocalSearch.hpp:25
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::FactoredVector::bases
std::vector< BasisFunction > bases
Definition: FactoredMatrix.hpp:111
AIToolbox::Factored::Action
Factors Action
Definition: Types.hpp:69
AIToolbox::Factored::Bandit::UpdateGraphImpl< VariableElimination, QFunction >::operator()
void operator()(VE::Graph &graph, const QFunction &qf, const Action &A)
Definition: GraphUtils.hpp:135
AIToolbox::Factored::Bandit::MakeGraphImpl< LocalSearch, Iterable >::operator()
LocalSearch::Graph operator()(const Iterable &inputRules, const Action &A)
Definition: GraphUtils.hpp:161
ReusingIterativeLocalSearch.hpp
MaxPlus.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
AIToolbox::Factored::Bandit
Definition: GraphUtils.hpp:12