aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
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-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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.