aGrUM  0.13.2
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 54 of file varMod2BNsMap.h.

Member Typedef Documentation

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

Definition at line 56 of file varMod2BNsMap.h.

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

Definition at line 60 of file varMod2BNsMap.h.

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

Definition at line 64 of file varMod2BNsMap.h.

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

Definition at line 62 of file varMod2BNsMap.h.

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

Definition at line 58 of file varMod2BNsMap.h.

Constructor & Destructor Documentation

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...
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:90
VarMod2BNsMap()
Default constructor that should be used only by InferenceEngine since it has a member variable...
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

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

200  {
201  // return something even if key does not exist
202  if (!_myVarHashs.exists(key)) return std::vector< std::vector< bool >* >();
203 
204  std::list< Size >& netsHash = _myVarHashs[key]; //.at(key);
205 
206  std::vector< dBN* > nets;
207  nets.resize(netsHash.size());
208 
209  std::list< Size >::iterator it = netsHash.begin();
210 
211  for (Size i = 0; i < netsHash.size(); i++, ++it) {
212  nets[i] = &_myHashNet /*.at(*/[*it]; //);
213  }
214 
215  return nets;
216  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
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:71
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:68
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 187 of file varMod2BNsMap_tpl.h.

187  {
188  return _currentSample;
189  }
dBN _currentSample
The current sampled IBayesNet.
Definition: varMod2BNsMap.h:83
template<typename GUM_SCALAR >
Size gum::credal::VarMod2BNsMap< GUM_SCALAR >::getEntrySize ( ) const

Get the number of IBayesNet stored.

Definition at line 255 of file varMod2BNsMap_tpl.h.

255  {
256  return _myHashNet.size();
257  }
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:68
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 220 of file varMod2BNsMap_tpl.h.

References GUM_ERROR.

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

193  {
194  return _sampleDef;
195  }
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:80
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 71 of file varMod2BNsMap_tpl.h.

72  {
74  std::list< Size >& nets =
75  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
76 
77  for (std::list< Size >::iterator it = nets.begin(); it != nets.end(); ++it) {
78  if (*it == _currentHash) return false;
79  }
80 
81  // add it
82  _myHashNet.set(_currentHash, bn); //[_currentHash] = bn;
83  // insert net hash in our key net list
84  nets.push_back(_currentHash);
85  // insert out key in the hash key list
88  std::list< varKey >()) /*[_currentHash]*/.push_back(key);
89  return true;
90  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
hashVars _myHashVars
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:75
std::hash< std::vector< bool > > _vectHash
The hash fuction.
Definition: varMod2BNsMap.h:87
Size _currentHash
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:85
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:71
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:68
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 93 of file varMod2BNsMap_tpl.h.

94  {
95  if (isBetter) {
96  // get all nets of this key (maybe entry does not exists)
97  std::list< Size >& old_nets =
98  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
99 
100  // for each one
101  for (std::list< Size >::iterator it = old_nets.begin();
102  it != old_nets.end();
103  ++it) {
104  // get all keys associated to this net
105  std::list< varKey >& netKeys =
106  _myHashVars.getWithDefault(*it, std::list< varKey >()); //[ *it ];
107 
108  // if we are the sole user, delete the net entry
109  if (netKeys.size() == 1) {
110  _myHashVars.erase(*it);
111  }
112  // other keys use the net, delete our key from list
113  else {
114  for (std::list< varKey >::iterator it2 = netKeys.begin();
115  it2 != netKeys.end();
116  ++it2) {
117  if (*it2 == key) {
118  netKeys.erase(it2);
119  break;
120  }
121  }
122  }
123  } // end of : for each old_net
124 
125  // clear all old_nets
126  old_nets.clear();
127  // insert new net with it's hash
129  _currentSample); //[_currentHash] = _currentSample;
130  // insert net hash in our key net list
131  old_nets.push_back(_currentHash);
132  // insert out key in the hash key list
134  .getWithDefault(_currentHash, std::list< varKey >()) /*[_currentHash]*/
135  .push_back(key);
136  return true;
137 
138  } // end of isBetter
139  // another opt net
140  else {
141  // check that we didn't add it for this key
142  std::list< Size >& nets =
143  _myVarHashs.getWithDefault(key, std::list< Size >()); //[ key ];
144 
145  for (std::list< Size >::iterator it = nets.begin(); it != nets.end();
146  ++it) {
147  if (*it == _currentHash) return false;
148  }
149 
150  // add it
152  // insert net hash in our key net list
153  nets.push_back(_currentHash);
154  // insert out key in the hash key list
155  _myHashVars.getWithDefault(_currentHash, std::list< varKey >())
156  .push_back(key);
157 
158  /*
159  // add it
160  _myHashNet[_currentHash] = _currentSample;
161  // insert net hash in our key net list
162  nets.push_back(_currentHash);
163  // insert out key in the hash key list
164  _myHashVars[_currentHash].push_back(key);
165  */
166  return true;
167  } // end of ! isBetter
168  }
hashVars _myHashVars
Map id - list(varKey) : get all variables, modalities associated to this optimal net id...
Definition: varMod2BNsMap.h:75
void erase(const Key &key)
Removes a given element from the hash table.
Size _currentHash
The current sampled IBayesNet hash.
Definition: varMod2BNsMap.h:85
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:83
varHashs _myVarHashs
Map varKey - list(id) : get all optimal nets id from the given key : variable, modality and min=0 ( o...
Definition: varMod2BNsMap.h:71
hashNet _myHashNet
Map id - dBN : get a net from it&#39;s id.
Definition: varMod2BNsMap.h:68
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  Size newCard = Size(c);
63  _sampleDef[node][pconf].resize(nBits);
64  }
65  }
66 
67  cnet = &cn;
68  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
const CredalNet< GUM_SCALAR > * cnet
A pointer to the CredalNet to be used.
Definition: varMod2BNsMap.h:90
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:80
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:52

+ Here is the call graph for this function:

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

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

Member Data Documentation

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

The current sampled IBayesNet hash.

Definition at line 85 of file varMod2BNsMap.h.

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

The current sampled IBayesNet.

Definition at line 83 of file varMod2BNsMap.h.

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

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

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

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

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

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


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