aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
IfmdpFactory.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 IFMDPFactory interface-like class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 
30 #ifndef GUM_ABSTRACT_FMDP_FACTORY_H
31 #define GUM_ABSTRACT_FMDP_FACTORY_H
32 
33 #include <string>
34 #include <vector>
35 
36 #include <agrum/tools/graphs/graphElements.h>
37 #include <agrum/tools/variables/discreteVariable.h>
38 
39 #include <agrum/FMDP/fmdp.h>
40 
41 
42 namespace gum {
43 
44  /**
45  * @brief The enumeration of states in which the factory can be in.
46  *
47  * Every documentation section's name indicates from which state you can
48  * call it's methods, and in which state it places the factory.
49  *
50  * There is an exception for the delegated CPT definition methods which do
51  * not changes the state of the factory.
52  */
53  enum class FMDPfactory_state : char
54  {
55  NONE,
56  VARIABLE,
57  ACTION,
58  TRANSITION,
59  COST,
60  REWARD,
61  DISCOUNT
62  };
63 
64  /**
65  * @class AbstractFMDPFactory
66  * @headerfile IfmdpFactory.h <agrum/FMDP/IfmdpFactory.h>
67  * @brief A factory class to ease Factored Markov Decision Process
68  * construction.
69  * @ingroup fmdp_group
70  * A FMDPFactory will never create a Factored Markov Decision Process and
71  * works on only one
72  * Factored Markov Decision Process.
73  *
74  * The only exception of this behaviour is when you create a copy of the
75  * factory,
76  * it will create a copy of its FMDP. This is useful when you want to create
77  * two FMDP sharing a common base.
78  * However be very careful because the copy will not delete its FMDP.
79  *
80  * Each method will raise an OperationNotAllowed if you call it when the
81  * factory
82  * is not in a valid state for that call. The error message is "Illegal
83  * state.".
84  *
85  */
86 
88  public:
89  // ==========================================================================
90  /// @name Constructor & destructor.
91  // ==========================================================================
92  /// @{
93 
94  //~ /**
95  //~ * @brief Copy constructor.
96  //~ * The copy will have an exact copy of the constructed Factored markov
97  // Decision Process in source.
98  //~ * @warning You can only copy a factory if its current state is NONE or
99  //~ * NETWORK.
100  //~ * @throw OperationNotAllowed Raised if the state of source is not NONE
101  // or
102  //~ * NETWORK.
103  //~ */
104  //~ FMDPFactory ( const FMDPFactory<GUM_SCALAR>& source );
105 
106  /**
107  * @brief Destructor.
108  *
109  * To prevent strange behaviour you should always destroy a FMDPFactory
110  * when it's state equals NONE.
111  *
112  * @throw FatalError Raised if the state of the factory prevents it to die
113  * peacefully.
114  */
115  virtual ~AbstractFMDPFactory(){};
116 
117  /// @}
118  // ==========================================================================
119  /// @name Getter and setters.
120  // ==========================================================================
121  /// @{
122 
123  /// Returns the current state of the factory.
124  virtual FMDPfactory_state state() const = 0;
125 
126  /// Returns a constant reference on a variable given it's name.
127  /// @throw NotFound Raised if no variable matches the name.
128  virtual const DiscreteVariable* variable(const std::string& name) const = 0;
129 
130  /// @}
131  // ==========================================================================
132  /// @name Variable declaration methods (NONE -> VARIABLE)
133  // ==========================================================================
134  /// @{
135 
136  /// Tells the factory that we're in a variable declaration.
137  virtual void startVariableDeclaration() = 0;
138 
139  /// Tells the factory the current variable's name.
140  /// @throw DuplicateElement Raised if a variable with the same name already
141  /// exist.
142  virtual void variableName(const std::string& name) = 0;
143 
144  /// Tells the factory the current variable's description.
145  virtual void variableDescription(const std::string& desc) = 0;
146 
147  /// Adds a modality to the current variable.
148  virtual void addModality(const std::string& name) = 0;
149 
150  /// Tells the factory that we're out of a variable declaration.
151  /// @throw UndefinedElement Raised if the variable isn't defined (or not
152  /// enough defined).
153  virtual void endVariableDeclaration() = 0;
154 
155  /// @}
156  // ==========================================================================
157  /// @name Action declaration methods (NONE -> ACTION)
158  // ==========================================================================
159  /// @{
160 
161  /// Tells the factory that we're in an action declaration.
162  virtual void startActionDeclaration() = 0;
163 
164  /// Tells the factory to add an action to the current fmdp.
165  virtual void addAction(const std::string& action) = 0;
166 
167  /// Tells the factory that we're out of an action declaration.
168  virtual void endActionDeclaration() = 0;
169 
170  /// @}
171  // ==========================================================================
172  /// @name Transition declaration methods (NONE -> TRANSITION <- ACTION)
173  // ==========================================================================
174  /// @{
175 
176  /// Tells the factory that we're in a transition declaration.
177  virtual void startTransitionDeclaration() = 0;
178 
179  /// Tells the factory to add a transition table to the current fmdp.
180  virtual void addTransition(const std::string& var,
181  const MultiDimAdressable* transition)
182  = 0;
183 
184  /// Tells the factory to add a transition table to the current fmdp.
185  /// This transition table will be extracted from incorporated
186  /// multiDimFunctionGraph.
187  virtual void addTransition(const std::string& var) = 0;
188 
189  /// Tells the factory that we're out of a transition declaration.
190  virtual void endTransitionDeclaration() = 0;
191 
192  /// @}
193  // ==========================================================================
194  /// @name Cost declaration methods (NONE -> COST <- ACTION)
195  // ==========================================================================
196  /// @{
197 
198  /// Tells the factory that we're in a cost declaration.
199  virtual void startCostDeclaration() = 0;
200 
201  /// Tells the factory to add a cost table to the current fmdp.
202  virtual void addCost(const MultiDimAdressable* cost) = 0;
203 
204  /// Tells the factory to add current decision diagram it has as a cost
205  /// table.
206  virtual void addCost() = 0;
207 
208  /// Tells the factory that we're out of a cost declaration.
209  virtual void endCostDeclaration() = 0;
210 
211  /// @}
212  // ==========================================================================
213  /// @name Reward declaration methods (NONE -> REWARD <- ACTION)
214  // ==========================================================================
215  /// @{
216 
217  /// Tells the factory that we're in a cost declaration.
218  virtual void startRewardDeclaration() = 0;
219 
220  /// Tells the factory that we're in a reward declaration mode where the
221  /// global reward diagram is an operation between
222  /// simplier decision diagram..
223  virtual void setOperationModeOn(std::string operationType) = 0;
224 
225  /// Tells the factory to add a reward table to the current fmdp.
226  virtual void addReward(const MultiDimAdressable* reward) = 0;
227 
228  /// Tells the factory to add current decision diagram it has as a reward
229  /// table.
230  virtual void addReward() = 0;
231 
232  /// Tells the factory that we're out of a cost declaration.
233  virtual void endRewardDeclaration() = 0;
234 
235  /// @}
236  // ==========================================================================
237  /// @name Discount declaration methods (NONE -> DISCOUNT)
238  // ==========================================================================
239  /// @{
240 
241  /// Tells the factory that we're in a cost declaration.
242  virtual void startDiscountDeclaration() = 0;
243 
244  /// Tells the factory to add a cost table to the current fmdp.
245  virtual void addDiscount(float discount) = 0;
246 
247  /// Tells the factory that we're out of a cost declaration.
248  virtual void endDiscountDeclaration() = 0;
249 
250  /// @}
251  // ==========================================================================
252  /// @name FunctionGraph Creation specific methods
253  // ==========================================================================
254  /// @{
255 
256  /// Insert in diagram a non terminal node
258 
259  /// Insert in diagram a terminal node
260  virtual NodeId addTerminalNode(float value) = 0;
261 
262  /// Insert in diagram an arc
263  virtual void addArc(NodeId from, NodeId to, Idx modality) = 0;
264 
265  /// Set the root of the diagram
266  virtual void setRoot(NodeId rootId) = 0;
267 
268  /// @}
269 
270  /**
271  * @name verbosity control
272  * @{
273  */
274  void setVerbose() { verbose__ = true; }
275 
276  void resetVerbose() { verbose__ = false; }
277 
278  bool isVerbose() { return verbose__; }
279  /// @}
280 
281  private:
282  bool verbose__;
283  };
284 } /* namespace gum */
285 
286 
287 #endif // GUM_ABSTRACT_FMDP_FACTORY_H
virtual void setRoot(NodeId rootId)=0
Set the root of the diagram.
virtual void setOperationModeOn(std::string operationType)=0
Tells the factory that we&#39;re in a reward declaration mode where the global reward diagram is an opera...
virtual ~AbstractFMDPFactory()
Destructor.
Definition: IfmdpFactory.h:115
virtual void startRewardDeclaration()=0
Tells the factory that we&#39;re in a cost declaration.
virtual void startVariableDeclaration()=0
Tells the factory that we&#39;re in a variable declaration.
virtual NodeId addInternalNode(std::string name_of_var)=0
Insert in diagram a non terminal node.
virtual void addArc(NodeId from, NodeId to, Idx modality)=0
Insert in diagram an arc.
virtual void endTransitionDeclaration()=0
Tells the factory that we&#39;re out of a transition declaration.
virtual void variableDescription(const std::string &desc)=0
Tells the factory the current variable&#39;s description.
virtual void addModality(const std::string &name)=0
Adds a modality to the current variable.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual void addReward(const MultiDimAdressable *reward)=0
Tells the factory to add a reward table to the current fmdp.
FMDPfactory_state
The enumeration of states in which the factory can be in.
Definition: IfmdpFactory.h:53
virtual void startDiscountDeclaration()=0
Tells the factory that we&#39;re in a cost declaration.
virtual NodeId addTerminalNode(float value)=0
Insert in diagram a terminal node.
virtual void addDiscount(float discount)=0
Tells the factory to add a cost table to the current fmdp.
virtual void endCostDeclaration()=0
Tells the factory that we&#39;re out of a cost declaration.
virtual FMDPfactory_state state() const =0
Returns the current state of the factory.
virtual void addCost(const MultiDimAdressable *cost)=0
Tells the factory to add a cost table to the current fmdp.
virtual void addTransition(const std::string &var, const MultiDimAdressable *transition)=0
Tells the factory to add a transition table to the current fmdp.
virtual const DiscreteVariable * variable(const std::string &name) const =0
Returns a constant reference on a variable given it&#39;s name.
virtual void endVariableDeclaration()=0
Tells the factory that we&#39;re out of a variable declaration.
virtual void endDiscountDeclaration()=0
Tells the factory that we&#39;re out of a cost declaration.
virtual void endActionDeclaration()=0
Tells the factory that we&#39;re out of an action declaration.
virtual void variableName(const std::string &name)=0
Tells the factory the current variable&#39;s name.
virtual void addCost()=0
Tells the factory to add current decision diagram it has as a cost table.
virtual void startCostDeclaration()=0
Tells the factory that we&#39;re in a cost declaration.
A factory class to ease Factored Markov Decision Process construction.
Definition: IfmdpFactory.h:87
virtual void endRewardDeclaration()=0
Tells the factory that we&#39;re out of a cost declaration.
virtual void addAction(const std::string &action)=0
Tells the factory to add an action to the current fmdp.
virtual void addTransition(const std::string &var)=0
Tells the factory to add a transition table to the current fmdp. This transition table will be extrac...
virtual void startActionDeclaration()=0
Tells the factory that we&#39;re in an action declaration.
virtual void startTransitionDeclaration()=0
Tells the factory that we&#39;re in a transition declaration.
virtual void addReward()=0
Tells the factory to add current decision diagram it has as a reward table.