aGrUM 2.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-2025 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-2025 *
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 __CREDAL_NET__H__
43#define __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 <sys/stat.h>
75
76// 64 bits for windows (long is 32 bits)
77#ifdef _MSC_VER
78typedef __int64 int64_t;
79typedef unsigned __int64 uint64_t;
80#else
81# include <stdint.h>
82#endif
83
84namespace gum {
85 namespace credal {
86
96 template < typename GUM_SCALAR >
97 class CredalNet {
98 public:
100 enum class NodeType : char { Precise, Credal, Vacuous, Indic };
101
104
110 CredalNet();
111
125 CredalNet(const std::string& src_min_num, const std::string& src_max_den = "");
126
139 CredalNet(const BayesNet< GUM_SCALAR >& src_min_num,
140 const BayesNet< GUM_SCALAR >& src_max_den = BayesNet< GUM_SCALAR >());
141
145 ~CredalNet();
146
148
151
158 NodeId addVariable(const std::string& name, const Size& card);
159
165 void addArc(const NodeId& tail, const NodeId& head);
166
183 void setCPTs(const NodeId& id,
184 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt);
185
206 void setCPT(const NodeId& id,
207 Size& entry,
208 const std::vector< std::vector< GUM_SCALAR > >& cpt);
209
230 void setCPT(const NodeId& id,
231 Instantiation ins,
232 const std::vector< std::vector< GUM_SCALAR > >& cpt);
233
248 void fillConstraints(const NodeId& id,
249 const std::vector< GUM_SCALAR >& lower,
250 const std::vector< GUM_SCALAR >& upper);
251
268 void fillConstraint(const NodeId& id,
269 const Idx& entry,
270 const std::vector< GUM_SCALAR >& lower,
271 const std::vector< GUM_SCALAR >& upper);
272
289 void fillConstraint(const NodeId& id,
290 Instantiation ins,
291 const std::vector< GUM_SCALAR >& lower,
292 const std::vector< GUM_SCALAR >& upper);
293
301
307 Size domainSize(const NodeId& id);
308
310
313
339 void bnToCredal(GUM_SCALAR beta, bool oneNet, bool keepZeroes);
340 void bnToCredal(GUM_SCALAR beta, bool oneNet);
341
342
353
361 void intervalToCredal();
362
376
388 void idmLearning(const Idx s = 0, const bool keepZeroes = false);
389
401
403
404 // other utility member methods
405 // PH void saveCNet ( const std::string &cn_path ) const;
406 // PH void loadCNet ( const std::string &src_cn_path );
407
421 void saveBNsMinMax(const std::string& min_path, const std::string& max_path) const;
422
423 // PH void vacants ( int &result ) const;
424 // PH void notVacants ( int &result ) const;
425 // PH void averageVertices ( GUM_SCALAR &result ) const;
426
432 std::string toString() const;
433 // PH void toPNG ( const std::string &png_path ) const;
434
443 void computeBinaryCPTMinMax(); // REDO THIS IN PRIVATE !!!
444
447
452 const BayesNet< GUM_SCALAR >& src_bn() const;
453
459 const BayesNet< GUM_SCALAR >& current_bn() const;
460
466 credalNet_currentCpt() const;
467
473 credalNet_srcCpt() const;
474
481 NodeType currentNodeType(const NodeId& id) const;
482
489 NodeType nodeType(const NodeId& id) const;
490
495 const GUM_SCALAR& epsilonMin() const;
496
501 const GUM_SCALAR& epsilonMax() const;
502
507 const GUM_SCALAR& epsilonMean() const;
508
513 bool isSeparatelySpecified() const;
514
521 bool hasComputedBinaryCPTMinMax() const;
522
531 const std::vector< std::vector< GUM_SCALAR > >& get_binaryCPT_min() const;
532
541 const std::vector< std::vector< GUM_SCALAR > >& get_binaryCPT_max() const;
542
543 // PH const std::vector< std::vector< NodeId > > & var_bits() const;
544
546
547 protected:
548
549 private:
551 GUM_SCALAR _precisionC_; // = 1e6;
553 GUM_SCALAR _deltaC_; // = 5;
554
558 GUM_SCALAR _epsilonMin_;
562 GUM_SCALAR _epsilonMax_;
566 GUM_SCALAR _epsilonMoy_;
567
571 GUM_SCALAR _epsRedund_; //= 1e-6;
572
576 GUM_SCALAR _epsF_; // = 1e-6;
580 GUM_SCALAR _denMax_; // = 1e6; // beware LRS
581
583 GUM_SCALAR _precision_; // = 1e6; // beware LRS
584
589
591 BayesNet< GUM_SCALAR > _src_bn_;
592
594 BayesNet< GUM_SCALAR > _src_bn_min_;
596 BayesNet< GUM_SCALAR > _src_bn_max_;
597
599 BayesNet< GUM_SCALAR >* _current_bn_; // = nullptr;
600
603
607
610
615
625 typename std::vector< std::vector< GUM_SCALAR > > _binCptMin_;
626
633 typename std::vector< std::vector< GUM_SCALAR > > _binCptMax_;
634
636 void _sort_varType_();
637
645 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt) const;
646
656 void _intervalToCredal_();
657
661 void _initParams_();
662
666 void _initCNNets_(const std::string& src_min_num, const std::string& src_max_den);
667
671 void _initCNNets_(const BayesNet< GUM_SCALAR >& src_min_num,
672 const BayesNet< GUM_SCALAR >& src_max_den);
673
690 void _bnCopy_(BayesNet< GUM_SCALAR >& bn_dest);
691
692 // void _H2Vcdd_ ( const std::vector< std::vector< GUM_SCALAR > > & h_rep,
693 // std::vector< std::vector< GUM_SCALAR > > & v_rep ) const;
705 void _H2Vlrs_(const std::vector< std::vector< GUM_SCALAR > >& h_rep,
706 std::vector< std::vector< GUM_SCALAR > >& v_rep) const;
707
708 }; // CredalNet
709
710
711#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
712 extern template class CredalNet< double >;
713#endif
714
715 } // namespace credal
716} // namespace gum
717
719
720#endif // __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:97
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:566
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:553
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition credalNet.h:596
NodeId addVariable(const std::string &name, const Size &card)
Adds a discrete node into the network.
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition credalNet.h:599
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs).
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
bool _hasComputedBinaryCPTMinMax_
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition credalNet.h:618
Size domainSize(const NodeId &id)
Get the cardinality of a node
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition credalNet.h:602
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:583
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
Instantiation instantiation(const NodeId &id)
Get an Instantiation from a node id, usefull to fill the constraints of the network
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:625
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:591
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:612
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:606
bool isSeparatelySpecified() const
void _sort_varType_()
Set the NodeType of each node
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 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:562
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:633
NodeType nodeType(const NodeId &id) const
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 )
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition credalNet.h:588
NodeProperty< std::vector< NodeId > > _var_bits_
Corresponding bits of each variable.
Definition credalNet.h:609
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:594
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:558
const GUM_SCALAR & epsilonMin() const
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition credalNet.h:614
GUM_SCALAR _denMax_
Highest possible denominator allowed when using farey.
Definition credalNet.h:580
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:571
GUM_SCALAR _precisionC_
1e6 by default, used by fracC as precision.
Definition credalNet.h:551
NodeType
NodeType to speed-up computations in some algorithms.
Definition credalNet.h:100
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:576
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
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.