aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
actionSet.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 MDDOperatorStrategy planer class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 
30 // =========================================================================
31 #ifndef GUM_ACTION_SET_H
32 #define GUM_ACTION_SET_H
33 // =========================================================================
34 #include <thread>
35 // =========================================================================
36 #include <agrum/tools/core/argMaxSet.h>
37 #include <agrum/tools/core/functors.h>
38 #include <agrum/tools/core/inline.h>
39 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
40 // =========================================================================
41 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
42 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/terminalNodePolicies/SetTerminalNodePolicy.h>
43 // =========================================================================
44 #include <agrum/FMDP/fmdp.h>
45 // =========================================================================
46 
47 namespace gum {
48 
49  /**
50  * @struct ArgumentMaximisesAction actionSet.h
51  * <agrum/FMDP/planning/actionSet.h>
52  * @brief Argument Maximization function object class
53  * @ingroup core
54  *
55  * Returns the set that has the maximal value between its two arguments sets
56  */
57  template < typename GUM_SCALAR >
59  // ###########################################################################
60  /// @name Operator()
61  // ###########################################################################
62  /// @{
63 
64  const GUM_SCALAR& operator()(const GUM_SCALAR& x, const GUM_SCALAR& y) const {
65  if (x > y) { return x; }
66  if (x < y) { return y; }
67 
68  temp__ = x;
69  temp__ += y;
70  return temp__;
71  }
72 
73  private:
74  mutable GUM_SCALAR temp__;
75  };
76 
77 
78  /**
79  * @class ActionSet
80  * @headerfile actionSet.h <agrum/FMDP/planning/actionSet.h>
81  * @brief A class to store the optimal actions.
82  * @ingroup fmdp_group
83  *
84  * Stores the ids of optimal actions. To be used as leaves on optimal policy
85  * tree or function graph
86  *
87  */
88  class ActionSet {
89  public:
90  // ###########################################################################
91  /// @name CNL
92  // ###########################################################################
93  /// @{
94 
95  // ============================================================================
96  /// Constructor
97  // ============================================================================
99  GUM_CONSTRUCTOR(ActionSet);
100  actionSeq__ = new Sequence< Idx >();
101  }
102 
103  ActionSet(const ActionSet& src) {
104  GUM_CONSTRUCTOR(ActionSet);
105  actionSeq__ = new Sequence< Idx >();
106  for (auto idi = src.beginSafe(); idi != src.endSafe(); ++idi)
107  actionSeq__->insert(*idi);
108  }
109 
110  ActionSet& operator=(const ActionSet& src) {
111  actionSeq__ = new Sequence< Idx >();
112  for (auto idi = src.beginSafe(); idi != src.endSafe(); ++idi)
113  actionSeq__->insert(*idi);
114  return *this;
115  }
116 
117  // ============================================================================
118  /// Destructor
119  // ============================================================================
121  GUM_DESTRUCTOR(ActionSet);
122  delete actionSeq__;
123  }
124 
125  // ============================================================================
126  /// Allocators and Deallocators redefinition
127  // ============================================================================
128  void* operator new(size_t s) {
129  return SmallObjectAllocator::instance().allocate(s);
130  }
131  void operator delete(void* p) {
132  SmallObjectAllocator::instance().deallocate(p, sizeof(ActionSet));
133  }
134 
135  /// @}
136 
137  // ###########################################################################
138  /// @name Iterators
139  // ###########################################################################
140  /// @{
141 
142  // ============================================================================
143  /// Iterator beginning
144  // ============================================================================
146  return actionSeq__->beginSafe();
147  }
148 
149  // ============================================================================
150  /// Iterator end
151  // ============================================================================
152  SequenceIteratorSafe< Idx > endSafe() const { return actionSeq__->endSafe(); }
153 
154  /// @}
155 
156  // ###########################################################################
157  /// @name Operators
158  // ###########################################################################
159  /// @{
160 
161  // ============================================================================
162  /// Ajout d'un élément
163  // ============================================================================
164  ActionSet& operator+=(const Idx& elem) {
165  actionSeq__->insert(elem);
166  return *this;
167  }
168 
169  // ============================================================================
170  /// Use to insert the content of another set inside this one
171  // ============================================================================
172  ActionSet& operator+=(const ActionSet& src) {
173  for (auto iter = src.beginSafe(); iter != src.endSafe(); ++iter)
174  if (!actionSeq__->exists(*iter)) actionSeq__->insert(*iter);
175  return *this;
176  }
177 
178  // ============================================================================
179  /// Use to insert the content of another set inside this one
180  // ============================================================================
181  ActionSet& operator-=(const ActionSet& src) {
182  for (auto iter = src.beginSafe(); iter != src.endSafe(); ++iter)
183  if (actionSeq__->exists(*iter)) actionSeq__->erase(*iter);
184  return *this;
185  }
186 
187  // ============================================================================
188  /// Gives the ith element
189  // ============================================================================
190  const Idx& operator[](const Idx i) const { return actionSeq__->atPos(i); }
191 
192  // ============================================================================
193  /// Compares two ActionSet to check if they are equals
194  // ============================================================================
195  bool operator==(const ActionSet& compared) const {
196  for (auto iter = compared.beginSafe(); iter != compared.endSafe(); ++iter)
197  if (!actionSeq__->exists(*iter)) return false;
198  for (auto iter = this->beginSafe(); iter != this->endSafe(); ++iter)
199  if (!compared.exists(*iter)) return false;
200  return true;
201  }
202  bool operator!=(const ActionSet& compared) const {
203  return !(*this == compared);
204  }
205 
206  /// @}
207 
208  // ============================================================================
209  /// Gives the size
210  // ============================================================================
211  Size size() const { return actionSeq__->size(); }
212 
213  bool exists(const Idx& elem) const { return actionSeq__->exists(elem); }
214 
215  private:
216  /// The very bone of the ActionSet
218 
219  friend std::ostream& operator<<(std::ostream& streamy, const ActionSet& objy) {
220  streamy << objy.actionSeq__->toString();
221  return streamy;
222  }
223  };
224 } // namespace gum
225 #endif // GUM_ACTION_SET_H
Sequence< Idx > * actionSeq__
The very bone of the ActionSet.
Definition: actionSet.h:217
const GUM_SCALAR & operator()(const GUM_SCALAR &x, const GUM_SCALAR &y) const
Definition: actionSet.h:64
bool operator==(const ActionSet &compared) const
Compares two ActionSet to check if they are equals.
Definition: actionSet.h:195
Size size() const
Gives the size.
Definition: actionSet.h:211
A class to store the optimal actions.
Definition: actionSet.h:88
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ActionSet(const ActionSet &src)
Constructor.
Definition: actionSet.h:103
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: actionSet.h:128
~ActionSet()
Destructor.
Definition: actionSet.h:120
bool exists(const Idx &elem) const
Definition: actionSet.h:213
SequenceIteratorSafe< Idx > endSafe() const
Iterator end.
Definition: actionSet.h:152
ActionSet & operator=(const ActionSet &src)
Constructor.
Definition: actionSet.h:110
ActionSet & operator+=(const ActionSet &src)
Use to insert the content of another set inside this one.
Definition: actionSet.h:172
ActionSet()
Constructor.
Definition: actionSet.h:98
ActionSet & operator-=(const ActionSet &src)
Use to insert the content of another set inside this one.
Definition: actionSet.h:181
SequenceIteratorSafe< Idx > beginSafe() const
Iterator beginning.
Definition: actionSet.h:145
ActionSet & operator+=(const Idx &elem)
Ajout d&#39;un élément.
Definition: actionSet.h:164
bool operator!=(const ActionSet &compared) const
Ajout d&#39;un élément.
Definition: actionSet.h:202
const Idx & operator[](const Idx i) const
Gives the ith element.
Definition: actionSet.h:190
void operator delete(void *p)
Constructor.
Definition: actionSet.h:131
<agrum/FMDP/planning/actionSet.h>
Definition: actionSet.h:58