aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
fmdp.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Class for implementation of factored markov decision process.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 #ifndef GUM_FACTORED_MARKOV_DECISION_PROCESS_H
30 #define GUM_FACTORED_MARKOV_DECISION_PROCESS_H
31 //======================================================================
32 #include <string>
33 #include <vector>
34 //======================================================================
35 #include <agrum/tools/core/bijection.h>
36 #include <agrum/tools/core/hashTable.h>
37 #include <agrum/tools/core/set.h>
38 //======================================================================
39 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
40 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
41 #include <agrum/tools/variables/discreteVariable.h>
42 //======================================================================
43 
44 namespace gum {
45  /**
46  * @class FMDP
47  * @brief This class is used to implement factored decision process.
48  * @ingroup fmdp_group
49  *
50  * This class supports a mechanism which allows user to give, for each
51  * variable ivolved in the process, a default transition probability table
52  * and to describe for specific actions a different table.
53  *
54  */
55 
56  template < typename GUM_SCALAR >
57  class FMDP {
58  template < typename GUM_SCALAR_O >
59  using VarTransitionTable
61 
62  public:
63  // ===========================================================================
64  /// @name Constructors, Destructors.
65  // ===========================================================================
66  /// @{
67 
68  /**
69  * Default constructor.
70  */
71  FMDP(bool onDestructionDeleteVar = false);
72 
73  /**
74  * Default destructor.
75  */
76  ~FMDP();
77 
78  /// @}
79 
80  // ===========================================================================
81  /// @name Variable Handling Methods.
82  // ===========================================================================
83  /// @{
84 
85  /**
86  * Adds a variable to FMDP description
87  * @throw DuplicateElement if a similar variable already exists
88  */
89  void addVariable(const DiscreteVariable* var);
90 
91  /**
92  * Returns an iterator reference to he beginning of the list of variables
93  */
95  return _varSeq_.beginSafe();
96  }
97 
98  /**
99  * Returns an iterator reference to the end of the list of variables
100  */
102  return _varSeq_.endSafe();
103  }
104 
105  /**
106  * Returns the primed variable associate to the given main variable
107  */
109  return _main2primed_.second(mainVar);
110  }
111 
112  /**
113  * Returns the map binding main variables and prime variables
114  */
115  INLINE const Bijection< const DiscreteVariable*, const DiscreteVariable* >&
116  mapMainPrime() const {
117  return _main2primed_;
118  }
119 
120  /// @}
121 
122  // ===========================================================================
123  /// @name Action Handling Methods.
124  // ===========================================================================
125  /// @{
126 
127  /**
128  * Adds an action to FMDP description
129  * @throw DuplicateElement if an action with same name already exists
130  */
131  void addAction(Idx actionId, const std::string& action);
132 
133  /**
134  * Returns an iterator reference to he beginning of the list of actions
135  */
136  SequenceIteratorSafe< Idx > beginActions() const { return _actionSeq_.beginSafe(); }
137 
138  /**
139  * Returns an iterator reference to the end of the list of actions
140  */
141  SequenceIteratorSafe< Idx > endActions() const { return _actionSeq_.endSafe(); }
142 
143  /// @}
144 
145  // ===========================================================================
146  /// @name Transition methods.
147  // ===========================================================================
148  /// @{
149 
150  /**
151  * Adds a variable transition table to specified action
152  * @throw NotFound if action or var does not exists
153  * @throw DuplicateElement if variable already has a transition for this
154  * action
155  */
156  void addTransitionForAction(Idx actionId,
157  const DiscreteVariable* var,
158  const MultiDimImplementation< GUM_SCALAR >* transition);
159 
160  /**
161  * Adds a default variable transition
162  * @throw NotFound if var does not exists
163  * @throw DuplicateElement if variable already has a default transition
164  */
165  void addTransition(const DiscreteVariable* var,
166  const MultiDimImplementation< GUM_SCALAR >* transition) {
167  this->addTransitionForAction(0, var, transition);
168  }
169 
170  /**
171  * Returns transition associated to given in parameter variable and the
172  * given action
173  */
175  const DiscreteVariable* v) const;
176 
177  /// @}
178 
179  // ===========================================================================
180  /// @name Cost methods.
181  // ===========================================================================
182  /// @{
183 
184  /**
185  * Adds a cost table to specified action
186  * @throw NotFound if action does not exists
187  */
188  void addCostForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR >* cost);
189 
190  /**
191  * Adds a default variable cost
192  * @throw DuplicateElement if a default cost exists already
193  */
194  void addCost(const MultiDimImplementation< GUM_SCALAR >* cost) {
195  this->addCostForAction(0, cost);
196  }
197 
198  /**
199  * Returns the reward table of mdp
200  */
201  const MultiDimImplementation< GUM_SCALAR >* cost(Idx actionId = 0) const;
202 
203  /// @}
204 
205  // ===========================================================================
206  /// @name Reward methods.
207  // ===========================================================================
208  /// @{
209 
210  /**
211  * Adds a default variable reward
212  * @throw DuplicateElement if a default reward exists already
213  */
214  void addRewardForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR >* reward);
215 
216  /**
217  * Adds a default variable reward
218  * @throw DuplicateElement if a default reward exists already
219  */
220  void addReward(const MultiDimImplementation< GUM_SCALAR >* reward) {
221  this->addRewardForAction(0, reward);
222  }
223 
224  /**
225  * Returns the reward table of mdp
226  */
227  const MultiDimImplementation< GUM_SCALAR >* reward(Idx actionId = 0) const;
228 
229  /// @}
230 
231  // ===========================================================================
232  /// @name FMPD miscelleanous methods.
233  // ===========================================================================
234  /// @{
235 
236  /**
237  * Returns name of action given in parameter
238  */
239  const std::string& actionName(Idx actionId) const;
240 
241  /**
242  * Returns action id
243  */
244  Idx actionId(const std::string&) const;
245 
246  /**
247  * Displays the FMDP in a Dot format
248  */
249  std::string toString() const;
250 
251  /**
252  * Returns the map binding main variables and prime variables
253  */
254  Size size() const;
255 
256 
257  /// @}
258 
259  private:
260  /// Sequence de variables and its iterator
262 
264 
265  /// Mapping from a main variable to its associated primed version
267 
268  /// Bijection mapping an action name to its id
270 
271  /// Table which give for each action a table containing variables transition
272  /// cpt
274 
275  /// Table which give for each action cost table
277 
278  /// Table which give for each action reward table
280 
281  /// Boolean indicates whether or not main variables should be deleted on
282  /// destruction of this instance
283  /// Usually the case when fmdp has been initialized with the factory
285  };
286 
287 
288 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
289  extern template class FMDP< double >;
290 #endif
291 
292 
293 } /* namespace gum */
294 
295 
296 #include <agrum/FMDP/fmdp_tpl.h>
297 
298 #endif // GUM_FACTORED_MARKOV_DECISION_PROCESS_H
~FMDP()
Default destructor.
Definition: fmdp_tpl.h:68
Sequence< const DiscreteVariable *> _varSeq_
Sequence de variables and its iterator.
Definition: fmdp.h:261
void addTransition(const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a default variable transition.
Definition: fmdp.h:165
SequenceIteratorSafe< Idx > beginActions() const
Returns an iterator reference to he beginning of the list of actions.
Definition: fmdp.h:136
Idx actionId(const std::string &) const
Returns action id.
Definition: fmdp_tpl.h:325
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void addReward(const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Definition: fmdp.h:220
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
Definition: fmdp_tpl.h:314
SequenceIteratorSafe< const DiscreteVariable *> beginVariables() const
Returns an iterator reference to he beginning of the list of variables.
Definition: fmdp.h:94
Sequence< Idx > _actionSeq_
Definition: fmdp.h:263
void addCostForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a cost table to specified action.
Definition: fmdp_tpl.h:234
void addCost(const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a default variable cost.
Definition: fmdp.h:194
void addVariable(const DiscreteVariable *var)
Adds a variable to FMDP description.
Definition: fmdp_tpl.h:116
void addTransitionForAction(Idx actionId, const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a variable transition table to specified action.
Definition: fmdp_tpl.h:183
const MultiDimImplementation< GUM_SCALAR > * transition(Idx actionId, const DiscreteVariable *v) const
Returns transition associated to given in parameter variable and the given action.
Definition: fmdp_tpl.h:208
FMDP(bool onDestructionDeleteVar=false)
Default constructor.
Definition: fmdp_tpl.h:53
Size size() const
Returns the map binding main variables and prime variables.
Definition: fmdp_tpl.h:356
const MultiDimImplementation< GUM_SCALAR > * reward(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:293
void addRewardForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Definition: fmdp_tpl.h:274
const DiscreteVariable * main2prime(const DiscreteVariable *mainVar) const
Returns the primed variable associate to the given main variable.
Definition: fmdp.h:108
const MultiDimImplementation< GUM_SCALAR > * cost(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:251
SequenceIteratorSafe< Idx > endActions() const
Returns an iterator reference to the end of the list of actions.
Definition: fmdp.h:141
bool _onDestructionDeleteVars_
Boolean indicates whether or not main variables should be deleted on destruction of this instance Usu...
Definition: fmdp.h:284
Bijection< const DiscreteVariable *, const DiscreteVariable *> _main2primed_
Mapping from a main variable to its associated primed version.
Definition: fmdp.h:266
SequenceIteratorSafe< const DiscreteVariable *> endVariables() const
Returns an iterator reference to the end of the list of variables.
Definition: fmdp.h:101
Bijection< Idx, const std::string *> _actionMap_
Bijection mapping an action name to its id.
Definition: fmdp.h:269
void addAction(Idx actionId, const std::string &action)
Adds an action to FMDP description.
Definition: fmdp_tpl.h:144
std::string toString() const
Displays the FMDP in a Dot format.
Definition: fmdp_tpl.h:336