aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
varMod2BNsMap_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
44
45namespace gum {
46 namespace credal {
47
48 template < typename GUM_SCALAR >
50 cnet = nullptr;
51
52 GUM_CONSTRUCTOR(VarMod2BNsMap);
53 }
54
55 template < typename GUM_SCALAR >
61
62 template < typename GUM_SCALAR >
66
67 template < typename GUM_SCALAR >
69 auto* cpt = &cn.credalNet_currentCpt();
70 auto nNodes = cpt->size();
71 sampleDef_.resize(nNodes);
72
73 for (NodeId node = 0; node < nNodes; node++) {
74 auto pConfs = (*cpt)[node].size();
75 sampleDef_[node].resize(pConfs);
76
77 for (Size pconf = 0; pconf < pConfs; pconf++) {
78 Size nVertices = Size((*cpt)[node][pconf].size());
79 unsigned long b, c; // needed by superiorPow
80 superiorPow(static_cast< unsigned long >(nVertices), b, c);
81 Size nBits = Size(b);
82 sampleDef_[node][pconf].resize(nBits);
83 }
84 }
85
86 cnet = &cn;
87 }
88
89 template < typename GUM_SCALAR >
90 bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< bool >& bn,
91 const std::vector< Size >& key) {
93 std::list< Size >& nets = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
94
95 for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
96 if (*it == currentHash_) return false;
97 }
98
99 // add it
100 myHashNet_.set(currentHash_, bn); //[currentHash_] = bn;
101 // insert net hash in our key net list
102 nets.push_back(currentHash_);
103 // insert out key in the hash key list
104 myHashVars_.getWithDefault(currentHash_,
105 std::list< varKey >()) /*[currentHash_]*/.push_back(key);
106 return true;
107 }
108
109 template < typename GUM_SCALAR >
110 bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< Size >& key, const bool isBetter) {
111 if (isBetter) {
112 // get all nets of this key (maybe entry does not exists)
113 std::list< Size >& old_nets
114 = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
115
116 // for each one
117 for (std::list< Size >::iterator it = old_nets.begin(); it != old_nets.end(); ++it) {
118 // get all keys associated to this net
119 std::list< varKey >& netKeys
120 = myHashVars_.getWithDefault(*it, std::list< varKey >()); //[ *it ];
121
122 // if we are the sole user, delete the net entry
123 if (netKeys.size() == 1) {
124 myHashVars_.erase(*it);
125 }
126 // other keys use the net, delete our key from list
127 else {
128 for (std::list< varKey >::iterator it2 = netKeys.begin(); it2 != netKeys.end(); ++it2) {
129 if (*it2 == key) {
130 netKeys.erase(it2);
131 break;
132 }
133 }
134 }
135 } // end of : for each old_net
136
137 // clear all old_nets
138 old_nets.clear();
139 // insert new net with it's hash
141 currentSample_); //[currentHash_] = currentSample_;
142 // insert net hash in our key net list
143 old_nets.push_back(currentHash_);
144 // insert out key in the hash key list
146 .getWithDefault(currentHash_, std::list< varKey >()) /*[currentHash_]*/
147 .push_back(key);
148 return true;
149
150 } // end of isBetter
151 // another opt net
152 else {
153 // check that we didn't add it for this key
154 std::list< Size >& nets = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
155
156 for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
157 if (*it == currentHash_) return false;
158 }
159
160 // add it
162 // insert net hash in our key net list
163 nets.push_back(currentHash_);
164 // insert out key in the hash key list
165 myHashVars_.getWithDefault(currentHash_, std::list< varKey >()).push_back(key);
166
167 /*
168 // add it
169 myHashNet_[currentHash_] = currentSample_;
170 // insert net hash in our key net list
171 nets.push_back(currentHash_);
172 // insert out key in the hash key list
173 myHashVars_[currentHash_].push_back(key);
174 */
175 return true;
176 } // end of ! isBetter
177 }
178
179 template < typename GUM_SCALAR >
181 const std::vector< std::vector< std::vector< bool > > >& sample) {
182 currentSample_.clear();
183
184 for (Size i = 0; i < sample.size(); i++)
185 for (Size j = 0; j < sample[j].size(); j++)
186 for (Size k = 0; k < sample[i][j].size(); k++)
187 currentSample_.push_back(sample[i][j][k]);
188
189 // std::cout << sample << std::endl;
190 // std::cout << currentSample_ << std::endl;
191
193 }
194
195 template < typename GUM_SCALAR >
197 return currentSample_;
198 }
199
200 template < typename GUM_SCALAR >
201 const std::vector< std::vector< std::vector< bool > > >&
205
206 template < typename GUM_SCALAR >
207 const std::vector< std::vector< bool >* >
208 VarMod2BNsMap< GUM_SCALAR >::getBNOptsFromKey(const std::vector< Size >& key) {
209 // return something even if key does not exist
210 if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
211
212 std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
213
214 std::vector< dBN* > nets;
215 nets.resize(netsHash.size());
216
217 std::list< Size >::iterator it = netsHash.begin();
218
219 for (Size i = 0; i < netsHash.size(); i++, ++it) {
220 nets[i] = &myHashNet_ /*.at(*/[*it]; //);
221 }
222
223 return nets;
224 }
225
226 template < typename GUM_SCALAR >
227 std::vector< std::vector< std::vector< std::vector< bool > > > >
229 if (cnet == nullptr)
231 "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
232
233 if (!myVarHashs_.exists(key))
234 return std::vector< std::vector< std::vector< std::vector< bool > > > >();
235
236 std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
237
238 std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
239 nets.resize(netsHash.size(), sampleDef_);
240
241 std::list< Size >::iterator it = netsHash.begin();
242
243 for (Size i = 0; i < netsHash.size(); i++, ++it) {
244 // std::vector< std::vector< std::vector < bool > > > net(sampleDef_);
245 dBN::iterator it2 = myHashNet_ /*.at(*/[*it] /*)*/.begin();
246
247 for (Size j = 0; j < sampleDef_.size(); j++) {
248 for (Size k = 0; k < sampleDef_[j].size(); k++) {
249 for (Size l = 0; l < sampleDef_[j][k].size(); l++) {
250 nets[i][j][k][l] = *it2;
251 ++it2;
252 }
253 }
254 }
255 }
256
257 return nets;
258 }
259
260 template < typename GUM_SCALAR >
264
265 } // namespace credal
266} // namespace gum
Exception : operation not allowed.
Class template representing a Credal Network.
Definition credalNet.h:97
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
Size getEntrySize() const
Get the number of IBayesNet stored.
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
const dBN & getCurrentSample()
Get the current sample as a vector of bits without structure.
dBN currentSample_
The current sampled IBayesNet.
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.
std::hash< std::vector< bool > > vectHash_
The hash fuction.
hashNet myHashNet_
Map id - dBN : get a net from it's id.
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 CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
const std::vector< std::vector< std::vector< bool > > > & getSampleDef()
Get the sample structure.
Size currentHash_
The current sampled IBayesNet hash.
bool insert(const std::vector< bool > &bn, const std::vector< Size > &key)
Insert for thread fusion.
void setCurrentSample(const std::vector< std::vector< std::vector< bool > > > &sample)
Set the current thread sample and it's hash.
void setCNet(const CredalNet< GUM_SCALAR > &cn)
Initialize sampleDef from the CredalNet.
hashVars myHashVars_
Map id - list(varKey) : get all variables, modalities associated to this optimal net id.
std::vector< std::vector< std::vector< bool > > > sampleDef_
Since all samples have the same structure, this will be used as default initialization (copy construc...
VarMod2BNsMap(const CredalNet< GUM_SCALAR > &cn)
Thread constructor.
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
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:75
namespace for all credal networks entities
Definition agrum.h:61
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Class used to store optimum IBayesNet during some inference algorithms.