aGrUM  0.14.2
credalNet.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 __CREDAL_NET__H__
22 #define __CREDAL_NET__H__
23 
30 #include <agrum/agrum.h>
31 
32 #include <iostream>
33 #include <vector>
34 
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #else
38 # include <agrum/core/mvsc/unistd.h>
39 #endif
40 
41 //#include <sys/wait.h>
42 #include <algorithm>
43 #include <cstdlib>
44 #include <fcntl.h>
45 #include <fstream>
46 #include <set>
47 #include <sstream>
48 #include <string>
49 #include <sys/stat.h>
50 
51 #include <utility>
52 
53 #include <agrum/core/math/math.h>
54 #include <agrum/BN/BayesNet.h>
57 #include <agrum/core/exceptions.h>
58 
59 #include <agrum/core/math/pow.h> // custom pow functions with integers, faster implementation
60 #include <agrum/core/math/rational.h> // custom decimal to rational
61 
62 #include <agrum/CN/LrsWrapper.h>
63 
64 #include <agrum/core/OMPThreads.h>
65 
66 // 64 bits for windows (long is 32 bits)
67 #ifdef _MSC_VER
68 typedef __int64 int64_t;
69 typedef unsigned __int64 uint64_t;
70 #else
71 # include <stdint.h>
72 #endif
73 
74 namespace gum {
75  namespace credal {
76 
86  template < typename GUM_SCALAR >
87  class CredalNet {
88  public:
90  enum class NodeType : char { Precise, Credal, Vacuous, Indic };
91 
94 
100  CredalNet();
101 
119  CredalNet(const std::string& src_min_num,
120  const std::string& src_max_den = "");
121 
135  CredalNet(
136  const BayesNet< GUM_SCALAR >& src_min_num,
137  const BayesNet< GUM_SCALAR >& src_max_den = BayesNet< GUM_SCALAR >());
138 
142  ~CredalNet();
143 
145 
148 
155  NodeId addVariable(const std::string& name, const Size& card);
156 
162  void addArc(const NodeId& tail, const NodeId& head);
163 
181  void setCPTs(
182  const NodeId& id,
183  const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt);
184 
205  void setCPT(const NodeId& id,
206  Size& entry,
207  const std::vector< std::vector< GUM_SCALAR > >& cpt);
208 
230  void setCPT(const NodeId& id,
231  Instantiation ins,
232  const std::vector< std::vector< GUM_SCALAR > >& cpt);
233 
249  void fillConstraints(const NodeId& id,
250  const std::vector< GUM_SCALAR >& lower,
251  const std::vector< GUM_SCALAR >& upper);
252 
271  void fillConstraint(const NodeId& id,
272  const Idx& entry,
273  const std::vector< GUM_SCALAR >& lower,
274  const std::vector< GUM_SCALAR >& upper);
275 
292  void fillConstraint(const NodeId& id,
293  Instantiation ins,
294  const std::vector< GUM_SCALAR >& lower,
295  const std::vector< GUM_SCALAR >& upper);
296 
304 
310  Size domainSize(const NodeId& id);
311 
313 
316 
353  void bnToCredal(const GUM_SCALAR beta,
354  const bool oneNet,
355  const bool keepZeroes = false);
356 
368 
377  void intervalToCredal();
378 
394  void lagrangeNormalization();
395 
410  void idmLearning(const Idx s = 0, const bool keepZeroes = false);
411 
424 
426 
427  // other utility member methods
428  // PH void saveCNet ( const std::string &cn_path ) const;
429  // PH void loadCNet ( const std::string &src_cn_path );
430 
444  void saveBNsMinMax(const std::string& min_path,
445  const std::string& max_path) const;
446 
447  // PH void vacants ( int &result ) const;
448  // PH void notVacants ( int &result ) const;
449  // PH void averageVertices ( GUM_SCALAR &result ) const;
450 
456  std::string toString() const;
457  // PH void toPNG ( const std::string &png_path ) const;
458 
467  void computeCPTMinMax(); // REDO THIS IN PRIVATE !!!
468 
471 
476  const BayesNet< GUM_SCALAR >& src_bn() const;
477 
483  const BayesNet< GUM_SCALAR >& current_bn() const;
484 
489  const NodeProperty<
490  std::vector< std::vector< std::vector< GUM_SCALAR > > > >&
491  credalNet_currentCpt() const;
492 
497  const NodeProperty<
498  std::vector< std::vector< std::vector< GUM_SCALAR > > > >&
499  credalNet_srcCpt() const;
500 
507  NodeType currentNodeType(const NodeId& id) const;
508 
515  NodeType nodeType(const NodeId& id) const;
516 
521  const GUM_SCALAR& epsilonMin() const;
522 
527  const GUM_SCALAR& epsilonMax() const;
528 
533  const GUM_SCALAR& epsilonMean() const;
534 
539  const bool isSeparatelySpecified() const;
540 
550  const bool hasComputedCPTMinMax() const;
551 
560  const std::vector< std::vector< GUM_SCALAR > >& get_CPT_min() const;
561 
570  const std::vector< std::vector< GUM_SCALAR > >& get_CPT_max() const;
571 
572  // PH const std::vector< std::vector< NodeId > > & var_bits() const;
573 
575 
576  protected:
577  private:
579  GUM_SCALAR __precisionC; // = 1e6;
581  GUM_SCALAR __deltaC; // = 5;
582 
586  GUM_SCALAR __epsilonMin;
590  GUM_SCALAR __epsilonMax;
594  GUM_SCALAR __epsilonMoy;
595 
599  GUM_SCALAR __epsRedund; //= 1e-6;
600 
604  GUM_SCALAR __epsF; // = 1e-6;
608  GUM_SCALAR __denMax; // = 1e6; // beware LRS
609 
611  GUM_SCALAR __precision; // = 1e6; // beware LRS
612 
617 
620 
625 
628 
632 
635  __credalNet_current_cpt; // = nullptr;
636 
639 
644 
654  typename std::vector< std::vector< GUM_SCALAR > > __binCptMin;
655 
662  typename std::vector< std::vector< GUM_SCALAR > > __binCptMax;
663 
665  void __sort_varType();
666 
673  int __find_dNode_card(
674  const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt)
675  const;
676 
686  void __intervalToCredal();
687 
691  void __initParams();
692 
696  void __initCNNets(const std::string& src_min_num,
697  const std::string& src_max_den);
698 
702  void __initCNNets(const BayesNet< GUM_SCALAR >& src_min_num,
703  const BayesNet< GUM_SCALAR >& src_max_den);
704 
721  void __bnCopy(BayesNet< GUM_SCALAR >& bn_dest);
722 
723  // void __H2Vcdd ( const std::vector< std::vector< GUM_SCALAR > > & h_rep,
724  // std::vector< std::vector< GUM_SCALAR > > & v_rep ) const;
736  void __H2Vlrs(const std::vector< std::vector< GUM_SCALAR > >& h_rep,
737  std::vector< std::vector< GUM_SCALAR > >& v_rep) const;
738 
739  }; // CredalNet
740 
741 
742 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
743  extern template class CredalNet< double >;
744 #endif
745 
746  } // namespace credal
747 } // namespace gum
748 
749 #include <agrum/CN/credalNet_tpl.h>
750 
751 #endif // __CREDAL_NET__H__
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:594
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 )
Useful macros for maths.
std::vector< std::vector< GUM_SCALAR > > __binCptMax
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:662
Class representing a Bayesian Network.
Definition: BayesNet.h:76
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:638
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:616
GUM_SCALAR __epsF
Value under which a decimal number is considered to be zero when using __farey.
Definition: credalNet.h:604
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
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:641
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
void __initParams()
Initialize private constant variables after the Constructor has been called.
GUM_SCALAR __precision
Precision used by __frac.
Definition: credalNet.h:611
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:64
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
Implementation of pow functions with integers, faster than stl implementation.
Class representing Bayesian networks.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void computeCPTMinMax()
Used with binary networks to speed-up L2U inference.
The class for generic Hash Tables.
Definition: hashTable.h:676
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_max() const
Used with binary networks to speed-up L2U inference.
Definition of templatized reader of BIF files for Bayesian Networks.
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:87
NodeId addVariable(const std::string &name, const Size &card)
Adds a discrete node into the network.
Definition: credalNet_tpl.h:40
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_srcCpt() const
const GUM_SCALAR & epsilonMean() const
Lrs wrapper.
aGrUM&#39;s exceptions
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:647
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
Definition of classe for BIF file output manipulation.
NodeType
NodeType to speed-up computations in some algorithms.
Definition: credalNet.h:90
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
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:29
GUM_SCALAR __precisionC
1e6 by default, used by __fracC as precision.
Definition: credalNet.h:579
GUM_SCALAR __denMax
Highest possible denominator allowed when using __farey.
Definition: credalNet.h:608
Wrappers for OpenMP.
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
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:80
GUM_SCALAR __epsilonMax
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:590
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:631
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:586
Class template used to approximate decimal numbers by rationals.
Size Idx
Type for indexes.
Definition: types.h:50
GUM_SCALAR __deltaC
5 by default, used by __fracC as number of decimals.
Definition: credalNet.h:581
~CredalNet()
Destructor.
const GUM_SCALAR & epsilonMax() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void addArc(const NodeId &tail, const NodeId &head)
Adds an arc between two nodes.
Definition: credalNet_tpl.h:57
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
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:654
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:97
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
GUM_SCALAR __epsRedund
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:599