aGrUM  0.16.0
nodeDatabase_tpl.h
Go to the documentation of this file.
1 
29 // =========================================================================
31 // =========================================================================
32 
33 namespace gum {
34 
35  // ==========================================================================
36  // Constructor & destructor.
37  // ==========================================================================
38 
39  // ###################################################################
40  // Default constructor
41  // ###################################################################
42  template < TESTNAME AttributeSelection, bool isScalar >
44  const Set< const DiscreteVariable* >* attrSet,
45  const DiscreteVariable* value) :
46  __value(value) {
47  GUM_CONSTRUCTOR(NodeDatabase);
48 
50  attrSet->cbeginSafe();
51  varIter != attrSet->cendSafe();
52  ++varIter)
53  __attrTable.insert(*varIter, new TestPolicy< ValueType >());
54 
55  __nbObservation = 0;
56  }
57 
58 
59  // ###################################################################
60  // Default desstructor
61  // ###################################################################
62  template < TESTNAME AttributeSelection, bool isScalar >
64  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
65  ++varIter)
66  delete varIter.val();
67 
68  GUM_DESTRUCTOR(NodeDatabase);
69  }
70 
71 
72  // ==========================================================================
73  // Observation handling methods
74  // ==========================================================================
75 
76  // ###################################################################
77  /* Updates database with new observation
78  *
79  * Calls either @fn __addObservation( const Observation*, Int2Type<true>)
80  * or @fn __addObservation( const Observation*, Int2Type<false>)
81  * depending on if we're learning reward function or transition probability
82  */
83  // ###################################################################
84  template < TESTNAME AttributeSelection, bool isScalar >
86  const Observation* newObs) {
88  this->__addObservation(newObs, Int2Type< isScalar >());
89  }
90 
91  template < TESTNAME AttributeSelection, bool isScalar >
93  const Observation* newObs, Int2Type< true >) {
94  for (auto varIter = __attrTable.cbeginSafe();
95  varIter != __attrTable.cendSafe();
96  ++varIter)
97  varIter.val()->addObservation(newObs->rModality(varIter.key()),
98  newObs->reward());
99 
100  if (__valueCount.exists(newObs->reward()))
101  __valueCount[newObs->reward()]++;
102  else
103  __valueCount.insert(newObs->reward(), 1);
104  }
105 
106  template < TESTNAME AttributeSelection, bool isScalar >
108  const Observation* newObs, Int2Type< false >) {
109  for (auto varIter = __attrTable.cbeginSafe();
110  varIter != __attrTable.cendSafe();
111  ++varIter)
112  varIter.val()->addObservation(newObs->modality(varIter.key()),
113  newObs->modality(__value));
114 
115  if (__valueCount.exists(newObs->modality(__value)))
116  __valueCount[newObs->modality(__value)]++;
117  else
118  __valueCount.insert(newObs->modality(__value), 1);
119  }
120 
121 
122  // ==========================================================================
123  // Aggregation Methods
124  // ==========================================================================
125 
126 
127  // ###################################################################
128  // Merges given NodeDatabase informations into current nDB.
129  // ###################################################################
130  template < TESTNAME AttributeSelection, bool isScalar >
134  this->__nbObservation += src.nbObservation();
135 
136  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
137  ++varIter)
138  varIter.val()->add(*(src.testPolicy(varIter.key())));
139 
140  for (auto valIter = src.cbeginValues(); valIter != src.cendValues(); ++valIter)
141  if (__valueCount.exists(valIter.key()))
142  __valueCount[valIter.key()] += valIter.val();
143  else
144  __valueCount.insert(valIter.key(), valIter.val());
145 
146  return *this;
147  }
148 
149 
150  template < TESTNAME AttributeSelection, bool isScalar >
152  std::stringstream ss;
153 
154  ss << "NbObservation : " << this->nbObservation() << std::endl;
155  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
156  ++varIter)
157  ss << "\t\tVariable : " << varIter.key()->name()
158  << " - Associated Test : " << __attrTable[varIter.key()]->toString()
159  << std::endl;
160 
161  return ss.str();
162  }
163 } // End of namespace gum
164 
165 
166 // LEFT HERE ON PURPOSE
167 // NOT TO BE DELETED
168 
169 /*template<TESTNAME AttributeSelection, bool isScalar>
170 double *NodeDatabase<AttributeSelection, isScalar>::effectif(){
171  double* ret = static_cast<double*>(
172 SmallObjectAllocator::instance().allocate(sizeof(double)*__value->domainSize()));
173  for(Idx modality = 0; modality < __value->domainSize(); ++modality)
174  if( __valueCount.exists(modality) )
175  ret[modality] = (double)__valueCount[modality];
176  else
177  ret[modality] = 0.0;
178  return ret;
179 }*/
180 
181 /*template<TESTNAME AttributeSelection, bool isScalar>
182 double NodeDatabase<AttributeSelection, isScalar>::reward(){
183  double ret = 0.0;
184  for(auto valuTer = __valueCount.cbeginSafe(); valuTer !=
185 __valueCount.cendSafe(); ++valuTer)
186  ret += valuTer.key() * (double) valuTer.val();
187  return ret / __nbObservation;
188 }*/
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:170
const DiscreteVariable * __value
So does this reference on the value observed.
Definition: nodeDatabase.h:208
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:811
HashTable< ValueType, Idx > __valueCount
Definition: nodeDatabase.h:214
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
Definition: nodeDatabase.h:180
void __addObservation(const Observation *, Int2Type< true >)
Updates database with new observation.
typename TestSelect< AttributeSelection, GTestPolicy< GUM_SCALAR >, Chi2TestPolicy< GUM_SCALAR >, LeastSquareTestPolicy< GUM_SCALAR > >::type TestPolicy
Definition: nodeDatabase.h:66
NodeDatabase(const Set< const DiscreteVariable * > *, const DiscreteVariable *=nullptr)
Default constructor.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
const const_iterator_safe & cendSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:510
std::string toString() const
double reward() const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:119
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void addObservation(const Observation *)
Nb observation taken into account by this instance.
INLINE Idx rModality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:97
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition: set_tpl.h:495
const HashTableConstIteratorSafe< ValueType, Idx > cbeginValues() const
Iterators on value count to recopy correctly its content.
Definition: nodeDatabase.h:177
NodeDatabase< AttributeSelection, isScalar > & operator+=(const NodeDatabase< AttributeSelection, isScalar > &src)
Merges given NodeDatabase informations into current nDB.
INLINE Idx modality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:94
INLINE Idx nbObservation() const
Nb observation taken into account by this instance.
Definition: nodeDatabase.h:120
~NodeDatabase()
Default destructor.
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Definition: nodeDatabase.h:58
HashTable< const DiscreteVariable *, TestPolicy< ValueType > *> __attrTable
Table giving for every variables its instantiation.
Definition: nodeDatabase.h:205