aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
nodeDatabase.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 NodeDatabase class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 
29 // =========================================================================
30 #ifndef GUM_NODE_DATABASE_H
31 #define GUM_NODE_DATABASE_H
32 // =========================================================================
33 #include <agrum/tools/core/hashTable.h>
34 #include <agrum/tools/core/sequence.h>
35 // =========================================================================
36 #include <agrum/FMDP/learning/core/templateStrategy.h>
37 #include <agrum/FMDP/learning/core/testPolicy/Chi2TestPolicy.h>
38 #include <agrum/FMDP/learning/core/testPolicy/GTestPolicy.h>
39 #include <agrum/FMDP/learning/core/testPolicy/leastSquareTestPolicy.h>
40 #include <agrum/FMDP/learning/observation.h>
41 // =========================================================================
42 #include <agrum/tools/variables/discreteVariable.h>
43 // =========================================================================
44 
45 namespace gum {
46 
47  /**
48  * @class NodeDatabase nodeDatabase.h
49  * <agrum/FMDP/learning/datastructure/nodeDatabase.h>
50  * @brief
51  * @ingroup fmdp_group
52  *
53  */
54 
55 
56  template < TESTNAME AttributeSelection, bool isScalar >
57  class NodeDatabase {
58  typedef typename ValueSelect< isScalar, double, Idx >::type ValueType;
59 
60  template < typename GUM_SCALAR >
61  using TestPolicy =
66 
67  public:
68  // ==========================================================================
69  /// @name Constructor & destructor.
70  // ==========================================================================
71  /// @{
72 
73  // ###################################################################
74  /// Default constructor
75  // ###################################################################
76  NodeDatabase(const Set< const DiscreteVariable* >*,
77  const DiscreteVariable* = nullptr);
78 
79  // ###################################################################
80  /// Default destructor
81  // ###################################################################
82  ~NodeDatabase();
83 
84  // ============================================================================
85  /// Allocators and Deallocators redefinition
86  // ============================================================================
87  void* operator new(size_t s) {
88  return SmallObjectAllocator::instance().allocate(s);
89  }
90  void operator delete(void* p) {
91  SmallObjectAllocator::instance().deallocate(p, sizeof(NodeDatabase));
92  }
93 
94  /// @}
95 
96  // ==========================================================================
97  /// @name Observation handling methods
98  // ==========================================================================
99  /// @{
100 
101  // ###################################################################
102  /** Updates database with new observation
103  *
104  * Calls either @fn addObservation__( const Observation*, Int2Type<true>)
105  * or @fn addObservation__( const Observation*, Int2Type<false>)
106  * depending on if we're learning reward function or transition probability
107  **/
108  // ###################################################################
109  void addObservation(const Observation*);
110 
111  private:
112  void addObservation__(const Observation*, Int2Type< true >);
113  void addObservation__(const Observation*, Int2Type< false >);
114 
115  public:
116  // ###################################################################
117  /// Nb observation taken into account by this instance
118  // ###################################################################
119  INLINE Idx nbObservation() const { return nbObservation__; }
120 
121  /// @}
122 
123  // ==========================================================================
124  /// @name Variable Test Methods
125  // ==========================================================================
126  /// @{
127 
128  // ###################################################################
129  /// Indicates wether or not, node has sufficient observation so that
130  /// any statistic is relevant
131  // ###################################################################
133  return attrTable__[var]->isTestRelevant();
134  }
135 
136  // ###################################################################
137  /// Returns the performance of given variables according to selection
138  /// criterion
139  // ###################################################################
140  INLINE double testValue(const DiscreteVariable* var) const {
141  return attrTable__[var]->score();
142  }
143 
144  // ###################################################################
145  /// Returns the performance of given variables according to selection
146  /// secondary criterion (to break ties)
147  // ###################################################################
149  return attrTable__[var]->secondaryscore();
150  }
151 
152  /// @}
153 
154  // ==========================================================================
155  /// @name Aggregation Methods
156  // ==========================================================================
157  /// @{
158 
159  // ###################################################################
160  /// Merges given NodeDatabase informations into current nDB.
161  // ###################################################################
164 
165  // ###################################################################
166  /// Returns a reference to nDB test policy for given variable
167  /// (so that test policy information can be merged too)
168  // ###################################################################
170  return attrTable__[var];
171  }
172 
173  // ###################################################################
174  /// Iterators on value count to recopy correctly its content
175  // ###################################################################
177  return valueCount__.cbeginSafe();
178  }
180  return valueCount__.cendSafe();
181  }
182 
183  /// @}
184 
185  // ###################################################################
186  ///
187  // ###################################################################
188 
189  Idx effectif(Idx moda) const {
190  return valueCount__.exists(ValueType(moda)) ? valueCount__[ValueType(moda)]
191  : 0;
192  }
193 
194  Idx valueDomain() const { return valueDomain__(Int2Type< isScalar >()); }
195 
196  private:
197  Idx valueDomain__(Int2Type< true >) const { return valueCount__.size(); }
198  Idx valueDomain__(Int2Type< false >) const { return value__->domainSize(); }
199 
200  std::string toString() const;
201 
202  private:
203  /// Table giving for every variables its instantiation
205 
206  /// So does this reference on the value observed
208 
209  ///
211 
212  ///
214  };
215 
216 
217 } /* namespace gum */
218 
219 #include <agrum/FMDP/learning/datastructure/nodeDatabase_tpl.h>
220 
221 #endif // GUM_NODE_DATABASE_H
const TestPolicy< ValueType > * testPolicy(const DiscreteVariable *var) const
Returns a reference to nDB test policy for given variable (so that test policy information can be mer...
Definition: nodeDatabase.h:169
const DiscreteVariable * value__
So does this reference on the value observed.
Definition: nodeDatabase.h:207
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
Definition: nodeDatabase.h:179
Idx valueDomain() const
Definition: nodeDatabase.h:194
NodeDatabase(const Set< const DiscreteVariable * > *, const DiscreteVariable *=nullptr)
Default constructor.
HashTable< ValueType, Idx > valueCount__
Definition: nodeDatabase.h:213
void addObservation__(const Observation *, Int2Type< false >)
depending on if we&#39;re learning reward function or transition probability
std::string toString() const
void addObservation(const Observation *)
Nb observation taken into account by this instance.
const HashTableConstIteratorSafe< ValueType, Idx > cbeginValues() const
Iterators on value count to recopy correctly its content.
Definition: nodeDatabase.h:176
NodeDatabase< AttributeSelection, isScalar > & operator+=(const NodeDatabase< AttributeSelection, isScalar > &src)
Merges given NodeDatabase informations into current nDB.
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: nodeDatabase.h:87
Idx valueDomain__(Int2Type< false >) const
Definition: nodeDatabase.h:198
Idx effectif(Idx moda) const
Definition: nodeDatabase.h:189
void operator delete(void *p)
Default constructor.
Definition: nodeDatabase.h:90
~NodeDatabase()
Default destructor.