aGrUM  0.16.0
credalNet.h
Go to the documentation of this file.
1 
23 #ifndef __CREDAL_NET__H__
24 #define __CREDAL_NET__H__
25 
32 #include <agrum/agrum.h>
33 
34 #include <iostream>
35 #include <vector>
36 
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #else
40 # include <agrum/core/mvsc/unistd.h>
41 #endif
42 
43 //#include <sys/wait.h>
44 #include <algorithm>
45 #include <cstdlib>
46 #include <fcntl.h>
47 #include <fstream>
48 #include <set>
49 #include <sstream>
50 #include <string>
51 #include <sys/stat.h>
52 
53 #include <utility>
54 
55 #include <agrum/core/math/math.h>
56 #include <agrum/BN/BayesNet.h>
59 #include <agrum/core/exceptions.h>
60 
61 #include <agrum/core/math/pow.h> // custom pow functions with integers, faster implementation
62 #include <agrum/core/math/rational.h> // custom decimal to rational
63 
64 #include <agrum/CN/LrsWrapper.h>
65 
66 #include <agrum/core/OMPThreads.h>
67 
68 // 64 bits for windows (long is 32 bits)
69 #ifdef _MSC_VER
70 typedef __int64 int64_t;
71 typedef unsigned __int64 uint64_t;
72 #else
73 # include <stdint.h>
74 #endif
75 
76 namespace gum {
77  namespace credal {
78 
88  template < typename GUM_SCALAR >
89  class CredalNet {
90  public:
92  enum class NodeType : char { Precise, Credal, Vacuous, Indic };
93 
96 
102  CredalNet();
103 
121  CredalNet(const std::string& src_min_num,
122  const std::string& src_max_den = "");
123 
137  CredalNet(
138  const BayesNet< GUM_SCALAR >& src_min_num,
139  const BayesNet< GUM_SCALAR >& src_max_den = BayesNet< GUM_SCALAR >());
140 
144  ~CredalNet();
145 
147 
150 
157  NodeId addVariable(const std::string& name, const Size& card);
158 
164  void addArc(const NodeId& tail, const NodeId& head);
165 
183  void setCPTs(
184  const NodeId& id,
185  const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt);
186 
207  void setCPT(const NodeId& id,
208  Size& entry,
209  const std::vector< std::vector< GUM_SCALAR > >& cpt);
210 
232  void setCPT(const NodeId& id,
233  Instantiation ins,
234  const std::vector< std::vector< GUM_SCALAR > >& cpt);
235 
251  void fillConstraints(const NodeId& id,
252  const std::vector< GUM_SCALAR >& lower,
253  const std::vector< GUM_SCALAR >& upper);
254 
273  void fillConstraint(const NodeId& id,
274  const Idx& entry,
275  const std::vector< GUM_SCALAR >& lower,
276  const std::vector< GUM_SCALAR >& upper);
277 
294  void fillConstraint(const NodeId& id,
295  Instantiation ins,
296  const std::vector< GUM_SCALAR >& lower,
297  const std::vector< GUM_SCALAR >& upper);
298 
306 
312  Size domainSize(const NodeId& id);
313 
315 
318 
355  void bnToCredal(const GUM_SCALAR beta,
356  const bool oneNet,
357  const bool keepZeroes = false);
358 
370 
379  void intervalToCredal();
380 
396  void lagrangeNormalization();
397 
412  void idmLearning(const Idx s = 0, const bool keepZeroes = false);
413 
426 
428 
429  // other utility member methods
430  // PH void saveCNet ( const std::string &cn_path ) const;
431  // PH void loadCNet ( const std::string &src_cn_path );
432 
446  void saveBNsMinMax(const std::string& min_path,
447  const std::string& max_path) const;
448 
449  // PH void vacants ( int &result ) const;
450  // PH void notVacants ( int &result ) const;
451  // PH void averageVertices ( GUM_SCALAR &result ) const;
452 
458  std::string toString() const;
459  // PH void toPNG ( const std::string &png_path ) const;
460 
469  void computeCPTMinMax(); // REDO THIS IN PRIVATE !!!
470 
473 
478  const BayesNet< GUM_SCALAR >& src_bn() const;
479 
485  const BayesNet< GUM_SCALAR >& current_bn() const;
486 
491  const NodeProperty<
492  std::vector< std::vector< std::vector< GUM_SCALAR > > > >&
493  credalNet_currentCpt() const;
494 
499  const NodeProperty<
500  std::vector< std::vector< std::vector< GUM_SCALAR > > > >&
501  credalNet_srcCpt() const;
502 
509  NodeType currentNodeType(const NodeId& id) const;
510 
517  NodeType nodeType(const NodeId& id) const;
518 
523  const GUM_SCALAR& epsilonMin() const;
524 
529  const GUM_SCALAR& epsilonMax() const;
530 
535  const GUM_SCALAR& epsilonMean() const;
536 
541  const bool isSeparatelySpecified() const;
542 
552  const bool hasComputedCPTMinMax() const;
553 
562  const std::vector< std::vector< GUM_SCALAR > >& get_CPT_min() const;
563 
572  const std::vector< std::vector< GUM_SCALAR > >& get_CPT_max() const;
573 
574  // PH const std::vector< std::vector< NodeId > > & var_bits() const;
575 
577 
578  protected:
579  private:
581  GUM_SCALAR __precisionC; // = 1e6;
583  GUM_SCALAR __deltaC; // = 5;
584 
588  GUM_SCALAR __epsilonMin;
592  GUM_SCALAR __epsilonMax;
596  GUM_SCALAR __epsilonMoy;
597 
601  GUM_SCALAR __epsRedund; //= 1e-6;
602 
606  GUM_SCALAR __epsF; // = 1e-6;
610  GUM_SCALAR __denMax; // = 1e6; // beware LRS
611 
613  GUM_SCALAR __precision; // = 1e6; // beware LRS
614 
619 
622 
627 
630 
634 
637  __credalNet_current_cpt; // = nullptr;
638 
641 
646 
656  typename std::vector< std::vector< GUM_SCALAR > > __binCptMin;
657 
664  typename std::vector< std::vector< GUM_SCALAR > > __binCptMax;
665 
667  void __sort_varType();
668 
675  int __find_dNode_card(
676  const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt)
677  const;
678 
688  void __intervalToCredal();
689 
693  void __initParams();
694 
698  void __initCNNets(const std::string& src_min_num,
699  const std::string& src_max_den);
700 
704  void __initCNNets(const BayesNet< GUM_SCALAR >& src_min_num,
705  const BayesNet< GUM_SCALAR >& src_max_den);
706 
723  void __bnCopy(BayesNet< GUM_SCALAR >& bn_dest);
724 
725  // void __H2Vcdd ( const std::vector< std::vector< GUM_SCALAR > > & h_rep,
726  // std::vector< std::vector< GUM_SCALAR > > & v_rep ) const;
738  void __H2Vlrs(const std::vector< std::vector< GUM_SCALAR > >& h_rep,
739  std::vector< std::vector< GUM_SCALAR > >& v_rep) const;
740 
741  }; // CredalNet
742 
743 
744 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
745  extern template class CredalNet< double >;
746 #endif
747 
748  } // namespace credal
749 } // namespace gum
750 
751 #include <agrum/CN/credalNet_tpl.h>
752 
753 #endif // __CREDAL_NET__H__
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:596
void setCPT(const NodeId &id, Size &entry, const std::vector< std::vector< GUM_SCALAR > > &cpt)
Set the vertices of one credal set of a given node ( any instantiation index )
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::vector< std::vector< GUM_SCALAR > > __binCptMax
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:664
Class representing a Bayesian Network.
Definition: BayesNet.h:78
void __bnCopy(BayesNet< GUM_SCALAR > &bn_dest)
const bool isSeparatelySpecified() const
void saveBNsMinMax(const std::string &min_path, const std::string &max_path) const
If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet...
std::string toString() const
NodeProperty< std::vector< NodeId > > __var_bits
Corresponding bits of each variable.
Definition: credalNet.h:640
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:618
GUM_SCALAR __epsF
Value under which a decimal number is considered to be zero when using __farey.
Definition: credalNet.h:606
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:629
void fillConstraints(const NodeId &id, const std::vector< GUM_SCALAR > &lower, const std::vector< GUM_SCALAR > &upper)
Set the interval constraints of the credal sets of a given node ( all instantiations ) ...
NodeProperty< NodeType > __original_nodeType
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:643
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:645
void __initParams()
Initialize private constant variables after the Constructor has been called.
GUM_SCALAR __precision
Precision used by __frac.
Definition: credalNet.h:613
void setCPTs(const NodeId &id, const std::vector< std::vector< std::vector< GUM_SCALAR > > > &cpt)
Set the vertices of the credal sets ( all of the conditionals ) of a given node
Definition: credalNet_tpl.h:66
NodeType currentNodeType(const NodeId &id) const
void __initCNNets(const std::string &src_min_num, const std::string &src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
void fillConstraint(const NodeId &id, const Idx &entry, const std::vector< GUM_SCALAR > &lower, const std::vector< GUM_SCALAR > &upper)
Set the interval constraints of a credal set of a given node ( from an instantiation index ) ...
void __sort_varType()
Set the NodeType of each node
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void computeCPTMinMax()
Used with binary networks to speed-up L2U inference.
The class for generic Hash Tables.
Definition: hashTable.h:679
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_max() const
Used with binary networks to speed-up L2U inference.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Instantiation instantiation(const NodeId &id)
Get an Instantiation from a node id, usefull to fill the constraints of the network ...
void approximatedBinarization()
Approximate binarization.
void lagrangeNormalization()
Normalize counts of a BayesNet storing counts of each events such that no probability is 0...
Class template representing a Credal Network.
Definition: credalNet.h:89
NodeId addVariable(const std::string &name, const Size &card)
Adds a discrete node into the network.
Definition: credalNet_tpl.h:42
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_srcCpt() const
const GUM_SCALAR & epsilonMean() const
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:649
int __find_dNode_card(const std::vector< std::vector< std::vector< GUM_SCALAR > > > &var_cpt) const
void __H2Vlrs(const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
NodeType
NodeType to speed-up computations in some algorithms.
Definition: credalNet.h:92
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:624
const GUM_SCALAR & epsilonMin() const
NodeType nodeType(const NodeId &id) const
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_min() const
Used with binary networks to speed-up L2U inference.
void __intervalToCredal()
Computes the vertices of each credal set according to their interval definition (does not use lrs)...
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
GUM_SCALAR __precisionC
1e6 by default, used by __fracC as precision.
Definition: credalNet.h:581
GUM_SCALAR __denMax
Highest possible denominator allowed when using __farey.
Definition: credalNet.h:610
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:637
const BayesNet< GUM_SCALAR > & current_bn() const
Size domainSize(const NodeId &id)
Get the cardinality of a node
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
GUM_SCALAR __epsilonMax
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:592
void intervalToCredal()
Computes the vertices of each credal set according to their interval definition (uses lrs)...
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:633
const bool hasComputedCPTMinMax() const
void bnToCredal(const GUM_SCALAR beta, const bool oneNet, const bool keepZeroes=false)
Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of poin...
GUM_SCALAR __epsilonMin
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:588
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size Idx
Type for indexes.
Definition: types.h:53
GUM_SCALAR __deltaC
5 by default, used by __fracC as number of decimals.
Definition: credalNet.h:583
~CredalNet()
Destructor.
const GUM_SCALAR & epsilonMax() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void addArc(const NodeId &tail, const NodeId &head)
Adds an arc between two nodes.
Definition: credalNet_tpl.h:59
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
const BayesNet< GUM_SCALAR > & src_bn() const
std::vector< std::vector< GUM_SCALAR > > __binCptMin
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:656
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
void idmLearning(const Idx s=0, const bool keepZeroes=false)
Learns parameters from a BayesNet storing counts of events.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:626
GUM_SCALAR __epsRedund
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:601