aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
nodeDatabase_tpl.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 Template implémentations of the NodeDatabase class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 // =========================================================================
29 #include <agrum/FMDP/learning/datastructure/nodeDatabase.h>
30 // =========================================================================
31 
32 namespace gum {
33 
34  // ==========================================================================
35  // Constructor & destructor.
36  // ==========================================================================
37 
38  // ###################################################################
39  // Default constructor
40  // ###################################################################
41  template < TESTNAME AttributeSelection, bool isScalar >
42  NodeDatabase< AttributeSelection, isScalar >::NodeDatabase(
43  const Set< const DiscreteVariable* >* attrSet,
44  const DiscreteVariable* value) :
45  value__(value) {
46  GUM_CONSTRUCTOR(NodeDatabase);
47 
48  for (SetIteratorSafe< const DiscreteVariable* > varIter
49  = attrSet->cbeginSafe();
50  varIter != attrSet->cendSafe();
51  ++varIter)
52  attrTable__.insert(*varIter, new TestPolicy< ValueType >());
53 
54  nbObservation__ = 0;
55  }
56 
57 
58  // ###################################################################
59  // Default desstructor
60  // ###################################################################
61  template < TESTNAME AttributeSelection, bool isScalar >
64  ++varIter)
65  delete varIter.val();
66 
68  }
69 
70 
71  // ==========================================================================
72  // Observation handling methods
73  // ==========================================================================
74 
75  // ###################################################################
76  /* Updates database with new observation
77  *
78  * Calls either @fn addObservation__( const Observation*, Int2Type<true>)
79  * or @fn addObservation__( const Observation*, Int2Type<false>)
80  * depending on if we're learning reward function or transition probability
81  */
82  // ###################################################################
83  template < TESTNAME AttributeSelection, bool isScalar >
85  const Observation* newObs) {
88  }
89 
90  template < TESTNAME AttributeSelection, bool isScalar >
92  const Observation* newObs,
93  Int2Type< true >) {
94  for (auto varIter = attrTable__.cbeginSafe();
96  ++varIter)
98  newObs->reward());
99 
101  valueCount__[newObs->reward()]++;
102  else
104  }
105 
106  template < TESTNAME AttributeSelection, bool isScalar >
108  const Observation* newObs,
109  Int2Type< false >) {
110  for (auto varIter = attrTable__.cbeginSafe();
112  ++varIter)
115 
118  else
120  }
121 
122 
123  // ==========================================================================
124  // Aggregation Methods
125  // ==========================================================================
126 
127 
128  // ###################################################################
129  // Merges given NodeDatabase informations into current nDB.
130  // ###################################################################
131  template < TESTNAME AttributeSelection, bool isScalar >
135  this->nbObservation__ += src.nbObservation();
136 
138  ++varIter)
139  varIter.val()->add(*(src.testPolicy(varIter.key())));
140 
141  for (auto valIter = src.cbeginValues(); valIter != src.cendValues(); ++valIter)
144  else
146 
147  return *this;
148  }
149 
150 
151  template < TESTNAME AttributeSelection, bool isScalar >
154 
155  ss << "NbObservation : " << this->nbObservation() << std::endl;
157  ++varIter)
158  ss << "\t\tVariable : " << varIter.key()->name()
159  << " - Associated Test : " << attrTable__[varIter.key()]->toString()
160  << std::endl;
161 
162  return ss.str();
163  }
164 } // End of namespace gum
165 
166 
167 // LEFT HERE ON PURPOSE
168 // NOT TO BE DELETED
169 
170 /*template<TESTNAME AttributeSelection, bool isScalar>
171 double *NodeDatabase<AttributeSelection, isScalar>::effectif(){
172  double* ret = static_cast<double*>(
173 SmallObjectAllocator::instance().allocate(sizeof(double)*value__->domainSize()));
174  for(Idx modality = 0; modality < value__->domainSize(); ++modality)
175  if( valueCount__.exists(modality) )
176  ret[modality] = (double)valueCount__[modality];
177  else
178  ret[modality] = 0.0;
179  return ret;
180 }*/
181 
182 /*template<TESTNAME AttributeSelection, bool isScalar>
183 double NodeDatabase<AttributeSelection, isScalar>::reward(){
184  double ret = 0.0;
185  for(auto valuTer = valueCount__.cbeginSafe(); valuTer !=
186 valueCount__.cendSafe(); ++valuTer)
187  ret += valuTer.key() * (double) valuTer.val();
188  return ret / nbObservation__;
189 }*/
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669