aGrUM  0.20.3
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 Member Functions

void saveBNsMinMax (const std::string &min_path, const std::string &max_path) const
 If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet. More...
 
std::string toString () const
 
void computeBinaryCPTMinMax ()
 Used with binary networks to speed-up L2U inference. More...
 
Constructors / Destructors
 CredalNet ()
 Constructor used to create a CredalNet step by step, i.e. More...
 
 CredalNet (const std::string &src_min_num, const std::string &src_max_den="")
 Constructor for interval defined credal network which takes 2 BayesNet file path. More...
 
 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. More...
 
 ~CredalNet ()
 Destructor. More...
 
Credal network creation
NodeId addVariable (const std::string &name, const Size &card)
 Adds a discrete node into the network. More...
 
void addArc (const NodeId &tail, const NodeId &head)
 Adds an arc between two nodes. More...
 
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 More...
 
void setCPT (const NodeId &id, Size &entry, const std::vector< std::vector< GUM_SCALAR > > &cpt)
 Set the vertices of one credal set of a given node ( any instantiation index ) More...
 
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 ) More...
 
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 ) More...
 
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 ) More...
 
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 ) More...
 
Instantiation instantiation (const NodeId &id)
 Get an Instantiation from a node id, usefull to fill the constraints of the network More...
 
Size domainSize (const NodeId &id)
 Get the cardinality of a node More...
 
Public manipulation methods
void bnToCredal (const GUM_SCALAR beta, const bool oneNet, const bool keepZeroes=false)
 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. More...
 
void intervalToCredalWithFiles ()
 
void intervalToCredal ()
 Computes the vertices of each credal set according to their interval definition (uses lrs). More...
 
void lagrangeNormalization ()
 Normalize counts of a BayesNet storing counts of each events such that no probability is 0. More...
 
void idmLearning (const Idx s=0, const bool keepZeroes=false)
 Learns parameters from a BayesNet storing counts of events. More...
 
void approximatedBinarization ()
 Approximate binarization. More...
 
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
 
const bool isSeparatelySpecified () const
 
const bool hasComputedBinaryCPTMinMax () const
 
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_min () const
 Used with binary networks to speed-up L2U inference. More...
 
const std::vector< std::vector< GUM_SCALAR > > & get_binaryCPT_max () const
 Used with binary networks to speed-up L2U inference. More...
 

Public Types

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

Detailed Description

template<typename 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()

Definition at line 88 of file credalNet.h.

Member Enumeration Documentation

◆ NodeType

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

NodeType to speed-up computations in some algorithms.

Enumerator
Precise 
Credal 
Vacuous 
Indic 

Definition at line 91 of file credalNet.h.

91  : char
92  {
93  Precise,
94  Credal,
95  Vacuous,
96  Indic
97  };

Constructor & Destructor Documentation

◆ CredalNet() [1/3]

template<typename 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 potentials.

Definition at line 31 of file credalNet_tpl.h.

31  {
32  _initParams_();
33 
34  _src_bn_ = BayesNet< GUM_SCALAR >();
35  _src_bn_min_ = BayesNet< GUM_SCALAR >();
36  _src_bn_max_ = BayesNet< GUM_SCALAR >();
37 
38  GUM_CONSTRUCTOR(CredalNet);
39  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
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:590

◆ CredalNet() [2/3]

template<typename GUM_SCALAR >
gum::credal::CredalNet< GUM_SCALAR >::CredalNet ( const std::string &  src_min_num,
const std::string &  src_max_den = "" 
)

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

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 path to a BayesNet which contains lower probabilities.
src_max_denThe ( optional ) path to a BayesNet which contains upper probabilities.

Definition at line 390 of file credalNet_tpl.h.

391  {
392  _initParams_();
393  _initCNNets_(src_min_num, src_max_den);
394 
395  GUM_CONSTRUCTOR(CredalNet);
396  }
void _initCNNets_(const std::string &src_min_num, const std::string &src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
void _initParams_()
Initialize private constant variables after the Constructor has been called.

◆ CredalNet() [3/3]

template<typename 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 399 of file credalNet_tpl.h.

400  {
401  _initParams_();
402  _initCNNets_(src_min_num, src_max_den);
403 
404  GUM_CONSTRUCTOR(CredalNet);
405  }
void _initCNNets_(const std::string &src_min_num, const std::string &src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
void _initParams_()
Initialize private constant variables after the Constructor has been called.

◆ ~CredalNet()

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

Destructor.

Definition at line 408 of file credalNet_tpl.h.

408  {
409  if (_current_bn_ != nullptr) delete _current_bn_;
410 
411  if (_credalNet_current_cpt_ != nullptr) delete _credalNet_current_cpt_;
412 
413  if (_current_nodeType_ != nullptr) delete _current_nodeType_;
414 
415  GUM_DESTRUCTOR(CredalNet);
416  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:608

Member Function Documentation

◆ _bnCopy_()

template<typename 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 1446 of file credalNet_tpl.h.

1446  {
1447  const BayesNet< GUM_SCALAR >* _current_bn_;
1448 
1449  if (this->_current_bn_ == nullptr)
1450  _current_bn_ = &this->_src_bn_;
1451  else
1452  _current_bn_ = this->_current_bn_;
1453 
1454  for (auto node: _current_bn_->nodes())
1455  dest.add(_current_bn_->variable(node));
1456 
1457  dest.beginTopologyTransformation();
1458 
1459  for (auto node: _current_bn_->nodes()) {
1460  for (auto parent_idIt: _current_bn_->cpt(node).variablesSequence()) {
1461  if (_current_bn_->nodeId(*parent_idIt) != node)
1462  dest.addArc(_current_bn_->nodeId(*parent_idIt), node);
1463  } // end of : for each parent in order of appearence
1464  } // end of : for each variable
1465 
1466  dest.endTopologyTransformation();
1467  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585

◆ _find_dNode_card_()

template<typename 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 1434 of file credalNet_tpl.h.

1435  {
1436  Size vertices_size = 0;
1437 
1438  for (auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd; ++entry) {
1439  if (entry->size() > vertices_size) vertices_size = Size(entry->size());
1440  }
1441 
1442  return int(vertices_size);
1443  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ _H2Vlrs_()

template<typename 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 1522 of file credalNet_tpl.h.

1523  {
1524  // write H rep file
1525  int64_t num, den;
1526 
1527  std::string sinefile = getUniqueFileName(); // generate unique file name, we
1528  // need to add .ine or .ext for lrs
1529  // to know which input it is (Hrep
1530  // to Vrep or Vrep to Hrep)
1531  sinefile += ".ine";
1532 
1533  std::ofstream h_file(sinefile.c_str(), std::ios::out | std::ios::trunc);
1534 
1535  if (!h_file.good())
1536  GUM_ERROR(IOError, " __H2Vlrs : could not open lrs input file : " << sinefile)
1537 
1538  h_file << "H - representation\n";
1539  h_file << "begin\n";
1540  h_file << h_rep.size() << ' ' << h_rep[0].size() << " rational\n";
1541 
1542  for (auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1543  for (auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2; ++it2) {
1544  // get integer fraction from decimal value
1545  // smallest numerator & denominator is farley, also
1546  // best precision
1548  den,
1549  ((*it2 > 0) ? *it2 : -*it2),
1550  int64_t(_denMax_),
1551  _epsF_);
1552 
1553  h_file << ((*it2 > 0) ? num : -num) << '/' << den << ' ';
1554  }
1555 
1556  h_file << '\n';
1557  }
1558 
1559  h_file << "end\n";
1560  h_file.close();
1561 
1562  // call lrs
1563  // lrs arguments
1564  char* args[3];
1565 
1566  std::string soft_name = "lrs";
1567  std::string extfile(sinefile);
1568  extfile += ".ext";
1569 
1570  args[0] = new char[soft_name.size()];
1571  args[1] = new char[sinefile.size()];
1572  args[2] = new char[extfile.size()];
1573 
1574  strcpy(args[0], soft_name.c_str());
1575  strcpy(args[1], sinefile.c_str());
1576  strcpy(args[2], extfile.c_str());
1577 
1578  // standard cout to null (avoid lrs flooding)
1579  int old_cout, new_cout;
1580  fflush(stdout);
1581  old_cout = dup(1);
1582 
1583  new_cout = open("/dev/null", O_WRONLY);
1584  dup2(new_cout, 1);
1585  close(new_cout);
1586 
1587  lrs_main(3, args);
1588 
1589  // restore standard cout
1590  fflush(stdout);
1591  dup2(old_cout, 1);
1592  close(old_cout);
1593 
1594  delete[] args[2];
1595  delete[] args[1];
1596  delete[] args[0];
1597 
1598  // read V rep file
1599  std::ifstream v_file(extfile.c_str() /*extfilename.c_str()*/, std::ios::in);
1600 
1601  if (!v_file.good()) GUM_ERROR(IOError, " __H2Vlrs : could not open lrs ouput file : ")
1602 
1603  std::string line, tmp;
1604  char * cstr, *p;
1605  GUM_SCALAR probability;
1606 
1607  std::string::size_type pos;
1608  bool keep_going = true;
1609  // int vertices;
1610 
1611  std::vector< GUM_SCALAR > vertex;
1612 
1613  v_file.ignore(256, 'l');
1614 
1615  while (v_file.good() && keep_going) {
1616  getline(v_file, line);
1617 
1618  if (line.size() == 0)
1619  continue;
1620  else if (line.compare("end") == 0) {
1621  keep_going = false;
1622  // this is to get vertices number :
1623  /*getline ( v_file, line );
1624  std::string::size_type pos, end_pos;
1625  pos = line.find ( "vertices = " );
1626  end_pos = line.find ( "rays", pos + 9 );
1627  vertices = atoi ( line.substr ( pos + 9, end_pos - pos - 9 ).c_str()
1628  );*/
1629  break;
1630  } else if (line[1] != '1') {
1631  GUM_ERROR(IOError,
1632  " __H2Vlrs : reading something other than a vertex from "
1633  "lrs output file : ");
1634  }
1635 
1636  line = line.substr(2);
1637  cstr = new char[line.size() + 1];
1638  strcpy(cstr, line.c_str());
1639 
1640  p = strtok(cstr, " ");
1641 
1642  while (p != nullptr) {
1643  tmp = p;
1644 
1645  if (tmp.compare("1") == 0 || tmp.compare("0") == 0)
1646  probability = GUM_SCALAR(atof(tmp.c_str()));
1647  else {
1648  pos = tmp.find("/");
1649  probability = GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1650  / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1651  }
1652 
1653  vertex.push_back(probability);
1654  p = strtok(nullptr, " ");
1655  } // end of : for all tokens
1656 
1657  delete[] p;
1658  delete[] cstr;
1659 
1660  bool is_redund = false;
1661 
1662 #pragma omp parallel
1663  {
1664  int this_thread = getThreadNumber();
1665  int num_threads = getNumberOfRunningThreads();
1666 
1667  auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1668  auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1669 
1670  for (auto p = begin_pos; p < end_pos; p++) {
1671 #pragma omp flush(is_redund)
1672 
1673  if (is_redund) break;
1674 
1675  bool thread_redund = true;
1676 
1677  auto vsize = vertex.size();
1678 
1679  for (Size modality = 0; modality < vsize; modality++) {
1680  if (std::fabs(vertex[modality] - v_rep[p][modality]) > _epsRedund_) {
1681  thread_redund = false;
1682  break;
1683  }
1684  }
1685 
1686  if (thread_redund) {
1687  is_redund = true;
1688 #pragma omp flush(is_redund)
1689  }
1690  } // end of : each thread for
1691  } // end of : parallel
1692 
1693  if (!is_redund) v_rep.push_back(vertex);
1694 
1695  vertex.clear();
1696 
1697  } // end of : file
1698 
1699  v_file.close();
1700 
1701  if (std::remove(sinefile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + sinefile)
1702 
1703  if (std::remove(extfile.c_str()) != 0) GUM_ERROR(IOError, "error removing : " + extfile)
1704  }
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
GUM_SCALAR _epsF_
Value under which a decimal number is considered to be zero when using farey.
Definition: credalNet.h:570
unsigned int getThreadNumber()
Get the calling thread id.
std::string getUniqueFileName()
Returns a path to a unique file name.
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...
Definition: rational_tpl.h:35
GUM_SCALAR _denMax_
Highest possible denominator allowed when using farey.
Definition: credalNet.h:574
GUM_SCALAR _epsRedund_
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:565
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ _initCNNets_() [1/2]

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::_initCNNets_ ( const std::string &  src_min_num,
const std::string &  src_max_den 
)
private

Initialize private BayesNet variables after the Constructor has been called.

Definition at line 1403 of file credalNet_tpl.h.

1404  {
1405  BIFReader< GUM_SCALAR > reader(&_src_bn_, src_min_num);
1406  std::string other;
1407 
1408  if (src_max_den.compare("") != 0)
1409  other = src_max_den;
1410  else
1411  other = src_min_num;
1412 
1413  BIFReader< GUM_SCALAR > reader_min(&_src_bn_min_, src_min_num);
1414  BIFReader< GUM_SCALAR > reader_max(&_src_bn_max_, other);
1415 
1416  reader.proceed();
1417  reader_min.proceed();
1418  reader_max.proceed();
1419  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ _initCNNets_() [2/2]

template<typename 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 1422 of file credalNet_tpl.h.

1423  {
1424  _src_bn_ = src_min_num;
1425  _src_bn_min_ = src_min_num;
1426 
1427  if (src_max_den.size() > 0)
1428  _src_bn_max_ = src_max_den;
1429  else
1430  _src_bn_max_ = src_min_num;
1431  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ _initParams_()

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

Initialize private constant variables after the Constructor has been called.

Definition at line 1376 of file credalNet_tpl.h.

1376  {
1377  _epsilonMin_ = 0;
1378  _epsilonMax_ = 0;
1379  _epsilonMoy_ = 0;
1380 
1381  _epsRedund_ = GUM_SCALAR(1e-6);
1382 
1383  // farey algorithm
1384  _epsF_ = GUM_SCALAR(1e-6);
1385  _denMax_ = GUM_SCALAR(1e6); // beware LRSWrapper
1386 
1387  // continued fractions, beware LRSWrapper
1388  // decimal paces ( _epsC_ * _precisionC_ == 1)
1389  _precisionC_ = GUM_SCALAR(1e6);
1390  _deltaC_ = 5;
1391 
1392  // old custom algorithm
1393  _precision_ = GUM_SCALAR(1e6); // beware LRSWrapper
1394 
1395  _current_bn_ = nullptr;
1396  _credalNet_current_cpt_ = nullptr;
1397  _current_nodeType_ = nullptr;
1398 
1400  }
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:552
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
GUM_SCALAR _precisionC_
1e6 by default, used by fracC as precision.
Definition: credalNet.h:545
GUM_SCALAR _epsF_
Value under which a decimal number is considered to be zero when using farey.
Definition: credalNet.h:570
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
GUM_SCALAR _deltaC_
5 by default, used by fracC as number of decimals.
Definition: credalNet.h:547
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:556
bool _hasComputedBinaryCPTMinMax_
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:612
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:560
GUM_SCALAR _denMax_
Highest possible denominator allowed when using farey.
Definition: credalNet.h:574
GUM_SCALAR _epsRedund_
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:565
GUM_SCALAR _precision_
Precision used by frac.
Definition: credalNet.h:577
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:608

◆ _intervalToCredal_()

template<typename 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 674 of file credalNet_tpl.h.

674  {
675  if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
676 
677  _credalNet_src_cpt_.resize(_src_bn_.size());
678 
679  for (auto node: _src_bn_.nodes()) {
680  const Potential< GUM_SCALAR >* const potential_min(&_src_bn_min_.cpt(node));
681  const Potential< GUM_SCALAR >* const potential_max(&_src_bn_max_.cpt(node));
682 
683  Size var_dSize = _src_bn_.variable(node).domainSize();
684  Size entry_size = potential_min->domainSize() / var_dSize;
685 
686  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(entry_size);
687 
688  Instantiation ins_min(potential_min);
689  Instantiation ins_max(potential_max);
690 
691  ins_min.setFirst();
692  ins_max.setFirst();
693 
694  std::vector< GUM_SCALAR > lower(var_dSize);
695  std::vector< GUM_SCALAR > upper(var_dSize);
696 
697  for (Size entry = 0; entry < entry_size; entry++) {
698  for (Size modality = 0; modality < var_dSize; modality++, ++ins_min, ++ins_max) {
699  lower[modality] = potential_min->get(ins_min);
700  upper[modality] = potential_max->get(ins_max);
701  }
702 
703  bool all_equals = true;
704  std::vector< std::vector< GUM_SCALAR > > vertices;
705 
706  for (Size modality = 0; modality < var_dSize; modality++) {
707  if (std::fabs(upper[modality] - lower[modality]) < 1e-6) continue;
708 
709  all_equals = false;
710  std::vector< GUM_SCALAR > vertex(var_dSize);
711  vertex[modality] = upper[modality];
712 
713  for (Size mod = 0; mod < var_dSize; mod++) {
714  if (modality != mod) vertex[mod] = lower[mod];
715  }
716 
717  GUM_SCALAR total = 0;
718 
719  auto vsize = vertex.size();
720 
721  for (Size i = 0; i < vsize; i++)
722  total += vertex[i];
723 
724  if (std::fabs(total - 1.) > 1e-6)
725  GUM_ERROR(CPTNoSumTo1,
726  _src_bn_.variable(node).name() << " " << entry << std::endl
727  << vertex << std::endl);
728 
729  vertices.push_back(vertex);
730  }
731 
732  if (all_equals) {
733  std::vector< GUM_SCALAR > vertex(var_dSize);
734 
735  for (Size modality = 0; modality < var_dSize; modality++)
736  vertex[modality] = lower[modality];
737 
738  GUM_SCALAR total = 0.;
739 
740  auto vsize = vertex.size();
741 
742  for (Size i = 0; i < vsize; i++)
743  total += vertex[i];
744 
745  if (std::fabs(total - 1.) > 1e-6)
746  GUM_ERROR(CPTNoSumTo1,
747  _src_bn_.variable(node).name() << " " << entry << std::endl
748  << vertex << std::endl);
749 
750  vertices.push_back(vertex);
751  }
752 
753  var_cpt[entry] = vertices;
754  }
755 
756  _credalNet_src_cpt_.insert(node, var_cpt);
757 
758  } // end of : for each variable (node)
759 
760  // get precise/credal/vacuous status of each variable
761  _sort_varType_();
762  _separatelySpecified_ = true;
763  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:582
void _sort_varType_()
Set the NodeType of each node
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ _sort_varType_()

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

Set the NodeType of each node

Definition at line 1707 of file credalNet_tpl.h.

1707  {
1708  NodeProperty< NodeType >* _current_nodeType_;
1709  const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1711 
1712  const BayesNet< GUM_SCALAR >* _current_bn_;
1713 
1714  if (this->_current_bn_ == nullptr)
1715  _current_bn_ = &_src_bn_;
1716  else
1717  _current_bn_ = this->_current_bn_;
1718 
1719  if (this->_credalNet_current_cpt_ == nullptr)
1720  _credalNet_current_cpt_ = &_credalNet_src_cpt_;
1721  else
1722  _credalNet_current_cpt_ = this->_credalNet_current_cpt_;
1723 
1724  if (this->_current_nodeType_ == nullptr)
1725  _current_nodeType_ = &_original_nodeType_;
1726  else
1727  _current_nodeType_ = this->_current_nodeType_;
1728 
1729  /*if ( ! _current_nodeType_->empty() )
1730  _current_nodeType_->clear();*/
1731 
1732  for (auto node: _current_bn_->nodes()) {
1733  // indicatrices are already present
1734  if (_current_nodeType_->exists(node)) continue;
1735 
1736  bool precise = true, vacuous = true;
1737 
1738  for (auto entry = (*_credalNet_current_cpt_)[node].cbegin(),
1739  theEnd2 = (*_credalNet_current_cpt_)[node].cend();
1740  entry != theEnd2;
1741  ++entry) {
1742  auto vertices = entry->size();
1743  auto var_dSize = (*entry)[0].size();
1744 
1745  if (precise && vertices > 1) precise = false;
1746 
1747  if (vacuous && vertices == var_dSize) {
1748  std::vector< bool > elem(var_dSize, false);
1749 
1750  for (auto vertex = entry->cbegin(), vEnd = entry->cend(); vertex != vEnd; ++vertex) {
1751  for (auto probability = vertex->cbegin(), pEnd = vertex->cend(); probability != pEnd;
1752  ++probability) {
1753  if (*probability == 1) {
1754  elem[probability - vertex->begin()] = true;
1755  break;
1756  }
1757  } // end of : for each modality
1758 
1759  break; // not vacuous
1760  } // end of : for each vertex
1761 
1762  for (auto /*std::vector< bool >::const_iterator*/ probability = elem.cbegin();
1763  probability != elem.cend();
1764  ++probability)
1765  if (*probability == false) vacuous = false;
1766 
1767  } // end of : if vertices == dSize
1768  else
1769  vacuous = false;
1770 
1771  if (vacuous == false && precise == false) {
1772  _current_nodeType_->insert(node, NodeType::Credal);
1773  break;
1774  }
1775 
1776  } // end of : for each parents entry
1777 
1778  if (vacuous)
1779  _current_nodeType_->insert(node, NodeType::Vacuous);
1780  else if (precise)
1781  _current_nodeType_->insert(node, NodeType::Precise);
1782 
1783  } // end of : for each variable
1784  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:608
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:606

◆ addArc()

template<typename 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 58 of file credalNet_tpl.h.

58  {
59  _src_bn_.addArc(tail, head);
60  _src_bn_min_.addArc(tail, head);
61  _src_bn_max_.addArc(tail, head);
62  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ addVariable()

template<typename GUM_SCALAR >
NodeId gum::credal::CredalNet< GUM_SCALAR >::addVariable ( const std::string &  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 42 of file credalNet_tpl.h.

42  {
43  LabelizedVariable var(name, "node " + name, card);
44 
45  NodeId a = _src_bn_.add(var);
46  NodeId b = _src_bn_min_.add(var);
47  NodeId c = _src_bn_max_.add(var);
48 
49  if (a != b || a != c /*|| b != c*/)
50  GUM_ERROR(OperationNotAllowed,
51  "addVariable : not the same id over all networks : " << a << ", " << b << ", "
52  << c);
53 
54  return a;
55  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ approximatedBinarization()

template<typename 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 947 of file credalNet_tpl.h.

947  {
948  // don't forget to delete the old one ( _current_), if necessary at the end
949  auto bin_bn = new BayesNet< GUM_SCALAR >();
950 
951  // __bnCopy ( * _bin_bn_ );
952  // delete old one too
953  auto credalNet_bin_cpt
954  = new NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > >();
955 
956  // delete old one too
957  auto bin_nodeType = new NodeProperty< NodeType >();
958 
959  const BayesNet< GUM_SCALAR >* current_bn;
960  // const NodeProperty< nodeType > * _current_nodeType_;
961  const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
962  credalNet_current_cpt;
963 
964  if (this->_current_bn_ == nullptr)
965  current_bn = &this->_src_bn_;
966  else
967  current_bn = this->_current_bn_;
968 
969  if (this->_credalNet_current_cpt_ == nullptr)
970  credalNet_current_cpt = &this->_credalNet_src_cpt_;
971  else
972  credalNet_current_cpt = this->_credalNet_current_cpt_;
973 
974  /*if ( this-> _current_nodeType_ == nullptr )
975  _current_nodeType_ = & this-> _nodeType_;
976  else
977  _current_nodeType_ = this-> _current_nodeType_;*/
978 
979  if (!_var_bits_.empty()) _var_bits_.clear();
980 
981  bin_bn->beginTopologyTransformation();
982 
983  for (auto node: current_bn->nodes()) {
984  auto var_dSize = current_bn->variable(node).domainSize();
985 
986  if (var_dSize != 2) {
987  unsigned long b, c;
988  superiorPow((unsigned long)var_dSize, b, c);
989  Size nb_bits{Size(b)};
990 
991  std::string bit_name;
992  std::vector< NodeId > bits(nb_bits);
993 
994  for (Size bit = 0; bit < nb_bits; bit++) {
995  bit_name = current_bn->variable(node).name() + "-b";
996  std::stringstream ss;
997  ss << bit;
998  bit_name += ss.str();
999 
1000  LabelizedVariable var_bit(bit_name, "node " + bit_name, 2);
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 
1008  } // end of : if variable is not binary
1009  else {
1010  std::string bit_name = current_bn->variable(node).name();
1011  LabelizedVariable var_bit(bit_name, "node " + bit_name, 2);
1012  NodeId iD = bin_bn->add(var_bit);
1013 
1014  _var_bits_.insert(node, std::vector< NodeId >(1, iD));
1015  }
1016 
1017  } // end of : for each original variable
1018 
1019  for (auto node: current_bn->nodes()) {
1020  NodeSet parents = current_bn->parents(node);
1021 
1022  if (!parents.empty()) {
1023  for (auto par: current_bn->parents(node)) {
1024  for (Size parent_bit = 0, spbits = Size(_var_bits_[par].size()); parent_bit < spbits;
1025  parent_bit++)
1026  for (Size var_bit = 0, mbits = Size(_var_bits_[node].size()); var_bit < mbits;
1027  var_bit++)
1028  bin_bn->addArc(_var_bits_[par][parent_bit], _var_bits_[node][var_bit]);
1029  }
1030  }
1031 
1032  // arcs with one's bits
1033  auto bitsize = _var_bits_[node].size();
1034 
1035  for (Size bit_c = 1; bit_c < bitsize; bit_c++)
1036  for (Size bit_p = 0; bit_p < bit_c; bit_p++)
1037  bin_bn->addArc(_var_bits_[node][bit_p], _var_bits_[node][bit_c]);
1038 
1039  } // end of : for each original variable
1040 
1041  bin_bn->endTopologyTransformation();
1042 
1043  // binarization of cpts
1044 
1045  auto varsize = current_bn->size();
1046 
1047  for (Size var = 0; var < varsize; var++) {
1048  auto bitsize = _var_bits_[var].size();
1049 
1050  for (Size i = 0; i < bitsize; i++) {
1051  Potential< GUM_SCALAR > const* potential(&bin_bn->cpt(_var_bits_[var][i]));
1052  Instantiation ins(potential);
1053  ins.setFirst();
1054 
1055  auto entry_size = potential->domainSize() / 2;
1056  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(entry_size);
1057 
1058  Size old_conf = 0;
1059 
1060  for (Size conf = 0; conf < entry_size; conf++) {
1061  std::vector< std::vector< GUM_SCALAR > > pvar_cpt;
1062  auto verticessize = (*credalNet_current_cpt)[var][old_conf].size();
1063 
1064  for (Size old_distri = 0; old_distri < verticessize; old_distri++) {
1065  const std::vector< GUM_SCALAR >& vertex
1066  = (*credalNet_current_cpt)[var][old_conf][old_distri];
1067  auto vertexsize = vertex.size();
1068 
1069  std::vector< Idx > incc(vertexsize, 0);
1070 
1071  for (Size preced = 0; preced < i; preced++) {
1072  auto bit_pos = ins.pos(bin_bn->variable(_var_bits_[var][preced]));
1073  auto val = ins.val(bit_pos);
1074 
1075  Size pas = Size(int2Pow((unsigned long)preced));
1076  Size elem;
1077 
1078  if (val == 0)
1079  elem = 0;
1080  else
1081  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((unsigned long)i));
1092 
1093  std::vector< GUM_SCALAR > distri(2, 0);
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 
1117  } // end of old distris
1118 
1119  // get min/max approx, 2 vertices
1120  std::vector< std::vector< GUM_SCALAR > > vertices(2, std::vector< GUM_SCALAR >(2, 1));
1121  vertices[1][1] = 0;
1122 
1123  auto new_verticessize = pvar_cpt.size();
1124 
1125  for (Size v = 0; v < new_verticessize; v++) {
1126  if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1127 
1128  if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1129  }
1130 
1131  vertices[0][0] = 1 - vertices[0][1];
1132  vertices[1][0] = 1 - vertices[1][1];
1133 
1134  pvar_cpt = vertices;
1135 
1136  var_cpt[conf] = pvar_cpt;
1137 
1138  ++ins;
1139  ++ins;
1140 
1141  old_conf++;
1142 
1143  if (old_conf == (*credalNet_current_cpt)[var].size()) old_conf = 0;
1144 
1145  } // end of new parent conf
1146 
1147  credalNet_bin_cpt->insert(_var_bits_[var][i], var_cpt);
1148 
1149  } // end of bit i
1150 
1151  } // end of old variable
1152 
1153  bin_bn->beginTopologyTransformation();
1154 
1155  /* indicatrices variables */
1156  auto old_varsize = _var_bits_.size();
1157 
1158  for (Size i = 0; i < old_varsize; i++) {
1159  auto bitsize = _var_bits_[i].size();
1160 
1161  // binary variable
1162  if (bitsize == 1) continue;
1163 
1164  auto old_card = _src_bn_.variable(i).domainSize();
1165 
1166  for (Size mod = 0; mod < old_card; mod++) {
1167  std::stringstream ss;
1168  ss << _src_bn_.variable(i).name();
1169  ss << "-v";
1170  ss << mod;
1171 
1172  LabelizedVariable var(ss.str(), "node " + ss.str(), 2);
1173  const NodeId indic = bin_bn->add(var);
1174 
1175  // arcs from one's bits
1176  for (Size bit = 0; bit < bitsize; bit++)
1177  bin_bn->addArc(_var_bits_[i][bit], indic);
1178 
1179  // cpt
1180  Size num = Size(int2Pow(long(bitsize)));
1181 
1182  std::vector< std::vector< std::vector< GUM_SCALAR > > > icpt(num);
1183 
1184  for (Size entry = 0; entry < num; entry++) {
1185  std::vector< std::vector< GUM_SCALAR > > vertices(1, std::vector< GUM_SCALAR >(2, 0));
1186 
1187  if (mod == entry)
1188  vertices[0][1] = 1;
1189  else
1190  vertices[0][0] = 1;
1191 
1192  icpt[entry] = vertices;
1193  }
1194 
1195  credalNet_bin_cpt->insert(indic, icpt);
1196 
1197  bin_nodeType->insert(indic, NodeType::Indic);
1198  } // end of each modality, i.e. as many indicatrice
1199  }
1200 
1201  bin_bn->endTopologyTransformation();
1202 
1203  if (this->_current_bn_ != nullptr) delete this->_current_bn_;
1204 
1205  this->_current_bn_ = bin_bn;
1206 
1207  if (this->_credalNet_current_cpt_ != nullptr) delete this->_credalNet_current_cpt_;
1208 
1209  this->_credalNet_current_cpt_ = credalNet_bin_cpt;
1210 
1211  if (this->_current_nodeType_ != nullptr) delete this->_current_nodeType_;
1212 
1213  this->_current_nodeType_ = bin_nodeType;
1214 
1215  _sort_varType_(); // will fill _bin_nodeType_ except for NodeType::Indic
1216  // variables
1217 
1219  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
unsigned long int2Pow(unsigned long exponent)
Specialized base 2 pow function with integer.
Definition: pow_inl.h:48
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
NodeProperty< std::vector< NodeId > > _var_bits_
Corresponding bits of each variable.
Definition: credalNet.h:603
void computeBinaryCPTMinMax()
Used with binary networks to speed-up L2U inference.
void _sort_varType_()
Set the NodeType of each node
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:608
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:54
const BayesNet< GUM_SCALAR > & current_bn() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size NodeId
Type for node ids.
Definition: graphElements.h:97

◆ bnToCredal()

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::bnToCredal ( const GUM_SCALAR  beta,
const bool  oneNet,
const bool  keepZeroes = false 
)

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

422  {
423  GUM_SCALAR epsi_min = 1.;
424  GUM_SCALAR epsi_max = 0.;
425  GUM_SCALAR epsi_moy = 0.;
426  GUM_SCALAR epsi_den = 0.;
427 
428  for (auto node: src_bn().nodes()) {
429  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(node));
430 
431  Potential< GUM_SCALAR >* const potential_min(
432  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_min_.cpt(node)));
433  Potential< GUM_SCALAR >* const potential_max(
434  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_max_.cpt(node)));
435 
436  Size var_dSize = _src_bn_.variable(node).domainSize();
437  Size entry_size = potential->domainSize() / var_dSize;
438 
439  Instantiation ins(potential);
440  Instantiation ins_min(potential_min);
441  Instantiation ins_max(potential_max);
442 
443  ins.setFirst();
444  ins_min.setFirst();
445  ins_max.setFirst();
446 
447  std::vector< GUM_SCALAR > vertex(var_dSize);
448 
449  for (Size entry = 0; entry < entry_size; entry++) {
450  GUM_SCALAR den;
451 
452  if (oneNet)
453  den = 0;
454  else
455  den = potential_max->get(ins_max);
456 
457  Size nbm = 0;
458 
459  for (Size modality = 0; modality < var_dSize; modality++) {
460  vertex[modality] = potential->get(ins);
461 
462  if (oneNet) {
463  den += vertex[modality];
464 
465  if (vertex[modality] < 1 && vertex[modality] > 0)
466  GUM_ERROR(OperationNotAllowed,
467  "bnToCredal : the BayesNet contains "
468  "probabilities and not event counts "
469  "although user precised oneNet = "
470  << oneNet);
471  }
472 
473  if (vertex[modality] > 0) nbm++;
474 
475  ++ins;
476  }
477 
479  if (!oneNet) {
480  GUM_SCALAR sum = 0;
481 
482  for (auto modality = vertex.cbegin(), theEnd = vertex.cend(); modality != theEnd;
483  ++modality) {
484  sum += *modality;
485  }
486 
487  if (std::fabs(1. - sum) > _epsRedund_) {
488  GUM_ERROR(CPTNoSumTo1,
489  _src_bn_.variable(node).name() << "(" << _epsRedund_ << ")"
490  << " " << entry << std::endl
491  << vertex << std::endl
492  << ins << std::endl);
493  }
494  }
495 
497 
498  GUM_SCALAR epsilon;
499 
500  if (beta == 0)
501  epsilon = 0;
502  else if (den == 0 || beta == 1)
503  epsilon = GUM_SCALAR(1.0);
504  else
505  epsilon = GUM_SCALAR(std::pow(beta, std::log1p(den)));
506 
507  epsi_moy += epsilon;
508  epsi_den += 1;
509 
510  if (epsilon > epsi_max) epsi_max = epsilon;
511 
512  if (epsilon < epsi_min) epsi_min = epsilon;
513 
514  GUM_SCALAR min, max;
515 
516  for (Size modality = 0; modality < var_dSize; modality++) {
517  if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
518  min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
519 
520  if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
521 
522  max = GUM_SCALAR(min + epsilon);
523  } else { // if ( ( vertex[modality] == 0 && keepZeroes ) || (
524  // vertex[modality] > 0 && nbm <= 1 ) || ( vertex[modality] == 0
525  // && nbm <= 1 ) ) {
526  min = vertex[modality];
527 
528  if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
529 
530  max = min;
531  }
532 
533  potential_min->set(ins_min, min);
534  potential_max->set(ins_max, max);
535 
536  ++ins_min;
537  ++ins_max;
538  } // end of : for each modality
539 
540  } // end of : for each entry
541 
542  } // end of : for each variable
543 
544  _epsilonMin_ = epsi_min;
545  _epsilonMax_ = epsi_max;
546  _epsilonMoy_ = (GUM_SCALAR)epsi_moy / (GUM_SCALAR)epsi_den;
547 
549  }
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:552
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs)...
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:556
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:560
GUM_SCALAR _epsRedund_
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:565
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
const BayesNet< GUM_SCALAR > & src_bn() const
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ computeBinaryCPTMinMax()

template<typename 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 1261 of file credalNet_tpl.h.

1261  {
1262  _binCptMin_.resize(current_bn().size());
1263  _binCptMax_.resize(current_bn().size());
1264 
1265  for (auto node: current_bn().nodes()) {
1266  auto pConf = credalNet_currentCpt()[node].size();
1267  std::vector< GUM_SCALAR > min(pConf);
1268  std::vector< GUM_SCALAR > max(pConf);
1269 
1270  for (Size pconf = 0; pconf < pConf; pconf++) {
1271  GUM_SCALAR v1, v2;
1272  v1 = credalNet_currentCpt()[node][pconf][0][1];
1273 
1274  if (credalNet_currentCpt()[node][pconf].size() > 1)
1275  v2 = credalNet_currentCpt()[node][pconf][1][1];
1276  else
1277  v2 = v1;
1278 
1279  GUM_SCALAR delta = v1 - v2;
1280  min[pconf] = (delta >= 0) ? v2 : v1;
1281  max[pconf] = (delta >= 0) ? v1 : v2;
1282  }
1283 
1284  _binCptMin_[node] = min;
1285  _binCptMax_[node] = max;
1286  }
1287 
1289  }
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:627
bool _hasComputedBinaryCPTMinMax_
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:612
const BayesNet< GUM_SCALAR > & current_bn() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
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:619

◆ credalNet_currentCpt()

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

1223  {
1224  if (_credalNet_current_cpt_ != nullptr) return *_credalNet_current_cpt_;
1225 
1226  return _credalNet_src_cpt_;
1227  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596

◆ credalNet_srcCpt()

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

1231  {
1232  return _credalNet_src_cpt_;
1233  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596

◆ current_bn()

template<typename 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 1360 of file credalNet_tpl.h.

1360  {
1361  if (_current_bn_ != nullptr) return *_current_bn_;
1362 
1363  return _src_bn_;
1364  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585

◆ currentNodeType()

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

Definition at line 1237 of file credalNet_tpl.h.

1237  {
1238  if (_current_nodeType_ != nullptr) return (*(_current_nodeType_))[id];
1239 
1240  return _original_nodeType_[id];
1241  }
NodeProperty< NodeType > * _current_nodeType_
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:608
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:606

◆ domainSize()

template<typename GUM_SCALAR >
INLINE 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 383 of file credalNet_tpl.h.

383  {
384  return _src_bn_.variable(id).domainSize();
385  }
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585

◆ epsilonMax()

template<typename 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 1309 of file credalNet_tpl.h.

1309  {
1310  return _epsilonMax_;
1311  }
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:556

◆ epsilonMean()

template<typename 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 1314 of file credalNet_tpl.h.

1314  {
1315  return _epsilonMoy_;
1316  }
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:560

◆ epsilonMin()

template<typename 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 1304 of file credalNet_tpl.h.

1304  {
1305  return _epsilonMin_;
1306  }
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:552

◆ fillConstraint() [1/2]

template<typename 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 264 of file credalNet_tpl.h.

267  {
268  Potential< GUM_SCALAR >* const potential_min(
269  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_min_.cpt(id)));
270  Potential< GUM_SCALAR >* const potential_max(
271  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_max_.cpt(id)));
272 
273  auto var_dSize = _src_bn_.variable(id).domainSize();
274 
275  if (lower.size() != var_dSize || upper.size() != var_dSize)
276  GUM_ERROR(SizeError,
277  "setCPT : variable modalities in cpts does not match for node id : "
278  << id << " with sizes of constraints : ( " << lower.size() << " || "
279  << upper.size() << " ) != " << var_dSize);
280 
281  auto entry_size = potential_min->domainSize() / var_dSize;
282 
283  if (entry >= entry_size)
284  GUM_ERROR(SizeError,
285  "setCPT : entry is greater or equal than entry size "
286  "(entries start at 0 up to entry_size - 1) : "
287  << entry << " >= " << entry_size);
288 
289  Instantiation min(potential_min);
290  Instantiation max(potential_max);
291  min.setFirst();
292  max.setFirst();
293 
294  Idx pos = 0;
295 
296  while (pos != entry) {
297  ++min;
298  ++max;
299  ++pos;
300  }
301 
302  for (Size i = 0; i < var_dSize; i++) {
303  potential_min->set(min, lower[i]);
304  potential_max->set(max, upper[i]);
305  ++min;
306  ++max;
307  }
308  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ fillConstraint() [2/2]

template<typename 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 ), therefor we pass it by value

Definition at line 311 of file credalNet_tpl.h.

314  {
315  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(id));
316  /*
317  auto var_dSize = _src_bn_.variable ( id ).domainSize();
318  auto entry_size = potential->domainSize() / var_dSize;
319  */
320  // to be sure of entry index reorder ins according to the bayes net
321  // potentials
322  // ( of the credal net )
323  // it WONT throw an error if the sequences are not equal not because of
324  // order
325  // but content, so we double check (before & after order correction)
326  // beware of slaves & master
327  Instantiation ref(potential);
328  ref.forgetMaster();
329 
330  ins.forgetMaster();
331 
332  const auto& vseq = ref.variablesSequence();
333 
334  if (ins.variablesSequence() != vseq) {
335  ins.reorder(ref);
336 
337  if (ins.variablesSequence() != vseq)
338  GUM_ERROR(OperationNotAllowed,
339  "setCPT : instantiation : "
340  << ins << " is not valid for node id " << id
341  << " which accepts instantiations such as (order is not "
342  "important) : "
343  << ref);
344  }
345 
346  Idx entry = 0, jump = 1;
347 
348  for (Idx i = 0, end = ins.nbrDim(); i < end; i++) {
349  if (_src_bn_.nodeId(ins.variable(i)) == id) continue;
350 
351  entry += ins.val(i) * jump;
352 
353  jump *= ins.variable(i).domainSize();
354  }
355 
356  /*
357  if ( entry >= entry_size )
358  GUM_ERROR ( SizeError, "setCPT : entry is greater or equal than entry
359  size
360  (entries start at 0 up to entry_size - 1) : " << entry << " >= " <<
361  entry_size
362  );
363 
364  if ( lower.size() != var_dSize || upper.size() != var_dSize )
365  GUM_ERROR ( SizeError, "setCPT : variable modalities in cpts does not
366  match
367  for node id : " << id << " with sizes of constraints : ( "<< lower.size()
368  << "
369  || " << upper.size() << " ) != " << var_dSize );
370  */
371  fillConstraint(id, entry, lower, upper);
372  }
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 ) ...
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ fillConstraints()

template<typename 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 251 of file credalNet_tpl.h.

253  {
254  try {
255  _src_bn_min_.cpt(id).fillWith(lower);
256  _src_bn_max_.cpt(id).fillWith(upper);
257  } catch (const SizeError&) {
258  GUM_ERROR(SizeError,
259  "fillConstraints : sizes does not match in fillWith for node id : " << id);
260  }
261  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ get_binaryCPT_max()

template<typename 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 1299 of file credalNet_tpl.h.

1299  {
1300  return _binCptMax_;
1301  }
std::vector< std::vector< GUM_SCALAR > > _binCptMax_
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:627

◆ get_binaryCPT_min()

template<typename 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 1293 of file credalNet_tpl.h.

1293  {
1294  return _binCptMin_;
1295  }
std::vector< std::vector< GUM_SCALAR > > _binCptMin_
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:619

◆ hasComputedBinaryCPTMinMax()

template<typename GUM_SCALAR >
const 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 1255 of file credalNet_tpl.h.

1255  {
1257  }
bool _hasComputedBinaryCPTMinMax_
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:612

◆ idmLearning()

template<typename 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 601 of file credalNet_tpl.h.

601  {
602  for (auto node: _src_bn_.nodes()) {
603  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(node));
604 
605  Potential< GUM_SCALAR >* const potential_min(
606  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_min_.cpt(node)));
607  Potential< GUM_SCALAR >* const potential_max(
608  const_cast< Potential< GUM_SCALAR >* const >(&_src_bn_max_.cpt(node)));
609 
610  Size var_dSize = _src_bn_.variable(node).domainSize();
611  Size entry_size = potential->domainSize() / var_dSize;
612 
613  Instantiation ins(potential);
614  Instantiation ins_min(potential_min);
615  Instantiation ins_max(potential_max);
616 
617  ins.setFirst();
618  ins_min.setFirst();
619  ins_max.setFirst();
620 
621  std::vector< GUM_SCALAR > vertex(var_dSize);
622 
623  for (Size entry = 0; entry < entry_size; entry++) {
624  GUM_SCALAR den = 0;
625  Size nbm = 0;
626 
627  for (Size modality = 0; modality < var_dSize; modality++) {
628  vertex[modality] = potential->get(ins);
629 
630  if (vertex[modality] < 1 && vertex[modality] > 0)
631  GUM_ERROR(OperationNotAllowed,
632  "idmLearning : the BayesNet contains "
633  "probabilities and not event counts.");
634 
635  den += vertex[modality];
636 
637  if (vertex[modality] > 0) nbm++;
638 
639  ++ins;
640  }
641 
642  if (nbm > 1 || !keepZeroes) den += s;
643 
644  GUM_SCALAR min, max;
645 
646  for (Size modality = 0; modality < var_dSize; modality++) {
647  min = vertex[modality];
648  max = min;
649 
650  if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
651 
652  min = GUM_SCALAR(min * 1.0 / den);
653  max = GUM_SCALAR(max * 1.0 / den);
654 
655  potential_min->set(ins_min, min);
656  potential_max->set(ins_max, max);
657 
658  ++ins_min;
659  ++ins_max;
660  } // end of : for each modality
661 
662  } // end of : for each entry
663 
664  } // end of : for each variable
665 
666  _epsilonMin_ = GUM_SCALAR(s);
667  _epsilonMax_ = GUM_SCALAR(s);
668  _epsilonMoy_ = GUM_SCALAR(s);
670  }
GUM_SCALAR _epsilonMin_
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:552
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
void _intervalToCredal_()
Computes the vertices of each credal set according to their interval definition (does not use lrs)...
GUM_SCALAR _epsilonMax_
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:556
GUM_SCALAR _epsilonMoy_
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:560
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ instantiation()

template<typename GUM_SCALAR >
INLINE Instantiation gum::credal::CredalNet< GUM_SCALAR >::instantiation ( const NodeId id)

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

bnet accessors / shortcuts

Parameters
idThe NodeId we want an instantiation from
Returns
The instantiation

Definition at line 378 of file credalNet_tpl.h.

378  {
379  return Instantiation(_src_bn_.cpt(id));
380  }
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585

◆ intervalToCredal()

template<typename 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 767 of file credalNet_tpl.h.

767  {
768  if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
769 
770  _credalNet_src_cpt_.resize(_src_bn_.size());
771 
772  LRSWrapper< GUM_SCALAR > lrsWrapper;
773 
774  for (auto node: _src_bn_.nodes()) {
775  const Potential< GUM_SCALAR >* const potential_min(&_src_bn_min_.cpt(node));
776  const Potential< GUM_SCALAR >* const potential_max(&_src_bn_max_.cpt(node));
777 
778  Size var_dSize = _src_bn_.variable(node).domainSize();
779  Size entry_size = potential_min->domainSize() / var_dSize;
780 
781  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(entry_size);
782 
783  Instantiation ins_min(potential_min);
784  Instantiation ins_max(potential_max);
785 
786  ins_min.setFirst();
787  ins_max.setFirst();
788 
789  lrsWrapper.setUpH(var_dSize);
790 
791  for (Size entry = 0; entry < entry_size; entry++) {
792  for (Size modality = 0; modality < var_dSize; modality++) {
793  if (potential_min->get(ins_min) > potential_max->get(ins_max)) {
794  GUM_ERROR(CPTError,
795  "For variable "
796  << _src_bn_.variable(node).name() << " (at " << ins_min
797  << "), the min is greater than the max : " << potential_min->get(ins_min)
798  << ">" << potential_max->get(ins_max) << ".");
799  }
800  lrsWrapper.fillH(potential_min->get(ins_min), potential_max->get(ins_max), modality);
801  ++ins_min;
802  ++ins_max;
803  }
804 
805  lrsWrapper.H2V();
806  var_cpt[entry] = lrsWrapper.getOutput();
807  lrsWrapper.nextHInput();
808  }
809 
810  _credalNet_src_cpt_.insert(node, var_cpt);
811 
812  } // end of : for each variable (node)
813 
814  // get precise/credal/vacuous status of each variable
815  _sort_varType_();
816  _separatelySpecified_ = true;
817  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:582
void _sort_varType_()
Set the NodeType of each node
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ intervalToCredalWithFiles()

template<typename 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 821 of file credalNet_tpl.h.

821  {
822  if (!_credalNet_src_cpt_.empty()) _credalNet_src_cpt_.clear();
823 
824  _credalNet_src_cpt_.resize(_src_bn_.size());
825 
826  for (auto node: _src_bn_.nodes()) {
827  const Potential< GUM_SCALAR >* const potential_min(&_src_bn_min_.cpt(node));
828  const Potential< GUM_SCALAR >* const potential_max(&_src_bn_max_.cpt(node));
829 
830  auto var_dSize = _src_bn_.variable(node).domainSize();
831  auto entry_size = potential_min->domainSize() / var_dSize;
832 
833  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(entry_size);
834 
835  Instantiation ins_min(potential_min);
836  Instantiation ins_max(potential_max);
837 
838  ins_min.setFirst();
839  ins_max.setFirst();
840 
841  // use iterator
842  for (Size entry = 0; entry < entry_size; entry++) {
843  std::vector< std::vector< GUM_SCALAR > > vertices;
844  std::vector< GUM_SCALAR > vertex(var_dSize); // if not interval
845 
846  std::vector< std::vector< GUM_SCALAR > > inequalities(
847  var_dSize * 2,
848  std::vector< GUM_SCALAR >(var_dSize + 1, 0));
849 
850  std::vector< GUM_SCALAR > sum_ineq1(var_dSize + 1, -1);
851  std::vector< GUM_SCALAR > sum_ineq2(var_dSize + 1, 1);
852  sum_ineq1[0] = 1;
853  sum_ineq2[0] = -1;
854 
855  bool isInterval = false;
856 
857  for (Size modality = 0; modality < var_dSize; modality++) {
858  inequalities[modality * 2][0] = -potential_min->get(ins_min);
859  inequalities[modality * 2 + 1][0] = potential_max->get(ins_max);
860  inequalities[modality * 2][modality + 1] = 1;
861  inequalities[modality * 2 + 1][modality + 1] = -1;
862 
863  vertex[modality] = inequalities[modality * 2 + 1][0];
864 
865  if (!isInterval
866  && (-inequalities[modality * 2][0] != inequalities[modality * 2 + 1][0]))
867  isInterval = true;
868 
869  ++ins_min;
870  ++ins_max;
871  }
872 
873  inequalities.push_back(sum_ineq1);
874  inequalities.push_back(sum_ineq2);
875 
876  if (!isInterval) {
877  vertices.push_back(vertex);
878  } else {
879  try {
880  _H2Vlrs_(inequalities, vertices);
881  // __H2Vcdd ( inequalities, vertices );
882  } catch (const std::exception& err) {
883  std::cout << err.what() << std::endl;
884  throw;
885  }
886 
887  } // end of : is interval
888 
889  if (entry == 0 && vertices.size() >= 2) {
890  auto tmp = vertices[0];
891  vertices[0] = vertices[1];
892  vertices[1] = tmp;
893  }
894 
895  var_cpt[entry] = vertices;
896 
897  } // end of : for each entry
898 
899  _credalNet_src_cpt_.insert(node, var_cpt);
900  // std::cout << _src_bn_.variable(node_idIt).name() << std::endl;
901  // std::cout << var_cpt << std::endl;
902 
903  } // end of : for each variable (node)
904 
905  // get precise/credal/vacuous status of each variable
906  _sort_varType_();
907  _separatelySpecified_ = true;
908  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:582
void _sort_varType_()
Set the NodeType of each node
void _H2Vlrs_(const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ isSeparatelySpecified()

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

Definition at line 1250 of file credalNet_tpl.h.

1250  {
1251  return _separatelySpecified_;
1252  }
bool _separatelySpecified_
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:582

◆ lagrangeNormalization()

template<typename 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 552 of file credalNet_tpl.h.

552  {
553  for (auto node: _src_bn_.nodes()) {
554  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(node));
555 
556  auto var_dSize = _src_bn_.variable(node).domainSize();
557  auto entry_size = potential->domainSize() / var_dSize;
558 
559  Instantiation ins(potential);
560 
561  ins.setFirst();
562 
563  std::vector< GUM_SCALAR > vertex(var_dSize);
564 
565  for (Size entry = 0; entry < entry_size; entry++) {
566  GUM_SCALAR den = 0;
567  bool zeroes = false;
568  Instantiation ins_prev = ins;
569 
570  for (Size modality = 0; modality < var_dSize; modality++) {
571  vertex[modality] = potential->get(ins);
572 
573  if (vertex[modality] < 1 && vertex[modality] > 0)
574  GUM_ERROR(OperationNotAllowed,
575  "lagrangeNormalization : the BayesNet "
576  "contains probabilities and not event "
577  "counts.");
578 
579  den += vertex[modality];
580 
581  if (!zeroes && vertex[modality] == 0) { zeroes = true; }
582 
583  ++ins;
584  }
585 
586  if (zeroes) {
587  ins = ins_prev;
588 
589  for (Size modality = 0; modality < var_dSize; modality++) {
590  potential->set(ins, potential->get(ins) + 1);
591  ++ins;
592  }
593  }
594 
595  } // end of : for each entry
596 
597  } // end of : for each variable
598  }
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ nodeType()

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

Definition at line 1245 of file credalNet_tpl.h.

1245  {
1246  return _original_nodeType_[id];
1247  }
NodeProperty< NodeType > _original_nodeType_
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:606

◆ saveBNsMinMax()

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::saveBNsMinMax ( const std::string &  min_path,
const std::string &  max_path 
) const

If this CredalNet was built over a perturbed BayesNet, one can save the intervals as two BayesNet.

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

916  {
917  BIFWriter< GUM_SCALAR > writer;
918 
919  std::string minfilename = min_path; //"min.bif";
920  std::string maxfilename = max_path; //"max.bif";
921  std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
922  std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
923 
924  if (!min_file.good())
925  GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << minfilename);
926 
927  if (!max_file.good()) {
928  min_file.close();
929  GUM_ERROR(IOError, "bnToCredal() : could not open stream : min_file : " << maxfilename);
930  }
931 
932  try {
933  writer.write(min_file, _src_bn_min_);
934  writer.write(max_file, _src_bn_max_);
935  } catch (Exception& err) {
936  GUM_SHOWERROR(err);
937  min_file.close();
938  max_file.close();
939  throw(err);
940  }
941 
942  min_file.close();
943  max_file.close();
944  }
BayesNet< GUM_SCALAR > _src_bn_min_
BayesNet used to store lower probabilities.
Definition: credalNet.h:588
#define GUM_SHOWERROR(e)
Definition: exceptions.h:57
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
BayesNet< GUM_SCALAR > _src_bn_max_
BayesNet used to store upper probabilities.
Definition: credalNet.h:590

◆ setCPT() [1/2]

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::setCPT ( const NodeId id,
Size entry,
const std::vector< std::vector< GUM_SCALAR > > &  cpt 
)

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

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

112  {
113  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(id));
114 
115  auto var_dSize = _src_bn_.variable(id).domainSize();
116  auto entry_size = potential->domainSize() / var_dSize;
117 
118  if (entry >= entry_size)
119  GUM_ERROR(SizeError,
120  "setCPT : entry is greater or equal than entry size "
121  "(entries start at 0 up to entry_size - 1) : "
122  << entry << " >= " << entry_size);
123 
124  if (cpt.size() == 0) GUM_ERROR(SizeError, "setCPT : empty credal set for entry : " << entry)
125 
126  for (const auto& vertex: cpt) {
127  if (vertex.size() != var_dSize)
128  GUM_ERROR(SizeError,
129  "setCPT : variable modalities in cpts does not "
130  "match for node id : "
131  << id << " with vertex " << vertex << " at entry " << entry << " : "
132  << vertex.size() << " != " << var_dSize);
133 
134  GUM_SCALAR sum = 0;
135 
136  for (const auto& prob: vertex) {
137  sum += prob;
138  }
139 
140  if (std::fabs(sum - 1) > 1e-6)
141  GUM_ERROR(CPTNoSumTo1,
142  "setCPT : a vertex coordinates does not sum to one for node id : "
143  << id << " at entry " << entry << " with vertex " << vertex);
144  }
145 
146  // !! auto does NOT use adress (if available) unless explicitly asked !!
147  auto& node_cpt = _credalNet_src_cpt_.getWithDefault(
148  id,
149  std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
150 
151  if (node_cpt[entry].size() != 0)
152  GUM_ERROR(DuplicateElement,
153  "setCPT : vertices of entry id " << entry
154  << " already set to : " << node_cpt[entry]
155  << ", cannot insert : " << cpt);
156 
157  node_cpt[entry] = cpt;
158 
160  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setCPT() [2/2]

template<typename 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 ), therefor we pass it by value

Definition at line 163 of file credalNet_tpl.h.

165  {
166  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(id));
167 
168  auto var_dSize = _src_bn_.variable(id).domainSize();
169  auto entry_size = potential->domainSize() / var_dSize;
170 
171  // to be sure of entry index reorder ins according to the bayes net
172  // potentials
173  // ( of the credal net )
174  // it WONT throw an error if the sequences are not equal not because of
175  // order
176  // but content, so we double check (before & after order correction)
177  // beware of slaves & master
178  Instantiation ref(potential);
179  ref.forgetMaster();
180 
181  ins.forgetMaster();
182 
183  const auto& vseq = ref.variablesSequence();
184 
185  if (ins.variablesSequence() != vseq) {
186  ins.reorder(ref);
187 
188  if (ins.variablesSequence() != vseq)
189  GUM_ERROR(OperationNotAllowed,
190  "setCPT : instantiation : "
191  << ins << " is not valid for node id " << id
192  << " which accepts instantiations such as (order is not "
193  "important) : "
194  << ref);
195  }
196 
197  Idx entry = 0, jump = 1;
198 
199  for (Idx i = 0, end = ins.nbrDim(); i < end; i++) {
200  if (_src_bn_.nodeId(ins.variable(i)) == id) continue;
201 
202  entry += ins.val(i) * jump;
203 
204  jump *= ins.variable(i).domainSize();
205  }
206 
207  if (entry >= entry_size)
208  GUM_ERROR(SizeError,
209  "setCPT : entry is greater or equal than entry size "
210  "(entries start at 0 up to entry_size - 1) : "
211  << entry << " >= " << entry_size);
212 
213  if (cpt.size() == 0) GUM_ERROR(SizeError, "setCPT : empty credal set for entry : " << entry)
214 
215  for (const auto& vertex: cpt) {
216  if (vertex.size() != var_dSize)
217  GUM_ERROR(SizeError,
218  "setCPT : variable modalities in cpts does not "
219  "match for node id : "
220  << id << " with vertex " << vertex << " at entry " << entry << " : "
221  << vertex.size() << " != " << var_dSize);
222 
223  GUM_SCALAR sum = 0;
224 
225  for (const auto& prob: vertex) {
226  sum += prob;
227  }
228 
229  if (std::fabs(sum - 1) > 1e-6)
230  GUM_ERROR(CPTNoSumTo1,
231  "setCPT : a vertex coordinates does not sum to one for node id : "
232  << id << " at entry " << entry << " with vertex " << vertex);
233  }
234 
235  auto& node_cpt = _credalNet_src_cpt_.getWithDefault(
236  id,
237  std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
238 
239  if (node_cpt[entry].size() != 0)
240  GUM_ERROR(DuplicateElement,
241  "setCPT : vertices of entry : " << ins << " id " << entry
242  << " already set to : " << node_cpt[entry]
243  << ", cannot insert : " << cpt);
244 
245  node_cpt[entry] = cpt;
246 
248  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ setCPTs()

template<typename 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 65 of file credalNet_tpl.h.

67  {
68  const Potential< GUM_SCALAR >* const potential(&_src_bn_.cpt(id));
69 
70  auto var_dSize = _src_bn_.variable(id).domainSize();
71  auto entry_size = potential->domainSize() / var_dSize;
72 
73  if (cpt.size() != entry_size)
74  GUM_ERROR(SizeError,
75  "setCPTs : entry sizes of cpts does not match for node id : "
76  << id << " : " << cpt.size() << " != " << entry_size);
77 
78  for (const auto& cset: cpt) {
79  if (cset.size() == 0)
80  GUM_ERROR(SizeError,
81  "setCPTs : vertices in credal set does not match for node id : "
82  << id << " with 0 vertices");
83 
84  for (const auto& vertex: cset) {
85  if (vertex.size() != var_dSize)
86  GUM_ERROR(SizeError,
87  "setCPTs : variable modalities in cpts does "
88  "not match for node id : "
89  << id << " with vertex " << vertex << " : " << vertex.size()
90  << " != " << var_dSize);
91 
92  GUM_SCALAR sum = 0;
93 
94  for (const auto& prob: vertex) {
95  sum += prob;
96  }
97 
98  if (std::fabs(sum - 1) > 1e-6)
99  GUM_ERROR(CPTNoSumTo1,
100  "setCPTs : a vertex coordinates does not "
101  "sum to one for node id : "
102  << id << " with vertex " << vertex);
103  }
104  }
105 
106  _credalNet_src_cpt_.insert(id, cpt);
107  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ src_bn()

template<typename 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 1367 of file credalNet_tpl.h.

1367  {
1368  return _src_bn_;
1369  }
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585

◆ toString()

template<typename 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 1319 of file credalNet_tpl.h.

1319  {
1320  std::stringstream output;
1321  const BayesNet< GUM_SCALAR >* _current_bn_;
1322  const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1324 
1325  if (this->_current_bn_ == nullptr)
1326  _current_bn_ = &this->_src_bn_;
1327  else
1328  _current_bn_ = this->_current_bn_;
1329 
1330  if (this->_credalNet_current_cpt_ == nullptr)
1331  _credalNet_current_cpt_ = &this->_credalNet_src_cpt_;
1332  else
1333  _credalNet_current_cpt_ = this->_credalNet_current_cpt_;
1334 
1335  for (auto node: _current_bn_->nodes()) {
1336  const Potential< GUM_SCALAR >* potential(&_current_bn_->cpt(node));
1337  auto pconfs = potential->domainSize() / _current_bn_->variable(node).domainSize();
1338 
1339  output << "\n" << _current_bn_->variable(node) << "\n";
1340 
1341  Instantiation ins(potential);
1342  ins.forgetMaster();
1343  ins.erase(_current_bn_->variable(node));
1344  ins.setFirst();
1345 
1346  for (Size pconf = 0; pconf < pconfs; pconf++) {
1347  output << ins << " : ";
1348  output << (*_credalNet_current_cpt_)[node][pconf] << "\n";
1349 
1350  if (pconf < pconfs - 1) ++ins;
1351  }
1352  }
1353 
1354  output << "\n";
1355 
1356  return output.str();
1357  }
BayesNet< GUM_SCALAR > * _current_bn_
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:593
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * _credalNet_current_cpt_
This CredalNet up-to-date CPTs.
Definition: credalNet.h:600
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > _credalNet_src_cpt_
This CredalNet original CPTs.
Definition: credalNet.h:596
BayesNet< GUM_SCALAR > _src_bn_
Original BayesNet (used as a DAG).
Definition: credalNet.h:585
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

Member Data Documentation

◆ _binCptMax_

template<typename 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 627 of file credalNet.h.

◆ _binCptMin_

template<typename 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 619 of file credalNet.h.

◆ _credalNet_current_cpt_

template<typename 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 600 of file credalNet.h.

◆ _credalNet_src_cpt_

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

This CredalNet original CPTs.

Definition at line 596 of file credalNet.h.

◆ _current_bn_

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

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

Definition at line 593 of file credalNet.h.

◆ _current_nodeType_

template<typename 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 608 of file credalNet.h.

◆ _deltaC_

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

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

Definition at line 547 of file credalNet.h.

◆ _denMax_

template<typename 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 574 of file credalNet.h.

◆ _epsF_

template<typename 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 570 of file credalNet.h.

◆ _epsilonMax_

template<typename 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 556 of file credalNet.h.

◆ _epsilonMin_

template<typename 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 552 of file credalNet.h.

◆ _epsilonMoy_

template<typename 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 560 of file credalNet.h.

◆ _epsRedund_

template<typename 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 565 of file credalNet.h.

◆ _hasComputedBinaryCPTMinMax_

template<typename 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 612 of file credalNet.h.

◆ _original_nodeType_

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

The NodeType of each node from the ORIGINAL network.

Definition at line 606 of file credalNet.h.

◆ _precision_

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

Precision used by frac.

Definition at line 577 of file credalNet.h.

◆ _precisionC_

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

1e6 by default, used by fracC as precision.

Definition at line 545 of file credalNet.h.

◆ _separatelySpecified_

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

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

Definition at line 582 of file credalNet.h.

◆ _src_bn_

template<typename GUM_SCALAR >
BayesNet< GUM_SCALAR > gum::credal::CredalNet< GUM_SCALAR >::_src_bn_
private

Original BayesNet (used as a DAG).

Is never modified.

Definition at line 585 of file credalNet.h.

◆ _src_bn_max_

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

BayesNet used to store upper probabilities.

Definition at line 590 of file credalNet.h.

◆ _src_bn_min_

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

BayesNet used to store lower probabilities.

Definition at line 588 of file credalNet.h.

◆ _var_bits_

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

Corresponding bits of each variable.

Deprecated:

Definition at line 603 of file credalNet.h.


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