aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
fusionContext.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 Fusion Context class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 
29 // =========================================================================
30 #ifndef GUM_FUSION_CONTEXT_H
31 #define GUM_FUSION_CONTEXT_H
32 // =========================================================================
33 #include <agrum/tools/core/multiPriorityQueue.h>
34 // =========================================================================
35 #include <agrum/tools/graphs/graphElements.h>
36 // =========================================================================
37 #include <agrum/FMDP/learning/core/templateStrategy.h>
38 #include <agrum/FMDP/learning/datastructure/leaves/abstractLeaf.h>
39 #include <agrum/FMDP/learning/datastructure/leaves/leafPair.h>
40 // =========================================================================
41 
42 namespace gum {
43 
44  /**
45  * @class FusionContext fusionContext.h
46  * <agrum/FMDP/learning/datastructure/leaves/fusionContext.h>
47  * @brief Contains leaves situation after a merging have been made
48  * @ingroup fmdp_group
49  *
50  */
51 
54 
55 
56  template < bool isInitial = false >
57  class FusionContext {
58  public:
59  // ==========================================================================
60  /// @name Constructor & destructor.
61  // ==========================================================================
62  /// @{
63 
64  // ###################################################################
65  /// Default constructor
66  // ###################################################################
68 
69  // ###################################################################
70  /// Default destructor
71  // ###################################################################
72  ~FusionContext();
73 
74  // ============================================================================
75  /// Allocators and Deallocators redefinition
76  // ============================================================================
77  void* operator new(size_t s) {
78  return SmallObjectAllocator::instance().allocate(s);
79  }
80  void operator delete(void* p) {
81  SmallObjectAllocator::instance().deallocate(p, sizeof(FusionContext));
82  }
83 
84  /// @}
85 
86  // ==========================================================================
87  /// @name Associated Leaves Handling methods
88  // ==========================================================================
89  /// @{
90 
91  // ###################################################################
92  ///
93  // ###################################################################
94  public:
95  bool containsAssociatedLeaf(AbstractLeaf* l) {
96  return containsAssociatedLeaf__(l, Int2Type< isInitial >());
97  }
98 
99  private:
100  bool containsAssociatedLeaf__(AbstractLeaf* l, Int2Type< false >) {
101  return leaf2Pair__.exists(l);
102  }
104  return false;
105  }
106 
107  // ###################################################################
108  ///
109  // ###################################################################
110  public:
111  bool associateLeaf(AbstractLeaf* l) {
112  return associateLeaf__(l, Int2Type< isInitial >());
113  }
114 
115  private:
116  bool associateLeaf__(AbstractLeaf*, Int2Type< false >);
117  bool associateLeaf__(AbstractLeaf*, Int2Type< true >) { return false; }
118 
119 
120  // ###################################################################
121  ///
122  // ###################################################################
123  public:
124  bool updateAssociatedLeaf(AbstractLeaf* l) {
125  return updateAssociatedLeaf__(l, Int2Type< isInitial >());
126  }
127 
128  private:
130  bool updateAssociatedLeaf__(AbstractLeaf*, Int2Type< true >) { return false; }
131 
132  public:
134  return updateAllAssociatedLeaves__(Int2Type< isInitial >());
135  }
136 
137  private:
138  bool updateAllAssociatedLeaves__(Int2Type< false >);
139  bool updateAllAssociatedLeaves__(Int2Type< true >) { return false; }
140 
141 
142  // ###################################################################
143  ///
144  /// @warning : won't delete Associated Pair created (because subsequent
145  /// fusioncontexts might be using it)
146  // ###################################################################
147  public:
148  bool deassociateLeaf(AbstractLeaf* l) {
149  return deassociateLeaf__(l, Int2Type< isInitial >());
150  }
151 
152  private:
153  bool deassociateLeaf__(AbstractLeaf*, Int2Type< false >);
154  bool deassociateLeaf__(AbstractLeaf*, Int2Type< true >) { return false; }
155 
156  /// @}
157 
158  public:
159  // ==========================================================================
160  /// @name Pair handling methods
161  // ==========================================================================
162  /// @{
163 
164  // ###################################################################
165  ///
166  // ###################################################################
167  bool addPair(LeafPair* p);
168 
169  // ###################################################################
170  ///
171  // ###################################################################
172  bool updatePair(LeafPair* p);
173 
174  // ###################################################################
175  ///
176  // ###################################################################
177  bool removePair(LeafPair* p);
178 
179 
180  pair_iterator beginPairs() { return pairsHeap__.allValues().beginSafe(); }
181  pair_iterator endPairs() { return pairsHeap__.allValues().endSafe(); }
182 
183  /// @}
184 
185  // ==========================================================================
186  /// @name Best Pair access methods
187  // ==========================================================================
188  /// @{
189 
190  // ###################################################################
191  ///
192  // ###################################################################
193  LeafPair* top() { return !pairsHeap__.empty() ? pairsHeap__.top() : nullptr; }
194 
195  // ###################################################################
196  ///
197  // ###################################################################
198  double topLikelyhood() {
199  return !pairsHeap__.empty() ? pairsHeap__.topPriority() : 1.0;
200  }
201 
202  /// @}
203 
204  // ==========================================================================
205  /// @name FusionContext Leaf and associated pairs handling methods
206  // ==========================================================================
207  /// @{
208 
209  // ###################################################################
210  ///
211  // ###################################################################
212  AbstractLeaf* leaf() { return leaf__; }
213 
214  // ###################################################################
215  ///
216  // ###################################################################
218  return leaf2Pair__.getWithDefault(l, nullptr);
219  }
220 
221  // ###################################################################
222  ///
223  // ###################################################################
224  public:
226  return associatedPairs__(Int2Type< isInitial >());
227  }
228 
229  private:
230  Set< LeafPair* > associatedPairs__(Int2Type< false >);
232  return Set< LeafPair* >();
233  }
234  /// @}
235 
236  public:
237  std::string toString();
238 
239  private:
240  MultiPriorityQueue< LeafPair*, double, std::less< double > > pairsHeap__;
241 
243 
245  };
246 
247 
248 } /* namespace gum */
249 
250 #include <agrum/FMDP/learning/datastructure/leaves/fusionContext_tpl.h>
251 
252 #endif // GUM_FUSION_CONTEXT_H
void operator delete(void *p)
Default constructor.
Definition: fusionContext.h:80
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
MultiPriorityQueue< LeafPair *, double, std::less< double > > pairsHeap__
bool removePair(LeafPair *p)
pair_iterator endPairs()
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: fusionContext.h:77
Set< LeafPair *> associatedPairs__(Int2Type< true >)
AbstractLeaf * leaf()
LeafPair * leafAssociatedPair(AbstractLeaf *l)
Set< LeafPair *> associatedPairs()
FusionContext(AbstractLeaf *)
Default constructor.
AbstractLeaf * leaf__
bool updateAssociatedLeaf__(AbstractLeaf *, Int2Type< true >)
bool addPair(LeafPair *p)
bool updateAllAssociatedLeaves__(Int2Type< true >)
bool deassociateLeaf(AbstractLeaf *l)
bool containsAssociatedLeaf__(AbstractLeaf *, Int2Type< true >)
bool containsAssociatedLeaf(AbstractLeaf *l)
Definition: fusionContext.h:95
LeafPair * top()
bool updateAllAssociatedLeaves()
~FusionContext()
Default destructor.
bool associateLeaf(AbstractLeaf *l)
std::string toString()
HashTable< AbstractLeaf *, LeafPair *> leaf2Pair__
bool associateLeaf__(AbstractLeaf *, Int2Type< true >)
bool updateAssociatedLeaf(AbstractLeaf *l)
bool deassociateLeaf__(AbstractLeaf *, Int2Type< true >)
bool updatePair(LeafPair *p)
pair_iterator beginPairs()