aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
actionSet.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 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) { return SmallObjectAllocator::instance().allocate(s); }
129  void operator delete(void* p) {
130  SmallObjectAllocator::instance().deallocate(p, sizeof(ActionSet));
131  }
132 
133  /// @}
134 
135  // ###########################################################################
136  /// @name Iterators
137  // ###########################################################################
138  /// @{
139 
140  // ============================================================================
141  /// Iterator beginning
142  // ============================================================================
143  SequenceIteratorSafe< Idx > beginSafe() const { return _actionSeq_->beginSafe(); }
144 
145  // ============================================================================
146  /// Iterator end
147  // ============================================================================
148  SequenceIteratorSafe< Idx > endSafe() const { return _actionSeq_->endSafe(); }
149 
150  /// @}
151 
152  // ###########################################################################
153  /// @name Operators
154  // ###########################################################################
155  /// @{
156 
157  // ============================================================================
158  /// Ajout d'un élément
159  // ============================================================================
160  ActionSet& operator+=(const Idx& elem) {
161  _actionSeq_->insert(elem);
162  return *this;
163  }
164 
165  // ============================================================================
166  /// Use to insert the content of another set inside this one
167  // ============================================================================
168  ActionSet& operator+=(const ActionSet& src) {
169  for (auto iter = src.beginSafe(); iter != src.endSafe(); ++iter)
170  if (!_actionSeq_->exists(*iter)) _actionSeq_->insert(*iter);
171  return *this;
172  }
173 
174  // ============================================================================
175  /// Use to insert the content of another set inside this one
176  // ============================================================================
177  ActionSet& operator-=(const ActionSet& src) {
178  for (auto iter = src.beginSafe(); iter != src.endSafe(); ++iter)
179  if (_actionSeq_->exists(*iter)) _actionSeq_->erase(*iter);
180  return *this;
181  }
182 
183  // ============================================================================
184  /// Gives the ith element
185  // ============================================================================
186  const Idx& operator[](const Idx i) const { return _actionSeq_->atPos(i); }
187 
188  // ============================================================================
189  /// Compares two ActionSet to check if they are equals
190  // ============================================================================
191  bool operator==(const ActionSet& compared) const {
192  for (auto iter = compared.beginSafe(); iter != compared.endSafe(); ++iter)
193  if (!_actionSeq_->exists(*iter)) return false;
194  for (auto iter = this->beginSafe(); iter != this->endSafe(); ++iter)
195  if (!compared.exists(*iter)) return false;
196  return true;
197  }
198  bool operator!=(const ActionSet& compared) const { return !(*this == compared); }
199 
200  /// @}
201 
202  // ============================================================================
203  /// Gives the size
204  // ============================================================================
205  Size size() const { return _actionSeq_->size(); }
206 
207  bool exists(const Idx& elem) const { return _actionSeq_->exists(elem); }
208 
209  private:
210  /// The very bone of the ActionSet
212 
213  friend std::ostream& operator<<(std::ostream& streamy, const ActionSet& objy) {
214  streamy << objy._actionSeq_->toString();
215  return streamy;
216  }
217  };
218 } // namespace gum
219 #endif // GUM_ACTION_SET_H
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:191
Size size() const
Gives the size.
Definition: actionSet.h:205
A class to store the optimal actions.
Definition: actionSet.h:88
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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:207
SequenceIteratorSafe< Idx > endSafe() const
Iterator end.
Definition: actionSet.h:148
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:168
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:177
SequenceIteratorSafe< Idx > beginSafe() const
Iterator beginning.
Definition: actionSet.h:143
Sequence< Idx > * _actionSeq_
The very bone of the ActionSet.
Definition: actionSet.h:211
ActionSet & operator+=(const Idx &elem)
Ajout d&#39;un élément.
Definition: actionSet.h:160
bool operator!=(const ActionSet &compared) const
Ajout d&#39;un élément.
Definition: actionSet.h:198
const Idx & operator[](const Idx i) const
Gives the ith element.
Definition: actionSet.h:186
void operator delete(void *p)
Constructor.
Definition: actionSet.h:129
<agrum/FMDP/planning/actionSet.h>
Definition: actionSet.h:58