aGrUM  0.16.0
varMod2BNsMap_tpl.h
Go to the documentation of this file.
1 
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  _sampleDef[node][pconf].resize(nBits);
63  }
64  }
65 
66  cnet = &cn;
67  }
68 
69  template < typename GUM_SCALAR >
70  bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< bool >& bn,
71  const std::vector< Size >& key) {
72  _currentHash = Size(_vectHash(bn));
73  std::list< Size >& nets =
74  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
75 
76  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
77  if (*it == _currentHash) return false;
78  }
79 
80  // add it
81  _myHashNet.set(_currentHash, bn); //[_currentHash] = bn;
82  // insert net hash in our key net list
83  nets.push_back(_currentHash);
84  // insert out key in the hash key list
85  _myHashVars
86  .getWithDefault(_currentHash,
87  std::list< varKey >()) /*[_currentHash]*/.push_back(key);
88  return true;
89  }
90 
91  template < typename GUM_SCALAR >
92  bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< Size >& key,
93  const bool isBetter) {
94  if (isBetter) {
95  // get all nets of this key (maybe entry does not exists)
96  std::list< Size >& old_nets =
97  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
98 
99  // for each one
100  for (std::list< Size >::iterator it = old_nets.begin();
101  it != old_nets.end();
102  ++it) {
103  // get all keys associated to this net
104  std::list< varKey >& netKeys =
105  _myHashVars.getWithDefault(*it, std::list< varKey >()); //[ *it ];
106 
107  // if we are the sole user, delete the net entry
108  if (netKeys.size() == 1) {
109  _myHashVars.erase(*it);
110  }
111  // other keys use the net, delete our key from list
112  else {
113  for (std::list< varKey >::iterator it2 = netKeys.begin();
114  it2 != netKeys.end();
115  ++it2) {
116  if (*it2 == key) {
117  netKeys.erase(it2);
118  break;
119  }
120  }
121  }
122  } // end of : for each old_net
123 
124  // clear all old_nets
125  old_nets.clear();
126  // insert new net with it's hash
127  _myHashNet.set(_currentHash,
128  _currentSample); //[_currentHash] = _currentSample;
129  // insert net hash in our key net list
130  old_nets.push_back(_currentHash);
131  // insert out key in the hash key list
132  _myHashVars
133  .getWithDefault(_currentHash, std::list< varKey >()) /*[_currentHash]*/
134  .push_back(key);
135  return true;
136 
137  } // end of isBetter
138  // another opt net
139  else {
140  // check that we didn't add it for this key
141  std::list< Size >& nets =
142  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
143 
144  for (std::list< Size >::iterator it = nets.begin(); it != nets.end();
145  ++it) {
146  if (*it == _currentHash) return false;
147  }
148 
149  // add it
150  _myHashNet.set(_currentHash, _currentSample);
151  // insert net hash in our key net list
152  nets.push_back(_currentHash);
153  // insert out key in the hash key list
154  _myHashVars.getWithDefault(_currentHash, std::list< varKey >())
155  .push_back(key);
156 
157  /*
158  // add it
159  _myHashNet[_currentHash] = _currentSample;
160  // insert net hash in our key net list
161  nets.push_back(_currentHash);
162  // insert out key in the hash key list
163  _myHashVars[_currentHash].push_back(key);
164  */
165  return true;
166  } // end of ! isBetter
167  }
168 
169  template < typename GUM_SCALAR >
171  const std::vector< std::vector< std::vector< bool > > >& sample) {
172  _currentSample.clear();
173 
174  for (Size i = 0; i < sample.size(); i++)
175  for (Size j = 0; j < sample[j].size(); j++)
176  for (Size k = 0; k < sample[i][j].size(); k++)
177  _currentSample.push_back(sample[i][j][k]);
178 
179  // std::cout << sample << std::endl;
180  // std::cout << _currentSample << std::endl;
181 
182  _currentHash = Size(_vectHash(_currentSample));
183  }
184 
185  template < typename GUM_SCALAR >
186  const std::vector< bool >& VarMod2BNsMap< GUM_SCALAR >::getCurrentSample() {
187  return _currentSample;
188  }
189 
190  template < typename GUM_SCALAR >
191  const std::vector< std::vector< std::vector< bool > > >&
193  return _sampleDef;
194  }
195 
196  template < typename GUM_SCALAR >
197  const std::vector< std::vector< bool >* >
199  const std::vector< Size >& key) {
200  // return something even if key does not exist
201  if (!_myVarHashs.exists(key)) return std::vector< std::vector< bool >* >();
202 
203  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
204 
205  std::vector< dBN* > nets;
206  nets.resize(netsHash.size());
207 
208  std::list< Size >::iterator it = netsHash.begin();
209 
210  for (Size i = 0; i < netsHash.size(); i++, ++it) {
211  nets[i] = &_myHashNet /*.at(*/[*it]; //);
212  }
213 
214  return nets;
215  }
216 
217  template < typename GUM_SCALAR >
218  std::vector< std::vector< std::vector< std::vector< bool > > > >
220  const std::vector< Size >& key) {
221  if (cnet == nullptr)
223  "No CredalNet associated to me ! Can't get FullBNOptsFromKey : "
224  << key);
225 
226  if (!_myVarHashs.exists(key))
227  return std::vector< std::vector< std::vector< std::vector< bool > > > >();
228 
229  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
230 
231  std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
232  nets.resize(netsHash.size(), _sampleDef);
233 
234  std::list< Size >::iterator it = netsHash.begin();
235 
236  for (Size i = 0; i < netsHash.size(); i++, ++it) {
237  // std::vector< std::vector< std::vector < bool > > > net(_sampleDef);
238  dBN::iterator it2 = _myHashNet /*.at(*/[*it] /*)*/.begin();
239 
240  for (Size j = 0; j < _sampleDef.size(); j++) {
241  for (Size k = 0; k < _sampleDef[j].size(); k++) {
242  for (Size l = 0; l < _sampleDef[j][k].size(); l++) {
243  nets[i][j][k][l] = *it2;
244  ++it2;
245  }
246  }
247  }
248  }
249 
250  return nets;
251  }
252 
253  template < typename GUM_SCALAR >
255  return _myHashNet.size();
256  }
257 
258  } // namespace credal
259 } // namespace gum
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Class template representing a Credal Network.
Definition: credalNet.h:89
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...
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...
Size getEntrySize() const
Get the number of IBayesNet stored.
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:55
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
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:56
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void setCurrentSample(const std::vector< std::vector< std::vector< bool > > > &sample)
Set the current thread sample and it&#39;s hash.