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

Protected Attributes

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

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()

Definition at line 55 of file varMod2BNsMap.h.

Member Typedef Documentation

◆ dBN

template<typename GUM_SCALAR >
typedef std::vector< bool > gum::credal::VarMod2BNsMap< GUM_SCALAR >::dBN
private

Definition at line 57 of file varMod2BNsMap.h.

◆ hashNet

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

Definition at line 61 of file varMod2BNsMap.h.

◆ hashVars

template<typename GUM_SCALAR >
typedef gum::HashTable< Size, std::list< varKey > > gum::credal::VarMod2BNsMap< GUM_SCALAR >::hashVars
private

Definition at line 65 of file varMod2BNsMap.h.

◆ varHashs

template<typename GUM_SCALAR >
typedef gum::HashTable< varKey, std::list< Size > > gum::credal::VarMod2BNsMap< GUM_SCALAR >::varHashs
private

Definition at line 63 of file varMod2BNsMap.h.

◆ varKey

template<typename GUM_SCALAR >
typedef std::vector< Size > gum::credal::VarMod2BNsMap< GUM_SCALAR >::varKey
private

Definition at line 59 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 35 of file varMod2BNsMap_tpl.h.

35  {
36  setCNet(cn);
37 
38  GUM_CONSTRUCTOR(VarMod2BNsMap);
39  }
void setCNet(const CredalNet< GUM_SCALAR > &cn)
Initialize sampleDef__ from the CredalNet.
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable...

◆ 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 28 of file varMod2BNsMap_tpl.h.

28  {
29  cnet = nullptr;
30 
31  GUM_CONSTRUCTOR(VarMod2BNsMap);
32  }
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:91
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable...

◆ ~VarMod2BNsMap()

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

Destructor.

Definition at line 42 of file varMod2BNsMap_tpl.h.

42  {
43  GUM_DESTRUCTOR(VarMod2BNsMap);
44  }
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable...

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

198  {
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;
205  nets.resize(netsHash.size());
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  }
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:72
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
hashNet myHashNet_
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:69
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ 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 185 of file varMod2BNsMap_tpl.h.

185  {
186  return currentSample_;
187  }
dBN currentSample_
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:84

◆ getEntrySize()

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

Get the number of IBayesNet stored.

Definition at line 253 of file varMod2BNsMap_tpl.h.

253  {
254  return myHashNet_.size();
255  }
Size size() const noexcept
Returns the number of elements stored into the hashtable.
hashNet myHashNet_
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:69

◆ 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 218 of file varMod2BNsMap_tpl.h.

219  {
220  if (cnet == nullptr)
221  GUM_ERROR(OperationNotAllowed,
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;
231  nets.resize(netsHash.size(), sampleDef_);
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  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
std::vector< std::vector< std::vector< bool > > > sampleDef_
Since all samples have the same structure, this will be used as default initialization (copy construc...
Definition: varMod2BNsMap.h:81
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:72
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:91
hashNet myHashNet_
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:69
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 191 of file varMod2BNsMap_tpl.h.

191  {
192  return sampleDef_;
193  }
std::vector< std::vector< std::vector< bool > > > sampleDef_
Since all samples have the same structure, this will be used as default initialization (copy construc...
Definition: varMod2BNsMap.h:81

◆ 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 69 of file varMod2BNsMap_tpl.h.

70  {
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
82  nets.push_back(currentHash_);
83  // insert out key in the hash key list
86  std::list< varKey >()) /*[currentHash_]*/.push_back(key);
87  return true;
88  }
hashVars myHashVars_
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:76
Size currentHash_
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:86
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:72
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
hashNet myHashNet_
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:69
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
std::hash< std::vector< bool > > vectHash_
The hash fuction.
Definition: varMod2BNsMap.h:88
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ 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 91 of file varMod2BNsMap_tpl.h.

92  {
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
129  old_nets.push_back(currentHash_);
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
151  nets.push_back(currentHash_);
152  // insert out key in the hash key list
153  myHashVars_.getWithDefault(currentHash_, std::list< varKey >())
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  }
hashVars myHashVars_
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:76
dBN currentSample_
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:84
Size currentHash_
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:86
void erase(const Key &key)
Removes a given element from the hash table.
varHashs myVarHashs_
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:72
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
hashNet myHashNet_
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:69
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.

◆ 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 47 of file varMod2BNsMap_tpl.h.

47  {
48  auto* cpt = &cn.credalNet_currentCpt();
49  auto nNodes = cpt->size();
50  sampleDef_.resize(nNodes);
51 
52  for (NodeId node = 0; node < nNodes; node++) {
53  auto pConfs = (*cpt)[node].size();
54  sampleDef_[node].resize(pConfs);
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);
61  sampleDef_[node][pconf].resize(nBits);
62  }
63  }
64 
65  cnet = &cn;
66  }
std::vector< std::vector< std::vector< bool > > > sampleDef_
Since all samples have the same structure, this will be used as default initialization (copy construc...
Definition: varMod2BNsMap.h:81
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:91
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:54
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ 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 169 of file varMod2BNsMap_tpl.h.

170  {
171  currentSample_.clear();
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++)
176  currentSample_.push_back(sample[i][j][k]);
177 
178  // std::cout << sample << std::endl;
179  // std::cout << currentSample_ << std::endl;
180 
182  }
dBN currentSample_
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:84
Size currentHash_
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:86
std::hash< std::vector< bool > > vectHash_
The hash fuction.
Definition: varMod2BNsMap.h:88
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

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 91 of file varMod2BNsMap.h.

◆ currentHash_

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

The current sampled IBayesNet hash.

Definition at line 86 of file varMod2BNsMap.h.

◆ currentSample_

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

The current sampled IBayesNet.

Definition at line 84 of file varMod2BNsMap.h.

◆ 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 69 of file varMod2BNsMap.h.

◆ 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 76 of file varMod2BNsMap.h.

◆ 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 72 of file varMod2BNsMap.h.

◆ 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 81 of file varMod2BNsMap.h.

◆ 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 88 of file varMod2BNsMap.h.


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