aGrUM  0.13.2
varMod2BNsMap_tpl.h
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * Copyright (C) 2017 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
4  * {prenom.nom}_at_lip6.fr *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program 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 General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20  ***************************************************************************/
21 
22 
23 #include <agrum/CN/varMod2BNsMap.h>
24 
25 namespace gum {
26  namespace credal {
27 
28  template < typename GUM_SCALAR >
30  cnet = nullptr;
31 
32  GUM_CONSTRUCTOR(VarMod2BNsMap);
33  }
34 
35  template < typename GUM_SCALAR >
37  setCNet(cn);
38 
39  GUM_CONSTRUCTOR(VarMod2BNsMap);
40  }
41 
42  template < typename GUM_SCALAR >
44  GUM_DESTRUCTOR(VarMod2BNsMap);
45  }
46 
47  template < typename GUM_SCALAR >
49  auto* cpt = &cn.credalNet_currentCpt();
50  auto nNodes = cpt->size();
51  _sampleDef.resize(nNodes);
52 
53  for (NodeId node = 0; node < nNodes; node++) {
54  auto pConfs = (*cpt)[node].size();
55  _sampleDef[node].resize(pConfs);
56 
57  for (Size pconf = 0; pconf < pConfs; pconf++) {
58  Size nVertices = Size((*cpt)[node][pconf].size());
59  unsigned long b, c; // needed by superiorPow
60  superiorPow(static_cast< unsigned long >(nVertices), b, c);
61  Size nBits = Size(b);
62  Size newCard = Size(c);
63  _sampleDef[node][pconf].resize(nBits);
64  }
65  }
66 
67  cnet = &cn;
68  }
69 
70  template < typename GUM_SCALAR >
71  bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< bool >& bn,
72  const std::vector< Size >& key) {
73  _currentHash = Size(_vectHash(bn));
74  std::list< Size >& nets =
75  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
76 
77  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
78  if (*it == _currentHash) return false;
79  }
80 
81  // add it
82  _myHashNet.set(_currentHash, bn); //[_currentHash] = bn;
83  // insert net hash in our key net list
84  nets.push_back(_currentHash);
85  // insert out key in the hash key list
86  _myHashVars
87  .getWithDefault(_currentHash,
88  std::list< varKey >()) /*[_currentHash]*/.push_back(key);
89  return true;
90  }
91 
92  template < typename GUM_SCALAR >
93  bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< Size >& key,
94  const bool isBetter) {
95  if (isBetter) {
96  // get all nets of this key (maybe entry does not exists)
97  std::list< Size >& old_nets =
98  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
99 
100  // for each one
101  for (std::list< Size >::iterator it = old_nets.begin();
102  it != old_nets.end();
103  ++it) {
104  // get all keys associated to this net
105  std::list< varKey >& netKeys =
106  _myHashVars.getWithDefault(*it, std::list< varKey >()); //[ *it ];
107 
108  // if we are the sole user, delete the net entry
109  if (netKeys.size() == 1) {
110  _myHashVars.erase(*it);
111  }
112  // other keys use the net, delete our key from list
113  else {
114  for (std::list< varKey >::iterator it2 = netKeys.begin();
115  it2 != netKeys.end();
116  ++it2) {
117  if (*it2 == key) {
118  netKeys.erase(it2);
119  break;
120  }
121  }
122  }
123  } // end of : for each old_net
124 
125  // clear all old_nets
126  old_nets.clear();
127  // insert new net with it's hash
128  _myHashNet.set(_currentHash,
129  _currentSample); //[_currentHash] = _currentSample;
130  // insert net hash in our key net list
131  old_nets.push_back(_currentHash);
132  // insert out key in the hash key list
133  _myHashVars
134  .getWithDefault(_currentHash, std::list< varKey >()) /*[_currentHash]*/
135  .push_back(key);
136  return true;
137 
138  } // end of isBetter
139  // another opt net
140  else {
141  // check that we didn't add it for this key
142  std::list< Size >& nets =
143  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
144 
145  for (std::list< Size >::iterator it = nets.begin(); it != nets.end();
146  ++it) {
147  if (*it == _currentHash) return false;
148  }
149 
150  // add it
151  _myHashNet.set(_currentHash, _currentSample);
152  // insert net hash in our key net list
153  nets.push_back(_currentHash);
154  // insert out key in the hash key list
155  _myHashVars.getWithDefault(_currentHash, std::list< varKey >())
156  .push_back(key);
157 
158  /*
159  // add it
160  _myHashNet[_currentHash] = _currentSample;
161  // insert net hash in our key net list
162  nets.push_back(_currentHash);
163  // insert out key in the hash key list
164  _myHashVars[_currentHash].push_back(key);
165  */
166  return true;
167  } // end of ! isBetter
168  }
169 
170  template < typename GUM_SCALAR >
172  const std::vector< std::vector< std::vector< bool > > >& sample) {
173  _currentSample.clear();
174 
175  for (Size i = 0; i < sample.size(); i++)
176  for (Size j = 0; j < sample[j].size(); j++)
177  for (Size k = 0; k < sample[i][j].size(); k++)
178  _currentSample.push_back(sample[i][j][k]);
179 
180  // std::cout << sample << std::endl;
181  // std::cout << _currentSample << std::endl;
182 
183  _currentHash = Size(_vectHash(_currentSample));
184  }
185 
186  template < typename GUM_SCALAR >
187  const std::vector< bool >& VarMod2BNsMap< GUM_SCALAR >::getCurrentSample() {
188  return _currentSample;
189  }
190 
191  template < typename GUM_SCALAR >
192  const std::vector< std::vector< std::vector< bool > > >&
194  return _sampleDef;
195  }
196 
197  template < typename GUM_SCALAR >
198  const std::vector< std::vector< bool >* >
200  const std::vector< Size >& key) {
201  // return something even if key does not exist
202  if (!_myVarHashs.exists(key)) return std::vector< std::vector< bool >* >();
203 
204  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
205 
206  std::vector< dBN* > nets;
207  nets.resize(netsHash.size());
208 
209  std::list< Size >::iterator it = netsHash.begin();
210 
211  for (Size i = 0; i < netsHash.size(); i++, ++it) {
212  nets[i] = &_myHashNet /*.at(*/[*it]; //);
213  }
214 
215  return nets;
216  }
217 
218  template < typename GUM_SCALAR >
219  std::vector< std::vector< std::vector< std::vector< bool > > > >
221  const std::vector< Size >& key) {
222  if (cnet == nullptr)
223  GUM_ERROR(
225  "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
226 
227  if (!_myVarHashs.exists(key))
228  return std::vector< std::vector< std::vector< std::vector< bool > > > >();
229 
230  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
231 
232  std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
233  nets.resize(netsHash.size(), _sampleDef);
234 
235  std::list< Size >::iterator it = netsHash.begin();
236 
237  for (Size i = 0; i < netsHash.size(); i++, ++it) {
238  // std::vector< std::vector< std::vector < bool > > > net(_sampleDef);
239  dBN::iterator it2 = _myHashNet /*.at(*/[*it] /*)*/.begin();
240 
241  for (Size j = 0; j < _sampleDef.size(); j++) {
242  for (Size k = 0; k < _sampleDef[j].size(); k++) {
243  for (Size l = 0; l < _sampleDef[j][k].size(); l++) {
244  nets[i][j][k][l] = *it2;
245  ++it2;
246  }
247  }
248  }
249  }
250 
251  return nets;
252  }
253 
254  template < typename GUM_SCALAR >
256  return _myHashNet.size();
257  }
258 
259  } // namespace credal
260 } // namespace gum
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
const std::vector< dBN * > getBNOptsFromKey(const std::vector< Size > &key)
Get optimum IBayesNet (s) without structure of the given variable, modality for min or max...
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Class template representing a Credal Network.
Definition: credalNet.h:87
Size getEntrySize() const
Get the number of IBayesNet stored.
std::vector< std::vector< std::vector< std::vector< bool > > > > getFullBNOptsFromKey(const std::vector< Size > &key)
Get optimum IBayesNet (s) with structure of the given variable, modality for min or max...
void superiorPow(unsigned long card, unsigned long &num_bits, unsigned long &new_card)
Compute the superior and closest power of two of an integer.
Definition: pow_inl.h:52
void setCNet(const CredalNet< GUM_SCALAR > &cn)
Initialize __sampleDef from the CredalNet.
const dBN & getCurrentSample()
Get the current sample as a vector of bits without structure.
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable...
const std::vector< std::vector< std::vector< bool > > > & getSampleDef()
Get the sample structure.
Class used to store optimum IBayesNet during some inference algorithms.
bool insert(const std::vector< bool > &bn, const std::vector< Size > &key)
Insert for thread fusion.
Class used to store optimum IBayesNet during some inference algorithms.
Definition: varMod2BNsMap.h:54
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
void setCurrentSample(const std::vector< std::vector< std::vector< bool > > > &sample)
Set the current thread sample and it&#39;s hash.