aGrUM 2.3.2
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<typename 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<typename GUM_SCALAR>
using gum::credal::VarMod2BNsMap< GUM_SCALAR >::dBN = std::vector< bool >
private

Definition at line 74 of file varMod2BNsMap.h.

◆ hashNet

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

Definition at line 76 of file varMod2BNsMap.h.

◆ hashVars

template<typename 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<typename 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<typename 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<typename 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 56 of file varMod2BNsMap_tpl.h.

56 {
57 setCNet(cn);
58
60 }
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<typename 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 49 of file varMod2BNsMap_tpl.h.

49 {
50 cnet = nullptr;
51
53 }
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<typename GUM_SCALAR>
gum::credal::VarMod2BNsMap< GUM_SCALAR >::~VarMod2BNsMap ( )

Destructor.

Definition at line 63 of file varMod2BNsMap_tpl.h.

63 {
65 }

References VarMod2BNsMap().

Here is the call graph for this function:

Member Function Documentation

◆ getBNOptsFromKey()

template<typename 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 208 of file varMod2BNsMap_tpl.h.

208 {
209 // return something even if key does not exist
210 if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
211
213
215 nets.resize(netsHash.size());
216
218
219 for (Size i = 0; i < netsHash.size(); i++, ++it) {
220 nets[i] = &myHashNet_ /*.at(*/[*it]; //);
221 }
222
223 return nets;
224 }
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<typename 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 196 of file varMod2BNsMap_tpl.h.

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

References currentSample_.

◆ getEntrySize()

template<typename 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<typename 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 228 of file varMod2BNsMap_tpl.h.

228 {
229 if (cnet == nullptr)
231 "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
232
233 if (!myVarHashs_.exists(key))
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:72

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

◆ getSampleDef()

template<typename 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 202 of file varMod2BNsMap_tpl.h.

202 {
203 return sampleDef_;
204 }

References sampleDef_.

◆ insert() [1/2]

template<typename 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 90 of file varMod2BNsMap_tpl.h.

91 {
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 }
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<typename 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 110 of file varMod2BNsMap_tpl.h.

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

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

◆ setCNet()

template<typename 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 68 of file varMod2BNsMap_tpl.h.

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

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<typename 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 180 of file varMod2BNsMap_tpl.h.

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

References currentHash_, currentSample_, and vectHash_.

Member Data Documentation

◆ cnet

template<typename 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<typename 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<typename 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<typename 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<typename 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<typename 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<typename 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<typename 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: