aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
IfmdpFactory.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 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, const MultiDimAdressable* transition) = 0;
181 
182  /// Tells the factory to add a transition table to the current fmdp.
183  /// This transition table will be extracted from incorporated
184  /// multiDimFunctionGraph.
185  virtual void addTransition(const std::string& var) = 0;
186 
187  /// Tells the factory that we're out of a transition declaration.
188  virtual void endTransitionDeclaration() = 0;
189 
190  /// @}
191  // ==========================================================================
192  /// @name Cost declaration methods (NONE -> COST <- ACTION)
193  // ==========================================================================
194  /// @{
195 
196  /// Tells the factory that we're in a cost declaration.
197  virtual void startCostDeclaration() = 0;
198 
199  /// Tells the factory to add a cost table to the current fmdp.
200  virtual void addCost(const MultiDimAdressable* cost) = 0;
201 
202  /// Tells the factory to add current decision diagram it has as a cost
203  /// table.
204  virtual void addCost() = 0;
205 
206  /// Tells the factory that we're out of a cost declaration.
207  virtual void endCostDeclaration() = 0;
208 
209  /// @}
210  // ==========================================================================
211  /// @name Reward declaration methods (NONE -> REWARD <- ACTION)
212  // ==========================================================================
213  /// @{
214 
215  /// Tells the factory that we're in a cost declaration.
216  virtual void startRewardDeclaration() = 0;
217 
218  /// Tells the factory that we're in a reward declaration mode where the
219  /// global reward diagram is an operation between
220  /// simplier decision diagram..
221  virtual void setOperationModeOn(std::string operationType) = 0;
222 
223  /// Tells the factory to add a reward table to the current fmdp.
224  virtual void addReward(const MultiDimAdressable* reward) = 0;
225 
226  /// Tells the factory to add current decision diagram it has as a reward
227  /// table.
228  virtual void addReward() = 0;
229 
230  /// Tells the factory that we're out of a cost declaration.
231  virtual void endRewardDeclaration() = 0;
232 
233  /// @}
234  // ==========================================================================
235  /// @name Discount declaration methods (NONE -> DISCOUNT)
236  // ==========================================================================
237  /// @{
238 
239  /// Tells the factory that we're in a cost declaration.
240  virtual void startDiscountDeclaration() = 0;
241 
242  /// Tells the factory to add a cost table to the current fmdp.
243  virtual void addDiscount(float discount) = 0;
244 
245  /// Tells the factory that we're out of a cost declaration.
246  virtual void endDiscountDeclaration() = 0;
247 
248  /// @}
249  // ==========================================================================
250  /// @name FunctionGraph Creation specific methods
251  // ==========================================================================
252  /// @{
253 
254  /// Insert in diagram a non terminal node
256 
257  /// Insert in diagram a terminal node
258  virtual NodeId addTerminalNode(float value) = 0;
259 
260  /// Insert in diagram an arc
261  virtual void addArc(NodeId from, NodeId to, Idx modality) = 0;
262 
263  /// Set the root of the diagram
264  virtual void setRoot(NodeId rootId) = 0;
265 
266  /// @}
267 
268  /**
269  * @name verbosity control
270  * @{
271  */
272  void setVerbose() { _verbose_ = true; }
273 
274  void resetVerbose() { _verbose_ = false; }
275 
276  bool isVerbose() { return _verbose_; }
277  /// @}
278 
279  private:
280  bool _verbose_;
281  };
282 } /* namespace gum */
283 
284 
285 #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:643
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.