aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
fmdpFactory.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 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,
167  const MultiDimAdressable* transition);
168 
169  /// Tells the factory to add a transition table to the current fmdp.
170  /// This transition table will be extracted from incorporated
171  /// multiDimFunctionGraph.
172  void addTransition(const std::string& var);
173 
174  /// Tells the factory that we're out of a transition declaration.
176 
177  /// @}
178  // ==========================================================================
179  /// @name Cost declaration methods (NONE -> COST <- ACTION)
180  // ==========================================================================
181  /// @{
182 
183  /// Tells the factory that we're in a cost declaration.
184  void startCostDeclaration();
185 
186  /// Tells the factory to add a cost table to the current fmdp.
187  void addCost(const MultiDimAdressable* cost);
188 
189  /// Tells the factory to add a cost table to the current fmdp.
190  /// This cost table will be extracted from incorporated
191  /// multiDimFunctionGraph.
192  void addCost();
193 
194  /// Tells the factory that we're out of a cost declaration.
195  void endCostDeclaration();
196 
197  /// @}
198  // ==========================================================================
199  /// @name Reward declaration methods (NONE -> REWARD <- ACTION)
200  // ==========================================================================
201  /// @{
202 
203  /// Tells the factory that we're in a reward declaration.
204  void startRewardDeclaration();
205 
206  /// Tells the factory that we're in a reward declaration mode where the
207  /// global reward diagram is an operation between
208  /// simplier decision diagram..
209  void setOperationModeOn(std::string operationType);
210 
211  /// Tells the factory to add a reward table to the current fmdp.
212  void addReward(const MultiDimAdressable* reward);
213 
214  /// Tells the factory to add a reward table to the current fmdp.
215  /// This reward table will be extracted from incorporated
216  /// multiDimFunctionGraph.
217  void addReward();
218 
219  /// Tells the factory that we're out of a cost declaration.
220  void endRewardDeclaration();
221 
222  /// @}
223  // ==========================================================================
224  /// @name Discount declaration methods (NONE -> DISCOUNT)
225  // ==========================================================================
226  /// @{
227 
228  /// Tells the factory that we're in a cost declaration.
230 
231  /// Tells the factory to add a cost table to the current fmdp.
232  void addDiscount(float discount);
233 
234  /// Tells the factory that we're out of a cost declaration.
235  void endDiscountDeclaration();
236 
237  /// @}
238  // ==========================================================================
239  /// @name FunctionGraph Creation specific methods
240  /// @brief methods used by internal FunctionGraphfactory to build up
241  /// a multidimFunctionGraph
242  // ==========================================================================
243  /// @{
244 
245  /// Insert in diagram a non terminal node
247 
248  /// Insert in diagram a terminal node
250 
251  ///
252  void addArc(NodeId from, NodeId to, Idx modality);
253 
254  /// add an arc in diagram
255  void setRoot(NodeId rootId);
256 
257  /// @}
258 
259  private:
260  // ==========================================================================
261  /// @name Current constructed FMDP's parts.
262  // ==========================================================================
263  /// @{
264 
265  /// Depending on the context this flag is used for some VERY important
266  /// reasons.
268 
269  /// Depending on the context this flag is used for some VERY important
270  /// reasons.
272 
273  /// Just to keep track of strings between two start/end calls.
275 
276  /// Just to keep track of multidim between two start/end calls.
278 
279  /// Used in VARIABLE mode
280  /// Checks if in stringBag__ there is no other modality with the same name.
281  void checkModalityInBag__(const std::string& mod);
282 
283  /// Reset the different parts used to constructed the FMDP.
284  void resetParts__();
285 
286  /// Insert every variables in the function graph
288 
289  /// Insert every variables in the function graph
291 
292  /// @}
293 
294  /// State stack.
296 
297  /// The constructed FMDP
299 
300  /// The FunctionGraph we're building at a given time
302 
303  /// Mapping between a declared variable's name and itself.
305 
306  /// Action Id counter
308 
309  /// Copy operator is illegal, use only copy constructor.
310  FMDPFactory< GUM_SCALAR >& operator=(const FMDPFactory< GUM_SCALAR >& source);
311 
312  /// Raise an OperationNotAllowed with the message "Illegal state."
313  void illegalStateError__(const std::string& s);
314  };
315 
316 
317 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
318  extern template class FMDPFactory< double >;
319 #endif
320 
321 } /* namespace gum */
322 
323 
324 #include <agrum/FMDP/fmdpFactory_tpl.h>
325 
326 #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...
bool foo_flag__
Depending on the context this flag is used for some VERY important reasons.
Definition: fmdpFactory.h:267
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 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.
std::vector< const MultiDimImplementation< GUM_SCALAR > *> ddBag__
Just to keep track of multidim between two start/end calls.
Definition: fmdpFactory.h:277
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
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 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.
void setRoot(NodeId rootId)
add an arc in diagram
FMDP< GUM_SCALAR > * fmdp__
The constructed FMDP.
Definition: fmdpFactory.h:298
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.
std::vector< std::string > stringBag__
Just to keep track of strings between two start/end calls.
Definition: fmdpFactory.h:274
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.
HashTable< std::string, const DiscreteVariable *> varNameMap__
Mapping between a declared variable&#39;s name and itself.
Definition: fmdpFactory.h:304
void endDiscountDeclaration()
Tells the factory that we&#39;re out of a cost declaration.
~FMDPFactory()
Destructor.
void addTransition(const std::string &var, const MultiDimAdressable *transition)
Tells the factory to add a transition table to the current fmdp.
void addCost()
Tells the factory to add a cost table to the current fmdp. This cost table will be extracted from inc...
void endRewardDeclaration()
Tells the factory that we&#39;re out of a cost declaration.
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 resetParts__()
Reset the different parts used to constructed the FMDP.
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.
MultiDimFunctionGraph< GUM_SCALAR > * FunctionGraph__
The FunctionGraph we&#39;re building at a given time.
Definition: fmdpFactory.h:301
void checkModalityInBag__(const std::string &mod)
Used in VARIABLE mode Checks if in stringBag__ there is no other modality with the same name...
void variableDescription(const std::string &desc)
Tells the factory the current variable&#39;s description.
bool bar_flag__
Depending on the context this flag is used for some VERY important reasons.
Definition: fmdpFactory.h:271
std::vector< FMDPfactory_state > states__
State stack.
Definition: fmdpFactory.h:295
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 finalizeFunctionGraph__()
Insert every variables in the function graph.
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:307
void startTransitionDeclaration()
Tells the factory that we&#39;re in a transition declaration.
void illegalStateError__(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".