aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
varMod2BNsMap_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 #include <agrum/CN/tools/varMod2BNsMap.h>
23 
24 namespace gum {
25  namespace credal {
26 
27  template < typename GUM_SCALAR >
28  VarMod2BNsMap< GUM_SCALAR >::VarMod2BNsMap() {
29  cnet = nullptr;
30 
31  GUM_CONSTRUCTOR(VarMod2BNsMap);
32  }
33 
34  template < typename GUM_SCALAR >
36  setCNet(cn);
37 
39  }
40 
41  template < typename GUM_SCALAR >
44  }
45 
46  template < typename GUM_SCALAR >
48  auto* cpt = &cn.credalNet_currentCpt();
49  auto nNodes = cpt->size();
51 
52  for (NodeId node = 0; node < nNodes; node++) {
53  auto pConfs = (*cpt)[node].size();
55 
56  for (Size pconf = 0; pconf < pConfs; pconf++) {
57  Size nVertices = Size((*cpt)[node][pconf].size());
58  unsigned long b, c; // needed by superiorPow
59  superiorPow(static_cast< unsigned long >(nVertices), b, c);
60  Size nBits = Size(b);
62  }
63  }
64 
65  cnet = &cn;
66  }
67 
68  template < typename GUM_SCALAR >
69  bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< bool >& bn,
70  const std::vector< Size >& key) {
72  std::list< Size >& nets
73  = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
74 
75  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
76  if (*it == currentHash_) return false;
77  }
78 
79  // add it
80  myHashNet_.set(currentHash_, bn); //[currentHash_] = bn;
81  // insert net hash in our key net list
83  // insert out key in the hash key list
86  std::list< varKey >()) /*[currentHash_]*/.push_back(key);
87  return true;
88  }
89 
90  template < typename GUM_SCALAR >
92  const bool isBetter) {
93  if (isBetter) {
94  // get all nets of this key (maybe entry does not exists)
95  std::list< Size >& old_nets
96  = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
97 
98  // for each one
99  for (std::list< Size >::iterator it = old_nets.begin();
100  it != old_nets.end();
101  ++it) {
102  // get all keys associated to this net
103  std::list< varKey >& netKeys
104  = myHashVars_.getWithDefault(*it, std::list< varKey >()); //[ *it ];
105 
106  // if we are the sole user, delete the net entry
107  if (netKeys.size() == 1) {
108  myHashVars_.erase(*it);
109  }
110  // other keys use the net, delete our key from list
111  else {
112  for (std::list< varKey >::iterator it2 = netKeys.begin();
113  it2 != netKeys.end();
114  ++it2) {
115  if (*it2 == key) {
116  netKeys.erase(it2);
117  break;
118  }
119  }
120  }
121  } // end of : for each old_net
122 
123  // clear all old_nets
124  old_nets.clear();
125  // insert new net with it's hash
127  currentSample_); //[currentHash_] = currentSample_;
128  // insert net hash in our key net list
130  // insert out key in the hash key list
132  .getWithDefault(currentHash_, std::list< varKey >()) /*[currentHash_]*/
133  .push_back(key);
134  return true;
135 
136  } // end of isBetter
137  // another opt net
138  else {
139  // check that we didn't add it for this key
140  std::list< Size >& nets
141  = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
142 
143  for (std::list< Size >::iterator it = nets.begin(); it != nets.end();
144  ++it) {
145  if (*it == currentHash_) return false;
146  }
147 
148  // add it
150  // insert net hash in our key net list
152  // insert out key in the hash key list
154  .push_back(key);
155 
156  /*
157  // add it
158  myHashNet_[currentHash_] = currentSample_;
159  // insert net hash in our key net list
160  nets.push_back(currentHash_);
161  // insert out key in the hash key list
162  myHashVars_[currentHash_].push_back(key);
163  */
164  return true;
165  } // end of ! isBetter
166  }
167 
168  template < typename GUM_SCALAR >
170  const std::vector< std::vector< std::vector< bool > > >& sample) {
172 
173  for (Size i = 0; i < sample.size(); i++)
174  for (Size j = 0; j < sample[j].size(); j++)
175  for (Size k = 0; k < sample[i][j].size(); k++)
177 
178  // std::cout << sample << std::endl;
179  // std::cout << currentSample_ << std::endl;
180 
182  }
183 
184  template < typename GUM_SCALAR >
186  return currentSample_;
187  }
188 
189  template < typename GUM_SCALAR >
190  const std::vector< std::vector< std::vector< bool > > >&
192  return sampleDef_;
193  }
194 
195  template < typename GUM_SCALAR >
196  const std::vector< std::vector< bool >* >
198  const std::vector< Size >& key) {
199  // return something even if key does not exist
200  if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
201 
202  std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
203 
204  std::vector< dBN* > nets;
206 
207  std::list< Size >::iterator it = netsHash.begin();
208 
209  for (Size i = 0; i < netsHash.size(); i++, ++it) {
210  nets[i] = &myHashNet_ /*.at(*/[*it]; //);
211  }
212 
213  return nets;
214  }
215 
216  template < typename GUM_SCALAR >
217  std::vector< std::vector< std::vector< std::vector< bool > > > >
219  const std::vector< Size >& key) {
220  if (cnet == nullptr)
222  "No CredalNet associated to me ! Can't get FullBNOptsFromKey : "
223  << key);
224 
225  if (!myVarHashs_.exists(key))
226  return std::vector< std::vector< std::vector< std::vector< bool > > > >();
227 
228  std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
229 
230  std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
232 
233  std::list< Size >::iterator it = netsHash.begin();
234 
235  for (Size i = 0; i < netsHash.size(); i++, ++it) {
236  // std::vector< std::vector< std::vector < bool > > > net(sampleDef_);
237  dBN::iterator it2 = myHashNet_ /*.at(*/[*it] /*)*/.begin();
238 
239  for (Size j = 0; j < sampleDef_.size(); j++) {
240  for (Size k = 0; k < sampleDef_[j].size(); k++) {
241  for (Size l = 0; l < sampleDef_[j][k].size(); l++) {
242  nets[i][j][k][l] = *it2;
243  ++it2;
244  }
245  }
246  }
247  }
248 
249  return nets;
250  }
251 
252  template < typename GUM_SCALAR >
254  return myHashNet_.size();
255  }
256 
257  } // namespace credal
258 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
namespace for all credal networks entities
Definition: LpInterface.cpp:37