aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
fmdp.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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
60  = HashTable< const DiscreteVariable*,
62 
63  public:
64  // ===========================================================================
65  /// @name Constructors, Destructors.
66  // ===========================================================================
67  /// @{
68 
69  /**
70  * Default constructor.
71  */
72  FMDP(bool onDestructionDeleteVar = false);
73 
74  /**
75  * Default destructor.
76  */
77  ~FMDP();
78 
79  /// @}
80 
81  // ===========================================================================
82  /// @name Variable Handling Methods.
83  // ===========================================================================
84  /// @{
85 
86  /**
87  * Adds a variable to FMDP description
88  * @throw DuplicateElement if a similar variable already exists
89  */
90  void addVariable(const DiscreteVariable* var);
91 
92  /**
93  * Returns an iterator reference to he beginning of the list of variables
94  */
96  return varSeq__.beginSafe();
97  }
98 
99  /**
100  * Returns an iterator reference to the end of the list of variables
101  */
103  return varSeq__.endSafe();
104  }
105 
106  /**
107  * Returns the primed variable associate to the given main variable
108  */
110  return main2primed__.second(mainVar);
111  }
112 
113  /**
114  * Returns the map binding main variables and prime variables
115  */
116  INLINE const Bijection< const DiscreteVariable*, const DiscreteVariable* >&
117  mapMainPrime() const {
118  return main2primed__;
119  }
120 
121  /// @}
122 
123  // ===========================================================================
124  /// @name Action Handling Methods.
125  // ===========================================================================
126  /// @{
127 
128  /**
129  * Adds an action to FMDP description
130  * @throw DuplicateElement if an action with same name already exists
131  */
132  void addAction(Idx actionId, const std::string& action);
133 
134  /**
135  * Returns an iterator reference to he beginning of the list of actions
136  */
138  return actionSeq__.beginSafe();
139  }
140 
141  /**
142  * Returns an iterator reference to the end of the list of actions
143  */
145  return actionSeq__.endSafe();
146  }
147 
148  /// @}
149 
150  // ===========================================================================
151  /// @name Transition methods.
152  // ===========================================================================
153  /// @{
154 
155  /**
156  * Adds a variable transition table to specified action
157  * @throw NotFound if action or var does not exists
158  * @throw DuplicateElement if variable already has a transition for this
159  * action
160  */
162  Idx actionId,
163  const DiscreteVariable* var,
164  const MultiDimImplementation< GUM_SCALAR >* transition);
165 
166  /**
167  * Adds a default variable transition
168  * @throw NotFound if var does not exists
169  * @throw DuplicateElement if variable already has a default transition
170  */
171  void addTransition(const DiscreteVariable* var,
172  const MultiDimImplementation< GUM_SCALAR >* transition) {
173  this->addTransitionForAction(0, var, transition);
174  }
175 
176  /**
177  * Returns transition associated to given in parameter variable and the
178  * given action
179  */
181  transition(Idx actionId, const DiscreteVariable* v) const;
182 
183  /// @}
184 
185  // ===========================================================================
186  /// @name Cost methods.
187  // ===========================================================================
188  /// @{
189 
190  /**
191  * Adds a cost table to specified action
192  * @throw NotFound if action does not exists
193  */
194  void addCostForAction(Idx actionId,
195  const MultiDimImplementation< GUM_SCALAR >* cost);
196 
197  /**
198  * Adds a default variable cost
199  * @throw DuplicateElement if a default cost exists already
200  */
201  void addCost(const MultiDimImplementation< GUM_SCALAR >* cost) {
202  this->addCostForAction(0, cost);
203  }
204 
205  /**
206  * Returns the reward table of mdp
207  */
208  const MultiDimImplementation< GUM_SCALAR >* cost(Idx actionId = 0) const;
209 
210  /// @}
211 
212  // ===========================================================================
213  /// @name Reward methods.
214  // ===========================================================================
215  /// @{
216 
217  /**
218  * Adds a default variable reward
219  * @throw DuplicateElement if a default reward exists already
220  */
221  void addRewardForAction(Idx actionId,
222  const MultiDimImplementation< GUM_SCALAR >* reward);
223 
224  /**
225  * Adds a default variable reward
226  * @throw DuplicateElement if a default reward exists already
227  */
228  void addReward(const MultiDimImplementation< GUM_SCALAR >* reward) {
229  this->addRewardForAction(0, reward);
230  }
231 
232  /**
233  * Returns the reward table of mdp
234  */
235  const MultiDimImplementation< GUM_SCALAR >* reward(Idx actionId = 0) const;
236 
237  /// @}
238 
239  // ===========================================================================
240  /// @name FMPD miscelleanous methods.
241  // ===========================================================================
242  /// @{
243 
244  /**
245  * Returns name of action given in parameter
246  */
247  const std::string& actionName(Idx actionId) const;
248 
249  /**
250  * Returns action id
251  */
252  Idx actionId(const std::string&) const;
253 
254  /**
255  * Displays the FMDP in a Dot format
256  */
257  std::string toString() const;
258 
259  /**
260  * Returns the map binding main variables and prime variables
261  */
262  Size size() const;
263 
264 
265  /// @}
266 
267  private:
268  /// Sequence de variables and its iterator
270 
272 
273  /// Mapping from a main variable to its associated primed version
275 
276  /// Bijection mapping an action name to its id
278 
279  /// Table which give for each action a table containing variables transition
280  /// cpt
282 
283  /// Table which give for each action cost table
286 
287  /// Table which give for each action reward table
290 
291  /// Boolean indicates whether or not main variables should be deleted on
292  /// destruction of this instance
293  /// Usually the case when fmdp has been initialized with the factory
295  };
296 
297 
298 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
299  extern template class FMDP< double >;
300 #endif
301 
302 
303 } /* namespace gum */
304 
305 
306 #include <agrum/FMDP/fmdp_tpl.h>
307 
308 #endif // GUM_FACTORED_MARKOV_DECISION_PROCESS_H
~FMDP()
Default destructor.
Definition: fmdp_tpl.h:68
void addTransition(const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a default variable transition.
Definition: fmdp.h:171
SequenceIteratorSafe< Idx > beginActions() const
Returns an iterator reference to he beginning of the list of actions.
Definition: fmdp.h:137
Idx actionId(const std::string &) const
Returns action id.
Definition: fmdp_tpl.h:360
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void addReward(const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Definition: fmdp.h:228
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
Definition: fmdp_tpl.h:349
bool onDestructionDeleteVars__
Boolean indicates whether or not main variables should be deleted on destruction of this instance Usu...
Definition: fmdp.h:294
Bijection< const DiscreteVariable *, const DiscreteVariable *> main2primed__
Mapping from a main variable to its associated primed version.
Definition: fmdp.h:274
SequenceIteratorSafe< const DiscreteVariable *> beginVariables() const
Returns an iterator reference to he beginning of the list of variables.
Definition: fmdp.h:95
void addCostForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a cost table to specified action.
Definition: fmdp_tpl.h:257
Bijection< Idx, const std::string *> actionMap__
Bijection mapping an action name to its id.
Definition: fmdp.h:277
void addCost(const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a default variable cost.
Definition: fmdp.h:201
void addVariable(const DiscreteVariable *var)
Adds a variable to FMDP description.
Definition: fmdp_tpl.h:124
void addTransitionForAction(Idx actionId, const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a variable transition table to specified action.
Definition: fmdp_tpl.h:198
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:227
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:394
const MultiDimImplementation< GUM_SCALAR > * reward(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:326
void addRewardForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Definition: fmdp_tpl.h:303
const DiscreteVariable * main2prime(const DiscreteVariable *mainVar) const
Returns the primed variable associate to the given main variable.
Definition: fmdp.h:109
const MultiDimImplementation< GUM_SCALAR > * cost(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:279
SequenceIteratorSafe< Idx > endActions() const
Returns an iterator reference to the end of the list of actions.
Definition: fmdp.h:144
SequenceIteratorSafe< const DiscreteVariable *> endVariables() const
Returns an iterator reference to the end of the list of variables.
Definition: fmdp.h:102
Sequence< Idx > actionSeq__
Definition: fmdp.h:271
void addAction(Idx actionId, const std::string &action)
Adds an action to FMDP description.
Definition: fmdp_tpl.h:153
std::string toString() const
Displays the FMDP in a Dot format.
Definition: fmdp_tpl.h:372
Sequence< const DiscreteVariable *> varSeq__
Sequence de variables and its iterator.
Definition: fmdp.h:269