aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
fmdpFactory.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 Headers of the FMDPFactory class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 
30 #ifndef GUM_FMDP_FACTORY_H
31 #define GUM_FMDP_FACTORY_H
32 //======================================================================
33 #include <string>
34 #include <vector>
35 //======================================================================
36 #include <agrum/tools/core/sequence.h>
37 
38 #include <agrum/tools/variables/labelizedVariable.h>
39 
40 #include <agrum/FMDP/IfmdpFactory.h>
41 #include <agrum/FMDP/fmdp.h>
42 
43 
44 namespace gum {
45 
46  /**
47  * @class FMDPFactory
48  * @headerfile fmdpFactory.h <agrum/FMDP/fmdpFactory.h>
49  * @brief A factory class to ease Factored Markov Decision Process
50  * construction.
51  * @ingroup fmdp_group
52  *
53  * A FMDPFactory will never create a Factored Markov Decision Process and
54  * works on only one
55  * Factored Markov Decision Process.
56  *
57  * The only exception of this behaviour is when you create a copy of the
58  * factory,
59  * it will create a copy of its FMDP. This is useful when you want to create
60  * two FMDP sharing a common base.
61  * However be very careful because the copy will not delete its FMDP.
62  *
63  * Each method will raise an OperationNotAllowed if you call it when the
64  * factory
65  * is not in a valid state for that call. The error message is "Illegal
66  * state.".
67  *
68  */
69 
70  template < typename GUM_SCALAR >
72  public:
73  // ==========================================================================
74  /// @name Constructor & destructor.
75  // ==========================================================================
76  /// @{
77 
78  /**
79  * Use this constructor if you want to use an already created factored
80  * markov decision process.
81  * @param fmdp A pointer over the @ref FMDP filled by this factory.
82  */
83  FMDPFactory(FMDP< GUM_SCALAR >* fmdp);
84 
85  /**
86  * @brief Destructor.
87  *
88  * To prevent strange behaviour you should always destroy a FMDPFactory
89  * when it's state equals NONE.
90  *
91  * @throw FatalError Raised if the state of the factory prevents it to die
92  * peacefully.
93  */
94  ~FMDPFactory();
95 
96  /// @}
97  // ==========================================================================
98  /// @name Getter and setters.
99  // ==========================================================================
100  /// @{
101 
102  /**
103  * Returns the Factored Markov Decision Process created by this factory.
104  * @throw OperationNotAllowed Raise if the state of the factory is different
105  * than NONE.
106  */
107  FMDP< GUM_SCALAR >* fmdp() const;
108 
109  /// Returns the current state of the factory.
110  FMDPfactory_state state() const;
111 
112  /// Returns a constant reference on a variable given it's name.
113  /// @throw NotFound Raised if no variable matches the name.
114  const DiscreteVariable* variable(const std::string& name) const;
115 
116  /// @}
117  // ==========================================================================
118  /// @name Variable declaration methods (NONE -> VARIABLE)
119  // ==========================================================================
120  /// @{
121 
122  /// Tells the factory that we're in a variable declaration.
124 
125  /// Tells the factory the current variable's name.
126  /// @throw DuplicateElement Raised if a variable with the same name already
127  /// exist.
128  void variableName(const std::string& name);
129 
130  /// Tells the factory the current variable's description.
131  void variableDescription(const std::string& desc);
132 
133  /// Adds a modality to the current variable.
134  void addModality(const std::string& name);
135 
136  /// Tells the factory that we're out of a variable declaration.
137  /// @throw UndefinedElement Raised if the variable isn't defined (or not
138  /// enough defined).
139  void endVariableDeclaration();
140 
141  /// @}
142  // ==========================================================================
143  /// @name Action declaration methods (NONE -> ACTION)
144  // ==========================================================================
145  /// @{
146 
147  /// Tells the factory that we're in an action declaration.
148  void startActionDeclaration();
149 
150  /// Tells the factory to add an action to the current fmdp.
151  void addAction(const std::string& action);
152 
153  /// Tells the factory that we're out of an action declaration.
154  void endActionDeclaration();
155 
156  /// @}
157  // ==========================================================================
158  /// @name Transition declaration methods (NONE -> TRANSITION <- ACTION)
159  // ==========================================================================
160  /// @{
161 
162  /// Tells the factory that we're in a transition declaration.
164 
165  /// Tells the factory to add a transition table to the current fmdp.
166  void addTransition(const std::string& var, const MultiDimAdressable* transition);
167 
168  /// Tells the factory to add a transition table to the current fmdp.
169  /// This transition table will be extracted from incorporated
170  /// multiDimFunctionGraph.
171  void addTransition(const std::string& var);
172 
173  /// Tells the factory that we're out of a transition declaration.
175 
176  /// @}
177  // ==========================================================================
178  /// @name Cost declaration methods (NONE -> COST <- ACTION)
179  // ==========================================================================
180  /// @{
181 
182  /// Tells the factory that we're in a cost declaration.
183  void startCostDeclaration();
184 
185  /// Tells the factory to add a cost table to the current fmdp.
186  void addCost(const MultiDimAdressable* cost);
187 
188  /// Tells the factory to add a cost table to the current fmdp.
189  /// This cost table will be extracted from incorporated
190  /// multiDimFunctionGraph.
191  void addCost();
192 
193  /// Tells the factory that we're out of a cost declaration.
194  void endCostDeclaration();
195 
196  /// @}
197  // ==========================================================================
198  /// @name Reward declaration methods (NONE -> REWARD <- ACTION)
199  // ==========================================================================
200  /// @{
201 
202  /// Tells the factory that we're in a reward declaration.
203  void startRewardDeclaration();
204 
205  /// Tells the factory that we're in a reward declaration mode where the
206  /// global reward diagram is an operation between
207  /// simplier decision diagram..
208  void setOperationModeOn(std::string operationType);
209 
210  /// Tells the factory to add a reward table to the current fmdp.
211  void addReward(const MultiDimAdressable* reward);
212 
213  /// Tells the factory to add a reward table to the current fmdp.
214  /// This reward table will be extracted from incorporated
215  /// multiDimFunctionGraph.
216  void addReward();
217 
218  /// Tells the factory that we're out of a cost declaration.
219  void endRewardDeclaration();
220 
221  /// @}
222  // ==========================================================================
223  /// @name Discount declaration methods (NONE -> DISCOUNT)
224  // ==========================================================================
225  /// @{
226 
227  /// Tells the factory that we're in a cost declaration.
229 
230  /// Tells the factory to add a cost table to the current fmdp.
231  void addDiscount(float discount);
232 
233  /// Tells the factory that we're out of a cost declaration.
234  void endDiscountDeclaration();
235 
236  /// @}
237  // ==========================================================================
238  /// @name FunctionGraph Creation specific methods
239  /// @brief methods used by internal FunctionGraphfactory to build up
240  /// a multidimFunctionGraph
241  // ==========================================================================
242  /// @{
243 
244  /// Insert in diagram a non terminal node
246 
247  /// Insert in diagram a terminal node
249 
250  ///
251  void addArc(NodeId from, NodeId to, Idx modality);
252 
253  /// add an arc in diagram
254  void setRoot(NodeId rootId);
255 
256  /// @}
257 
258  private:
259  // ==========================================================================
260  /// @name Current constructed FMDP's parts.
261  // ==========================================================================
262  /// @{
263 
264  /// Depending on the context this flag is used for some VERY important
265  /// reasons.
267 
268  /// Depending on the context this flag is used for some VERY important
269  /// reasons.
271 
272  /// Just to keep track of strings between two start/end calls.
274 
275  /// Just to keep track of multidim between two start/end calls.
277 
278  /// Used in VARIABLE mode
279  /// Checks if in _stringBag_ there is no other modality with the same name.
280  void _checkModalityInBag_(const std::string& mod);
281 
282  /// Reset the different parts used to constructed the FMDP.
283  void _resetParts_();
284 
285  /// Insert every variables in the function graph
287 
288  /// Insert every variables in the function graph
290 
291  /// @}
292 
293  /// State stack.
295 
296  /// The constructed FMDP
298 
299  /// The FunctionGraph we're building at a given time
301 
302  /// Mapping between a declared variable's name and itself.
304 
305  /// Action Id counter
307 
308  /// Copy operator is illegal, use only copy constructor.
309  FMDPFactory< GUM_SCALAR >& operator=(const FMDPFactory< GUM_SCALAR >& source);
310 
311  /// Raise an OperationNotAllowed with the message "Illegal state."
312  void _illegalStateError_(const std::string& s);
313  };
314 
315 
316 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
317  extern template class FMDPFactory< double >;
318 #endif
319 
320 } /* namespace gum */
321 
322 
323 #include <agrum/FMDP/fmdpFactory_tpl.h>
324 
325 #endif // GUM_FMDP_FACTORY_H
void addReward()
Tells the factory to add a reward table to the current fmdp. This reward table will be extracted from...
void _finalizeFunctionGraph_()
Insert every variables in the function graph.
void endTransitionDeclaration()
Tells the factory that we&#39;re out of a transition declaration.
FMDPfactory_state state() const
Returns the current state of the factory.
void endActionDeclaration()
Tells the factory that we&#39;re out of an action declaration.
A factory class to ease Factored Markov Decision Process construction.
Definition: fmdpFactory.h:71
void _initializeFunctionGraph_()
Insert every variables in the function graph.
void _resetParts_()
Reset the different parts used to constructed the FMDP.
void addTransition(const std::string &var)
Tells the factory to add a transition table to the current fmdp. This transition table will be extrac...
void addCost(const MultiDimAdressable *cost)
Tells the factory to add a cost table to the current fmdp.
FMDPFactory< GUM_SCALAR > & operator=(const FMDPFactory< GUM_SCALAR > &source)
Copy operator is illegal, use only copy constructor.
void addAction(const std::string &action)
Tells the factory to add an action to the current fmdp.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void startDiscountDeclaration()
Tells the factory that we&#39;re in a cost declaration.
void startActionDeclaration()
Tells the factory that we&#39;re in an action declaration.
void addDiscount(float discount)
Tells the factory to add a cost table to the current fmdp.
void startVariableDeclaration()
Tells the factory that we&#39;re in a variable declaration.
void _checkModalityInBag_(const std::string &mod)
Used in VARIABLE mode Checks if in stringBag there is no other modality with the same name...
void endCostDeclaration()
Tells the factory that we&#39;re out of a cost declaration.
void addArc(NodeId from, NodeId to, Idx modality)
Insert in diagram a non terminal node.
HashTable< std::string, const DiscreteVariable *> _varNameMap_
Mapping between a declared variable&#39;s name and itself.
Definition: fmdpFactory.h:303
MultiDimFunctionGraph< GUM_SCALAR > * _FunctionGraph_
The FunctionGraph we&#39;re building at a given time.
Definition: fmdpFactory.h:300
void setRoot(NodeId rootId)
add an arc in diagram
const DiscreteVariable * variable(const std::string &name) const
Returns a constant reference on a variable given it&#39;s name.
void addModality(const std::string &name)
Adds a modality to the current variable.
FMDP< GUM_SCALAR > * fmdp() const
Returns the Factored Markov Decision Process created by this factory.
void addReward(const MultiDimAdressable *reward)
Tells the factory to add a reward table to the current fmdp.
FMDP< GUM_SCALAR > * _fmdp_
The constructed FMDP.
Definition: fmdpFactory.h:297
void endDiscountDeclaration()
Tells the factory that we&#39;re out of a cost declaration.
~FMDPFactory()
Destructor.
std::vector< FMDPfactory_state > _states_
State stack.
Definition: fmdpFactory.h:294
void addTransition(const std::string &var, const MultiDimAdressable *transition)
Tells the factory to add a transition table to the current fmdp.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
void addCost()
Tells the factory to add a cost table to the current fmdp. This cost table will be extracted from inc...
std::vector< const MultiDimImplementation< GUM_SCALAR > *> _ddBag_
Just to keep track of multidim between two start/end calls.
Definition: fmdpFactory.h:276
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
Definition: fmdpFactory.h:266
void endRewardDeclaration()
Tells the factory that we&#39;re out of a cost declaration.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
Definition: fmdpFactory.h:270
void endVariableDeclaration()
Tells the factory that we&#39;re out of a variable declaration.
NodeId addInternalNode(std::string name_of_var)
Insert in diagram a non terminal node.
void startCostDeclaration()
Tells the factory that we&#39;re in a cost declaration.
NodeId addTerminalNode(float value)
Insert in diagram a terminal node.
void variableName(const std::string &name)
Tells the factory the current variable&#39;s name.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
Definition: fmdpFactory.h:273
void variableDescription(const std::string &desc)
Tells the factory the current variable&#39;s description.
void setOperationModeOn(std::string operationType)
Tells the factory that we&#39;re in a reward declaration mode where the global reward diagram is an opera...
void startRewardDeclaration()
Tells the factory that we&#39;re in a reward declaration.
FMDPFactory(FMDP< GUM_SCALAR > *fmdp)
Use this constructor if you want to use an already created factored markov decision process...
Idx _actionIdcpt_
Action Id counter.
Definition: fmdpFactory.h:306
void startTransitionDeclaration()
Tells the factory that we&#39;re in a transition declaration.