aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
credalNet.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
42#ifndef GUM_CREDAL_NET_H
43#define GUM_CREDAL_NET_H
44
50
51#include <iostream>
52#include <vector>
53
54#include <agrum/agrum.h>
55
56// #include <sys/wait.h>
57#include <algorithm>
58#include <cstdlib>
59#include <fcntl.h>
60#include <fstream>
61#include <set>
62#include <sstream>
63#include <string>
64#include <utility>
65
67#include <agrum/base/core/math/pow.h> // custom pow functions with integers, faster implementation
73
74#include <string_view>
75#include <sys/stat.h>
76
77// 64 bits for windows (long is 32 bits)
78#ifdef _MSC_VER
79typedef __int64 int64_t;
80typedef unsigned __int64 uint64_t;
81#else
82# include <stdint.h>
83#endif
84
85namespace gum {
86 namespace dot {
87 class DotOptions;
88 } // namespace dot
89
90 namespace credal {
100 template < GUM_Numeric GUM_SCALAR >
101 class CredalNet {
102 public:
104 enum class NodeType : char { Precise, Credal, Vacuous, Indic };
105
108
114 CredalNet();
115
129 CredalNet(std::string_view src_min_num, std::string_view src_max_den = "");
130
143 CredalNet(const BayesNet< GUM_SCALAR >& src_min_num,
144 const BayesNet< GUM_SCALAR >& src_max_den = BayesNet< GUM_SCALAR >());
145
149 ~CredalNet();
150
152
155
162 NodeId addVariable(std::string_view name, const Size& card);
163
169 void addArc(const NodeId& tail, const NodeId& head);
170
187 void setCPTs(const NodeId& id,
188 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt);
189
210 void setCPT(const NodeId& id,
211 const Size& entry,
212 const std::vector< std::vector< GUM_SCALAR > >& cpt);
213
234 void setCPT(const NodeId& id,
235 Instantiation ins,
236 const std::vector< std::vector< GUM_SCALAR > >& cpt);
237
252 void fillConstraints(const NodeId& id,
253 const std::vector< GUM_SCALAR >& lower,
254 const std::vector< GUM_SCALAR >& upper);
255
272 void fillConstraint(const NodeId& id,
273 const Idx& entry,
274 const std::vector< GUM_SCALAR >& lower,
275 const std::vector< GUM_SCALAR >& upper);
276
293 void fillConstraint(const NodeId& id,
294 Instantiation ins,
295 const std::vector< GUM_SCALAR >& lower,
296 const std::vector< GUM_SCALAR >& upper);
297
305
311 Size domainSize(const NodeId& id);
312
314
317
343 void bnToCredal(GUM_SCALAR beta, bool oneNet, bool keepZeroes);
344 void bnToCredal(GUM_SCALAR beta, bool oneNet);
345
346
357
365 void intervalToCredal();
366
380
392 void idmLearning(const Idx s = 0, const bool keepZeroes = false);
393
405
407
408 // other utility member methods
409 // PH void saveCNet ( const std::string &cn_path ) const;
410 // PH void loadCNet ( const std::string &src_cn_path );
411
425 void saveBNsMinMax(std::string_view min_path, std::string_view max_path);
426
427 // PH void vacants ( int &result ) const;
428 // PH void notVacants ( int &result ) const;
429 // PH void averageVertices ( GUM_SCALAR &result ) const;
430
436 std::string toString() const;
437
445 std::string toDot() const;
446
451 std::string toDot(const dot::DotOptions& options) const;
452 // PH void toPNG ( const std::string &png_path ) const;
453
463
466
471 const BayesNet< GUM_SCALAR >& src_bn() const;
472
478 const BayesNet< GUM_SCALAR >& current_bn() const;
479
485 credalNet_currentCpt() const;
486
492 credalNet_srcCpt() const;
493
500 NodeType currentNodeType(const NodeId& id) const;
501
508 NodeType nodeType(const NodeId& id) const;
509
514 const GUM_SCALAR& epsilonMin() const;
515
520 const GUM_SCALAR& epsilonMax() const;
521
526 const GUM_SCALAR& epsilonMean() const;
527
532 bool isSeparatelySpecified() const;
533
540 bool hasComputedBinaryCPTMinMax() const;
541
550 const std::vector< std::vector< GUM_SCALAR > >& get_binaryCPT_min() const;
551
560 const std::vector< std::vector< GUM_SCALAR > >& get_binaryCPT_max() const;
561
562 // PH const std::vector< std::vector< NodeId > > & var_bits() const;
563
565
566 protected:
567
568 private:
570 GUM_SCALAR _precisionC_; // = 1e6;
572 GUM_SCALAR _deltaC_; // = 5;
573
577 GUM_SCALAR _epsilonMin_;
581 GUM_SCALAR _epsilonMax_;
585 GUM_SCALAR _epsilonMoy_;
586
590 GUM_SCALAR _epsRedund_; // core.epsilonStandard (1e-6 by default), read at construction
591
595 GUM_SCALAR _epsF_; // core.epsilonStandard (1e-6 by default), read at construction
599 GUM_SCALAR _denMax_; // = 1e6; // beware LRS
600
602 GUM_SCALAR _precision_; // = 1e6; // beware LRS
603
608
610 BayesNet< GUM_SCALAR > _src_bn_;
611
613 BayesNet< GUM_SCALAR > _src_bn_min_;
615 BayesNet< GUM_SCALAR > _src_bn_max_;
616
618 BayesNet< GUM_SCALAR >* _current_bn_; // = nullptr;
619
622
626
629
634
644 typename std::vector< std::vector< GUM_SCALAR > > _binCptMin_;
645
652 typename std::vector< std::vector< GUM_SCALAR > > _binCptMax_;
653
655 void _sort_varType_();
656
664 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt) const;
665
675 void _intervalToCredal_();
676
680 void _initParams_();
681
685 void _initCNNets_(std::string_view src_min_num, std::string_view src_max_den);
686
690 void _initCNNets_(const BayesNet< GUM_SCALAR >& src_min_num,
691 const BayesNet< GUM_SCALAR >& src_max_den);
692
709 void _bnCopy_(BayesNet< GUM_SCALAR >& bn_dest);
710
711 // void _H2Vcdd_ ( const std::vector< std::vector< GUM_SCALAR > > & h_rep,
712 // std::vector< std::vector< GUM_SCALAR > > & v_rep ) const;
724 void _H2Vlrs_(const std::vector< std::vector< GUM_SCALAR > >& h_rep,
725 std::vector< std::vector< GUM_SCALAR > >& v_rep) const;
726 }; // CredalNet
727
728
729#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
730 extern template class GUM_PUBLIC_CN CredalNet< double >;
731#endif
732 } // namespace credal
733} // namespace gum
734
736
737#endif // GUM_CREDAL_NET_H
Definition of templatized reader of BIF files for Bayesian networks.
Definition of class for BIF file output manipulation.
Lrs wrapper.
Class for assigning/browsing values to tuples of discrete variables.
Class template representing a Credal Network.
Definition credalNet.h:101
void _H2Vlrs_(const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const
void _initParams_()
Initialize private constant variables after the Constructor has been called.
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:585
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
GUM_SCALAR _deltaC_
5 by default, used by fracC as number of decimals.
Definition credalNet.h:572
Size domainSize(const NodeId &id)
Get the cardinality of a node
std::string toDot() const
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition credalNet.h:615
void _initCNNets_(std::string_view src_min_num, std::string_view src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition credalNet.h:618
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs).
std::string toString() const
bool _hasComputedBinaryCPTMinMax_
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition credalNet.h:637
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition credalNet.h:621
const GUM_SCALAR & epsilonMax() const
void _bnCopy_(BayesNet< GUM_SCALAR > &bn_dest)
bool hasComputedBinaryCPTMinMax() const
GUM_SCALAR _precision_
Precision used by frac.
Definition credalNet.h:602
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
void bnToCredal(GUM_SCALAR beta, bool oneNet, bool keepZeroes)
Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of poin...
void intervalToCredal()
Computes the vertices of each credal set according to their interval definition (uses lrs).
const GUM_SCALAR & epsilonMean() const
std::vector< std::vector< GUM_SCALAR > > _binCptMin_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:644
void saveBNsMinMax(std::string_view min_path, std::string_view max_path)
If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet.
void approximatedBinarization()
Approximate binarization.
const BayesNet< GUM_SCALAR > & src_bn() const
int _find_dNode_card_(const std::vector< std::vector< std::vector< GUM_SCALAR > > > &var_cpt) const
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition credalNet.h:610
void computeBinaryCPTMinMax()
Used with binary networks to speed-up L2U inference.
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_srcCpt() const
NodeType currentNodeType(const NodeId &id) const
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition credalNet.h:631
void lagrangeNormalization()
Normalize counts of a BayesNet storing counts of each events such that no probability is 0.
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition credalNet.h:625
bool isSeparatelySpecified() const
void _sort_varType_()
Set the NodeType of each node
void idmLearning(const Idx s=0, const bool keepZeroes=false)
Learns parameters from a BayesNet storing counts of events.
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:581
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:652
NodeType nodeType(const NodeId &id) const
Instantiation instantiation(const NodeId &id)
Get an Instantiation from a node id, useful to fill the constraints of the network
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition credalNet.h:607
void setCPT(const NodeId &id, const Size &entry, const std::vector< std::vector< GUM_SCALAR > > &cpt)
Set the vertices of one credal set of a given node ( any instantiation index )
NodeProperty< std::vector< NodeId > > _var_bits_
Corresponding bits of each variable.
Definition credalNet.h:628
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_max() const
Used with binary networks to speed-up L2U inference.
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition credalNet.h:613
NodeId addVariable(std::string_view name, const Size &card)
Adds a discrete node into the network.
const BayesNet< GUM_SCALAR > & current_bn() const
void addArc(const NodeId &tail, const NodeId &head)
Adds an arc between two nodes.
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_min() const
Used with binary networks to speed-up L2U inference.
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 )
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:577
const GUM_SCALAR & epsilonMin() const
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition credalNet.h:633
GUM_SCALAR _denMax_
Highest possible denominator allowed when using farey.
Definition credalNet.h:599
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 )
GUM_SCALAR _epsRedund_
Value under which a decimal number is considered to be zero when computing redundant vertices.
Definition credalNet.h:590
GUM_SCALAR _precisionC_
1e6 by default, used by fracC as precision.
Definition credalNet.h:570
NodeType
NodeType to speed-up computations in some algorithms.
Definition credalNet.h:104
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
GUM_SCALAR _epsF_
Value under which a decimal number is considered to be zero when using farey.
Definition credalNet.h:595
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
aGrUM's exceptions
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
namespace for all credal networks entities
Definition agrum.h:61
template class GUM_PUBLIC_CN CredalNet< double >
Definition credalNet.cpp:53
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Implementation of pow functions with integers, faster than stl implementation.
The class to use to execute a function by several threads.
Utility functions used for exploiting OpenMP/STL parallelism.