aGrUM  0.15.0
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 56 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 58 of file varMod2BNsMap.h.

◆ hashNet

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

Definition at line 62 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 66 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 64 of file varMod2BNsMap.h.

◆ varKey

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

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

36  {
37  setCNet(cn);
38 
39  GUM_CONSTRUCTOR(VarMod2BNsMap);
40  }
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 29 of file varMod2BNsMap_tpl.h.

29  {
30  cnet = nullptr;
31 
32  GUM_CONSTRUCTOR(VarMod2BNsMap);
33  }
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:92
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 43 of file varMod2BNsMap_tpl.h.

43  {
44  GUM_DESTRUCTOR(VarMod2BNsMap);
45  }
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 198 of file varMod2BNsMap_tpl.h.

199  {
200  // return something even if key does not exist
201  if (!_myVarHashs.exists(key)) return std::vector< std::vector< bool >* >();
202 
203  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
204 
205  std::vector< dBN* > nets;
206  nets.resize(netsHash.size());
207 
208  std::list< Size >::iterator it = netsHash.begin();
209 
210  for (Size i = 0; i < netsHash.size(); i++, ++it) {
211  nets[i] = &_myHashNet /*.at(*/[*it]; //);
212  }
213 
214  return nets;
215  }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
varHashs _myVarHashs
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:73
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:70
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

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

186  {
187  return _currentSample;
188  }
dBN _currentSample
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:85

◆ getEntrySize()

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

Get the number of IBayesNet stored.

Definition at line 254 of file varMod2BNsMap_tpl.h.

254  {
255  return _myHashNet.size();
256  }
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:70

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

References GUM_ERROR.

220  {
221  if (cnet == nullptr)
222  GUM_ERROR(OperationNotAllowed,
223  "No CredalNet associated to me ! Can't get FullBNOptsFromKey : "
224  << key);
225 
226  if (!_myVarHashs.exists(key))
227  return std::vector< std::vector< std::vector< std::vector< bool > > > >();
228 
229  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
230 
231  std::vector< std::vector< std::vector< std::vector< bool > > > > nets;
232  nets.resize(netsHash.size(), _sampleDef);
233 
234  std::list< Size >::iterator it = netsHash.begin();
235 
236  for (Size i = 0; i < netsHash.size(); i++, ++it) {
237  // std::vector< std::vector< std::vector < bool > > > net(_sampleDef);
238  dBN::iterator it2 = _myHashNet /*.at(*/[*it] /*)*/.begin();
239 
240  for (Size j = 0; j < _sampleDef.size(); j++) {
241  for (Size k = 0; k < _sampleDef[j].size(); k++) {
242  for (Size l = 0; l < _sampleDef[j][k].size(); l++) {
243  nets[i][j][k][l] = *it2;
244  ++it2;
245  }
246  }
247  }
248  }
249 
250  return nets;
251  }
iterator begin()
Returns an unsafe iterator pointing to the beginning of the hashtable.
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:92
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:82
varHashs _myVarHashs
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:73
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:70
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

192  {
193  return _sampleDef;
194  }
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:82

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

71  {
73  std::list< Size >& nets =
74  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
75 
76  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
77  if (*it == _currentHash) return false;
78  }
79 
80  // add it
81  _myHashNet.set(_currentHash, bn); //[_currentHash] = bn;
82  // insert net hash in our key net list
83  nets.push_back(_currentHash);
84  // insert out key in the hash key list
87  std::list< varKey >()) /*[_currentHash]*/.push_back(key);
88  return true;
89  }
hashVars _myHashVars
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:77
std::hash< std::vector< bool > > _vectHash
The hash fuction.
Definition: varMod2BNsMap.h:89
Size _currentHash
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:87
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
varHashs _myVarHashs
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:73
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:70
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

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

93  {
94  if (isBetter) {
95  // get all nets of this key (maybe entry does not exists)
96  std::list< Size >& old_nets =
97  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
98 
99  // for each one
100  for (std::list< Size >::iterator it = old_nets.begin();
101  it != old_nets.end();
102  ++it) {
103  // get all keys associated to this net
104  std::list< varKey >& netKeys =
105  _myHashVars.getWithDefault(*it, std::list< varKey >()); //[ *it ];
106 
107  // if we are the sole user, delete the net entry
108  if (netKeys.size() == 1) {
109  _myHashVars.erase(*it);
110  }
111  // other keys use the net, delete our key from list
112  else {
113  for (std::list< varKey >::iterator it2 = netKeys.begin();
114  it2 != netKeys.end();
115  ++it2) {
116  if (*it2 == key) {
117  netKeys.erase(it2);
118  break;
119  }
120  }
121  }
122  } // end of : for each old_net
123 
124  // clear all old_nets
125  old_nets.clear();
126  // insert new net with it's hash
128  _currentSample); //[_currentHash] = _currentSample;
129  // insert net hash in our key net list
130  old_nets.push_back(_currentHash);
131  // insert out key in the hash key list
133  .getWithDefault(_currentHash, std::list< varKey >()) /*[_currentHash]*/
134  .push_back(key);
135  return true;
136 
137  } // end of isBetter
138  // another opt net
139  else {
140  // check that we didn't add it for this key
141  std::list< Size >& nets =
142  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
143 
144  for (std::list< Size >::iterator it = nets.begin(); it != nets.end();
145  ++it) {
146  if (*it == _currentHash) return false;
147  }
148 
149  // add it
151  // insert net hash in our key net list
152  nets.push_back(_currentHash);
153  // insert out key in the hash key list
154  _myHashVars.getWithDefault(_currentHash, std::list< varKey >())
155  .push_back(key);
156 
157  /*
158  // add it
159  _myHashNet[_currentHash] = _currentSample;
160  // insert net hash in our key net list
161  nets.push_back(_currentHash);
162  // insert out key in the hash key list
163  _myHashVars[_currentHash].push_back(key);
164  */
165  return true;
166  } // end of ! isBetter
167  }
hashVars _myHashVars
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:77
void erase(const Key &key)
Removes a given element from the hash table.
Size _currentHash
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:87
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
dBN _currentSample
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:85
varHashs _myVarHashs
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:73
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:70

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

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

48  {
49  auto* cpt = &cn.credalNet_currentCpt();
50  auto nNodes = cpt->size();
51  _sampleDef.resize(nNodes);
52 
53  for (NodeId node = 0; node < nNodes; node++) {
54  auto pConfs = (*cpt)[node].size();
55  _sampleDef[node].resize(pConfs);
56 
57  for (Size pconf = 0; pconf < pConfs; pconf++) {
58  Size nVertices = Size((*cpt)[node][pconf].size());
59  unsigned long b, c; // needed by superiorPow
60  superiorPow(static_cast< unsigned long >(nVertices), b, c);
61  Size nBits = Size(b);
62  _sampleDef[node][pconf].resize(nBits);
63  }
64  }
65 
66  cnet = &cn;
67  }
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:92
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:82
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:55
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size NodeId
Type for node ids.
Definition: graphElements.h:98
+ Here is the call 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 170 of file varMod2BNsMap_tpl.h.

171  {
172  _currentSample.clear();
173 
174  for (Size i = 0; i < sample.size(); i++)
175  for (Size j = 0; j < sample[j].size(); j++)
176  for (Size k = 0; k < sample[i][j].size(); k++)
177  _currentSample.push_back(sample[i][j][k]);
178 
179  // std::cout << sample << std::endl;
180  // std::cout << _currentSample << std::endl;
181 
183  }
std::hash< std::vector< bool > > _vectHash
The hash fuction.
Definition: varMod2BNsMap.h:89
Size _currentHash
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:87
dBN _currentSample
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:85
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48

Member Data Documentation

◆ _currentHash

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

The current sampled IBayesNet hash.

Definition at line 87 of file varMod2BNsMap.h.

◆ _currentSample

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

The current sampled IBayesNet.

Definition at line 85 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 70 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 77 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 73 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 82 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 89 of file varMod2BNsMap.h.

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


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