aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
gum::credal::CredalNet< GUM_SCALAR > Class Template Reference

Class template representing a Credal Network. More...

#include <agrum/CN/credalNet.h>

Collaboration diagram for gum::credal::CredalNet< GUM_SCALAR >:
[legend]

Public Types

enum class  NodeType : char { Precise , Credal , Vacuous , Indic }
 NodeType to speed-up computations in some algorithms. More...

Public Member Functions

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.
std::string toString () const
std::string toDot () const
std::string toDot (const dot::DotOptions &options) const
void computeBinaryCPTMinMax ()
 Used with binary networks to speed-up L2U inference.
Constructors / Destructors
 CredalNet ()
 Constructor used to create a CredalNet step by step, i.e.
 CredalNet (std::string_view src_min_num, std::string_view src_max_den="")
 Constructor for interval defined credal network which takes 2 BayesNet file path.
 CredalNet (const BayesNet< GUM_SCALAR > &src_min_num, const BayesNet< GUM_SCALAR > &src_max_den=BayesNet< GUM_SCALAR >())
 Constructor for interval defined credal network which takes 2 BayesNet.
 ~CredalNet ()
 Destructor.
Credal network creation
NodeId addVariable (std::string_view name, const Size &card)
 Adds a discrete node into the network.
void addArc (const NodeId &tail, const NodeId &head)
 Adds an arc between two nodes.
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
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 )
void setCPT (const NodeId &id, Instantiation ins, const std::vector< std::vector< GUM_SCALAR > > &cpt)
 Set the vertices of one credal set of a given node ( any instantiation )
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 )
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 fillConstraint (const NodeId &id, Instantiation ins, const std::vector< GUM_SCALAR > &lower, const std::vector< GUM_SCALAR > &upper)
 Set the interval constraints of a credal sets of a given node ( from an instantiation )
Instantiation instantiation (const NodeId &id)
 Get an Instantiation from a node id, useful to fill the constraints of the network
Size domainSize (const NodeId &id)
 Get the cardinality of a node
Public manipulation methods
void bnToCredal (GUM_SCALAR beta, bool oneNet, bool keepZeroes)
 Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of point probabilities and then computes each vertex of each credal set.
void bnToCredal (GUM_SCALAR beta, bool oneNet)
 Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of point probabilities and then computes each vertex of each credal set.
void intervalToCredalWithFiles ()
void intervalToCredal ()
 Computes the vertices of each credal set according to their interval definition (uses lrs).
void lagrangeNormalization ()
 Normalize counts of a BayesNet storing counts of each events such that no probability is 0.
void idmLearning (const Idx s=0, const bool keepZeroes=false)
 Learns parameters from a BayesNet storing counts of events.
void approximatedBinarization ()
 Approximate binarization.
Getters and setters
const BayesNet< GUM_SCALAR > & src_bn () const
const BayesNet< GUM_SCALAR > & current_bn () const
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt () const
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_srcCpt () const
NodeType currentNodeType (const NodeId &id) const
NodeType nodeType (const NodeId &id) const
const GUM_SCALAR & epsilonMin () const
const GUM_SCALAR & epsilonMax () const
const GUM_SCALAR & epsilonMean () const
bool isSeparatelySpecified () const
bool hasComputedBinaryCPTMinMax () const
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_min () const
 Used with binary networks to speed-up L2U inference.
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_max () const
 Used with binary networks to speed-up L2U inference.

Private Member Functions

void _sort_varType_ ()
 Set the NodeType of each node
int _find_dNode_card_ (const std::vector< std::vector< std::vector< GUM_SCALAR > > > &var_cpt) const
void _intervalToCredal_ ()
 Computes the vertices of each credal set according to their interval definition (does not use lrs).
void _initParams_ ()
 Initialize private constant variables after the Constructor has been called.
void _initCNNets_ (std::string_view src_min_num, std::string_view src_max_den)
 Initialize private BayesNet variables after the Constructor has been called.
void _initCNNets_ (const BayesNet< GUM_SCALAR > &src_min_num, const BayesNet< GUM_SCALAR > &src_max_den)
 Initialize private BayesNet variables after the Constructor has been called.
void _bnCopy_ (BayesNet< GUM_SCALAR > &bn_dest)
void _H2Vlrs_ (const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const

Private Attributes

GUM_SCALAR _precisionC_
 1e6 by default, used by fracC as precision.
GUM_SCALAR _deltaC_
 5 by default, used by fracC as number of decimals.
GUM_SCALAR _epsilonMin_
 The lowest perturbation of the BayesNet provided as input for this CredalNet.
GUM_SCALAR _epsilonMax_
 The highest perturbation of the BayesNet provided as input for this CredalNet.
GUM_SCALAR _epsilonMoy_
 The average perturbation of the BayesNet provided as input for this CredalNet.
GUM_SCALAR _epsRedund_
 Value under which a decimal number is considered to be zero when computing redundant vertices.
GUM_SCALAR _epsF_
 Value under which a decimal number is considered to be zero when using farey.
GUM_SCALAR _denMax_
 Highest possible denominator allowed when using farey.
GUM_SCALAR _precision_
 Precision used by frac.
bool _separatelySpecified_
 TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
BayesNet< GUM_SCALAR > _src_bn_
 Original BayesNet (used as a DAG).
BayesNet< GUM_SCALAR > _src_bn_min_
 BayesNet used to store lower probabilities.
BayesNet< GUM_SCALAR > _src_bn_max_
 BayesNet used to store upper probabilities.
BayesNet< GUM_SCALAR > * _current_bn_
 Up-to-date BayesNet (used as a DAG).
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
 This CredalNet original CPTs.
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
 This CredalNet up-to-date CPTs.
NodeProperty< std::vector< NodeId > > _var_bits_
 Corresponding bits of each variable.
NodeProperty< NodeType > _original_nodeType_
 The NodeType of each node from the ORIGINAL network.
NodeProperty< NodeType > * _current_nodeType_
 The NodeType of each node from the up-to-date network.
bool _hasComputedBinaryCPTMinMax_
 Used by L2U, to know if lower and upper probabilities over the second modality has been stored in order to speed-up the algorithm.
std::vector< std::vector< GUM_SCALAR > > _binCptMin_
 Used with binary networks to speed-up L2U inference.
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
 Used with binary networks to speed-up L2U inference.

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::credal::CredalNet< GUM_SCALAR >

Class template representing a Credal Network.

Template Parameters
GUM_SCALARA floating type ( float, GUM_SCALAR, long GUM_SCALAR ... ).
Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6)

Definition at line 101 of file credalNet.h.

Member Enumeration Documentation

◆ NodeType

template<GUM_Numeric GUM_SCALAR>
enum class gum::credal::CredalNet::NodeType : char
strong

NodeType to speed-up computations in some algorithms.

Enumerator
Precise 
Credal 
Vacuous 
Indic 

Definition at line 104 of file credalNet.h.

104: char { Precise, Credal, Vacuous, Indic };

Constructor & Destructor Documentation

◆ CredalNet() [1/3]

template<GUM_Numeric GUM_SCALAR>
gum::credal::CredalNet< GUM_SCALAR >::CredalNet ( )

Constructor used to create a CredalNet step by step, i.e.

node by node, arc by arc, manually filling tensors.

Definition at line 57 of file credalNet_tpl.h.

57 {
59
63
65 }
Class template representing a Credal Network.
Definition credalNet.h:101
void _initParams_()
Initialize private constant variables after the Constructor has been called.
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition credalNet.h:615
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition credalNet.h:610
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition credalNet.h:613
CredalNet()
Constructor used to create a CredalNet step by step, i.e.

References CredalNet(), _initParams_(), _src_bn_, _src_bn_max_, and _src_bn_min_.

Referenced by CredalNet(), CredalNet(), CredalNet(), and ~CredalNet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CredalNet() [2/3]

template<GUM_Numeric GUM_SCALAR>
gum::credal::CredalNet< GUM_SCALAR >::CredalNet ( std::string_view src_min_num,
std::string_view src_max_den = "" )

Constructor for interval defined credal network which takes 2 BayesNet file path.

One can also provide a single BayesNet to perturb it's probability distributions into credal sets according to another BayesNet containing the amount cases, for each node, of each parent instantiation met during learning, i.e. \( p(X = 0 \mid pa(X) = j) = N_{pa(X) = j} \).

Parameters
src_min_numThe path to a BayesNet which contains lower probabilities.
src_max_denThe ( optional ) path to a BayesNet which contains upper probabilities.

Definition at line 415 of file credalNet_tpl.h.

415 {
416 _initParams_();
418
420 }
void _initCNNets_(std::string_view src_min_num, std::string_view src_max_den)
Initialize private BayesNet variables after the Constructor has been called.

References CredalNet(), _initCNNets_(), and _initParams_().

Here is the call graph for this function:

◆ CredalNet() [3/3]

template<GUM_Numeric GUM_SCALAR>
gum::credal::CredalNet< GUM_SCALAR >::CredalNet ( const BayesNet< GUM_SCALAR > & src_min_num,
const BayesNet< GUM_SCALAR > & src_max_den = BayesNet< GUM_SCALAR >() )

Constructor for interval defined credal network which takes 2 BayesNet.

One can also provide a single BayesNet in order to perturb it's probability distributions into credal sets according to another BayesNet containing the number of cases, for each node, of each parent instantiation met during learning, i.e. \( p(X = 0 \mid pa(X) = j) = N_{pa(X) = j} \).

Parameters
src_min_numThe BayesNet which contains lower probabilities.
src_max_denThe ( optional ) BayesNet which contains upper probabilities.

Definition at line 423 of file credalNet_tpl.h.

References CredalNet(), _initCNNets_(), and _initParams_().

Here is the call graph for this function:

◆ ~CredalNet()

template<GUM_Numeric GUM_SCALAR>
gum::credal::CredalNet< GUM_SCALAR >::~CredalNet ( )

Destructor.

Definition at line 432 of file credalNet_tpl.h.

432 {
433 if (_current_bn_ != nullptr) delete _current_bn_;
434
436
437 if (_current_nodeType_ != nullptr) delete _current_nodeType_;
438
440 }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition credalNet.h:618
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition credalNet.h:625
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition credalNet.h:633

References CredalNet(), _credalNet_current_cpt_, _current_bn_, and _current_nodeType_.

Here is the call graph for this function:

Member Function Documentation

◆ _bnCopy_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_bnCopy_ ( BayesNet< GUM_SCALAR > & bn_dest)
private
Deprecated
Warning
May be useless since the BayesNet copy constructor seems to now work well (parent order is preserved).

Copy the up-to-date BayesNet associated with this CredalNet. Since all we care about is the DAG, only arcs are copied. Because the order with which arcs are created is important, the function iterates over the CPTs variables to be sure parent order stays the same from a net to it's copy.

Parameters
bn_destThe reference to the new copy

Definition at line 1461 of file credalNet_tpl.h.

1461 {
1463
1464 for (auto node: bn_ptr->nodes())
1465 dest.add(bn_ptr->variable(node));
1466
1467 dest.beginTopologyTransformation();
1468
1469 for (auto node: bn_ptr->nodes()) {
1470 for (auto parent_idIt: bn_ptr->cpt(node).variablesSequence()) {
1471 if (bn_ptr->nodeId(*parent_idIt) != node) dest.addArc(bn_ptr->nodeId(*parent_idIt), node);
1472 } // end of : for each parent in order of appearence
1473 } // end of : for each variable
1474
1475 dest.endTopologyTransformation();
1476 }
void addArc(const NodeId &tail, const NodeId &head)
Adds an arc between two nodes.

References _current_bn_, and _src_bn_.

◆ _find_dNode_card_()

template<GUM_Numeric GUM_SCALAR>
int gum::credal::CredalNet< GUM_SCALAR >::_find_dNode_card_ ( const std::vector< std::vector< std::vector< GUM_SCALAR > > > & var_cpt) const
private
Deprecated
Parameters
var_cptThe reference to a node CPT which may need a Decision Node.
Returns
Returns the cardinality of the Decision Node.

Definition at line 1449 of file credalNet_tpl.h.

1450 {
1451 Size vertices_size = 0;
1452
1453 for (auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd; ++entry) {
1454 if (entry->size() > vertices_size) vertices_size = Size(entry->size());
1455 }
1456
1457 return int(vertices_size);
1458 }

◆ _H2Vlrs_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_H2Vlrs_ ( const std::vector< std::vector< GUM_SCALAR > > & h_rep,
std::vector< std::vector< GUM_SCALAR > > & v_rep ) const
private
Deprecated
one should use the LrsWrapper class Computes the V-representation of a credal set, i.e. it's vertices, from it's H-representation, i.e. the hyper-plan inequalities. Uses lrs.
Parameters
h_repA constant reference to the H-representation of a credal set.
v_repA reference to the V-representation of the same credal set.

Definition at line 1531 of file credalNet_tpl.h.

1532 {
1533 // write H rep file
1534 int64_t num, den;
1535
1536 std::string sinefile = getUniqueFileName(); // generate unique file name, we
1537 // need to add .ine or .ext for lrs
1538 // to know which input it is (Hrep
1539 // to Vrep or Vrep to Hrep)
1540 sinefile += ".ine";
1541
1543
1544 if (!h_file.good())
1545 GUM_ERROR(IOError, " __H2Vlrs : could not open lrs input file : " << sinefile)
1546
1547 h_file << "H - representation\n";
1548 h_file << "begin\n";
1549 h_file << h_rep.size() << ' ' << h_rep[0].size() << " rational\n";
1550
1551 for (auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1552 for (auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2; ++it2) {
1553 // get integer fraction from decimal value
1554 // smallest numerator & denominator is farley, also
1555 // best precision
1557 den,
1558 ((*it2 > 0) ? *it2 : -*it2),
1560 _epsF_);
1561
1562 h_file << ((*it2 > 0) ? num : -num) << '/' << den << ' ';
1563 }
1564
1565 h_file << '\n';
1566 }
1567
1568 h_file << "end\n";
1569 h_file.close();
1570
1571 // call lrs
1573 extfile += ".ext";
1574
1575 std::string lrs_arg0 = "lrs";
1578 char* lrs_argv[3] = {lrs_arg0.data(), lrs_arg1.data(), lrs_arg2.data()};
1579
1580 // it may need to redirect stdout to a file
1581 lrs_main(3, lrs_argv);
1582
1583 // read V rep file
1584 std::ifstream v_file(extfile.c_str() /*extfilename.c_str()*/, std::ios::in);
1585
1586 if (!v_file.good()) GUM_ERROR(IOError, " __H2Vlrs : could not open lrs output file : ")
1587
1588 std::string line, tmp;
1589 char * cstr, *p;
1591
1592 std::string::size_type pos;
1593 bool keep_going = true;
1594 // int vertices;
1595
1597
1598 v_file.ignore(256, 'l');
1599
1600 while (v_file.good() && keep_going) {
1602
1603 if (line.size() == 0) continue;
1604 else if (line.compare("end") == 0) {
1605 keep_going = false;
1606 // this is to get vertices number :
1607 /*getline ( v_file, line );
1608 std::string::size_type pos, end_pos;
1609 pos = line.find ( "vertices = " );
1610 end_pos = line.find ( "rays", pos + 9 );
1611 vertices = atoi ( line.substr ( pos + 9, end_pos - pos - 9 ).c_str()
1612 );*/
1613 break;
1614 } else if (line[1] != '1') {
1616 " __H2Vlrs : reading something other than a vertex from "
1617 "lrs output file : ");
1618 }
1619
1620 line = line.substr(2);
1621 cstr = new char[line.size() + 1];
1622 strcpy(cstr, line.c_str());
1623
1624 p = strtok(cstr, " ");
1625
1626 while (p != nullptr) {
1627 tmp = p;
1628
1629 if (tmp.compare("1") == 0 || tmp.compare("0") == 0)
1630 probability = GUM_SCALAR(atof(tmp.c_str()));
1631 else {
1632 pos = tmp.find("/");
1633 probability = GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1634 / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1635 }
1636
1637 vertex.push_back(probability);
1638 p = strtok(nullptr, " ");
1639 } // end of : for all tokens
1640
1641 delete[] cstr;
1642
1643 // compute is_redund using multiple threads:
1644 // compute the max number of threads to use (avoid nested threads)
1647 : 1; // no nested multithreading
1648
1649 const auto nsize = v_rep.size();
1651
1652 // prepare the data used by the threads
1653 const auto ranges = gum::dispatchRangeToThreads(0, nsize, (unsigned int)(real_nb_threads));
1654 std::vector< Size > t_redund(real_nb_threads); // use Size to avoid false sharing
1655
1656 // create the function to be executed by the threads
1658 const std::size_t nb_threads) {
1659 const auto vsize = vertex.size();
1661
1662 for (Idx i = ranges[this_thread].first, end = ranges[this_thread].second; i < end; i++) {
1663 thread_redund = 1;
1664 for (Idx modality = 0; modality < vsize; ++modality) {
1666 thread_redund = 0;
1667 break;
1668 }
1669 }
1670
1671 if (thread_redund) return;
1672 }
1673 };
1674
1675 // launch the threads
1677
1678 // aggregate the results
1679 bool is_redund = false;
1680 for (const auto thread_redund: t_redund) {
1681 if (thread_redund) {
1682 is_redund = true;
1683 break;
1684 }
1685 }
1686
1687
1688 /*
1689 // old openMP code:
1690 #pragma omp parallel
1691 {
1692 int this_thread = threadsOMP::getThreadNumber();
1693 int num_threads = threadsOMP::getNumberOfRunningThreads();
1694
1695 auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1696 auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1697
1698 for (auto p = begin_pos; p < end_pos; p++) {
1699 #pragma omp flush(is_redund)
1700
1701 if (is_redund) break;
1702
1703 bool thread_redund = true;
1704
1705 auto vsize = vertex.size();
1706
1707 for (Size modality = 0; modality < vsize; modality++) {
1708 if (std::fabs(vertex[modality] - v_rep[p][modality]) > _epsRedund_) {
1709 thread_redund = false;
1710 break;
1711 }
1712 }
1713
1714 if (thread_redund) {
1715 is_redund = true;
1716 #pragma omp flush(is_redund)
1717 int i=0; // this line to work around a weird syntax error with msvc
1718 }
1719 } // end of : each thread for
1720 } // end of : parallel
1721 */
1722
1723 if (!is_redund) v_rep.push_back(vertex);
1724
1725 vertex.clear();
1726 } // end of : file
1727
1728 v_file.close();
1729
1730 if (std::remove(sinefile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + sinefile)
1731
1732 if (std::remove(extfile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + extfile)
1733 }
static void farey(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000L, const GUM_SCALAR &zero=static_cast< GUM_SCALAR >(config().epsilonStandard()))
Find the rational close enough to a given ( decimal ) number in [-1,1] and whose denominator is not h...
GUM_SCALAR _denMax_
Highest possible denominator allowed when using farey.
Definition credalNet.h:599
GUM_SCALAR _epsRedund_
Value under which a decimal number is considered to be zero when computing redundant vertices.
Definition credalNet.h:590
GUM_SCALAR _epsF_
Value under which a decimal number is considered to be zero when using farey.
Definition credalNet.h:595
std::vector< std::pair< Idx, Idx > > dispatchRangeToThreads(const Idx beg, const Idx end, const unsigned int nb_threads)
returns a vector equally splitting elements of a range among threads
Definition threads.cpp:76
unsigned int getNumberOfThreads()
returns the max number of threads used by default when entering the next parallel region
static void execute(std::size_t nb_threads, FUNCTION exec_func, ARGS &&... func_args)
executes a function using several threads
static int nbRunningThreadsExecutors()
indicates how many threadExecutors are currently running

References _denMax_, _epsF_, _epsRedund_, gum::dispatchRangeToThreads(), gum::threadsSTL::ThreadExecutor::execute(), gum::Rational< GUM_SCALAR >::farey(), gum::getNumberOfThreads(), gum::getUniqueFileName(), GUM_ERROR, and gum::threadsSTL::ThreadExecutor::nbRunningThreadsExecutors().

Referenced by intervalToCredalWithFiles().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _initCNNets_() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_initCNNets_ ( const BayesNet< GUM_SCALAR > & src_min_num,
const BayesNet< GUM_SCALAR > & src_max_den )
private

Initialize private BayesNet variables after the Constructor has been called.

Definition at line 1439 of file credalNet_tpl.h.

1440 {
1443
1444 if (src_max_den.size() > 0) _src_bn_max_ = src_max_den;
1446 }

References _src_bn_, _src_bn_max_, and _src_bn_min_.

◆ _initCNNets_() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_initCNNets_ ( std::string_view src_min_num,
std::string_view src_max_den )
private

Initialize private BayesNet variables after the Constructor has been called.

Definition at line 1422 of file credalNet_tpl.h.

1423 {
1426
1427 if (src_max_den.compare("") != 0) other = src_max_den;
1428 else other = src_min_num;
1429
1432
1433 reader.proceed();
1434 reader_min.proceed();
1435 reader_max.proceed();
1436 }

References _src_bn_, _src_bn_max_, _src_bn_min_, and gum::BIFReader< GUM_SCALAR >::proceed().

Referenced by CredalNet(), and CredalNet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _initParams_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_initParams_ ( )
private

Initialize private constant variables after the Constructor has been called.

Definition at line 1395 of file credalNet_tpl.h.

1395 {
1396 _epsilonMin_ = 0;
1397 _epsilonMax_ = 0;
1398 _epsilonMoy_ = 0;
1399
1401
1402 // farey algorithm
1404 _denMax_ = GUM_SCALAR(1e6); // beware LRSWrapper
1405
1406 // continued fractions, beware LRSWrapper
1407 // decimal paces ( _epsC_ * _precisionC_ == 1)
1409 _deltaC_ = 5;
1410
1411 // old custom algorithm
1412 _precision_ = GUM_SCALAR(1e6); // beware LRSWrapper
1413
1414 _current_bn_ = nullptr;
1415 _credalNet_current_cpt_ = nullptr;
1416 _current_nodeType_ = nullptr;
1417
1419 }
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:585
GUM_SCALAR _deltaC_
5 by default, used by fracC as number of decimals.
Definition credalNet.h:572
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
GUM_SCALAR _precision_
Precision used by frac.
Definition credalNet.h:602
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:581
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:577
GUM_SCALAR _precisionC_
1e6 by default, used by fracC as precision.
Definition credalNet.h:570

References _credalNet_current_cpt_, _current_bn_, _current_nodeType_, _deltaC_, _denMax_, _epsF_, _epsilonMax_, _epsilonMin_, _epsilonMoy_, _epsRedund_, _hasComputedBinaryCPTMinMax_, _precision_, _precisionC_, and gum::config().

Referenced by CredalNet(), CredalNet(), and CredalNet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _intervalToCredal_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_intervalToCredal_ ( )
private

Computes the vertices of each credal set according to their interval definition (does not use lrs).

Only works with credal sets defined such that when one modality reach it's upper probability, all others are at their lowest.

Called by bnToCredal and idmLearning.

Definition at line 689 of file credalNet_tpl.h.

689 {
690 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
691
692 _credalNet_src_cpt_.resize(_src_bn_.size());
693
694 for (auto node: _src_bn_.nodes()) {
697
698 Size var_dSize = _src_bn_.variable(node).domainSize();
700
702
705
706 ins_min.setFirst();
707 ins_max.setFirst();
708
711
712 // the tolerance is read once per node, not at each test
713 const double epsilon = config().epsilonStandard();
714
715 for (Size entry = 0; entry < entry_size; entry++) {
716 for (Size modality = 0; modality < var_dSize; modality++, ++ins_min, ++ins_max) {
719 }
720
721 bool all_equals = true;
723
724 for (Size modality = 0; modality < var_dSize; modality++) {
725 if (std::fabs(upper[modality] - lower[modality]) < epsilon) continue;
726
727 all_equals = false;
730
731 for (Size mod = 0; mod < var_dSize; mod++) {
732 if (modality != mod) vertex[mod] = lower[mod];
733 }
734
735 GUM_SCALAR total = 0;
736
737 auto vsize = vertex.size();
738
739 for (Size i = 0; i < vsize; i++)
740 total += vertex[i];
741
742 if (std::fabs(total - 1.) > epsilon)
744 _src_bn_.variable(node).name()
745 << " does not sum to one for " << entry << std::endl
746 << vertex << std::endl);
747
748 vertices.push_back(vertex);
749 }
750
751 if (all_equals) {
753
754 for (Size modality = 0; modality < var_dSize; modality++)
756
757 GUM_SCALAR total = 0.;
758
759 auto vsize = vertex.size();
760
761 for (Size i = 0; i < vsize; i++)
762 total += vertex[i];
763
764 if (std::fabs(total - 1.) > epsilon)
766 _src_bn_.variable(node).name()
767 << " does not sum to one for " << entry << std::endl
768 << vertex << std::endl);
769
770 vertices.push_back(vertex);
771 }
772
774 }
775
777 } // end of : for each variable (node)
778
779 // get precise/credal/vacuous status of each variable
782 }
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:621
void _sort_varType_()
Set the NodeType of each node
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition credalNet.h:607

References _credalNet_src_cpt_, _separatelySpecified_, _sort_varType_(), _src_bn_, _src_bn_max_, _src_bn_min_, gum::config(), gum::Configuration::epsilonStandard(), GUM_ERROR, and gum::Instantiation::setFirst().

Referenced by bnToCredal(), and idmLearning().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _sort_varType_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::_sort_varType_ ( )
private

Set the NodeType of each node

Definition at line 1736 of file credalNet_tpl.h.

1736 {
1742
1743 for (auto node: bn_ptr->nodes()) {
1744 // indicatrices are already present
1745 if (nodeType_ptr->exists(node)) continue;
1746
1747 bool precise = true, vacuous = true;
1748
1749 for (auto entry = (*cpt_ptr)[node].cbegin(), theEnd2 = (*cpt_ptr)[node].cend();
1750 entry != theEnd2;
1751 ++entry) {
1752 auto vertices = entry->size();
1753 auto var_dSize = (*entry)[0].size();
1754
1755 if (precise && vertices > 1) precise = false;
1756
1757 if (vacuous && vertices == var_dSize) {
1759
1760 for (auto vertex = entry->cbegin(), vEnd = entry->cend(); vertex != vEnd; ++vertex) {
1761 for (auto probability = vertex->cbegin(), pEnd = vertex->cend(); probability != pEnd;
1762 ++probability) {
1763 if (*probability == 1) {
1764 elem[probability - vertex->begin()] = true;
1765 break;
1766 }
1767 } // end of : for each modality
1768 } // end of : for each vertex
1769
1770 for (auto probability = elem.cbegin(); probability != elem.cend(); ++probability)
1771 if (*probability == false) vacuous = false;
1772 } // end of : if vertices == dSize
1773 else
1774 vacuous = false;
1775
1776 if (vacuous == false && precise == false) {
1778 break;
1779 }
1780 } // end of : for each parents entry
1781
1783 else if (precise) nodeType_ptr->insert(node, NodeType::Precise);
1784 } // end of : for each variable
1785 }
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition credalNet.h:631

References _credalNet_current_cpt_, _credalNet_src_cpt_, _current_bn_, _current_nodeType_, _original_nodeType_, _src_bn_, Credal, gum::HashTable< Key, Val >::exists(), gum::HashTable< Key, Val >::insert(), Precise, and Vacuous.

Referenced by _intervalToCredal_(), approximatedBinarization(), intervalToCredal(), and intervalToCredalWithFiles().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addArc()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::addArc ( const NodeId & tail,
const NodeId & head )

Adds an arc between two nodes.

Parameters
tailThe NodeId of the tail node
headThe NodeId of the head node

Definition at line 84 of file credalNet_tpl.h.

84 {
85 _src_bn_.addArc(tail, head);
86 _src_bn_min_.addArc(tail, head);
87 _src_bn_max_.addArc(tail, head);
88 }

References _src_bn_, _src_bn_max_, and _src_bn_min_.

◆ addVariable()

template<GUM_Numeric GUM_SCALAR>
NodeId gum::credal::CredalNet< GUM_SCALAR >::addVariable ( std::string_view name,
const Size & card )

Adds a discrete node into the network.

Parameters
nameThe name of the discrete variable to be added
cardThe cardinality of the variable
Returns
The NodeId of the variable in the network

Definition at line 68 of file credalNet_tpl.h.

68 {
70
71 NodeId a = _src_bn_.add(var);
72 NodeId b = _src_bn_min_.add(var);
73 NodeId c = _src_bn_max_.add(var);
74
75 if (a != b || a != c /*|| b != c*/)
77 "addVariable : not the same id over all networks : " << a << ", " << b << ", "
78 << c);
79
80 return a;
81 }

References _src_bn_, _src_bn_max_, _src_bn_min_, and GUM_ERROR.

◆ approximatedBinarization()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::approximatedBinarization ( )

Approximate binarization.

Each bit has a lower and upper probability which is the lowest - resp. highest - over all vertices of the credal set. Enlarge the original credal sets and may induce huge imprecision.

Warning
Enlarge the original credal sets and therefore induce huge imprecision by propagation. Not recommended, use MCSampling or something else instead.

Definition at line 962 of file credalNet_tpl.h.

962 {
963 // don't forget to delete the old one ( _current_), if necessary at the end
964 auto bin_bn = new BayesNet< GUM_SCALAR >();
965
966 // __bnCopy ( * _bin_bn_ );
967 // delete old one too
970
971 // delete old one too
973
977
978 if (this->_current_bn_ == nullptr) current_bn = &this->_src_bn_;
979 else current_bn = this->_current_bn_;
980
981 if (this->_credalNet_current_cpt_ == nullptr)
984
985 if (!_var_bits_.empty()) _var_bits_.clear();
986
987 bin_bn->beginTopologyTransformation();
988
989 for (auto node: current_bn->nodes()) {
990 auto var_dSize = current_bn->variable(node).domainSize();
991
992 if (var_dSize != 2) {
993 unsigned long b;
994 unsigned long c;
995 superiorPow(static_cast< unsigned long >(var_dSize), b, c);
996 Size nb_bits{b};
997
1000
1001 for (Size bit = 0; bit < nb_bits; bit++) {
1002 bit_name = current_bn->variable(node).name() + "-b";
1004
1006 NodeId iD = bin_bn->add(var_bit);
1007
1008 bits[bit] = iD;
1009 } // end of : for each bit
1010
1011 _var_bits_.insert(node, bits);
1012 } // end of : if variable is not binary
1013 else {
1014 const std::string bit_name = current_bn->variable(node).name();
1016 const NodeId iD = bin_bn->add(var_bit);
1017
1019 }
1020 } // end of : for each original variable
1021
1022 for (auto node: current_bn->nodes()) {
1023 if (NodeSet parents = current_bn->parents(node); !parents.empty()) {
1024 for (auto par: current_bn->parents(node)) {
1025 for (Size parent_bit = 0, spbits = static_cast< Size >(_var_bits_[par].size());
1027 parent_bit++)
1028 for (Size var_bit = 0, mbits = static_cast< Size >(_var_bits_[node].size());
1029 var_bit < mbits;
1030 var_bit++)
1032 }
1033 }
1034
1035 // arcs with one's bits
1036 const auto bitsize = _var_bits_[node].size();
1037
1038 for (Size bit_c = 1; bit_c < bitsize; bit_c++)
1039 for (Size bit_p = 0; bit_p < bit_c; bit_p++)
1041 } // end of : for each original variable
1042
1043 bin_bn->endTopologyTransformation();
1044
1045 // binarization of cpts
1046
1047 const auto varsize = current_bn->size();
1048
1049 for (Size var = 0; var < varsize; var++) {
1050 const auto bitsize = _var_bits_[var].size();
1051
1052 for (Size i = 0; i < bitsize; i++) {
1055 ins.setFirst();
1056
1057 auto entry_size = tensor->domainSize() / 2;
1059
1060 Size old_conf = 0;
1061
1062 for (Size conf = 0; conf < entry_size; conf++) {
1064 auto verticessize = (*credalNet_current_cpt)[var][old_conf].size();
1065
1068 = (*credalNet_current_cpt)[var][old_conf][old_distri];
1069 auto vertexsize = vertex.size();
1070
1072
1073 for (Size preced = 0; preced < i; preced++) {
1074 auto bit_pos = ins.pos(bin_bn->variable(_var_bits_[var][preced]));
1075 auto val = ins.val(bit_pos);
1076
1078 Size elem;
1079
1080 if (val == 0) elem = 0;
1081 else elem = pas;
1082
1083 while (elem < vertexsize) {
1084 incc[elem]++;
1085 elem++;
1086
1087 if (elem % pas == 0) elem += pas;
1088 }
1089 }
1090
1091 Size pas = Size(int2Pow(i));
1092
1094 int pos = 1;
1095
1096 for (Size elem = 0; elem < vertexsize; elem++) {
1097 if (elem % pas == 0) pos = -pos;
1098
1099 if (incc[elem] == i)
1100 (pos < 0) ? (distri[0] += vertex[elem]) : (distri[1] += vertex[elem]);
1101 }
1102
1103 if (i > 0) {
1104 GUM_SCALAR den = distri[0] + distri[1];
1105
1106 if (den == 0) {
1107 distri[0] = 0;
1108 distri[1] = 0;
1109 } else {
1110 distri[0] /= den;
1111 distri[1] /= den;
1112 }
1113 }
1114
1115 pvar_cpt.push_back(distri);
1116 } // end of old distris
1117
1118 // get min/max approx, 2 vertices
1120 vertices[1][1] = 0;
1121
1122 const auto new_verticessize = pvar_cpt.size();
1123
1124 for (Size v = 0; v < new_verticessize; v++) {
1125 if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1126
1127 if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1128 }
1129
1130 vertices[0][0] = 1 - vertices[0][1];
1131 vertices[1][0] = 1 - vertices[1][1];
1132
1134
1136
1137 ++ins;
1138 ++ins;
1139
1140 old_conf++;
1141
1143 } // end of new parent conf
1144
1146 } // end of bit i
1147 } // end of old variable
1148
1149 bin_bn->beginTopologyTransformation();
1150
1151 /* indicatrices variables */
1152 const auto old_varsize = _var_bits_.size();
1153
1154 for (Size i = 0; i < old_varsize; i++) {
1155 auto bitsize = _var_bits_[i].size();
1156
1157 // binary variable
1158 if (bitsize == 1) continue;
1159
1160 auto old_card = _src_bn_.variable(i).domainSize();
1161
1162 for (Size mod = 0; mod < old_card; mod++) {
1163 auto ss_str = std::format("{}-v{}", _src_bn_.variable(i).name(), mod);
1164
1165 LabelizedVariable var(ss_str, "node " + ss_str, 2);
1166 const NodeId indic = bin_bn->add(var);
1167
1168 // arcs from one's bits
1169 for (Size bit = 0; bit < bitsize; bit++)
1171
1172 // cpt
1173 Size num = Size(int2Pow(long(bitsize)));
1174
1176
1177 for (Size entry = 0; entry < num; entry++) {
1179
1180 if (mod == entry) vertices[0][1] = 1;
1181 else vertices[0][0] = 1;
1182
1183 icpt[entry] = vertices;
1184 }
1185
1186 credalNet_bin_cpt->insert(indic, icpt);
1187
1189 } // end of each modality, i.e. as many indicatrice
1190 }
1191
1192 bin_bn->endTopologyTransformation();
1193
1194 if (this->_current_bn_ != nullptr) delete this->_current_bn_;
1195
1196 this->_current_bn_ = bin_bn;
1197
1198 if (this->_credalNet_current_cpt_ != nullptr) delete this->_credalNet_current_cpt_;
1199
1201
1202 if (this->_current_nodeType_ != nullptr) delete this->_current_nodeType_;
1203
1205
1206 _sort_varType_(); // will fill _bin_nodeType_ except for NodeType::Indic
1207 // variables
1208
1210 }
void computeBinaryCPTMinMax()
Used with binary networks to speed-up L2U inference.
NodeProperty< std::vector< NodeId > > _var_bits_
Corresponding bits of each variable.
Definition credalNet.h:628
const BayesNet< GUM_SCALAR > & current_bn() const

References _credalNet_current_cpt_, _credalNet_src_cpt_, _current_bn_, _current_nodeType_, _sort_varType_(), _src_bn_, _var_bits_, gum::HashTable< Key, Val >::clear(), computeBinaryCPTMinMax(), current_bn(), Indic, gum::HashTable< Key, Val >::insert(), gum::int2Pow(), gum::Instantiation::pos(), gum::Instantiation::setFirst(), gum::superiorPow(), and gum::Instantiation::val().

Here is the call graph for this function:

◆ bnToCredal() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::bnToCredal ( GUM_SCALAR beta,
bool oneNet )

Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of point probabilities and then computes each vertex of each credal set.

The perturbations are done according to the number of cases met for each node and each of it's parent instantiation, i.e. \( \epsilon = *\beta^{ln(N_{pa(X) = j} + 1)} \) is the imprecision introduced which leads to \( \underline{p}(X = i \mid pa(X) = j) = (1 - \epsilon) p(X = i *\mid pa(X) = j) \) and \( \overline{p}(X = i \mid pa(X) = j) *=\underline{p}(X =i \mid pa(X) = j) + \epsilon \). Use this method when using a single BayesNet storing counts of events with oneNet set to TRUE or when using two BayesNet, one with lower probabilities and one with upper probabilities, with oneNet set to FALSE.

Parameters
betaThe beta used to perturbate the network. \( 0 \leq \beta *\leq 1 \).
oneNetBoolean used as a flag. Set to TRUE if one BayesNet if provided with counts, to FALSE if two BayesNet are provided; one with probabilities (the lower net) and one with denominators over the first modalities (the upper net).
keepZeroesBoolean used as a flag as whether or not - respectively TRUE or FALSE - we keep zeroes as zeroes. Default is FALSE, i.e. zeroes are not kept.

Definition at line 444 of file credalNet_tpl.h.

444 {
445 this->bnToCredal(beta, oneNet, false);
446 }
void bnToCredal(GUM_SCALAR beta, bool oneNet, bool keepZeroes)
Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of poin...

References bnToCredal().

Here is the call graph for this function:

◆ bnToCredal() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::bnToCredal ( GUM_SCALAR beta,
bool oneNet,
bool keepZeroes )

Perturbates the BayesNet provided as input for this CredalNet by generating intervals instead of point probabilities and then computes each vertex of each credal set.

The perturbations are done according to the number of cases met for each node and each of it's parent instantiation, i.e. \( \epsilon = *\beta^{ln(N_{pa(X) = j} + 1)} \) is the imprecision introduced which leads to \( \underline{p}(X = i \mid pa(X) = j) = (1 - \epsilon) p(X = i *\mid pa(X) = j) \) and \( \overline{p}(X = i \mid pa(X) = j) *=\underline{p}(X =i \mid pa(X) = j) + \epsilon \). Use this method when using a single BayesNet storing counts of events with oneNet set to TRUE or when using two BayesNet, one with lower probabilities and one with upper probabilities, with oneNet set to FALSE.

Parameters
betaThe beta used to perturbate the network. \( 0 \leq \beta *\leq 1 \).
oneNetBoolean used as a flag. Set to TRUE if one BayesNet if provided with counts, to FALSE if two BayesNet are provided; one with probabilities (the lower net) and one with denominators over the first modalities (the upper net).
keepZeroesBoolean used as a flag as whether or not - respectively TRUE or FALSE - we keep zeroes as zeroes. Default is FALSE, i.e. zeroes are not kept.

Definition at line 449 of file credalNet_tpl.h.

449 {
450 GUM_SCALAR epsi_min = 1.;
451 GUM_SCALAR epsi_max = 0.;
452 GUM_SCALAR epsi_moy = 0.;
453 GUM_SCALAR epsi_den = 0.;
454
455 for (auto node: src_bn().nodes()) {
456 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(node));
457
459 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_min_.cpt(node)));
461 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_max_.cpt(node)));
462
463 Size var_dSize = _src_bn_.variable(node).domainSize();
465
469
470 ins.setFirst();
471 ins_min.setFirst();
472 ins_max.setFirst();
473
475
476 for (Size entry = 0; entry < entry_size; entry++) {
478
479 if (oneNet) den = 0;
480 else den = tensor_max->get(ins_max);
481
482 Size nbm = 0;
483
484 for (Size modality = 0; modality < var_dSize; modality++) {
485 vertex[modality] = tensor->get(ins);
486
487 if (oneNet) {
488 den += vertex[modality];
489
490 if (vertex[modality] < 1 && vertex[modality] > 0)
492 "bnToCredal : the BayesNet contains "
493 "probabilities and not event counts "
494 "although user precised oneNet = "
495 << oneNet);
496 }
497
498 if (vertex[modality] > 0) nbm++;
499
500 ++ins;
501 }
502
504 if (!oneNet) {
505 GUM_SCALAR sum = 0;
506
507 for (auto modality = vertex.cbegin(), theEnd = vertex.cend(); modality != theEnd;
508 ++modality) {
509 sum += *modality;
510 }
511
512 if (std::fabs(1. - sum) > _epsRedund_) {
514 _src_bn_.variable(node).name()
515 << "(" << _epsRedund_ << ") does not sum to one for" << " " << entry
516 << std::endl
517 << vertex << std::endl
518 << ins << std::endl);
519 }
520 }
521
523
525
526 if (beta == 0) epsilon = 0;
527 else if (den == 0 || beta == 1) epsilon = GUM_SCALAR(1.0);
529
530 epsi_moy += epsilon;
531 epsi_den += 1;
532
534
536
538
539 for (Size modality = 0; modality < var_dSize; modality++) {
540 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
541 min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
542
543 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
544
546 } else {
547 // if ( ( vertex[modality] == 0 && keepZeroes ) || (
548 // vertex[modality] > 0 && nbm <= 1 ) || ( vertex[modality] == 0
549 // && nbm <= 1 ) ) {
551
552 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
553
554 max = min;
555 }
556
557 tensor_min->set(ins_min, min);
558 tensor_max->set(ins_max, max);
559
560 ++ins_min;
561 ++ins_max;
562 } // end of : for each modality
563 } // end of : for each entry
564 } // end of : for each variable
565
569
571 }
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs).
const BayesNet< GUM_SCALAR > & src_bn() const

References _epsilonMax_, _epsilonMin_, _epsilonMoy_, _epsRedund_, _intervalToCredal_(), _src_bn_, _src_bn_max_, _src_bn_min_, GUM_ERROR, gum::Instantiation::setFirst(), and src_bn().

Referenced by bnToCredal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ computeBinaryCPTMinMax()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::computeBinaryCPTMinMax ( )

Used with binary networks to speed-up L2U inference.

Store the lower and upper probabilities of each node X over the "true" modality, i.e. respectively \( \underline{p}(X = 1 \mid pa(X) = j) \) and \( \overline{p}(X = 1 \mid pa(X) = j) \).

Definition at line 1252 of file credalNet_tpl.h.

1252 {
1253 // ASSUMPTION: NodeIds are dense 0..N-1 (no nodes ever removed from the BN).
1254 // If sparse NodeIds are introduced, indexing by node will go out of bounds.
1255 _binCptMin_.resize(current_bn().size());
1256 _binCptMax_.resize(current_bn().size());
1257
1258 for (auto node: current_bn().nodes()) {
1259 auto pConf = credalNet_currentCpt()[node].size();
1262
1263 for (Size pconf = 0; pconf < pConf; pconf++) {
1264 GUM_SCALAR v1, v2;
1265 v1 = credalNet_currentCpt()[node][pconf][0][1];
1266
1267 if (credalNet_currentCpt()[node][pconf].size() > 1)
1268 v2 = credalNet_currentCpt()[node][pconf][1][1];
1269 else v2 = v1;
1270
1271 GUM_SCALAR delta = v1 - v2;
1272 min[pconf] = (delta >= 0) ? v2 : v1;
1273 max[pconf] = (delta >= 0) ? v1 : v2;
1274 }
1275
1276 _binCptMin_[node] = min;
1277 _binCptMax_[node] = max;
1278 }
1279
1281 }
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
std::vector< std::vector< GUM_SCALAR > > _binCptMin_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:644
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:652

References _binCptMax_, _binCptMin_, _hasComputedBinaryCPTMinMax_, credalNet_currentCpt(), and current_bn().

Referenced by approximatedBinarization().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ credalNet_currentCpt()

template<GUM_Numeric GUM_SCALAR>
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & gum::credal::CredalNet< GUM_SCALAR >::credalNet_currentCpt ( ) const
Returns
Returns a constant reference to the ( up-to-date ) CredalNet CPTs.

Definition at line 1214 of file credalNet_tpl.h.

1214 {
1215 if (_credalNet_current_cpt_ != nullptr) return *_credalNet_current_cpt_;
1216
1217 return _credalNet_src_cpt_;
1218 }

References _credalNet_current_cpt_, and _credalNet_src_cpt_.

Referenced by computeBinaryCPTMinMax(), and gum::credal::VarMod2BNsMap< GUM_SCALAR >::setCNet().

Here is the caller graph for this function:

◆ credalNet_srcCpt()

template<GUM_Numeric GUM_SCALAR>
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & gum::credal::CredalNet< GUM_SCALAR >::credalNet_srcCpt ( ) const
Returns
Returns a constant reference to the ( up-to-date ) CredalNet CPTs.

Definition at line 1222 of file credalNet_tpl.h.

1222 {
1223 return _credalNet_src_cpt_;
1224 }

References _credalNet_src_cpt_.

◆ current_bn()

template<GUM_Numeric GUM_SCALAR>
const BayesNet< GUM_SCALAR > & gum::credal::CredalNet< GUM_SCALAR >::current_bn ( ) const
Returns
Returs a constant reference to the actual BayesNet (used as a DAG, it's CPTs does not matter).

Definition at line 1379 of file credalNet_tpl.h.

1379 {
1380 if (_current_bn_ != nullptr) return *_current_bn_;
1381
1382 return _src_bn_;
1383 }

References _current_bn_, and _src_bn_.

Referenced by gum::credal::CNLoopyPropagation< GUM_SCALAR >::CNLoopyPropagation(), approximatedBinarization(), computeBinaryCPTMinMax(), and toDot().

Here is the caller graph for this function:

◆ currentNodeType()

template<GUM_Numeric GUM_SCALAR>
CredalNet< GUM_SCALAR >::NodeType gum::credal::CredalNet< GUM_SCALAR >::currentNodeType ( const NodeId & id) const
Parameters
idThe constant reference to the chosen NodeId
Returns
Returns the type of the chosen node in the ( up-to-date ) CredalNet current_bn if any, src_bn otherwise.

Definition at line 1228 of file credalNet_tpl.h.

1228 {
1229 if (_current_nodeType_ != nullptr) return (*(_current_nodeType_))[id];
1230
1231 return _original_nodeType_[id];
1232 }

References _current_nodeType_, and _original_nodeType_.

◆ domainSize()

template<GUM_Numeric GUM_SCALAR>
Size gum::credal::CredalNet< GUM_SCALAR >::domainSize ( const NodeId & id)

Get the cardinality of a node

Parameters
idThe NodeId of the node
Returns
The cardinality of the node

Definition at line 408 of file credalNet_tpl.h.

408 {
409 return _src_bn_.variable(id).domainSize();
410 }

References _src_bn_.

◆ epsilonMax()

template<GUM_Numeric GUM_SCALAR>
const GUM_SCALAR & gum::credal::CredalNet< GUM_SCALAR >::epsilonMax ( ) const
Returns
Returns a constant reference to the highest perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 1301 of file credalNet_tpl.h.

1301 {
1302 return _epsilonMax_;
1303 }

References _epsilonMax_.

◆ epsilonMean()

template<GUM_Numeric GUM_SCALAR>
const GUM_SCALAR & gum::credal::CredalNet< GUM_SCALAR >::epsilonMean ( ) const
Returns
Returns a constant reference to the average perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 1306 of file credalNet_tpl.h.

1306 {
1307 return _epsilonMoy_;
1308 }

References _epsilonMoy_.

◆ epsilonMin()

template<GUM_Numeric GUM_SCALAR>
const GUM_SCALAR & gum::credal::CredalNet< GUM_SCALAR >::epsilonMin ( ) const
Returns
Returns a constant reference to the lowest perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 1296 of file credalNet_tpl.h.

1296 {
1297 return _epsilonMin_;
1298 }

References _epsilonMin_.

◆ fillConstraint() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::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 )

Parameters
idThe NodeId of the node
entryThe index of the instantiation excluding the given node ( only the parents are used to compute the index of the credal set )
lowerThe lower value for each probability in correct order
upperThe upper value for each probability in correct order

You need to call intervalToCredal when done filling all constraints.

Warning
: DOES change the BayesNet (s) associated to this credal net !
Note
we forget the master ref of ins to check variable order in the instantiation ( to get index ), therefore we pass it by value

Definition at line 289 of file credalNet_tpl.h.

292 {
294 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_min_.cpt(id)));
296 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_max_.cpt(id)));
297
298 auto var_dSize = _src_bn_.variable(id).domainSize();
299
300 if (lower.size() != var_dSize || upper.size() != var_dSize)
302 "setCPT : variable modalities in cpts does not match for node id : "
303 << id << " with sizes of constraints : ( " << lower.size() << " || "
304 << upper.size() << " ) != " << var_dSize);
305
307
308 if (entry >= entry_size)
310 "setCPT : entry is greater or equal than entry size "
311 "(entries start at 0 up to entry_size - 1) : "
312 << entry << " >= " << entry_size);
313
316 min.setFirst();
317 max.setFirst();
318
319 Idx pos = 0;
320
321 while (pos != entry) {
322 ++min;
323 ++max;
324 ++pos;
325 }
326
327 for (Size i = 0; i < var_dSize; i++) {
328 tensor_min->set(min, lower[i]);
329 tensor_max->set(max, upper[i]);
330 ++min;
331 ++max;
332 }
333 }

References _src_bn_, _src_bn_max_, _src_bn_min_, GUM_ERROR, and gum::Instantiation::setFirst().

Referenced by fillConstraint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fillConstraint() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::fillConstraint ( const NodeId & id,
Instantiation ins,
const std::vector< GUM_SCALAR > & lower,
const std::vector< GUM_SCALAR > & upper )

Set the interval constraints of a credal sets of a given node ( from an instantiation )

Parameters
idThe NodeId of the node
insThe Instantiation
lowerThe lower value for each probability in correct order
upperThe upper value for each probability in correct order

You need to call intervalToCredal when done filling all constraints.

Warning
: DOES change the BayesNet (s) associated to this credal net !
Note
we forget the master ref of ins to check variable order in the instantiation ( to get index ), therefore we pass it by value

Definition at line 336 of file credalNet_tpl.h.

339 {
340 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(id));
341 /*
342 auto var_dSize = _src_bn_.variable ( id ).domainSize();
343 auto entry_size = tensor->domainSize() / var_dSize;
344 */
345 // to be sure of entry index reorder ins according to the bayes net
346 // tensors
347 // ( of the credal net )
348 // it WONT throw an error if the sequences are not equal not because of
349 // order
350 // but content, so we double check (before & after order correction)
351 // beware of slaves & master
353 ref.forgetMaster();
354
355 ins.forgetMaster();
356
357 const auto& vseq = ref.variablesSequence();
358
359 if (ins.variablesSequence() != vseq) {
360 ins.reorder(ref);
361
362 if (ins.variablesSequence() != vseq)
364 "setCPT : instantiation : "
365 << ins << " is not valid for node id " << id
366 << " which accepts instantiations such as (order is not "
367 "important) : "
368 << ref);
369 }
370
371 Idx entry = 0, jump = 1;
372
373 for (Idx i = 0, end = ins.nbrDim(); i < end; i++) {
374 if (_src_bn_.nodeId(ins.variable(i)) == id) continue;
375
376 entry += ins.val(i) * jump;
377
378 jump *= ins.variable(i).domainSize();
379 }
380
381 /*
382 if ( entry >= entry_size )
383 GUM_ERROR ( SizeError, "setCPT : entry is greater or equal than entry
384 size
385 (entries start at 0 up to entry_size - 1) : " << entry << " >= " <<
386 entry_size
387 );
388
389 if ( lower.size() != var_dSize || upper.size() != var_dSize )
390 GUM_ERROR ( SizeError, "setCPT : variable modalities in cpts does not
391 match
392 for node id : " << id << " with sizes of constraints : ( "<< lower.size()
393 << "
394 || " << upper.size() << " ) != " << var_dSize );
395 */
397 }
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 )

References _src_bn_, gum::DiscreteVariable::domainSize(), fillConstraint(), gum::Instantiation::forgetMaster(), GUM_ERROR, gum::Instantiation::nbrDim(), gum::Instantiation::reorder(), gum::Instantiation::val(), gum::Instantiation::variable(), and gum::Instantiation::variablesSequence().

Here is the call graph for this function:

◆ fillConstraints()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::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 )

Parameters
idThe NodeId of the node
lowerThe lower value for each probability in correct order
upperThe upper value for each probability in correct order

You need to call intervalToCredal when done filling all constraints.

Warning
: DOES change the BayesNet (s) associated to this credal net !
Note
we forget the master ref of ins to check variable order in the instantiation ( to get index ), therefore we pass it by value

Definition at line 276 of file credalNet_tpl.h.

278 {
279 try {
280 _src_bn_min_.cpt(id).fillWith(lower);
281 _src_bn_max_.cpt(id).fillWith(upper);
282 } catch (const SizeError&) {
284 "fillConstraints : sizes does not match in fillWith for node id : " << id);
285 }
286 }

References _src_bn_max_, _src_bn_min_, and GUM_ERROR.

◆ get_binaryCPT_max()

template<GUM_Numeric GUM_SCALAR>
const std::vector< std::vector< GUM_SCALAR > > & gum::credal::CredalNet< GUM_SCALAR >::get_binaryCPT_max ( ) const

Used with binary networks to speed-up L2U inference.

Returns
Returns a constant reference to the upper probabilities of each node X over the "true" modality, i.e. \( \overline{p}(X = 1 \mid pa(X) = j) *\).

Definition at line 1291 of file credalNet_tpl.h.

1291 {
1292 return _binCptMax_;
1293 }

References _binCptMax_.

◆ get_binaryCPT_min()

template<GUM_Numeric GUM_SCALAR>
const std::vector< std::vector< GUM_SCALAR > > & gum::credal::CredalNet< GUM_SCALAR >::get_binaryCPT_min ( ) const

Used with binary networks to speed-up L2U inference.

Returns
Returns a constant reference to the lower probabilities of each node X over the "true" modality, i.e. \( \underline{p}(X = 1 \mid pa(X) = j) *\).

Definition at line 1285 of file credalNet_tpl.h.

1285 {
1286 return _binCptMin_;
1287 }

References _binCptMin_.

◆ hasComputedBinaryCPTMinMax()

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::CredalNet< GUM_SCALAR >::hasComputedBinaryCPTMinMax ( ) const
Returns
Returns TRUE if this CredalNet has called computeBinaryCPTMinMax() to speed-up inference with binary networks and L2U. This needs to be reworked as it is too easy to forget to call it and it can't be called within the inference engine (constness).

Definition at line 1246 of file credalNet_tpl.h.

1246 {
1248 }

References _hasComputedBinaryCPTMinMax_.

Referenced by gum::credal::CNLoopyPropagation< GUM_SCALAR >::CNLoopyPropagation().

Here is the caller graph for this function:

◆ idmLearning()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::idmLearning ( const Idx s = 0,
const bool keepZeroes = false )

Learns parameters from a BayesNet storing counts of events.

Use this method when using a single BayesNet storing counts of events. IDM model if s > 0, standard point probability if s = 0 (default value if none precised).

Parameters
sThe IDM parameter.
keepZeroesBoolean used as a flag as whether or not - respectively TRUE or FALSE - we keep zeroes as zeroes. Default is FALSE, i.e. zeroes are not kept.

Definition at line 618 of file credalNet_tpl.h.

618 {
619 for (auto node: _src_bn_.nodes()) {
620 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(node));
621
623 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_min_.cpt(node)));
625 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_max_.cpt(node)));
626
627 Size var_dSize = _src_bn_.variable(node).domainSize();
629
633
634 ins.setFirst();
635 ins_min.setFirst();
636 ins_max.setFirst();
637
639
640 for (Size entry = 0; entry < entry_size; entry++) {
641 GUM_SCALAR den = 0;
642 Size nbm = 0;
643
644 for (Size modality = 0; modality < var_dSize; modality++) {
645 vertex[modality] = tensor->get(ins);
646
647 if (vertex[modality] < 1 && vertex[modality] > 0)
649 "idmLearning : the BayesNet contains "
650 "probabilities and not event counts.");
651
652 den += vertex[modality];
653
654 if (vertex[modality] > 0) nbm++;
655
656 ++ins;
657 }
658
659 if (nbm > 1 || !keepZeroes) den += s;
660
662
663 for (Size modality = 0; modality < var_dSize; modality++) {
665 max = min;
666
667 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
668
669 min = GUM_SCALAR(min * 1.0 / den);
670 max = GUM_SCALAR(max * 1.0 / den);
671
672 tensor_min->set(ins_min, min);
673 tensor_max->set(ins_max, max);
674
675 ++ins_min;
676 ++ins_max;
677 } // end of : for each modality
678 } // end of : for each entry
679 } // end of : for each variable
680
685 }

References _epsilonMax_, _epsilonMin_, _epsilonMoy_, _intervalToCredal_(), _src_bn_, _src_bn_max_, _src_bn_min_, GUM_ERROR, and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ instantiation()

template<GUM_Numeric GUM_SCALAR>
Instantiation gum::credal::CredalNet< GUM_SCALAR >::instantiation ( const NodeId & id)

Get an Instantiation from a node id, useful to fill the constraints of the network

bnet accessors / shortcuts

Parameters
idThe NodeId we want an instantiation from
Returns
The instantiation

Definition at line 403 of file credalNet_tpl.h.

403 {
404 return Instantiation(_src_bn_.cpt(id));
405 }
Instantiation()
Default constructor: creates an empty tuple.

References gum::Instantiation::Instantiation(), and _src_bn_.

Here is the call graph for this function:

◆ intervalToCredal()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal ( )

Computes the vertices of each credal set according to their interval definition (uses lrs).

Use this method when using two BayesNet, one with lower probabilities and one with upper probabilities.

Definition at line 786 of file credalNet_tpl.h.

786 {
787 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
788
789 _credalNet_src_cpt_.resize(_src_bn_.size());
790
792
793 for (auto node: _src_bn_.nodes()) {
796
797 Size var_dSize = _src_bn_.variable(node).domainSize();
799
801
804
805 ins_min.setFirst();
806 ins_max.setFirst();
807
808 lrsWrapper.setUpH(var_dSize);
809
810 for (Size entry = 0; entry < entry_size; entry++) {
811 for (Size modality = 0; modality < var_dSize; modality++) {
812 if (tensor_min->get(ins_min) > tensor_max->get(ins_max)) {
814 "For variable "
815 << _src_bn_.variable(node).name() << " (at " << ins_min
816 << "), the min is greater than the max : " << tensor_min->get(ins_min)
817 << ">" << tensor_max->get(ins_max) << ".");
818 }
820 ++ins_min;
821 ++ins_max;
822 }
823
824 lrsWrapper.H2V();
825 var_cpt[entry] = lrsWrapper.getOutput();
826 lrsWrapper.nextHInput();
827 }
828
830 } // end of : for each variable (node)
831
832 // get precise/credal/vacuous status of each variable
835 }

References _credalNet_src_cpt_, _separatelySpecified_, _sort_varType_(), _src_bn_, _src_bn_max_, _src_bn_min_, gum::credal::LRSWrapper< GUM_SCALAR >::fillH(), gum::credal::LRSWrapper< GUM_SCALAR >::getOutput(), GUM_ERROR, gum::credal::LRSWrapper< GUM_SCALAR >::H2V(), gum::credal::LRSWrapper< GUM_SCALAR >::nextHInput(), gum::Instantiation::setFirst(), and gum::credal::LRSWrapper< GUM_SCALAR >::setUpH().

Here is the call graph for this function:

◆ intervalToCredalWithFiles()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::intervalToCredalWithFiles ( )
Deprecated
Use intervalToCredal ( lrsWrapper with no input / output files needed ).

Computes the vertices of each credal set according to their interval definition (uses lrs).

Use this method when using a single BayesNet storing counts of events.

Definition at line 839 of file credalNet_tpl.h.

839 {
840 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
841
842 _credalNet_src_cpt_.resize(_src_bn_.size());
843
844 for (auto node: _src_bn_.nodes()) {
847
848 auto var_dSize = _src_bn_.variable(node).domainSize();
850
852
855
856 ins_min.setFirst();
857 ins_max.setFirst();
858
859 // use iterator
860 for (Size entry = 0; entry < entry_size; entry++) {
862 std::vector< GUM_SCALAR > vertex(var_dSize); // if not interval
863
865 var_dSize * 2,
867
870 sum_ineq1[0] = 1;
871 sum_ineq2[0] = -1;
872
873 bool isInterval = false;
874
875 for (Size modality = 0; modality < var_dSize; modality++) {
876 inequalities[modality * 2][0] = -tensor_min->get(ins_min);
877 inequalities[modality * 2 + 1][0] = tensor_max->get(ins_max);
878 inequalities[modality * 2][modality + 1] = 1;
879 inequalities[modality * 2 + 1][modality + 1] = -1;
880
881 vertex[modality] = inequalities[modality * 2 + 1][0];
882
883 if (!isInterval
884 && (-inequalities[modality * 2][0] != inequalities[modality * 2 + 1][0]))
885 isInterval = true;
886
887 ++ins_min;
888 ++ins_max;
889 }
890
891 inequalities.push_back(sum_ineq1);
892 inequalities.push_back(sum_ineq2);
893
894 if (!isInterval) {
895 vertices.push_back(vertex);
896 } else {
897 try {
899 // __H2Vcdd ( inequalities, vertices );
900 } catch (const std::exception& err) {
901 std::cout << err.what() << std::endl;
902 throw;
903 }
904 } // end of : is interval
905
906 if (entry == 0 && vertices.size() >= 2) {
907 auto tmp = vertices[0];
908 vertices[0] = vertices[1];
909 vertices[1] = tmp;
910 }
911
913 } // end of : for each entry
914
916 // std::cout << _src_bn_.variable(node_idIt).name() << std::endl;
917 // std::cout << var_cpt << std::endl;
918 } // end of : for each variable (node)
919
920 // get precise/credal/vacuous status of each variable
923 }
void _H2Vlrs_(const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const

References _credalNet_src_cpt_, _H2Vlrs_(), _separatelySpecified_, _sort_varType_(), _src_bn_, _src_bn_max_, _src_bn_min_, and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ isSeparatelySpecified()

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::CredalNet< GUM_SCALAR >::isSeparatelySpecified ( ) const
Returns
Returns TRUE if this CredalNet is separately and interval specified, FALSE otherwise.

Definition at line 1241 of file credalNet_tpl.h.

1241 {
1242 return _separatelySpecified_;
1243 }

References _separatelySpecified_.

Referenced by gum::credal::CNLoopyPropagation< GUM_SCALAR >::CNLoopyPropagation().

Here is the caller graph for this function:

◆ lagrangeNormalization()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::lagrangeNormalization ( )

Normalize counts of a BayesNet storing counts of each events such that no probability is 0.

Use this method when using a single BayesNet storing counts of events. Lagrange normalization. This call is irreversible and modify counts stored by _src_bn_.

Doest not performs computations of the parameters but keeps normalized counts of events only. Call idmLearning to compute the probabilities (with any parameter value).

Definition at line 574 of file credalNet_tpl.h.

574 {
575 for (auto node: _src_bn_.nodes()) {
576 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(node));
577
578 auto var_dSize = _src_bn_.variable(node).domainSize();
580
582
583 ins.setFirst();
584
586
587 for (Size entry = 0; entry < entry_size; entry++) {
588 bool zeroes = false;
590
591 for (Size modality = 0; modality < var_dSize; modality++) {
592 vertex[modality] = tensor->get(ins);
593
594 if (vertex[modality] < 1 && vertex[modality] > 0)
596 "lagrangeNormalization : the BayesNet "
597 "contains probabilities and not event "
598 "counts.");
599
600 if (!zeroes && vertex[modality] == 0) { zeroes = true; }
601
602 ++ins;
603 }
604
605 if (zeroes) {
606 ins = ins_prev;
607
608 for (Size modality = 0; modality < var_dSize; modality++) {
609 tensor->set(ins, tensor->get(ins) + 1);
610 ++ins;
611 }
612 }
613 } // end of : for each entry
614 } // end of : for each variable
615 }

References _src_bn_, GUM_ERROR, and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ nodeType()

template<GUM_Numeric GUM_SCALAR>
CredalNet< GUM_SCALAR >::NodeType gum::credal::CredalNet< GUM_SCALAR >::nodeType ( const NodeId & id) const
Parameters
idThe constant reference to the chosen NodeId
Returns
Returns the type of the chosen node in the ( up-to-date ) CredalNet in src_bn.

Definition at line 1236 of file credalNet_tpl.h.

1236 {
1237 return _original_nodeType_[id];
1238 }

References _original_nodeType_.

◆ saveBNsMinMax()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::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.

to call after bnToCredal( GUM_SCALAR beta ) save a BN with lower probabilities and a BN with upper ones

Parameters
min_pathThe path to save the BayesNet which contains the lower probabilities of each node X, i.e. \( \underline{p}(X = i \mid pa(X) = *j) *\).
max_pathThe path to save the BayesNet which contains the upper probabilities of each node X, i.e. \( \overline{p}(X = i \mid pa(X) = *j) *\).

Definition at line 930 of file credalNet_tpl.h.

931 {
933
934 std::string minfilename(min_path); //"min.bif";
935 std::string maxfilename(max_path); //"max.bif";
938
939 if (!min_file.good())
940 GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << minfilename);
941
942 if (!max_file.good()) {
943 min_file.close();
944 GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << maxfilename);
945 }
946
947 try {
950 } catch (Exception& err) {
952 min_file.close();
953 max_file.close();
954 throw(err);
955 }
956
957 min_file.close();
958 max_file.close();
959 }

References _src_bn_max_, _src_bn_min_, GUM_ERROR, GUM_SHOWERROR, and gum::BNWriter< GUM_SCALAR >::write().

Here is the call graph for this function:

◆ setCPT() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::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 )

Parameters
idThe NodeId of the node
entryThe index of the instantiation ( from 0 to K - 1 ) excluding the given node ( only the parents are used to compute the index of the credal set )
cptThe vertices of every credal set ( for each instantiation of the parents )

Use this with either LRSWrapper or LpInterface to get the vertices of a credal set represented by linear constraints.

Warning
: Does not change the BayesNet (s) associated to this credal net !

Definition at line 135 of file credalNet_tpl.h.

137 {
138 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(id));
139
140 auto var_dSize = _src_bn_.variable(id).domainSize();
142
143 if (entry >= entry_size)
145 "setCPT : entry is greater or equal than entry size "
146 "(entries start at 0 up to entry_size - 1) : "
147 << entry << " >= " << entry_size);
148
149 if (cpt.size() == 0) GUM_ERROR(SizeError, "setCPT : empty credal set for entry : " << entry)
150
151 for (const auto& vertex: cpt) {
152 if (vertex.size() != var_dSize)
154 "setCPT : variable modalities in cpts does not "
155 "match for node id : "
156 << id << " with vertex " << vertex << " at entry " << entry << " : "
157 << vertex.size() << " != " << var_dSize);
158
159 GUM_SCALAR sum = 0;
160
161 for (const auto& prob: vertex) {
162 sum += prob;
163 }
164
167 "setCPT : a vertex coordinates does not sum to one for node id : "
168 << id << " at entry " << entry << " with vertex " << vertex);
169 }
170
171 // !! auto does NOT use adress (if available) unless explicitly asked !!
172 auto& node_cpt = _credalNet_src_cpt_.getWithDefault(
173 id,
175
176 if (node_cpt[entry].size() != 0)
178 "setCPT : vertices of entry id " << entry
179 << " already set to : " << node_cpt[entry]
180 << ", cannot insert : " << cpt);
181
182 node_cpt[entry] = cpt;
183
185 }

References _credalNet_src_cpt_, _src_bn_, gum::config(), GUM_ERROR, and gum::isCloseToOne().

Here is the call graph for this function:

◆ setCPT() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::setCPT ( const NodeId & id,
Instantiation ins,
const std::vector< std::vector< GUM_SCALAR > > & cpt )

Set the vertices of one credal set of a given node ( any instantiation )

Parameters
idThe NodeId of the node
insThe Instantiation ( only the parents matter to find the credal set index )
cptThe vertices of every credal set ( for each instantiation of the parents )

Use this with either LRSWrapper or LpInterface to get the vertices of a credal set represented by linear constraints.

Warning
: Does not change the BayesNet (s) associated to this credal net !
Note
we forget the master ref of ins to check variable order in the instantiation ( to get index ), therefore we pass it by value

Definition at line 188 of file credalNet_tpl.h.

190 {
191 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(id));
192
193 auto var_dSize = _src_bn_.variable(id).domainSize();
195
196 // to be sure of entry index reorder ins according to the bayes net
197 // tensors
198 // ( of the credal net )
199 // it WONT throw an error if the sequences are not equal not because of
200 // order
201 // but content, so we double check (before & after order correction)
202 // beware of slaves & master
204 ref.forgetMaster();
205
206 ins.forgetMaster();
207
208 const auto& vseq = ref.variablesSequence();
209
210 if (ins.variablesSequence() != vseq) {
211 ins.reorder(ref);
212
213 if (ins.variablesSequence() != vseq)
215 "setCPT : instantiation : "
216 << ins << " is not valid for node id " << id
217 << " which accepts instantiations such as (order is not "
218 "important) : "
219 << ref);
220 }
221
222 Idx entry = 0, jump = 1;
223
224 for (Idx i = 0, end = ins.nbrDim(); i < end; i++) {
225 if (_src_bn_.nodeId(ins.variable(i)) == id) continue;
226
227 entry += ins.val(i) * jump;
228
229 jump *= ins.variable(i).domainSize();
230 }
231
232 if (entry >= entry_size)
234 "setCPT : entry is greater or equal than entry size "
235 "(entries start at 0 up to entry_size - 1) : "
236 << entry << " >= " << entry_size);
237
238 if (cpt.size() == 0) GUM_ERROR(SizeError, "setCPT : empty credal set for entry : " << entry)
239
240 for (const auto& vertex: cpt) {
241 if (vertex.size() != var_dSize)
243 "setCPT : variable modalities in cpts does not "
244 "match for node id : "
245 << id << " with vertex " << vertex << " at entry " << entry << " : "
246 << vertex.size() << " != " << var_dSize);
247
248 GUM_SCALAR sum = 0;
249
250 for (const auto& prob: vertex) {
251 sum += prob;
252 }
253
256 "setCPT : a vertex coordinates does not sum to one for node id : "
257 << id << " at entry " << entry << " with vertex " << vertex);
258 }
259
260 auto& node_cpt = _credalNet_src_cpt_.getWithDefault(
261 id,
263
264 if (node_cpt[entry].size() != 0)
266 "setCPT : vertices of entry : " << ins << " id " << entry
267 << " already set to : " << node_cpt[entry]
268 << ", cannot insert : " << cpt);
269
270 node_cpt[entry] = cpt;
271
273 }

References _credalNet_src_cpt_, _src_bn_, gum::config(), gum::DiscreteVariable::domainSize(), gum::Instantiation::forgetMaster(), GUM_ERROR, gum::isCloseToOne(), gum::Instantiation::nbrDim(), gum::Instantiation::reorder(), gum::Instantiation::val(), gum::Instantiation::variable(), and gum::Instantiation::variablesSequence().

Here is the call graph for this function:

◆ setCPTs()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::CredalNet< GUM_SCALAR >::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

Parameters
idThe NodeId of the node
cptThe vertices of every credal set ( for each instantiation of the parents )
Warning
: Does not change the BayesNet (s) associated to this credal net !

First dimension is instantiation position ( from 0 to K - 1 ). Second is the credal set vertice index Third is the vertex

Definition at line 91 of file credalNet_tpl.h.

93 {
94 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(id));
95
96 auto var_dSize = _src_bn_.variable(id).domainSize();
97
98 if (auto entry_size = tensor->domainSize() / var_dSize; cpt.size() != entry_size)
100 "setCPTs : entry sizes of cpts does not match for node id : "
101 << id << " : " << cpt.size() << " != " << entry_size);
102
103 for (const auto& cset: cpt) {
104 if (cset.size() == 0)
106 "setCPTs : vertices in credal set does not match for node id : "
107 << id << " with 0 vertices");
108
109 for (const auto& vertex: cset) {
110 if (vertex.size() != var_dSize)
112 "setCPTs : variable modalities in cpts does "
113 "not match for node id : "
114 << id << " with vertex " << vertex << " : " << vertex.size()
115 << " != " << var_dSize);
116
117 GUM_SCALAR sum = 0;
118
119 for (const auto& prob: vertex) {
120 sum += prob;
121 }
122
125 "setCPTs : a vertex coordinates does not "
126 "sum to one for node id : "
127 << id << " with vertex " << vertex);
128 }
129 }
130
131 _credalNet_src_cpt_.insert(id, cpt);
132 }

References _credalNet_src_cpt_, _src_bn_, gum::config(), GUM_ERROR, and gum::isCloseToOne().

Here is the call graph for this function:

◆ src_bn()

template<GUM_Numeric GUM_SCALAR>
const BayesNet< GUM_SCALAR > & gum::credal::CredalNet< GUM_SCALAR >::src_bn ( ) const
Returns
Returns a constant reference to the original BayesNet (used as a DAG, it's CPTs does not matter).

Definition at line 1386 of file credalNet_tpl.h.

1386 {
1387 return _src_bn_;
1388 }

References _src_bn_.

Referenced by bnToCredal().

Here is the caller graph for this function:

◆ toDot() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::string gum::credal::CredalNet< GUM_SCALAR >::toDot ( ) const
Returns
Returns a dot representation of the graph of this CredalNet (the current one if any, the source one otherwise).

The nodes are named after the variables, drawn as heptagons with the tooltip "(id) name", with the colors of the section credalnet of the configuration.

Definition at line 1342 of file credalNet_tpl.h.

1342 {
1343 return toDot(dot::DotOptions());
1344 }
std::string toDot() const

References toDot().

Referenced by toDot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toDot() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::string gum::credal::CredalNet< GUM_SCALAR >::toDot ( const dot::DotOptions & options) const
Returns
Returns a dot representation of the graph of this CredalNet, with the options given by the caller, indexed by the identifiers of the nodes of the graph.

Definition at line 1347 of file credalNet_tpl.h.

1347 {
1348 const auto& bn = current_bn();
1349 const auto style = dot::DotStyle::readCredalNet();
1350 dot::DotWriter writer(true);
1352
1353 for (const auto node: bn.nodes()) {
1354 const auto& name = bn.variable(node).name();
1356 options.node(node),
1357 "",
1358 "(" + std::to_string(node) + ") " + name);
1359 dot::addDefaultAttribute(attributes, "peripheries", "1");
1360 dot::addDefaultAttribute(attributes, "sides", "7");
1361 dot::addDefaultAttribute(attributes, "shape", "polygon");
1362 writer.node(name, attributes);
1363 }
1364
1365 for (const auto& arc: bn.arcs()) {
1366 auto attributes = dot::arcAttributes(options.arc(arc.tail(), arc.head()));
1368 "tooltip",
1369 std::to_string(arc.tail()) + "&nbsp;&rarr;&nbsp;"
1370 + std::to_string(arc.head()));
1371 dot::addDefaultAttribute(attributes, "fontsize", "10");
1372 writer.edge(bn.variable(arc.tail()).name(), bn.variable(arc.head()).name(), attributes);
1373 }
1374
1375 return writer.str();
1376 }
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
static DotStyle readCredalNet()
the style of the credal networks : the colors of the nodes are the ones of the section credalnet
Definition dotStyle.cpp:91

References gum::dot::addDefaultAttribute(), gum::dot::DotOptions::arc(), gum::dot::arcAttributes(), current_bn(), gum::dot::DotWriter::edge(), gum::dot::DotOptions::node(), gum::dot::DotWriter::node(), gum::dot::nodeAttributes(), gum::dot::DotStyle::readCredalNet(), gum::dot::DotWriter::str(), and gum::dot::writePreamble().

Here is the call graph for this function:

◆ toString()

template<GUM_Numeric GUM_SCALAR>
std::string gum::credal::CredalNet< GUM_SCALAR >::toString ( ) const
Returns
Returns the string representation of this CredalNet, i.e. it's CPTs (which also represent arcs).

Definition at line 1311 of file credalNet_tpl.h.

1311 {
1316
1317 for (auto node: bn_ptr->nodes()) {
1318 const Tensor< GUM_SCALAR >* tensor(&bn_ptr->cpt(node));
1319 auto pconfs = tensor->domainSize() / bn_ptr->variable(node).domainSize();
1320
1321 output << "\n" << bn_ptr->variable(node) << "\n";
1322
1324 ins.forgetMaster();
1325 ins.erase(bn_ptr->variable(node));
1326 ins.setFirst();
1327
1328 for (Size pconf = 0; pconf < pconfs; pconf++) {
1329 output << ins << " : ";
1330 output << (*cpt_ptr)[node][pconf] << "\n";
1331
1332 if (pconf < pconfs - 1) ++ins;
1333 }
1334 }
1335
1336 output << "\n";
1337
1338 return output.str();
1339 }

References _credalNet_current_cpt_, _credalNet_src_cpt_, _current_bn_, _src_bn_, gum::Instantiation::erase(), gum::Instantiation::forgetMaster(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

Member Data Documentation

◆ _binCptMax_

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< GUM_SCALAR > > gum::credal::CredalNet< GUM_SCALAR >::_binCptMax_
private

Used with binary networks to speed-up L2U inference.

Store the upper probabilities of each node X over the "true" modality, i.e. \(\overline{p}(X = 1 \mid pa(X) = j) \).

Definition at line 652 of file credalNet.h.

Referenced by computeBinaryCPTMinMax(), and get_binaryCPT_max().

◆ _binCptMin_

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< GUM_SCALAR > > gum::credal::CredalNet< GUM_SCALAR >::_binCptMin_
private

Used with binary networks to speed-up L2U inference.

Store the lower probabilities of each node X over the "true" modality, i.e. \(\underline{p}(X = 1 \mid pa(X) = j) \).

Definition at line 644 of file credalNet.h.

Referenced by computeBinaryCPTMinMax(), and get_binaryCPT_min().

◆ _credalNet_current_cpt_

template<GUM_Numeric GUM_SCALAR>
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > >* gum::credal::CredalNet< GUM_SCALAR >::_credalNet_current_cpt_
private

This CredalNet up-to-date CPTs.

Definition at line 625 of file credalNet.h.

Referenced by ~CredalNet(), _initParams_(), _sort_varType_(), approximatedBinarization(), credalNet_currentCpt(), and toString().

◆ _credalNet_src_cpt_

template<GUM_Numeric GUM_SCALAR>
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > gum::credal::CredalNet< GUM_SCALAR >::_credalNet_src_cpt_
private

◆ _current_bn_

template<GUM_Numeric GUM_SCALAR>
BayesNet< GUM_SCALAR >* gum::credal::CredalNet< GUM_SCALAR >::_current_bn_
private

Up-to-date BayesNet (used as a DAG).

Definition at line 618 of file credalNet.h.

Referenced by ~CredalNet(), _bnCopy_(), _initParams_(), _sort_varType_(), approximatedBinarization(), current_bn(), and toString().

◆ _current_nodeType_

template<GUM_Numeric GUM_SCALAR>
NodeProperty< NodeType >* gum::credal::CredalNet< GUM_SCALAR >::_current_nodeType_
private

The NodeType of each node from the up-to-date network.

Definition at line 633 of file credalNet.h.

Referenced by ~CredalNet(), _initParams_(), _sort_varType_(), approximatedBinarization(), and currentNodeType().

◆ _deltaC_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_deltaC_
private

5 by default, used by fracC as number of decimals.

Definition at line 572 of file credalNet.h.

Referenced by _initParams_().

◆ _denMax_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_denMax_
private

Highest possible denominator allowed when using farey.

A value too high may lead to lrs being unable to find vertices.

Definition at line 599 of file credalNet.h.

Referenced by _H2Vlrs_(), and _initParams_().

◆ _epsF_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_epsF_
private

Value under which a decimal number is considered to be zero when using farey.

Definition at line 595 of file credalNet.h.

Referenced by _H2Vlrs_(), and _initParams_().

◆ _epsilonMax_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_epsilonMax_
private

The highest perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 581 of file credalNet.h.

Referenced by _initParams_(), bnToCredal(), epsilonMax(), and idmLearning().

◆ _epsilonMin_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_epsilonMin_
private

The lowest perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 577 of file credalNet.h.

Referenced by _initParams_(), bnToCredal(), epsilonMin(), and idmLearning().

◆ _epsilonMoy_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_epsilonMoy_
private

The average perturbation of the BayesNet provided as input for this CredalNet.

Definition at line 585 of file credalNet.h.

Referenced by _initParams_(), bnToCredal(), epsilonMean(), and idmLearning().

◆ _epsRedund_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_epsRedund_
private

Value under which a decimal number is considered to be zero when computing redundant vertices.

Definition at line 590 of file credalNet.h.

Referenced by _H2Vlrs_(), _initParams_(), and bnToCredal().

◆ _hasComputedBinaryCPTMinMax_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::CredalNet< GUM_SCALAR >::_hasComputedBinaryCPTMinMax_
private

Used by L2U, to know if lower and upper probabilities over the second modality has been stored in order to speed-up the algorithm.

Definition at line 637 of file credalNet.h.

Referenced by _initParams_(), computeBinaryCPTMinMax(), and hasComputedBinaryCPTMinMax().

◆ _original_nodeType_

template<GUM_Numeric GUM_SCALAR>
NodeProperty< NodeType > gum::credal::CredalNet< GUM_SCALAR >::_original_nodeType_
private

The NodeType of each node from the ORIGINAL network.

Definition at line 631 of file credalNet.h.

Referenced by _sort_varType_(), currentNodeType(), and nodeType().

◆ _precision_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_precision_
private

Precision used by frac.

Definition at line 602 of file credalNet.h.

Referenced by _initParams_().

◆ _precisionC_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::CredalNet< GUM_SCALAR >::_precisionC_
private

1e6 by default, used by fracC as precision.

Definition at line 570 of file credalNet.h.

Referenced by _initParams_().

◆ _separatelySpecified_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::CredalNet< GUM_SCALAR >::_separatelySpecified_
private

TRUE if this CredalNet is separately and interval specified, FALSE otherwise.

Definition at line 607 of file credalNet.h.

Referenced by _intervalToCredal_(), intervalToCredal(), intervalToCredalWithFiles(), and isSeparatelySpecified().

◆ _src_bn_

◆ _src_bn_max_

template<GUM_Numeric GUM_SCALAR>
BayesNet< GUM_SCALAR > gum::credal::CredalNet< GUM_SCALAR >::_src_bn_max_
private

◆ _src_bn_min_

template<GUM_Numeric GUM_SCALAR>
BayesNet< GUM_SCALAR > gum::credal::CredalNet< GUM_SCALAR >::_src_bn_min_
private

◆ _var_bits_

template<GUM_Numeric GUM_SCALAR>
NodeProperty< std::vector< NodeId > > gum::credal::CredalNet< GUM_SCALAR >::_var_bits_
private

Corresponding bits of each variable.

Deprecated

Definition at line 628 of file credalNet.h.

Referenced by approximatedBinarization().


The documentation for this class was generated from the following files: