aGrUM 3.2.0
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 credal {
96 template < GUM_Numeric GUM_SCALAR >
97 class PYGUM_PUBLIC CredalNet {
98 public:
100 enum class NodeType : char { Precise, Credal, Vacuous, Indic };
101
104
110 CredalNet();
111
125 CredalNet(std::string_view src_min_num, std::string_view 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(std::string_view 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 const 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
300 Instantiation instantiation(const NodeId& id);
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
352 void intervalToCredalWithFiles();
353
361 void intervalToCredal();
362
375 void lagrangeNormalization();
376
388 void idmLearning(const Idx s = 0, const bool keepZeroes = false);
389
400 void approximatedBinarization();
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(std::string_view min_path, std::string_view max_path);
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();
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_(std::string_view src_min_num, std::string_view 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 }; // CredalNet
708
709
710#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
711 extern template class CredalNet< double >;
712#endif
713 } // namespace credal
714} // namespace gum
715
717
718#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: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
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
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:599
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs).
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
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition credalNet.h:602
void _bnCopy_(BayesNet< GUM_SCALAR > &bn_dest)
GUM_SCALAR _precision_
Precision used by frac.
Definition credalNet.h:583
std::vector< std::vector< GUM_SCALAR > > _binCptMin_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:625
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
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition credalNet.h:612
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition credalNet.h:606
void _sort_varType_()
Set the NodeType of each node
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
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
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition credalNet.h:594
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:558
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
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.