aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
45
46namespace gum {
47 namespace credal {
48
49 template < GUM_Numeric GUM_SCALAR >
51 cnet_ = nullptr;
52
53 GUM_CONSTRUCTOR(VarMod2BNsMap);
54 }
55
56 template < GUM_Numeric GUM_SCALAR >
62
63 template < GUM_Numeric GUM_SCALAR >
67
68 template < GUM_Numeric GUM_SCALAR >
70 auto* cpt = &cn.credalNet_currentCpt();
71 auto nNodes = cpt->size();
72 sampleDef_.resize(nNodes);
73
74 for (NodeId node = 0; node < nNodes; node++) {
75 auto pConfs = (*cpt)[node].size();
76 sampleDef_[node].resize(pConfs);
77
78 for (Size pconf = 0; pconf < pConfs; pconf++) {
79 Size nVertices = Size((*cpt)[node][pconf].size());
80 unsigned long b, c; // needed by superiorPow
81 superiorPow(static_cast< unsigned long >(nVertices), b, c);
82 Size nBits = Size(b);
83 sampleDef_[node][pconf].resize(nBits);
84 }
85 }
86
87 cnet_ = &cn;
88 }
89
90 template < GUM_Numeric GUM_SCALAR >
91 bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< bool >& bn,
92 const std::vector< Size >& key) {
94 std::list< Size >& nets = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
95
96 for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
97 if (*it == currentHash_) return false;
98 }
99
100 // add it
101 myHashNet_.set(currentHash_, bn); //[currentHash_] = bn;
102 // insert net hash in our key net list
103 nets.push_back(currentHash_);
104 // insert out key in the hash key list
105 myHashVars_.getWithDefault(currentHash_,
106 std::list< varKey >()) /*[currentHash_]*/.push_back(key);
107 return true;
108 }
109
110 template < GUM_Numeric GUM_SCALAR >
111 bool VarMod2BNsMap< GUM_SCALAR >::insert(const std::vector< Size >& key, const bool isBetter) {
112 if (isBetter) {
113 // get all nets of this key (maybe entry does not exists)
114 std::list< Size >& old_nets
115 = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
116
117 // for each one
118 for (std::list< Size >::iterator it = old_nets.begin(); it != old_nets.end(); ++it) {
119 // get all keys associated to this net
120 std::list< varKey >& netKeys
121 = myHashVars_.getWithDefault(*it, std::list< varKey >()); //[ *it ];
122
123 // if we are the sole user, delete the net entry
124 if (netKeys.size() == 1) {
125 myHashVars_.erase(*it);
126 }
127 // other keys use the net, delete our key from list
128 else {
129 for (std::list< varKey >::iterator it2 = netKeys.begin(); it2 != netKeys.end(); ++it2) {
130 if (*it2 == key) {
131 netKeys.erase(it2);
132 break;
133 }
134 }
135 }
136 } // end of : for each old_net
137
138 // clear all old_nets
139 old_nets.clear();
140 // insert new net with it's hash
142 currentSample_); //[currentHash_] = currentSample_;
143 // insert net hash in our key net list
144 old_nets.push_back(currentHash_);
145 // insert out key in the hash key list
147 .getWithDefault(currentHash_, std::list< varKey >()) /*[currentHash_]*/
148 .push_back(key);
149 return true;
150
151 } // end of isBetter
152 // another opt net
153 else {
154 // check that we didn't add it for this key
155 std::list< Size >& nets = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
156
157 for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
158 if (*it == currentHash_) return false;
159 }
160
161 // add it
163 // insert net hash in our key net list
164 nets.push_back(currentHash_);
165 // insert out key in the hash key list
166 myHashVars_.getWithDefault(currentHash_, std::list< varKey >()).push_back(key);
167
168 /*
169 // add it
170 myHashNet_[currentHash_] = currentSample_;
171 // insert net hash in our key net list
172 nets.push_back(currentHash_);
173 // insert out key in the hash key list
174 myHashVars_[currentHash_].push_back(key);
175 */
176 return true;
177 } // end of ! isBetter
178 }
179
180 template < GUM_Numeric GUM_SCALAR >
182 const std::vector< std::vector< std::vector< bool > > >& sample) {
183 currentSample_.clear();
184
185 for (Size i = 0; i < sample.size(); i++)
186 for (Size j = 0; j < sample[j].size(); j++)
187 for (Size k = 0; k < sample[i][j].size(); k++)
188 currentSample_.push_back(sample[i][j][k]);
189
190 // std::cout << sample << std::endl;
191 // std::cout << currentSample_ << std::endl;
192
194 }
195
196 template < GUM_Numeric GUM_SCALAR >
198 return currentSample_;
199 }
200
201 template < GUM_Numeric GUM_SCALAR >
202 const std::vector< std::vector< std::vector< bool > > >&
206
207 template < GUM_Numeric GUM_SCALAR >
208 const std::vector< std::vector< bool >* >
209 VarMod2BNsMap< GUM_SCALAR >::getBNOptsFromKey(const std::vector< Size >& key) {
210 // return something even if key does not exist
211 if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
212
213 std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
214
215 std::vector< dBN* > nets;
216 nets.resize(netsHash.size());
217
218 std::list< Size >::iterator it = netsHash.begin();
219
220 for (Size i = 0; i < netsHash.size(); i++, ++it) {
221 nets[i] = &myHashNet_ /*.at(*/[*it]; //);
222 }
223
224 return nets;
225 }
226
227 template < GUM_Numeric GUM_SCALAR >
228 std::vector< std::vector< std::vector< std::vector< bool > > > >
230 if (cnet_ == nullptr)
232 "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
233
234 if (!myVarHashs_.exists(key)) return {};
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 < GUM_Numeric 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 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.
const CredalNet< GUM_SCALAR > * cnet_
A pointer to the CredalNet to be used.
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:76
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:79
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.