aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::credal::VarMod2BNsMap< GUM_SCALAR > Class Template Reference

Class used to store optimum IBayesNet during some inference algorithms. More...

#include <agrum/CN/varMod2BNsMap.h>

Collaboration diagram for gum::credal::VarMod2BNsMap< GUM_SCALAR >:

Public Member Functions

bool insert (const std::vector< bool > &bn, const std::vector< Size > &key)
 Insert for thread fusion.
bool insert (const std::vector< Size > &key, const bool isBetter)
 Thread insert.
Constructors / Destructors
 VarMod2BNsMap (const CredalNet< GUM_SCALAR > &cn)
 Thread constructor.
 VarMod2BNsMap ()
 Default constructor that should be used only by InferenceEngine since it has a member variable.
 ~VarMod2BNsMap ()
 Destructor.
Getters and setters
void setCNet (const CredalNet< GUM_SCALAR > &cn)
 Initialize sampleDef from the CredalNet.
void setCurrentSample (const std::vector< std::vector< std::vector< bool > > > &sample)
 Set the current thread sample and it's hash.
const dBNgetCurrentSample ()
 Get the current sample as a vector of bits without structure.
const std::vector< std::vector< std::vector< bool > > > & getSampleDef ()
 Get the sample structure.
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.
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.
Size getEntrySize () const
 Get the number of IBayesNet stored.

Protected Attributes

hashNet myHashNet_
 Map id - dBN : get a net from it's id.
varHashs myVarHashs_
 Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( or max=1 ).
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 constructor) for any sample.
dBN currentSample_
 The current sampled IBayesNet.
Size currentHash_
 The current sampled IBayesNet hash.
std::hash< std::vector< bool > > vectHash_
 The hash fuction.
const CredalNet< GUM_SCALAR > * cnet_
 A pointer to the CredalNet to be used.

Private Types

using dBN = std::vector< bool >
using varKey = std::vector< Size >
using hashNet = gum::HashTable< Size, dBN >
using varHashs = gum::HashTable< varKey, std::list< Size > >
using hashVars = gum::HashTable< Size, std::list< varKey > >

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::credal::VarMod2BNsMap< GUM_SCALAR >

Class used to store optimum IBayesNet during some inference algorithms.

Template Parameters
GUM_SCALARA floating type ( float, double, long double ... ).
Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6)

Definition at line 73 of file varMod2BNsMap.h.

Member Typedef Documentation

◆ dBN

template<GUM_Numeric GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::dBN = std::vector< bool >
private

Definition at line 74 of file varMod2BNsMap.h.

◆ hashNet

template<GUM_Numeric GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::hashNet = gum::HashTable< Size, dBN >
private

Definition at line 76 of file varMod2BNsMap.h.

◆ hashVars

template<GUM_Numeric GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::hashVars = gum::HashTable< Size, std::list< varKey > >
private

Definition at line 78 of file varMod2BNsMap.h.

◆ varHashs

template<GUM_Numeric GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::varHashs = gum::HashTable< varKey, std::list< Size > >
private

Definition at line 77 of file varMod2BNsMap.h.

◆ varKey

template<GUM_Numeric GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::varKey = std::vector< Size >
private

Definition at line 75 of file varMod2BNsMap.h.

Constructor & Destructor Documentation

◆ VarMod2BNsMap() [1/2]

template<GUM_Numeric GUM_SCALAR>
gum::credal::VarMod2BNsMap< GUM_SCALAR >::VarMod2BNsMap ( const CredalNet< GUM_SCALAR > & cn)
explicit

Thread constructor.

Initialize sampleDef from the CredalNet.

Parameters
cnThe CredalNet to be used.

Definition at line 57 of file varMod2BNsMap_tpl.h.

57 {
58 setCNet(cn);
59
61 }
Class used to store optimum IBayesNet during some inference algorithms.
void setCNet(const CredalNet< GUM_SCALAR > &cn)
Initialize sampleDef from the CredalNet.
VarMod2BNsMap(const CredalNet< GUM_SCALAR > &cn)
Thread constructor.

References VarMod2BNsMap(), and setCNet().

Referenced by VarMod2BNsMap(), VarMod2BNsMap(), and ~VarMod2BNsMap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VarMod2BNsMap() [2/2]

template<GUM_Numeric GUM_SCALAR>
gum::credal::VarMod2BNsMap< GUM_SCALAR >::VarMod2BNsMap ( )

Default constructor that should be used only by InferenceEngine since it has a member variable.

Definition at line 50 of file varMod2BNsMap_tpl.h.

50 {
51 cnet_ = nullptr;
52
54 }
const CredalNet< GUM_SCALAR > * cnet_
A pointer to the CredalNet to be used.

References VarMod2BNsMap(), and cnet_.

Here is the call graph for this function:

◆ ~VarMod2BNsMap()

template<GUM_Numeric GUM_SCALAR>
gum::credal::VarMod2BNsMap< GUM_SCALAR >::~VarMod2BNsMap ( )

Destructor.

Definition at line 64 of file varMod2BNsMap_tpl.h.

64 {
66 }

References VarMod2BNsMap().

Here is the call graph for this function:

Member Function Documentation

◆ getBNOptsFromKey()

template<GUM_Numeric GUM_SCALAR>
const std::vector< std::vector< bool > * > gum::credal::VarMod2BNsMap< GUM_SCALAR >::getBNOptsFromKey ( const std::vector< Size > & key)

Get optimum IBayesNet (s) without structure of the given variable, modality for min or max.

Parameters
keyThe constant reference to the variable, modality, min or max.
Returns
The constant vector of not yet constant pointers to the nets.

Definition at line 209 of file varMod2BNsMap_tpl.h.

209 {
210 // return something even if key does not exist
211 if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
212
214
216 nets.resize(netsHash.size());
217
219
220 for (Size i = 0; i < netsHash.size(); i++, ++it) {
221 nets[i] = &myHashNet_ /*.at(*/[*it]; //);
222 }
223
224 return nets;
225 }
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
hashNet myHashNet_
Map id - dBN : get a net from it's id.

References myHashNet_, and myVarHashs_.

◆ getCurrentSample()

template<GUM_Numeric GUM_SCALAR>
const std::vector< bool > & gum::credal::VarMod2BNsMap< GUM_SCALAR >::getCurrentSample ( )

Get the current sample as a vector of bits without structure.

Returns
The constant reference to the sampled IBayesNet without structure.

Definition at line 197 of file varMod2BNsMap_tpl.h.

197 {
198 return currentSample_;
199 }
dBN currentSample_
The current sampled IBayesNet.

References currentSample_.

◆ getEntrySize()

template<GUM_Numeric GUM_SCALAR>
Size gum::credal::VarMod2BNsMap< GUM_SCALAR >::getEntrySize ( ) const

Get the number of IBayesNet stored.

Definition at line 261 of file varMod2BNsMap_tpl.h.

261 {
262 return myHashNet_.size();
263 }

References myHashNet_.

◆ getFullBNOptsFromKey()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< std::vector< std::vector< bool > > > > gum::credal::VarMod2BNsMap< GUM_SCALAR >::getFullBNOptsFromKey ( const std::vector< Size > & key)

Get optimum IBayesNet (s) with structure of the given variable, modality for min or max.

Parameters
keyThe constant reference to the variable, modality, min or max.
Returns
The vector of not yet constant pointers to the nets.

Definition at line 229 of file varMod2BNsMap_tpl.h.

229 {
230 if (cnet_ == nullptr)
232 "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
233
234 if (!myVarHashs_.exists(key)) return {};
235
237
239 nets.resize(netsHash.size(), sampleDef_);
240
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 }
std::vector< std::vector< std::vector< bool > > > sampleDef_
Since all samples have the same structure, this will be used as default initialization (copy construc...
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References cnet_, GUM_ERROR, myHashNet_, myVarHashs_, and sampleDef_.

◆ getSampleDef()

template<GUM_Numeric GUM_SCALAR>
const std::vector< std::vector< std::vector< bool > > > & gum::credal::VarMod2BNsMap< GUM_SCALAR >::getSampleDef ( )

Get the sample structure.

Returns
The constant reference to the sample structure.

Definition at line 203 of file varMod2BNsMap_tpl.h.

203 {
204 return sampleDef_;
205 }

References sampleDef_.

◆ insert() [1/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::VarMod2BNsMap< GUM_SCALAR >::insert ( const std::vector< bool > & bn,
const std::vector< Size > & key )

Insert for thread fusion.

All inserted nets are optimums and none will be deleted because a better one is being inserted.

Parameters
bnThe constant reference to the net to be inserted.
keyThe constant reference to the key at which we will insert the net.

Definition at line 91 of file varMod2BNsMap_tpl.h.

92 {
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 }
std::hash< std::vector< bool > > vectHash_
The hash fuction.
Size currentHash_
The current sampled IBayesNet hash.
hashVars myHashVars_
Map id - list(varKey) : get all variables, modalities associated to this optimal net id.

References currentHash_, myHashNet_, myHashVars_, myVarHashs_, and vectHash_.

◆ insert() [2/2]

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::VarMod2BNsMap< GUM_SCALAR >::insert ( const std::vector< Size > & key,
const bool isBetter )

Thread insert.

Parameters
keyThe key at which the net will be inserted.
isBetterTrue if the net is a better one, i.e. it gave better marginals.
Returns
True if the net was inserted, false otherwise.

Definition at line 111 of file varMod2BNsMap_tpl.h.

111 {
112 if (isBetter) {
113 // get all nets of this key (maybe entry does not exists)
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
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 }

References currentHash_, currentSample_, myHashNet_, myHashVars_, and myVarHashs_.

◆ setCNet()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::VarMod2BNsMap< GUM_SCALAR >::setCNet ( const CredalNet< GUM_SCALAR > & cn)

Initialize sampleDef from the CredalNet.

Shoud only be used by InferenceEngine to initialize it's member variable.

Parameters
cnThe CredalNet to be used.

Definition at line 69 of file varMod2BNsMap_tpl.h.

69 {
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++) {
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 }

References cnet_, gum::credal::CredalNet< GUM_SCALAR >::credalNet_currentCpt(), sampleDef_, and gum::superiorPow().

Referenced by VarMod2BNsMap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setCurrentSample()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::VarMod2BNsMap< GUM_SCALAR >::setCurrentSample ( const std::vector< std::vector< std::vector< bool > > > & sample)

Set the current thread sample and it's hash.

Parameters
sampleThe constant reference to the sample with structure.

Definition at line 181 of file varMod2BNsMap_tpl.h.

182 {
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 }

References currentHash_, currentSample_, and vectHash_.

Member Data Documentation

◆ cnet_

template<GUM_Numeric GUM_SCALAR>
const CredalNet< GUM_SCALAR >* gum::credal::VarMod2BNsMap< GUM_SCALAR >::cnet_
protected

A pointer to the CredalNet to be used.

Definition at line 104 of file varMod2BNsMap.h.

Referenced by VarMod2BNsMap(), getFullBNOptsFromKey(), and setCNet().

◆ currentHash_

template<GUM_Numeric GUM_SCALAR>
Size gum::credal::VarMod2BNsMap< GUM_SCALAR >::currentHash_
protected

The current sampled IBayesNet hash.

Definition at line 99 of file varMod2BNsMap.h.

Referenced by insert(), insert(), and setCurrentSample().

◆ currentSample_

template<GUM_Numeric GUM_SCALAR>
dBN gum::credal::VarMod2BNsMap< GUM_SCALAR >::currentSample_
protected

The current sampled IBayesNet.

Definition at line 97 of file varMod2BNsMap.h.

Referenced by getCurrentSample(), insert(), and setCurrentSample().

◆ myHashNet_

template<GUM_Numeric GUM_SCALAR>
hashNet gum::credal::VarMod2BNsMap< GUM_SCALAR >::myHashNet_
protected

Map id - dBN : get a net from it's id.

Definition at line 82 of file varMod2BNsMap.h.

Referenced by getBNOptsFromKey(), getEntrySize(), getFullBNOptsFromKey(), insert(), and insert().

◆ myHashVars_

template<GUM_Numeric GUM_SCALAR>
hashVars gum::credal::VarMod2BNsMap< GUM_SCALAR >::myHashVars_
protected

Map id - list(varKey) : get all variables, modalities associated to this optimal net id.

Definition at line 89 of file varMod2BNsMap.h.

Referenced by insert(), and insert().

◆ myVarHashs_

template<GUM_Numeric GUM_SCALAR>
varHashs gum::credal::VarMod2BNsMap< GUM_SCALAR >::myVarHashs_
protected

Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( or max=1 ).

Definition at line 85 of file varMod2BNsMap.h.

Referenced by getBNOptsFromKey(), getFullBNOptsFromKey(), insert(), and insert().

◆ sampleDef_

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< std::vector< bool > > > gum::credal::VarMod2BNsMap< GUM_SCALAR >::sampleDef_
protected

Since all samples have the same structure, this will be used as default initialization (copy constructor) for any sample.

Definition at line 94 of file varMod2BNsMap.h.

Referenced by getFullBNOptsFromKey(), getSampleDef(), and setCNet().

◆ vectHash_

template<GUM_Numeric GUM_SCALAR>
std::hash< std::vector< bool > > gum::credal::VarMod2BNsMap< GUM_SCALAR >::vectHash_
protected

The hash fuction.

Requires c++11/0x.

Definition at line 101 of file varMod2BNsMap.h.

Referenced by insert(), and setCurrentSample().


The documentation for this class was generated from the following files: