aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
nodeDatabase.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 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 = typename TestSelect< AttributeSelection,
65 
66  public:
67  // ==========================================================================
68  /// @name Constructor & destructor.
69  // ==========================================================================
70  /// @{
71 
72  // ###################################################################
73  /// Default constructor
74  // ###################################################################
75  NodeDatabase(const Set< const DiscreteVariable* >*, const DiscreteVariable* = nullptr);
76 
77  // ###################################################################
78  /// Default destructor
79  // ###################################################################
80  ~NodeDatabase();
81 
82  // ============================================================================
83  /// Allocators and Deallocators redefinition
84  // ============================================================================
85  void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
86  void operator delete(void* p) {
87  SmallObjectAllocator::instance().deallocate(p, sizeof(NodeDatabase));
88  }
89 
90  /// @}
91 
92  // ==========================================================================
93  /// @name Observation handling methods
94  // ==========================================================================
95  /// @{
96 
97  // ###################################################################
98  /** Updates database with new observation
99  *
100  * Calls either @fn _addObservation_( const Observation*, Int2Type<true>)
101  * or @fn _addObservation_( const Observation*, Int2Type<false>)
102  * depending on if we're learning reward function or transition probability
103  **/
104  // ###################################################################
105  void addObservation(const Observation*);
106 
107  private:
108  void _addObservation_(const Observation*, Int2Type< true >);
109  void _addObservation_(const Observation*, Int2Type< false >);
110 
111  public:
112  // ###################################################################
113  /// Nb observation taken into account by this instance
114  // ###################################################################
115  INLINE Idx nbObservation() const { return _nbObservation_; }
116 
117  /// @}
118 
119  // ==========================================================================
120  /// @name Variable Test Methods
121  // ==========================================================================
122  /// @{
123 
124  // ###################################################################
125  /// Indicates wether or not, node has sufficient observation so that
126  /// any statistic is relevant
127  // ###################################################################
129  return _attrTable_[var]->isTestRelevant();
130  }
131 
132  // ###################################################################
133  /// Returns the performance of given variables according to selection
134  /// criterion
135  // ###################################################################
136  INLINE double testValue(const DiscreteVariable* var) const { return _attrTable_[var]->score(); }
137 
138  // ###################################################################
139  /// Returns the performance of given variables according to selection
140  /// secondary criterion (to break ties)
141  // ###################################################################
143  return _attrTable_[var]->secondaryscore();
144  }
145 
146  /// @}
147 
148  // ==========================================================================
149  /// @name Aggregation Methods
150  // ==========================================================================
151  /// @{
152 
153  // ###################################################################
154  /// Merges given NodeDatabase informations into current nDB.
155  // ###################################################################
158 
159  // ###################################################################
160  /// Returns a reference to nDB test policy for given variable
161  /// (so that test policy information can be merged too)
162  // ###################################################################
164  return _attrTable_[var];
165  }
166 
167  // ###################################################################
168  /// Iterators on value count to recopy correctly its content
169  // ###################################################################
171  return _valueCount_.cbeginSafe();
172  }
174  return _valueCount_.cendSafe();
175  }
176 
177  /// @}
178 
179  // ###################################################################
180  ///
181  // ###################################################################
182 
183  Idx effectif(Idx moda) const {
184  return _valueCount_.exists(ValueType(moda)) ? _valueCount_[ValueType(moda)] : 0;
185  }
186 
187  Idx valueDomain() const { return _valueDomain_(Int2Type< isScalar >()); }
188 
189  private:
190  Idx _valueDomain_(Int2Type< true >) const { return _valueCount_.size(); }
191  Idx _valueDomain_(Int2Type< false >) const { return _value_->domainSize(); }
192 
193  std::string toString() const;
194 
195  private:
196  /// Table giving for every variables its instantiation
198 
199  /// So does this reference on the value observed
201 
202  ///
204 
205  ///
207  };
208 
209 
210 } /* namespace gum */
211 
212 #include <agrum/FMDP/learning/datastructure/nodeDatabase_tpl.h>
213 
214 #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:163
HashTable< ValueType, Idx > _valueCount_
Definition: nodeDatabase.h:206
Idx _valueDomain_(Int2Type< false >) const
Definition: nodeDatabase.h:191
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
Definition: nodeDatabase.h:173
Idx valueDomain() const
Definition: nodeDatabase.h:187
void _addObservation_(const Observation *, Int2Type< false >)
depending on if we&#39;re learning reward function or transition probability
NodeDatabase(const Set< const DiscreteVariable * > *, const DiscreteVariable *=nullptr)
Default constructor.
std::string toString() const
const DiscreteVariable * _value_
So does this reference on the value observed.
Definition: nodeDatabase.h:200
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:170
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:85
Idx effectif(Idx moda) const
Definition: nodeDatabase.h:183
void operator delete(void *p)
Default constructor.
Definition: nodeDatabase.h:86
~NodeDatabase()
Default destructor.