aGrUM  0.14.2
nodeDatabase_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 // =========================================================================
28 // =========================================================================
29 
30 namespace gum {
31 
32  // ==========================================================================
33  // Constructor & destructor.
34  // ==========================================================================
35 
36  // ###################################################################
37  // Default constructor
38  // ###################################################################
39  template < TESTNAME AttributeSelection, bool isScalar >
41  const Set< const DiscreteVariable* >* attrSet,
42  const DiscreteVariable* value) :
43  __value(value) {
44  GUM_CONSTRUCTOR(NodeDatabase);
45 
47  attrSet->cbeginSafe();
48  varIter != attrSet->cendSafe();
49  ++varIter)
50  __attrTable.insert(*varIter, new TestPolicy< ValueType >());
51 
52  __nbObservation = 0;
53  }
54 
55 
56  // ###################################################################
57  // Default desstructor
58  // ###################################################################
59  template < TESTNAME AttributeSelection, bool isScalar >
61  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
62  ++varIter)
63  delete varIter.val();
64 
65  GUM_DESTRUCTOR(NodeDatabase);
66  }
67 
68 
69  // ==========================================================================
70  // Observation handling methods
71  // ==========================================================================
72 
73  // ###################################################################
74  /* Updates database with new observation
75  *
76  * Calls either @fn __addObservation( const Observation*, Int2Type<true>)
77  * or @fn __addObservation( const Observation*, Int2Type<false>)
78  * depending on if we're learning reward function or transition probability
79  */
80  // ###################################################################
81  template < TESTNAME AttributeSelection, bool isScalar >
83  const Observation* newObs) {
85  this->__addObservation(newObs, Int2Type< isScalar >());
86  }
87 
88  template < TESTNAME AttributeSelection, bool isScalar >
90  const Observation* newObs, Int2Type< true >) {
91  for (auto varIter = __attrTable.cbeginSafe();
92  varIter != __attrTable.cendSafe();
93  ++varIter)
94  varIter.val()->addObservation(newObs->rModality(varIter.key()),
95  newObs->reward());
96 
97  if (__valueCount.exists(newObs->reward()))
98  __valueCount[newObs->reward()]++;
99  else
100  __valueCount.insert(newObs->reward(), 1);
101  }
102 
103  template < TESTNAME AttributeSelection, bool isScalar >
105  const Observation* newObs, Int2Type< false >) {
106  for (auto varIter = __attrTable.cbeginSafe();
107  varIter != __attrTable.cendSafe();
108  ++varIter)
109  varIter.val()->addObservation(newObs->modality(varIter.key()),
110  newObs->modality(__value));
111 
112  if (__valueCount.exists(newObs->modality(__value)))
113  __valueCount[newObs->modality(__value)]++;
114  else
115  __valueCount.insert(newObs->modality(__value), 1);
116  }
117 
118 
119  // ==========================================================================
120  // Aggregation Methods
121  // ==========================================================================
122 
123 
124  // ###################################################################
125  // Merges given NodeDatabase informations into current nDB.
126  // ###################################################################
127  template < TESTNAME AttributeSelection, bool isScalar >
131  this->__nbObservation += src.nbObservation();
132 
133  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
134  ++varIter)
135  varIter.val()->add(*(src.testPolicy(varIter.key())));
136 
137  for (auto valIter = src.cbeginValues(); valIter != src.cendValues(); ++valIter)
138  if (__valueCount.exists(valIter.key()))
139  __valueCount[valIter.key()] += valIter.val();
140  else
141  __valueCount.insert(valIter.key(), valIter.val());
142 
143  return *this;
144  }
145 
146 
147  template < TESTNAME AttributeSelection, bool isScalar >
149  std::stringstream ss;
150 
151  ss << "NbObservation : " << this->nbObservation() << std::endl;
152  for (auto varIter = __attrTable.beginSafe(); varIter != __attrTable.endSafe();
153  ++varIter)
154  ss << "\t\tVariable : " << varIter.key()->name()
155  << " - Associated Test : " << __attrTable[varIter.key()]->toString()
156  << std::endl;
157 
158  return ss.str();
159  }
160 } // End of namespace gum
161 
162 
163 // LEFT HERE ON PURPOSE
164 // NOT TO BE DELETED
165 
166 /*template<TESTNAME AttributeSelection, bool isScalar>
167 double *NodeDatabase<AttributeSelection, isScalar>::effectif(){
168  double* ret = static_cast<double*>(
169 SmallObjectAllocator::instance().allocate(sizeof(double)*__value->domainSize()));
170  for(Idx modality = 0; modality < __value->domainSize(); ++modality)
171  if( __valueCount.exists(modality) )
172  ret[modality] = (double)__valueCount[modality];
173  else
174  ret[modality] = 0.0;
175  return ret;
176 }*/
177 
178 /*template<TESTNAME AttributeSelection, bool isScalar>
179 double NodeDatabase<AttributeSelection, isScalar>::reward(){
180  double ret = 0.0;
181  for(auto valuTer = __valueCount.cbeginSafe(); valuTer !=
182 __valueCount.cendSafe(); ++valuTer)
183  ret += valuTer.key() * (double) valuTer.val();
184  return ret / __nbObservation;
185 }*/
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:167
const DiscreteVariable * __value
So does this reference on the value observed.
Definition: nodeDatabase.h:205
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:808
HashTable< ValueType, Idx > __valueCount
Definition: nodeDatabase.h:211
const HashTableConstIteratorSafe< ValueType, Idx > cendValues() const
Merges given NodeDatabase informations into current nDB.
Definition: nodeDatabase.h:177
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:63
NodeDatabase(const Set< const DiscreteVariable * > *, const DiscreteVariable *=nullptr)
Default constructor.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
const const_iterator_safe & cendSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:507
std::string toString() const
double reward() const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:116
Headers of the NodeDatabase class.
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:94
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition: set_tpl.h:492
const HashTableConstIteratorSafe< ValueType, Idx > cbeginValues() const
Iterators on value count to recopy correctly its content.
Definition: nodeDatabase.h:174
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:91
INLINE Idx nbObservation() const
Nb observation taken into account by this instance.
Definition: nodeDatabase.h:117
~NodeDatabase()
Default destructor.
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Definition: nodeDatabase.h:55
HashTable< const DiscreteVariable *, TestPolicy< ValueType > *> __attrTable
Table giving for every variables its instantiation.
Definition: nodeDatabase.h:202