aGrUM  0.20.3
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 187 of file varMod2BNsMap_tpl.h.

187  {
188  // return something even if key does not exist
189  if (!myVarHashs_.exists(key)) return std::vector< std::vector< bool >* >();
190 
191  std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
192 
193  std::vector< dBN* > nets;
194  nets.resize(netsHash.size());
195 
196  std::list< Size >::iterator it = netsHash.begin();
197 
198  for (Size i = 0; i < netsHash.size(); i++, ++it) {
199  nets[i] = &myHashNet_ /*.at(*/[*it]; //);
200  }
201 
202  return nets;
203  }
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 175 of file varMod2BNsMap_tpl.h.

175  {
176  return currentSample_;
177  }
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 240 of file varMod2BNsMap_tpl.h.

240  {
241  return myHashNet_.size();
242  }
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 207 of file varMod2BNsMap_tpl.h.

207  {
208  if (cnet == nullptr)
209  GUM_ERROR(OperationNotAllowed,
210  "No CredalNet associated to me ! Can't get FullBNOptsFromKey : " << key);
211 
212  if (!myVarHashs_.exists(key))
213  return std::vector< std::vector< std::vector< std::vector< bool > > > >();
214 
215  std::list< Size >& netsHash = myVarHashs_[key]; //.at(key);
216 
217  std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
218  nets.resize(netsHash.size(), sampleDef_);
219 
220  std::list< Size >::iterator it = netsHash.begin();
221 
222  for (Size i = 0; i < netsHash.size(); i++, ++it) {
223  // std::vector< std::vector< std::vector < bool > > > net(sampleDef_);
224  dBN::iterator it2 = myHashNet_ /*.at(*/[*it] /*)*/.begin();
225 
226  for (Size j = 0; j < sampleDef_.size(); j++) {
227  for (Size k = 0; k < sampleDef_[j].size(); k++) {
228  for (Size l = 0; l < sampleDef_[j][k].size(); l++) {
229  nets[i][j][k][l] = *it2;
230  ++it2;
231  }
232  }
233  }
234  }
235 
236  return nets;
237  }
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:51

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

181  {
182  return sampleDef_;
183  }
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 = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
73 
74  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
75  if (*it == currentHash_) return false;
76  }
77 
78  // add it
79  myHashNet_.set(currentHash_, bn); //[currentHash_] = bn;
80  // insert net hash in our key net list
81  nets.push_back(currentHash_);
82  // insert out key in the hash key list
84  std::list< varKey >()) /*[currentHash_]*/.push_back(key);
85  return true;
86  }
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 89 of file varMod2BNsMap_tpl.h.

89  {
90  if (isBetter) {
91  // get all nets of this key (maybe entry does not exists)
92  std::list< Size >& old_nets
93  = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
94 
95  // for each one
96  for (std::list< Size >::iterator it = old_nets.begin(); it != old_nets.end(); ++it) {
97  // get all keys associated to this net
98  std::list< varKey >& netKeys
99  = myHashVars_.getWithDefault(*it, std::list< varKey >()); //[ *it ];
100 
101  // if we are the sole user, delete the net entry
102  if (netKeys.size() == 1) {
103  myHashVars_.erase(*it);
104  }
105  // other keys use the net, delete our key from list
106  else {
107  for (std::list< varKey >::iterator it2 = netKeys.begin(); it2 != netKeys.end(); ++it2) {
108  if (*it2 == key) {
109  netKeys.erase(it2);
110  break;
111  }
112  }
113  }
114  } // end of : for each old_net
115 
116  // clear all old_nets
117  old_nets.clear();
118  // insert new net with it's hash
120  currentSample_); //[currentHash_] = currentSample_;
121  // insert net hash in our key net list
122  old_nets.push_back(currentHash_);
123  // insert out key in the hash key list
125  .getWithDefault(currentHash_, std::list< varKey >()) /*[currentHash_]*/
126  .push_back(key);
127  return true;
128 
129  } // end of isBetter
130  // another opt net
131  else {
132  // check that we didn't add it for this key
133  std::list< Size >& nets = myVarHashs_.getWithDefault(key, std::list< Size >()); //[ key ];
134 
135  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
136  if (*it == currentHash_) return false;
137  }
138 
139  // add it
141  // insert net hash in our key net list
142  nets.push_back(currentHash_);
143  // insert out key in the hash key list
144  myHashVars_.getWithDefault(currentHash_, std::list< varKey >()).push_back(key);
145 
146  /*
147  // add it
148  myHashNet_[currentHash_] = currentSample_;
149  // insert net hash in our key net list
150  nets.push_back(currentHash_);
151  // insert out key in the hash key list
152  myHashVars_[currentHash_].push_back(key);
153  */
154  return true;
155  } // end of ! isBetter
156  }
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 159 of file varMod2BNsMap_tpl.h.

160  {
161  currentSample_.clear();
162 
163  for (Size i = 0; i < sample.size(); i++)
164  for (Size j = 0; j < sample[j].size(); j++)
165  for (Size k = 0; k < sample[i][j].size(); k++)
166  currentSample_.push_back(sample[i][j][k]);
167 
168  // std::cout << sample << std::endl;
169  // std::cout << currentSample_ << std::endl;
170 
172  }
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: