aGrUM 3.1.1
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 >:

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
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 97 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 100 of file credalNet.h.

100: 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 55 of file credalNet_tpl.h.

55 {
57
61
63 }
Class template representing a Credal Network.
Definition credalNet.h:97
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:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition credalNet.h:591
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition credalNet.h:594
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 413 of file credalNet_tpl.h.

413 {
414 _initParams_();
416
418 }
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 421 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 430 of file credalNet_tpl.h.

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

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 1419 of file credalNet_tpl.h.

1419 {
1421
1422 for (auto node: bn_ptr->nodes())
1423 dest.add(bn_ptr->variable(node));
1424
1425 dest.beginTopologyTransformation();
1426
1427 for (auto node: bn_ptr->nodes()) {
1428 for (auto parent_idIt: bn_ptr->cpt(node).variablesSequence()) {
1429 if (bn_ptr->nodeId(*parent_idIt) != node) dest.addArc(bn_ptr->nodeId(*parent_idIt), node);
1430 } // end of : for each parent in order of appearence
1431 } // end of : for each variable
1432
1433 dest.endTopologyTransformation();
1434 }
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 1407 of file credalNet_tpl.h.

1408 {
1409 Size vertices_size = 0;
1410
1411 for (auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd; ++entry) {
1412 if (entry->size() > vertices_size) vertices_size = Size(entry->size());
1413 }
1414
1415 return int(vertices_size);
1416 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

◆ _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 1489 of file credalNet_tpl.h.

1490 {
1491 // write H rep file
1492 int64_t num, den;
1493
1494 std::string sinefile = getUniqueFileName(); // generate unique file name, we
1495 // need to add .ine or .ext for lrs
1496 // to know which input it is (Hrep
1497 // to Vrep or Vrep to Hrep)
1498 sinefile += ".ine";
1499
1501
1502 if (!h_file.good())
1503 GUM_ERROR(IOError, " __H2Vlrs : could not open lrs input file : " << sinefile)
1504
1505 h_file << "H - representation\n";
1506 h_file << "begin\n";
1507 h_file << h_rep.size() << ' ' << h_rep[0].size() << " rational\n";
1508
1509 for (auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1510 for (auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2; ++it2) {
1511 // get integer fraction from decimal value
1512 // smallest numerator & denominator is farley, also
1513 // best precision
1515 den,
1516 ((*it2 > 0) ? *it2 : -*it2),
1518 _epsF_);
1519
1520 h_file << ((*it2 > 0) ? num : -num) << '/' << den << ' ';
1521 }
1522
1523 h_file << '\n';
1524 }
1525
1526 h_file << "end\n";
1527 h_file.close();
1528
1529 // call lrs
1531 extfile += ".ext";
1532
1533 std::string lrs_arg0 = "lrs";
1536 char* lrs_argv[3] = {lrs_arg0.data(), lrs_arg1.data(), lrs_arg2.data()};
1537
1538 // it may need to redirect stdout to a file
1539 lrs_main(3, lrs_argv);
1540
1541 // read V rep file
1542 std::ifstream v_file(extfile.c_str() /*extfilename.c_str()*/, std::ios::in);
1543
1544 if (!v_file.good()) GUM_ERROR(IOError, " __H2Vlrs : could not open lrs output file : ")
1545
1546 std::string line, tmp;
1547 char * cstr, *p;
1549
1550 std::string::size_type pos;
1551 bool keep_going = true;
1552 // int vertices;
1553
1555
1556 v_file.ignore(256, 'l');
1557
1558 while (v_file.good() && keep_going) {
1560
1561 if (line.size() == 0) continue;
1562 else if (line.compare("end") == 0) {
1563 keep_going = false;
1564 // this is to get vertices number :
1565 /*getline ( v_file, line );
1566 std::string::size_type pos, end_pos;
1567 pos = line.find ( "vertices = " );
1568 end_pos = line.find ( "rays", pos + 9 );
1569 vertices = atoi ( line.substr ( pos + 9, end_pos - pos - 9 ).c_str()
1570 );*/
1571 break;
1572 } else if (line[1] != '1') {
1574 " __H2Vlrs : reading something other than a vertex from "
1575 "lrs output file : ");
1576 }
1577
1578 line = line.substr(2);
1579 cstr = new char[line.size() + 1];
1580 strcpy(cstr, line.c_str());
1581
1582 p = strtok(cstr, " ");
1583
1584 while (p != nullptr) {
1585 tmp = p;
1586
1587 if (tmp.compare("1") == 0 || tmp.compare("0") == 0)
1588 probability = GUM_SCALAR(atof(tmp.c_str()));
1589 else {
1590 pos = tmp.find("/");
1591 probability = GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1592 / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1593 }
1594
1595 vertex.push_back(probability);
1596 p = strtok(nullptr, " ");
1597 } // end of : for all tokens
1598
1599 delete[] cstr;
1600
1601 // compute is_redund using multiple threads:
1602 // compute the max number of threads to use (avoid nested threads)
1605 : 1; // no nested multithreading
1606
1607 const auto nsize = v_rep.size();
1609
1610 // prepare the data used by the threads
1611 const auto ranges = gum::dispatchRangeToThreads(0, nsize, (unsigned int)(real_nb_threads));
1612 std::vector< Size > t_redund(real_nb_threads); // use Size to avoid false sharing
1613
1614 // create the function to be executed by the threads
1616 const std::size_t nb_threads) {
1617 const auto vsize = vertex.size();
1619
1620 for (Idx i = ranges[this_thread].first, end = ranges[this_thread].second; i < end; i++) {
1621 thread_redund = 1;
1622 for (Idx modality = 0; modality < vsize; ++modality) {
1624 thread_redund = 0;
1625 break;
1626 }
1627 }
1628
1629 if (thread_redund) return;
1630 }
1631 };
1632
1633 // launch the threads
1635
1636 // aggregate the results
1637 bool is_redund = false;
1638 for (const auto thread_redund: t_redund) {
1639 if (thread_redund) {
1640 is_redund = true;
1641 break;
1642 }
1643 }
1644
1645
1646 /*
1647 // old openMP code:
1648 #pragma omp parallel
1649 {
1650 int this_thread = threadsOMP::getThreadNumber();
1651 int num_threads = threadsOMP::getNumberOfRunningThreads();
1652
1653 auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1654 auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1655
1656 for (auto p = begin_pos; p < end_pos; p++) {
1657 #pragma omp flush(is_redund)
1658
1659 if (is_redund) break;
1660
1661 bool thread_redund = true;
1662
1663 auto vsize = vertex.size();
1664
1665 for (Size modality = 0; modality < vsize; modality++) {
1666 if (std::fabs(vertex[modality] - v_rep[p][modality]) > _epsRedund_) {
1667 thread_redund = false;
1668 break;
1669 }
1670 }
1671
1672 if (thread_redund) {
1673 is_redund = true;
1674 #pragma omp flush(is_redund)
1675 int i=0; // this line to work around a weird syntax error with msvc
1676 }
1677 } // end of : each thread for
1678 } // end of : parallel
1679 */
1680
1681 if (!is_redund) v_rep.push_back(vertex);
1682
1683 vertex.clear();
1684 } // end of : file
1685
1686 v_file.close();
1687
1688 if (std::remove(sinefile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + sinefile)
1689
1690 if (std::remove(extfile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + extfile)
1691 }
static void farey(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000L, const GUM_SCALAR &zero=1e-6)
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: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 _epsF_
Value under which a decimal number is considered to be zero when using farey.
Definition credalNet.h:576
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::string getUniqueFileName()
Returns a path to a unique file name.
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 1397 of file credalNet_tpl.h.

1398 {
1401
1402 if (src_max_den.size() > 0) _src_bn_max_ = src_max_den;
1404 }

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 1380 of file credalNet_tpl.h.

1381 {
1384
1385 if (src_max_den.compare("") != 0) other = src_max_den;
1386 else other = src_min_num;
1387
1390
1391 reader.proceed();
1392 reader_min.proceed();
1393 reader_max.proceed();
1394 }

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 1353 of file credalNet_tpl.h.

1353 {
1354 _epsilonMin_ = 0;
1355 _epsilonMax_ = 0;
1356 _epsilonMoy_ = 0;
1357
1358 _epsRedund_ = GUM_SCALAR(1e-6);
1359
1360 // farey algorithm
1361 _epsF_ = GUM_SCALAR(1e-6);
1362 _denMax_ = GUM_SCALAR(1e6); // beware LRSWrapper
1363
1364 // continued fractions, beware LRSWrapper
1365 // decimal paces ( _epsC_ * _precisionC_ == 1)
1367 _deltaC_ = 5;
1368
1369 // old custom algorithm
1370 _precision_ = GUM_SCALAR(1e6); // beware LRSWrapper
1371
1372 _current_bn_ = nullptr;
1373 _credalNet_current_cpt_ = nullptr;
1374 _current_nodeType_ = nullptr;
1375
1377 }
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
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
GUM_SCALAR _precision_
Precision used by frac.
Definition credalNet.h:583
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:562
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition credalNet.h:558
GUM_SCALAR _precisionC_
1e6 by default, used by fracC as precision.
Definition credalNet.h:551

References _credalNet_current_cpt_, _current_bn_, _current_nodeType_, _deltaC_, _denMax_, _epsF_, _epsilonMax_, _epsilonMin_, _epsilonMoy_, _epsRedund_, _hasComputedBinaryCPTMinMax_, _precision_, and _precisionC_.

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

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 687 of file credalNet_tpl.h.

687 {
688 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
689
690 _credalNet_src_cpt_.resize(_src_bn_.size());
691
692 for (auto node: _src_bn_.nodes()) {
695
696 Size var_dSize = _src_bn_.variable(node).domainSize();
698
700
703
704 ins_min.setFirst();
705 ins_max.setFirst();
706
709
710 for (Size entry = 0; entry < entry_size; entry++) {
711 for (Size modality = 0; modality < var_dSize; modality++, ++ins_min, ++ins_max) {
714 }
715
716 bool all_equals = true;
718
719 for (Size modality = 0; modality < var_dSize; modality++) {
720 if (std::fabs(upper[modality] - lower[modality]) < 1e-6) continue;
721
722 all_equals = false;
725
726 for (Size mod = 0; mod < var_dSize; mod++) {
727 if (modality != mod) vertex[mod] = lower[mod];
728 }
729
730 GUM_SCALAR total = 0;
731
732 auto vsize = vertex.size();
733
734 for (Size i = 0; i < vsize; i++)
735 total += vertex[i];
736
737 if (std::fabs(total - 1.) > 1e-6)
739 _src_bn_.variable(node).name()
740 << " does not sum to one for " << entry << std::endl
741 << vertex << std::endl);
742
743 vertices.push_back(vertex);
744 }
745
746 if (all_equals) {
748
749 for (Size modality = 0; modality < var_dSize; modality++)
751
752 GUM_SCALAR total = 0.;
753
754 auto vsize = vertex.size();
755
756 for (Size i = 0; i < vsize; i++)
757 total += vertex[i];
758
759 if (std::fabs(total - 1.) > 1e-6)
761 _src_bn_.variable(node).name()
762 << " does not sum to one for " << entry << std::endl
763 << vertex << std::endl);
764
765 vertices.push_back(vertex);
766 }
767
769 }
770
772 } // end of : for each variable (node)
773
774 // get precise/credal/vacuous status of each variable
777 }
Size domainSize(const NodeId &id)
Get the cardinality of a node
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition credalNet.h:602
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:588

References _credalNet_src_cpt_, _separatelySpecified_, _sort_varType_(), _src_bn_, _src_bn_max_, _src_bn_min_, 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 1694 of file credalNet_tpl.h.

1694 {
1700
1701 for (auto node: bn_ptr->nodes()) {
1702 // indicatrices are already present
1703 if (nodeType_ptr->exists(node)) continue;
1704
1705 bool precise = true, vacuous = true;
1706
1707 for (auto entry = (*cpt_ptr)[node].cbegin(), theEnd2 = (*cpt_ptr)[node].cend();
1708 entry != theEnd2;
1709 ++entry) {
1710 auto vertices = entry->size();
1711 auto var_dSize = (*entry)[0].size();
1712
1713 if (precise && vertices > 1) precise = false;
1714
1715 if (vacuous && vertices == var_dSize) {
1717
1718 for (auto vertex = entry->cbegin(), vEnd = entry->cend(); vertex != vEnd; ++vertex) {
1719 for (auto probability = vertex->cbegin(), pEnd = vertex->cend(); probability != pEnd;
1720 ++probability) {
1721 if (*probability == 1) {
1722 elem[probability - vertex->begin()] = true;
1723 break;
1724 }
1725 } // end of : for each modality
1726 } // end of : for each vertex
1727
1728 for (auto probability = elem.cbegin(); probability != elem.cend(); ++probability)
1729 if (*probability == false) vacuous = false;
1730 } // end of : if vertices == dSize
1731 else
1732 vacuous = false;
1733
1734 if (vacuous == false && precise == false) {
1736 break;
1737 }
1738 } // end of : for each parents entry
1739
1741 else if (precise) nodeType_ptr->insert(node, NodeType::Precise);
1742 } // end of : for each variable
1743 }
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition credalNet.h:612

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 82 of file credalNet_tpl.h.

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

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 66 of file credalNet_tpl.h.

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

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 957 of file credalNet_tpl.h.

957 {
958 // don't forget to delete the old one ( _current_), if necessary at the end
959 auto bin_bn = new BayesNet< GUM_SCALAR >();
960
961 // __bnCopy ( * _bin_bn_ );
962 // delete old one too
965
966 // delete old one too
968
972
973 if (this->_current_bn_ == nullptr) current_bn = &this->_src_bn_;
974 else current_bn = this->_current_bn_;
975
976 if (this->_credalNet_current_cpt_ == nullptr)
979
980 if (!_var_bits_.empty()) _var_bits_.clear();
981
982 bin_bn->beginTopologyTransformation();
983
984 for (auto node: current_bn->nodes()) {
985 auto var_dSize = current_bn->variable(node).domainSize();
986
987 if (var_dSize != 2) {
988 unsigned long b;
989 unsigned long c;
990 superiorPow(static_cast< unsigned long >(var_dSize), b, c);
991 Size nb_bits{b};
992
995
996 for (Size bit = 0; bit < nb_bits; bit++) {
997 bit_name = current_bn->variable(node).name() + "-b";
999
1001 NodeId iD = bin_bn->add(var_bit);
1002
1003 bits[bit] = iD;
1004 } // end of : for each bit
1005
1006 _var_bits_.insert(node, bits);
1007 } // end of : if variable is not binary
1008 else {
1009 const std::string bit_name = current_bn->variable(node).name();
1011 const NodeId iD = bin_bn->add(var_bit);
1012
1014 }
1015 } // end of : for each original variable
1016
1017 for (auto node: current_bn->nodes()) {
1018 if (NodeSet parents = current_bn->parents(node); !parents.empty()) {
1019 for (auto par: current_bn->parents(node)) {
1020 for (Size parent_bit = 0, spbits = static_cast< Size >(_var_bits_[par].size());
1022 parent_bit++)
1023 for (Size var_bit = 0, mbits = static_cast< Size >(_var_bits_[node].size());
1024 var_bit < mbits;
1025 var_bit++)
1027 }
1028 }
1029
1030 // arcs with one's bits
1031 const auto bitsize = _var_bits_[node].size();
1032
1033 for (Size bit_c = 1; bit_c < bitsize; bit_c++)
1034 for (Size bit_p = 0; bit_p < bit_c; bit_p++)
1036 } // end of : for each original variable
1037
1038 bin_bn->endTopologyTransformation();
1039
1040 // binarization of cpts
1041
1042 const auto varsize = current_bn->size();
1043
1044 for (Size var = 0; var < varsize; var++) {
1045 const auto bitsize = _var_bits_[var].size();
1046
1047 for (Size i = 0; i < bitsize; i++) {
1050 ins.setFirst();
1051
1052 auto entry_size = tensor->domainSize() / 2;
1054
1055 Size old_conf = 0;
1056
1057 for (Size conf = 0; conf < entry_size; conf++) {
1059 auto verticessize = (*credalNet_current_cpt)[var][old_conf].size();
1060
1063 = (*credalNet_current_cpt)[var][old_conf][old_distri];
1064 auto vertexsize = vertex.size();
1065
1067
1068 for (Size preced = 0; preced < i; preced++) {
1069 auto bit_pos = ins.pos(bin_bn->variable(_var_bits_[var][preced]));
1070 auto val = ins.val(bit_pos);
1071
1073 Size elem;
1074
1075 if (val == 0) elem = 0;
1076 else elem = pas;
1077
1078 while (elem < vertexsize) {
1079 incc[elem]++;
1080 elem++;
1081
1082 if (elem % pas == 0) elem += pas;
1083 }
1084 }
1085
1086 Size pas = Size(int2Pow(i));
1087
1089 int pos = 1;
1090
1091 for (Size elem = 0; elem < vertexsize; elem++) {
1092 if (elem % pas == 0) pos = -pos;
1093
1094 if (incc[elem] == i)
1095 (pos < 0) ? (distri[0] += vertex[elem]) : (distri[1] += vertex[elem]);
1096 }
1097
1098 if (i > 0) {
1099 GUM_SCALAR den = distri[0] + distri[1];
1100
1101 if (den == 0) {
1102 distri[0] = 0;
1103 distri[1] = 0;
1104 } else {
1105 distri[0] /= den;
1106 distri[1] /= den;
1107 }
1108 }
1109
1110 pvar_cpt.push_back(distri);
1111 } // end of old distris
1112
1113 // get min/max approx, 2 vertices
1115 vertices[1][1] = 0;
1116
1117 const auto new_verticessize = pvar_cpt.size();
1118
1119 for (Size v = 0; v < new_verticessize; v++) {
1120 if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1121
1122 if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1123 }
1124
1125 vertices[0][0] = 1 - vertices[0][1];
1126 vertices[1][0] = 1 - vertices[1][1];
1127
1129
1131
1132 ++ins;
1133 ++ins;
1134
1135 old_conf++;
1136
1138 } // end of new parent conf
1139
1141 } // end of bit i
1142 } // end of old variable
1143
1144 bin_bn->beginTopologyTransformation();
1145
1146 /* indicatrices variables */
1147 const auto old_varsize = _var_bits_.size();
1148
1149 for (Size i = 0; i < old_varsize; i++) {
1150 auto bitsize = _var_bits_[i].size();
1151
1152 // binary variable
1153 if (bitsize == 1) continue;
1154
1155 auto old_card = _src_bn_.variable(i).domainSize();
1156
1157 for (Size mod = 0; mod < old_card; mod++) {
1158 auto ss_str = std::format("{}-v{}", _src_bn_.variable(i).name(), mod);
1159
1160 LabelizedVariable var(ss_str, "node " + ss_str, 2);
1161 const NodeId indic = bin_bn->add(var);
1162
1163 // arcs from one's bits
1164 for (Size bit = 0; bit < bitsize; bit++)
1166
1167 // cpt
1168 Size num = Size(int2Pow(long(bitsize)));
1169
1171
1172 for (Size entry = 0; entry < num; entry++) {
1174
1175 if (mod == entry) vertices[0][1] = 1;
1176 else vertices[0][0] = 1;
1177
1178 icpt[entry] = vertices;
1179 }
1180
1181 credalNet_bin_cpt->insert(indic, icpt);
1182
1184 } // end of each modality, i.e. as many indicatrice
1185 }
1186
1187 bin_bn->endTopologyTransformation();
1188
1189 if (this->_current_bn_ != nullptr) delete this->_current_bn_;
1190
1191 this->_current_bn_ = bin_bn;
1192
1193 if (this->_credalNet_current_cpt_ != nullptr) delete this->_credalNet_current_cpt_;
1194
1196
1197 if (this->_current_nodeType_ != nullptr) delete this->_current_nodeType_;
1198
1200
1201 _sort_varType_(); // will fill _bin_nodeType_ except for NodeType::Indic
1202 // variables
1203
1205 }
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:609
const BayesNet< GUM_SCALAR > & current_bn() const
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
uint64_t int2Pow(uint64_t exponent)
Specialized base 2 pow function with integer.
Definition pow_inl.h:70
void superiorPow(unsigned long card, unsigned long &num_bits, unsigned long &new_card)
Compute the superior and closest power of two of an integer.
Definition pow_inl.h:79

References _credalNet_current_cpt_, _credalNet_src_cpt_, _current_bn_, _current_nodeType_, _sort_varType_(), _src_bn_, _var_bits_, 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 442 of file credalNet_tpl.h.

442 {
443 this->bnToCredal(beta, oneNet, false);
444 }
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 447 of file credalNet_tpl.h.

447 {
448 GUM_SCALAR epsi_min = 1.;
449 GUM_SCALAR epsi_max = 0.;
450 GUM_SCALAR epsi_moy = 0.;
451 GUM_SCALAR epsi_den = 0.;
452
453 for (auto node: src_bn().nodes()) {
454 const Tensor< GUM_SCALAR >* const tensor(&_src_bn_.cpt(node));
455
457 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_min_.cpt(node)));
459 const_cast< Tensor< GUM_SCALAR >* const >(&_src_bn_max_.cpt(node)));
460
461 Size var_dSize = _src_bn_.variable(node).domainSize();
463
467
468 ins.setFirst();
469 ins_min.setFirst();
470 ins_max.setFirst();
471
473
474 for (Size entry = 0; entry < entry_size; entry++) {
476
477 if (oneNet) den = 0;
478 else den = tensor_max->get(ins_max);
479
480 Size nbm = 0;
481
482 for (Size modality = 0; modality < var_dSize; modality++) {
483 vertex[modality] = tensor->get(ins);
484
485 if (oneNet) {
486 den += vertex[modality];
487
488 if (vertex[modality] < 1 && vertex[modality] > 0)
490 "bnToCredal : the BayesNet contains "
491 "probabilities and not event counts "
492 "although user precised oneNet = "
493 << oneNet);
494 }
495
496 if (vertex[modality] > 0) nbm++;
497
498 ++ins;
499 }
500
502 if (!oneNet) {
503 GUM_SCALAR sum = 0;
504
505 for (auto modality = vertex.cbegin(), theEnd = vertex.cend(); modality != theEnd;
506 ++modality) {
507 sum += *modality;
508 }
509
510 if (std::fabs(1. - sum) > _epsRedund_) {
512 _src_bn_.variable(node).name()
513 << "(" << _epsRedund_ << ") does not sum to one for" << " " << entry
514 << std::endl
515 << vertex << std::endl
516 << ins << std::endl);
517 }
518 }
519
521
523
524 if (beta == 0) epsilon = 0;
525 else if (den == 0 || beta == 1) epsilon = GUM_SCALAR(1.0);
527
528 epsi_moy += epsilon;
529 epsi_den += 1;
530
532
534
536
537 for (Size modality = 0; modality < var_dSize; modality++) {
538 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
539 min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
540
541 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
542
544 } else {
545 // if ( ( vertex[modality] == 0 && keepZeroes ) || (
546 // vertex[modality] > 0 && nbm <= 1 ) || ( vertex[modality] == 0
547 // && nbm <= 1 ) ) {
549
550 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
551
552 max = min;
553 }
554
555 tensor_min->set(ins_min, min);
556 tensor_max->set(ins_max, max);
557
558 ++ins_min;
559 ++ins_max;
560 } // end of : for each modality
561 } // end of : for each entry
562 } // end of : for each variable
563
567
569 }
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 1247 of file credalNet_tpl.h.

1247 {
1248 // ASSUMPTION: NodeIds are dense 0..N-1 (no nodes ever removed from the BN).
1249 // If sparse NodeIds are introduced, indexing by node will go out of bounds.
1250 _binCptMin_.resize(current_bn().size());
1251 _binCptMax_.resize(current_bn().size());
1252
1253 for (auto node: current_bn().nodes()) {
1254 auto pConf = credalNet_currentCpt()[node].size();
1257
1258 for (Size pconf = 0; pconf < pConf; pconf++) {
1259 GUM_SCALAR v1, v2;
1260 v1 = credalNet_currentCpt()[node][pconf][0][1];
1261
1262 if (credalNet_currentCpt()[node][pconf].size() > 1)
1263 v2 = credalNet_currentCpt()[node][pconf][1][1];
1264 else v2 = v1;
1265
1266 GUM_SCALAR delta = v1 - v2;
1267 min[pconf] = (delta >= 0) ? v2 : v1;
1268 max[pconf] = (delta >= 0) ? v1 : v2;
1269 }
1270
1271 _binCptMin_[node] = min;
1272 _binCptMax_[node] = max;
1273 }
1274
1276 }
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:625
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition credalNet.h:633

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 1209 of file credalNet_tpl.h.

1209 {
1210 if (_credalNet_current_cpt_ != nullptr) return *_credalNet_current_cpt_;
1211
1212 return _credalNet_src_cpt_;
1213 }

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 1217 of file credalNet_tpl.h.

1217 {
1218 return _credalNet_src_cpt_;
1219 }

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 1337 of file credalNet_tpl.h.

1337 {
1338 if (_current_bn_ != nullptr) return *_current_bn_;
1339
1340 return _src_bn_;
1341 }

References _current_bn_, and _src_bn_.

Referenced by approximatedBinarization(), and computeBinaryCPTMinMax().

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 1223 of file credalNet_tpl.h.

1223 {
1224 if (_current_nodeType_ != nullptr) return (*(_current_nodeType_))[id];
1225
1226 return _original_nodeType_[id];
1227 }

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 406 of file credalNet_tpl.h.

406 {
407 return _src_bn_.variable(id).domainSize();
408 }

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 1296 of file credalNet_tpl.h.

1296 {
1297 return _epsilonMax_;
1298 }

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 1301 of file credalNet_tpl.h.

1301 {
1302 return _epsilonMoy_;
1303 }

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 1291 of file credalNet_tpl.h.

1291 {
1292 return _epsilonMin_;
1293 }

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 287 of file credalNet_tpl.h.

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

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 334 of file credalNet_tpl.h.

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

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

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 1286 of file credalNet_tpl.h.

1286 {
1287 return _binCptMax_;
1288 }

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 1280 of file credalNet_tpl.h.

1280 {
1281 return _binCptMin_;
1282 }

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 1241 of file credalNet_tpl.h.

1241 {
1243 }

References _hasComputedBinaryCPTMinMax_.

◆ 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 616 of file credalNet_tpl.h.

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

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 401 of file credalNet_tpl.h.

401 {
402 return Instantiation(_src_bn_.cpt(id));
403 }

References _src_bn_.

◆ 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 781 of file credalNet_tpl.h.

781 {
782 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
783
784 _credalNet_src_cpt_.resize(_src_bn_.size());
785
787
788 for (auto node: _src_bn_.nodes()) {
791
792 Size var_dSize = _src_bn_.variable(node).domainSize();
794
796
799
800 ins_min.setFirst();
801 ins_max.setFirst();
802
803 lrsWrapper.setUpH(var_dSize);
804
805 for (Size entry = 0; entry < entry_size; entry++) {
806 for (Size modality = 0; modality < var_dSize; modality++) {
807 if (tensor_min->get(ins_min) > tensor_max->get(ins_max)) {
809 "For variable "
810 << _src_bn_.variable(node).name() << " (at " << ins_min
811 << "), the min is greater than the max : " << tensor_min->get(ins_min)
812 << ">" << tensor_max->get(ins_max) << ".");
813 }
815 ++ins_min;
816 ++ins_max;
817 }
818
819 lrsWrapper.H2V();
820 var_cpt[entry] = lrsWrapper.getOutput();
821 lrsWrapper.nextHInput();
822 }
823
825 } // end of : for each variable (node)
826
827 // get precise/credal/vacuous status of each variable
830 }

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 834 of file credalNet_tpl.h.

834 {
835 if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
836
837 _credalNet_src_cpt_.resize(_src_bn_.size());
838
839 for (auto node: _src_bn_.nodes()) {
842
843 auto var_dSize = _src_bn_.variable(node).domainSize();
845
847
850
851 ins_min.setFirst();
852 ins_max.setFirst();
853
854 // use iterator
855 for (Size entry = 0; entry < entry_size; entry++) {
857 std::vector< GUM_SCALAR > vertex(var_dSize); // if not interval
858
860 var_dSize * 2,
862
865 sum_ineq1[0] = 1;
866 sum_ineq2[0] = -1;
867
868 bool isInterval = false;
869
870 for (Size modality = 0; modality < var_dSize; modality++) {
871 inequalities[modality * 2][0] = -tensor_min->get(ins_min);
872 inequalities[modality * 2 + 1][0] = tensor_max->get(ins_max);
873 inequalities[modality * 2][modality + 1] = 1;
874 inequalities[modality * 2 + 1][modality + 1] = -1;
875
876 vertex[modality] = inequalities[modality * 2 + 1][0];
877
878 if (!isInterval
879 && (-inequalities[modality * 2][0] != inequalities[modality * 2 + 1][0]))
880 isInterval = true;
881
882 ++ins_min;
883 ++ins_max;
884 }
885
886 inequalities.push_back(sum_ineq1);
887 inequalities.push_back(sum_ineq2);
888
889 if (!isInterval) {
890 vertices.push_back(vertex);
891 } else {
892 try {
894 // __H2Vcdd ( inequalities, vertices );
895 } catch (const std::exception& err) {
896 std::cout << err.what() << std::endl;
897 throw;
898 }
899 } // end of : is interval
900
901 if (entry == 0 && vertices.size() >= 2) {
902 auto tmp = vertices[0];
903 vertices[0] = vertices[1];
904 vertices[1] = tmp;
905 }
906
908 } // end of : for each entry
909
911 // std::cout << _src_bn_.variable(node_idIt).name() << std::endl;
912 // std::cout << var_cpt << std::endl;
913 } // end of : for each variable (node)
914
915 // get precise/credal/vacuous status of each variable
918 }
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 1236 of file credalNet_tpl.h.

1236 {
1237 return _separatelySpecified_;
1238 }

References _separatelySpecified_.

◆ 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 572 of file credalNet_tpl.h.

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

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 1231 of file credalNet_tpl.h.

1231 {
1232 return _original_nodeType_[id];
1233 }

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 925 of file credalNet_tpl.h.

926 {
928
929 std::string minfilename(min_path); //"min.bif";
930 std::string maxfilename(max_path); //"max.bif";
933
934 if (!min_file.good())
935 GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << minfilename);
936
937 if (!max_file.good()) {
938 min_file.close();
939 GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << maxfilename);
940 }
941
942 try {
945 } catch (Exception& err) {
947 min_file.close();
948 max_file.close();
949 throw(err);
950 }
951
952 min_file.close();
953 max_file.close();
954 }
#define GUM_SHOWERROR(e)
Definition exceptions.h:89

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 133 of file credalNet_tpl.h.

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

References _credalNet_src_cpt_, _src_bn_, 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 186 of file credalNet_tpl.h.

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

References _credalNet_src_cpt_, _src_bn_, 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 89 of file credalNet_tpl.h.

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

References _credalNet_src_cpt_, _src_bn_, 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 1344 of file credalNet_tpl.h.

1344 {
1345 return _src_bn_;
1346 }

References _src_bn_.

Referenced by bnToCredal().

Here is the caller 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 1306 of file credalNet_tpl.h.

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

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 633 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 625 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 606 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 599 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 614 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 553 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 580 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 576 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 562 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 558 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 566 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 571 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 618 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 612 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 583 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 551 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 588 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 609 of file credalNet.h.

Referenced by approximatedBinarization().


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