aGrUM  0.13.2
inferenceEngine.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef __INFERENCE_ENGINE__H__
22 #define __INFERENCE_ENGINE__H__
23 
30 
33 #include <map>
34 
35 #include <agrum/CN/LrsWrapper.h>
36 #include <agrum/CN/varMod2BNsMap.h>
38 #include <agrum/core/exceptions.h>
39 #include <agrum/core/math/pow.h>
41 
42 namespace gum {
43  namespace credal {
44 
56  template < typename GUM_SCALAR >
58  private:
59  //@beforeMerging swapping from typedef to using
63 
64  using dynExpe =
66 
69 
70  protected:
73 
78 
83 
86 
93 
100 
103 
108 
123 
136 
146 
153 
156 
159  void _repetitiveInit();
160 
167  void _initExpectations();
168 
174  void _initMarginals();
175 
179  void _initMarginalSets();
180 
182 
185 
194  inline const GUM_SCALAR _computeEpsilon();
195 
204  inline void _updateExpectations(const NodeId& id,
205  const std::vector< GUM_SCALAR >& vertex);
206 
218  inline void _updateCredalSets(const NodeId& id,
219  const std::vector< GUM_SCALAR >& vertex,
220  const bool& elimRedund = false);
221 
223 
226 
229  void _dynamicExpectations();
231 
232  public:
235 
236  // InferenceEngine ();
246  virtual ~InferenceEngine();
247 
249 
252 
253  virtual void makeInference() = 0;
255 
258 
263 
269 
275 
281 
287  void setRepetitiveInd(const bool repetitive);
288 
292  void storeVertices(const bool value);
298  void storeBNOpt(const bool value);
299 
304  bool repetitiveInd() const;
310 
314  bool storeVertices() const;
320  bool storeBNOpt() const;
322 
325 
329  void insertModalsFile(const std::string& path);
330 
335  void insertModals(
336  const std::map< std::string, std::vector< GUM_SCALAR > >& modals);
337 
342  virtual void insertEvidenceFile(const std::string& path);
343 
348  void insertEvidence(
349  const std::map< std::string, std::vector< GUM_SCALAR > >& eviMap);
350 
355  void
356  insertEvidence(const NodeProperty< std::vector< GUM_SCALAR > >& evidence);
357 
362  void insertQueryFile(const std::string& path);
363 
368  void insertQuery(const NodeProperty< std::vector< bool > >& query);
369 
371 
374 
381  virtual void eraseAllEvidence();
382 
388  const std::vector< GUM_SCALAR >& marginalMin(const NodeId id) const;
389 
395  const std::vector< GUM_SCALAR >& marginalMax(const NodeId id) const;
396 
402  const std::vector< GUM_SCALAR >&
403  marginalMin(const std::string& varName) const;
404 
410  const std::vector< GUM_SCALAR >&
411  marginalMax(const std::string& varName) const;
412 
418  const GUM_SCALAR& expectationMin(const NodeId id) const;
419 
425  const GUM_SCALAR& expectationMax(const NodeId id) const;
426 
432  const GUM_SCALAR& expectationMin(const std::string& varName) const;
433 
439  const GUM_SCALAR& expectationMax(const std::string& varName) const;
440 
452  const std::vector< GUM_SCALAR >&
453  dynamicExpMin(const std::string& varName) const;
454 
466  const std::vector< GUM_SCALAR >&
467  dynamicExpMax(const std::string& varName) const;
468 
474  const std::vector< std::vector< GUM_SCALAR > >&
475  vertices(const NodeId id) const;
476 
481  void saveMarginals(const std::string& path) const;
482 
487  void saveExpectations(const std::string& path) const;
488 
493  void saveVertices(const std::string& path) const;
494 
500  void dynamicExpectations(); // if someone forgets the protected call at
501  // the end
502  // of its own algorithm, the user can call it
503 
507  std::string toString() const;
508 
513  const std::string getApproximationSchemeMsg() {
514  return this->messageApproximationScheme();
515  }
516 
518  };
519 
520 
521  extern template class InferenceEngine< float >;
522  extern template class InferenceEngine< double >;
523  } // namespace credal
524 } // namespace gum
525 
527 
528 #endif
virtual ~InferenceEngine()
Destructor.
void saveExpectations(const std::string &path) const
Saves expectations to file.
void saveMarginals(const std::string &path) const
Saves marginals to file.
This file contains general scheme for iteratively convergent algorithms.
const GUM_SCALAR & expectationMax(const NodeId id) const
Get the upper expectation of a given node id.
margi _oldMarginalMin
Old lower marginals used to compute epsilon.
void insertModals(const std::map< std::string, std::vector< GUM_SCALAR > > &modals)
Insert variables modalities from map to compute expectations.
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
std::string messageApproximationScheme() const
Returns the approximation scheme message.
const std::vector< std::vector< GUM_SCALAR > > & vertices(const NodeId id) const
Get the vertice of a given node id.
Approximation Scheme.
void insertModalsFile(const std::string &path)
Insert variables modalities from file to compute expectations.
bool _storeBNOpt
Iterations limit stopping rule used by some algorithms such as CNMonteCarloSampling.
void _dynamicExpectations()
Rearrange lower and upper expectations to suit dynamic networks.
dynExpe _dynamicExpMin
Lower dynamic expectations.
credalSet _marginalSets
Credal sets vertices, if enabled.
const GUM_SCALAR & expectationMin(const NodeId id) const
Get the lower expectation of a given node id.
const std::string getApproximationSchemeMsg()
Get approximation scheme state.
const std::vector< GUM_SCALAR > & dynamicExpMax(const std::string &varName) const
Get the upper dynamic expectation of a given variable prefix (without the time step included...
virtual void insertEvidenceFile(const std::string &path)
Insert evidence from file.
void insertQueryFile(const std::string &path)
Insert query variables states from file.
margi _marginalMin
Lower marginals.
const std::vector< GUM_SCALAR > & dynamicExpMin(const std::string &varName) const
Get the lower dynamic expectation of a given variable prefix (without the time step included...
int _timeSteps
The number of time steps of this network (only usefull for dynamic networks).
const std::vector< GUM_SCALAR > & marginalMin(const NodeId id) const
Get the lower marginals of a given node id.
Implementation of pow functions with integers, faster than stl implementation.
margi _oldMarginalMax
Old upper marginals used to compute epsilon.
VarMod2BNsMap< GUM_SCALAR > * getVarMod2BNsMap()
Get optimum IBayesNet.
void dynamicExpectations()
Compute dynamic expectations.
const GUM_SCALAR _computeEpsilon()
Compute approximation scheme epsilon using the old marginals and the new ones.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::string toString() const
Print all nodes marginals to standart output.
void _repetitiveInit()
Initialize _t0 and _t1 clusters.
expe _expectationMax
Upper expectations, if some variables modalities were inserted.
InferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Construtor.
The class for generic Hash Tables.
Definition: hashTable.h:676
cluster _t0
Clusters of nodes used with dynamic networks.
the class for computing G2 scores
const NodeProperty< std::vector< NodeId > > & getT0Cluster() const
Get the _t0 cluster.
Class template representing a Credal Network.
Definition: credalNet.h:87
VarMod2BNsMap< GUM_SCALAR > _dbnOpt
Object used to efficiently store optimal bayes net during inference, for some algorithms.
virtual void makeInference()=0
To be redefined by each credal net algorithm.
Lrs wrapper.
aGrUM&#39;s exceptions
const CredalNet< GUM_SCALAR > * _credalNet
A pointer to the Credal Net used.
const NodeProperty< std::vector< NodeId > > & getT1Cluster() const
Get the _t1 cluster.
dynExpe _dynamicExpMax
Upper dynamic expectations.
bool storeVertices() const
Get the number of iterations without changes used to stop some algorithms.
void _initMarginals()
Initialize lower and upper old marginals and marginals before inference, with the lower marginal bein...
typename gum::HashTable< std::string, std::vector< GUM_SCALAR > > dynExpe
const std::vector< GUM_SCALAR > & marginalMax(const NodeId id) const
Get the upper marginals of a given node id.
void insertQuery(const NodeProperty< std::vector< bool > > &query)
Insert query variables and states from Property.
query _query
Holds the query nodes states.
bool _repetitiveInd
True if using repetitive independence ( dynamic network only ), False otherwise.
dynExpe _modal
Variables modalities used to compute expectations.
void saveVertices(const std::string &path) const
Saves vertices to file.
void _updateCredalSets(const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund=false)
Given a node id and one of it&#39;s possible vertex, update it&#39;s credal set.
Abstract class template representing a CredalNet inference engine.
void _initMarginalSets()
Initialize credal set vertices with empty sets.
const CredalNet< GUM_SCALAR > & credalNet()
Get this creadal network.
margi _evidence
Holds observed variables states.
bool repetitiveInd() const
Get the current independence status.
cluster _t1
Clusters of nodes used with dynamic networks.
expe _expectationMin
Lower expectations, if some variables modalities were inserted.
Class template used to approximate decimal numbers by rationals.
Class used to store optimum IBayesNet during some inference algorithms.
bool _storeVertices
True if credal sets vertices are stored, False otherwise.
void setRepetitiveInd(const bool repetitive)
Class used to store optimum IBayesNet during some inference algorithms.
Definition: varMod2BNsMap.h:54
void _initExpectations()
Initialize lower and upper expectations before inference, with the lower expectation being initialize...
void insertEvidence(const std::map< std::string, std::vector< GUM_SCALAR > > &eviMap)
Insert evidence from map.
void _updateExpectations(const NodeId &id, const std::vector< GUM_SCALAR > &vertex)
Given a node id and one of it&#39;s possible vertex obtained during inference, update this node lower and...
margi _marginalMax
Upper marginals.
virtual void eraseAllEvidence()
Erase all inference related data to perform another one.