aGrUM  0.14.1
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 computeCPTMinMax ()
 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 hasComputedCPTMinMax () const
 
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_min () const
 Used with binary networks to speed-up L2U inference. More...
 
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_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 87 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 90 of file credalNet.h.

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

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

29  {
30  __initParams();
31 
32  __src_bn = BayesNet< GUM_SCALAR >();
33  __src_bn_min = BayesNet< GUM_SCALAR >();
34  __src_bn_max = BayesNet< GUM_SCALAR >();
35 
36  GUM_CONSTRUCTOR(CredalNet);
37  }
void __initParams()
Initialize private constant variables after the Constructor has been called.
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:29
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624

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

References GUM_SHOWERROR.

401  {
402  try {
403  __initParams();
404  __initCNNets(src_min_num, src_max_den);
405  } catch (Exception& err) {
406  GUM_SHOWERROR(err);
407  throw(err);
408  }
409 
410  GUM_CONSTRUCTOR(CredalNet);
411  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
void __initParams()
Initialize private constant variables after the Constructor has been called.
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:29

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

References GUM_SHOWERROR.

415  {
416  try {
417  __initParams();
418  __initCNNets(src_min_num, src_max_den);
419  } catch (Exception& err) {
420  GUM_SHOWERROR(err);
421  throw(err);
422  }
423 
424  GUM_CONSTRUCTOR(CredalNet);
425  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
void __initParams()
Initialize private constant variables after the Constructor has been called.
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:29

◆ ~CredalNet()

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

Destructor.

Definition at line 428 of file credalNet_tpl.h.

428  {
429  if (__current_bn != nullptr) delete __current_bn;
430 
431  if (__credalNet_current_cpt != nullptr) delete __credalNet_current_cpt;
432 
433  if (__current_nodeType != nullptr) delete __current_nodeType;
434 
435  GUM_DESTRUCTOR(CredalNet);
436  }
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:29
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635

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

References gum::BayesNet< GUM_SCALAR >::add(), gum::BayesNet< GUM_SCALAR >::addArc(), gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation(), gum::BayesNet< GUM_SCALAR >::cpt(), gum::BayesNet< GUM_SCALAR >::endTopologyTransformation(), gum::BayesNet< GUM_SCALAR >::nodeId(), gum::DAGmodel::nodes(), and gum::BayesNet< GUM_SCALAR >::variable().

1510  {
1511  const BayesNet< GUM_SCALAR >* __current_bn;
1512 
1513  if (this->__current_bn == nullptr)
1514  __current_bn = &this->__src_bn;
1515  else
1516  __current_bn = this->__current_bn;
1517 
1518  for (auto node : __current_bn->nodes())
1519  dest.add(__current_bn->variable(node));
1520 
1521  dest.beginTopologyTransformation();
1522 
1523  for (auto node : __current_bn->nodes()) {
1524  for (auto parent_idIt : __current_bn->cpt(node).variablesSequence()) {
1525  if (__current_bn->nodeId(*parent_idIt) != node)
1526  dest.addArc(__current_bn->nodeId(*parent_idIt), node);
1527  } // end of : for each parent in order of appearence
1528  } // end of : for each variable
1529 
1530  dest.endTopologyTransformation();
1531  }
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
+ Here is the call graph for this function:

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

1498  {
1499  Size vertices_size = 0;
1500 
1501  for (auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd;
1502  ++entry) {
1503  if (entry->size() > vertices_size) vertices_size = Size(entry->size());
1504  }
1505 
1506  return int(vertices_size);
1507  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

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

References gum::Rational< GUM_SCALAR >::farey(), gum::getNumberOfRunningThreads(), gum::getThreadNumber(), gum::getUniqueFileName(), and GUM_ERROR.

1588  {
1589  // write H rep file
1590  int64_t num, den;
1591 
1592  std::string sinefile =
1593  getUniqueFileName(); // generate unique file name, we
1594  // need to add .ine or .ext for lrs
1595  // to know which input it is (Hrep
1596  // to Vrep or Vrep to Hrep)
1597  sinefile += ".ine";
1598 
1599  std::ofstream h_file(sinefile.c_str(), std::ios::out | std::ios::trunc);
1600 
1601  if (!h_file.good())
1602  GUM_ERROR(IOError,
1603  "__H2Vlrs : could not open lrs input file : " << sinefile);
1604 
1605  h_file << "H - representation\n";
1606  h_file << "begin\n";
1607  h_file << h_rep.size() << ' ' << h_rep[0].size() << " rational\n";
1608 
1609  for (auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1610  for (auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2;
1611  ++it2) {
1612  // get integer fraction from decimal value
1613  // smallest numerator & denominator is farley, also
1614  // best precision
1616  num, den, ((*it2 > 0) ? *it2 : -*it2), int64_t(__denMax), __epsF);
1617 
1618  h_file << ((*it2 > 0) ? num : -num) << '/' << den << ' ';
1619  }
1620 
1621  h_file << '\n';
1622  }
1623 
1624  h_file << "end\n";
1625  h_file.close();
1626 
1627  // call lrs
1628  // lrs arguments
1629  char* args[3];
1630 
1631  std::string soft_name = "lrs";
1632  std::string extfile(sinefile);
1633  extfile += ".ext";
1634 
1635  args[0] = new char[soft_name.size()];
1636  args[1] = new char[sinefile.size()];
1637  args[2] = new char[extfile.size()];
1638 
1639  strcpy(args[0], soft_name.c_str());
1640  strcpy(args[1], sinefile.c_str());
1641  strcpy(args[2], extfile.c_str());
1642 
1643  // standard cout to null (avoid lrs flooding)
1644  int old_cout, new_cout;
1645  fflush(stdout);
1646  old_cout = dup(1);
1647 
1648  new_cout = open("/dev/null", O_WRONLY);
1649  dup2(new_cout, 1);
1650  close(new_cout);
1651 
1652  lrs_main(3, args);
1653 
1654  // restore standard cout
1655  fflush(stdout);
1656  dup2(old_cout, 1);
1657  close(old_cout);
1658 
1659  delete[] args[2];
1660  delete[] args[1];
1661  delete[] args[0];
1662 
1663  // read V rep file
1664  std::ifstream v_file(extfile.c_str() /*extfilename.c_str()*/, std::ios::in);
1665 
1666  if (!v_file.good())
1667  GUM_ERROR(IOError, "__H2Vlrs : could not open lrs ouput file : ");
1668 
1669  std::string line, tmp;
1670  char * cstr, *p;
1671  GUM_SCALAR probability;
1672 
1673  std::string::size_type pos;
1674  bool keep_going = true;
1675  // int vertices;
1676 
1677  std::vector< GUM_SCALAR > vertex;
1678 
1679  v_file.ignore(256, 'l');
1680 
1681  while (v_file.good() && keep_going) {
1682  getline(v_file, line);
1683 
1684  if (line.size() == 0)
1685  continue;
1686  else if (line.compare("end") == 0) {
1687  keep_going = false;
1688  // this is to get vertices number :
1689  /*getline ( v_file, line );
1690  std::string::size_type pos, end_pos;
1691  pos = line.find ( "vertices = " );
1692  end_pos = line.find ( "rays", pos + 9 );
1693  vertices = atoi ( line.substr ( pos + 9, end_pos - pos - 9 ).c_str()
1694  );*/
1695  break;
1696  } else if (line[1] != '1') {
1697  GUM_ERROR(IOError,
1698  "__H2Vlrs : reading something other than a vertex from "
1699  "lrs output file : ");
1700  }
1701 
1702  line = line.substr(2);
1703  cstr = new char[line.size() + 1];
1704  strcpy(cstr, line.c_str());
1705 
1706  p = strtok(cstr, " ");
1707 
1708  while (p != nullptr) {
1709  tmp = p;
1710 
1711  if (tmp.compare("1") == 0 || tmp.compare("0") == 0)
1712  probability = GUM_SCALAR(atof(tmp.c_str()));
1713  else {
1714  pos = tmp.find("/");
1715  probability =
1716  GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1717  / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1718  }
1719 
1720  vertex.push_back(probability);
1721  p = strtok(nullptr, " ");
1722  } // end of : for all tokens
1723 
1724  delete[] p;
1725  delete[] cstr;
1726 
1727  bool is_redund = false;
1728 
1729 #pragma omp parallel
1730  {
1731  int this_thread = getThreadNumber();
1732  int num_threads = getNumberOfRunningThreads();
1733 
1734  auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1735  auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1736 
1737  for (auto p = begin_pos; p < end_pos; p++) {
1738 #pragma omp flush(is_redund)
1739 
1740  if (is_redund) break;
1741 
1742  bool thread_redund = true;
1743 
1744  auto vsize = vertex.size();
1745 
1746  for (Size modality = 0; modality < vsize; modality++) {
1747  if (std::fabs(vertex[modality] - v_rep[p][modality]) > __epsRedund) {
1748  thread_redund = false;
1749  break;
1750  }
1751  }
1752 
1753  if (thread_redund) {
1754  is_redund = true;
1755 #pragma omp flush(is_redund)
1756  }
1757  } // end of : each thread for
1758  } // end of : parallel
1759 
1760  if (!is_redund) v_rep.push_back(vertex);
1761 
1762  vertex.clear();
1763 
1764  } // end of : file
1765 
1766  v_file.close();
1767 
1768  if (std::remove(sinefile.c_str()) != 0)
1769  GUM_ERROR(IOError, "error removing : " + sinefile);
1770 
1771  if (std::remove(extfile.c_str()) != 0)
1772  GUM_ERROR(IOError, "error removing : " + extfile);
1773  }
GUM_SCALAR __epsF
Value under which a decimal number is considered to be zero when using __farey.
Definition: credalNet.h:604
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
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:34
GUM_SCALAR __denMax
Highest possible denominator allowed when using __farey.
Definition: credalNet.h:608
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
GUM_SCALAR __epsRedund
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:599
+ Here is the call graph for this function:

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

References GUM_SHOWERROR, and gum::BIFReader< GUM_SCALAR >::proceed().

1448  {
1449  BIFReader< GUM_SCALAR > reader(&__src_bn, src_min_num);
1450  std::string other;
1451 
1452  if (src_max_den.compare("") != 0)
1453  other = src_max_den;
1454  else
1455  other = src_min_num;
1456 
1457  BIFReader< GUM_SCALAR > reader_min(&__src_bn_min, src_min_num);
1458  BIFReader< GUM_SCALAR > reader_max(&__src_bn_max, other);
1459 
1460  try {
1461  reader.proceed();
1462  } catch (Exception& err) {
1463  GUM_SHOWERROR(err);
1464  throw(err);
1465  }
1466 
1467  try {
1468  reader_min.proceed();
1469  } catch (Exception& err) {
1470  GUM_SHOWERROR(err);
1471  throw(err);
1472  }
1473 
1474  try {
1475  reader_max.proceed();
1476  } catch (Exception& err) {
1477  GUM_SHOWERROR(err);
1478  throw(err);
1479  }
1480  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

References gum::DAGmodel::size().

1485  {
1486  __src_bn = src_min_num;
1487  __src_bn_min = src_min_num;
1488 
1489  if (src_max_den.size() > 0)
1490  __src_bn_max = src_max_den;
1491  else
1492  __src_bn_max = src_min_num;
1493  }
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

1420  {
1421  __epsilonMin = 0;
1422  __epsilonMax = 0;
1423  __epsilonMoy = 0;
1424 
1425  __epsRedund = GUM_SCALAR(1e-6);
1426 
1427  // farey algorithm
1428  __epsF = GUM_SCALAR(1e-6);
1429  __denMax = GUM_SCALAR(1e6); // beware LRSWrapper
1430 
1431  // continued fractions, beware LRSWrapper
1432  // decimal paces (__epsC * __precisionC == 1)
1433  __precisionC = GUM_SCALAR(1e6);
1434  __deltaC = 5;
1435 
1436  // old custom algorithm
1437  __precision = GUM_SCALAR(1e6); // beware LRSWrapper
1438 
1439  __current_bn = nullptr;
1440  __credalNet_current_cpt = nullptr;
1441  __current_nodeType = nullptr;
1442 
1443  __hasComputedCPTMinMax = false;
1444  }
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:594
GUM_SCALAR __epsF
Value under which a decimal number is considered to be zero when using __farey.
Definition: credalNet.h:604
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
GUM_SCALAR __precision
Precision used by __frac.
Definition: credalNet.h:611
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:647
GUM_SCALAR __precisionC
1e6 by default, used by __fracC as precision.
Definition: credalNet.h:579
GUM_SCALAR __denMax
Highest possible denominator allowed when using __farey.
Definition: credalNet.h:608
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
GUM_SCALAR __epsilonMax
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:590
GUM_SCALAR __epsilonMin
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:586
GUM_SCALAR __deltaC
5 by default, used by __fracC as number of decimals.
Definition: credalNet.h:581
GUM_SCALAR __epsRedund
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:599

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

References gum::DiscreteVariable::domainSize(), GUM_ERROR, gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

695  {
696  if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
697 
698  __credalNet_src_cpt.resize(__src_bn.size());
699 
700  for (auto node : __src_bn.nodes()) {
701  const Potential< GUM_SCALAR >* const potential_min(
702  &__src_bn_min.cpt(node));
703  const Potential< GUM_SCALAR >* const potential_max(
704  &__src_bn_max.cpt(node));
705 
706  Size var_dSize = __src_bn.variable(node).domainSize();
707  Size entry_size = potential_min->domainSize() / var_dSize;
708 
709  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
710  entry_size);
711 
712  Instantiation ins_min(potential_min);
713  Instantiation ins_max(potential_max);
714 
715  ins_min.setFirst();
716  ins_max.setFirst();
717 
718  std::vector< GUM_SCALAR > lower(var_dSize);
719  std::vector< GUM_SCALAR > upper(var_dSize);
720 
721  for (Size entry = 0; entry < entry_size; entry++) {
722  for (Size modality = 0; modality < var_dSize;
723  modality++, ++ins_min, ++ins_max) {
724  lower[modality] = potential_min->get(ins_min);
725  upper[modality] = potential_max->get(ins_max);
726  }
727 
728  bool all_equals = true;
729  std::vector< std::vector< GUM_SCALAR > > vertices;
730 
731  for (Size modality = 0; modality < var_dSize; modality++) {
732  if (std::fabs(upper[modality] - lower[modality]) < 1e-6) continue;
733 
734  all_equals = false;
735  std::vector< GUM_SCALAR > vertex(var_dSize);
736  vertex[modality] = upper[modality];
737 
738  for (Size mod = 0; mod < var_dSize; mod++) {
739  if (modality != mod) vertex[mod] = lower[mod];
740  }
741 
742  GUM_SCALAR total = 0;
743 
744  auto vsize = vertex.size();
745 
746  for (Size i = 0; i < vsize; i++)
747  total += vertex[i];
748 
749  if (std::fabs(total - 1.) > 1e-6)
750  GUM_ERROR(CPTNoSumTo1,
751  __src_bn.variable(node).name() << " " << entry << std::endl
752  << vertex << std::endl);
753 
754  vertices.push_back(vertex);
755  }
756 
757  if (all_equals) {
758  std::vector< GUM_SCALAR > vertex(var_dSize);
759 
760  for (Size modality = 0; modality < var_dSize; modality++)
761  vertex[modality] = lower[modality];
762 
763  GUM_SCALAR total = 0.;
764 
765  auto vsize = vertex.size();
766 
767  for (Size i = 0; i < vsize; i++)
768  total += vertex[i];
769 
770  if (std::fabs(total - 1.) > 1e-6)
771  GUM_ERROR(CPTNoSumTo1,
772  __src_bn.variable(node).name() << " " << entry << std::endl
773  << vertex << std::endl);
774 
775  vertices.push_back(vertex);
776  }
777 
778  var_cpt[entry] = vertices;
779  }
780 
781  __credalNet_src_cpt.insert(node, var_cpt);
782 
783  } // end of : for each variable (node)
784 
785  // get precise/credal/vacuous status of each variable
786  __sort_varType();
787  __separatelySpecified = true;
788  }
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:616
void __sort_varType()
Set the NodeType of each node
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

◆ __sort_varType()

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

Set the NodeType of each node

Definition at line 1776 of file credalNet_tpl.h.

References gum::HashTable< Key, Val, Alloc >::exists(), gum::HashTable< Key, Val, Alloc >::insert(), gum::DAGmodel::nodes(), and gum::HashTable< Key, Val, Alloc >::size().

1776  {
1777  NodeProperty< NodeType >* __current_nodeType;
1778  const NodeProperty<
1779  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1781 
1782  const BayesNet< GUM_SCALAR >* __current_bn;
1783 
1784  if (this->__current_bn == nullptr)
1785  __current_bn = &__src_bn;
1786  else
1787  __current_bn = this->__current_bn;
1788 
1789  if (this->__credalNet_current_cpt == nullptr)
1790  __credalNet_current_cpt = &__credalNet_src_cpt;
1791  else
1792  __credalNet_current_cpt = this->__credalNet_current_cpt;
1793 
1794  if (this->__current_nodeType == nullptr)
1795  __current_nodeType = &__original_nodeType;
1796  else
1797  __current_nodeType = this->__current_nodeType;
1798 
1799  /*if ( ! __current_nodeType->empty() )
1800  __current_nodeType->clear();*/
1801 
1802  for (auto node : __current_bn->nodes()) {
1803  // indicatrices are already present
1804  if (__current_nodeType->exists(node)) continue;
1805 
1806  bool precise = true, vacuous = true;
1807 
1808  for (auto entry = (*__credalNet_current_cpt)[node].cbegin(),
1809  theEnd2 = (*__credalNet_current_cpt)[node].cend();
1810  entry != theEnd2;
1811  ++entry) {
1812  auto vertices = entry->size();
1813  auto var_dSize = (*entry)[0].size();
1814 
1815  if (precise && vertices > 1) precise = false;
1816 
1817  if (vacuous && vertices == var_dSize) {
1818  std::vector< bool > elem(var_dSize, false);
1819 
1820  for (auto vertex = entry->cbegin(), vEnd = entry->cend();
1821  vertex != vEnd;
1822  ++vertex) {
1823  for (auto probability = vertex->cbegin(), pEnd = vertex->cend();
1824  probability != pEnd;
1825  ++probability) {
1826  if (*probability == 1) {
1827  elem[probability - vertex->begin()] = true;
1828  break;
1829  }
1830  } // end of : for each modality
1831 
1832  break; // not vacuous
1833  } // end of : for each vertex
1834 
1835  for (auto /*std::vector< bool >::const_iterator*/ probability =
1836  elem.cbegin();
1837  probability != elem.cend();
1838  ++probability)
1839  if (*probability == false) vacuous = false;
1840 
1841  } // end of : if vertices == dSize
1842  else
1843  vacuous = false;
1844 
1845  if (vacuous == false && precise == false) {
1846  __current_nodeType->insert(node, NodeType::Credal);
1847  break;
1848  }
1849 
1850  } // end of : for each parents entry
1851 
1852  if (vacuous)
1853  __current_nodeType->insert(node, NodeType::Vacuous);
1854  else if (precise)
1855  __current_nodeType->insert(node, NodeType::Precise);
1856 
1857  } // end of : for each variable
1858  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
NodeProperty< NodeType > __original_nodeType
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:641
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
+ Here is the call graph for this function:

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

57  {
58  __src_bn.addArc(tail, head);
59  __src_bn_min.addArc(tail, head);
60  __src_bn_max.addArc(tail, head);
61  }
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624

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

References GUM_ERROR.

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

◆ 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 orignal credal sets and may induce huge imprecision.

Warning
Enlarge the orignal credal sets and therefor induce huge imprecision by propagation. Not recommanded, use MCSampling or something else instead.

Definition at line 977 of file credalNet_tpl.h.

References gum::BayesNet< GUM_SCALAR >::add(), gum::BayesNet< GUM_SCALAR >::addArc(), gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation(), gum::HashTable< Key, Val, Alloc >::clear(), gum::BayesNet< GUM_SCALAR >::cpt(), gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), gum::Set< Key, Alloc >::empty(), gum::BayesNet< GUM_SCALAR >::endTopologyTransformation(), gum::int2Pow(), gum::MultiDimDecorator< GUM_SCALAR >::pos(), gum::HashTable< Key, Val, Alloc >::size(), gum::superiorPow(), and gum::BayesNet< GUM_SCALAR >::variable().

977  {
978  // don't forget to delete the old one (__current), if necessary at the end
979  BayesNet< GUM_SCALAR >* __bin_bn = new BayesNet< GUM_SCALAR >();
980 
981  //__bnCopy ( *__bin_bn );
982  // delete old one too
983  auto __credalNet_bin_cpt = new NodeProperty<
984  std::vector< std::vector< std::vector< GUM_SCALAR > > > >();
985 
986  // delete old one too
987  NodeProperty< NodeType >* __bin_nodeType = new NodeProperty< NodeType >();
988 
989  const BayesNet< GUM_SCALAR >* __current_bn;
990  // const NodeProperty< nodeType > *__current_nodeType;
991  const NodeProperty<
992  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
994 
995  if (this->__current_bn == nullptr)
996  __current_bn = &this->__src_bn;
997  else
998  __current_bn = this->__current_bn;
999 
1000  if (this->__credalNet_current_cpt == nullptr)
1001  __credalNet_current_cpt = &this->__credalNet_src_cpt;
1002  else
1003  __credalNet_current_cpt = this->__credalNet_current_cpt;
1004 
1005  /*if ( this->__current_nodeType == nullptr )
1006  __current_nodeType = & this->__nodeType;
1007  else
1008  __current_nodeType = this->__current_nodeType;*/
1009 
1010  if (!__var_bits.empty()) __var_bits.clear();
1011 
1012  __bin_bn->beginTopologyTransformation();
1013 
1014  for (auto node : __current_bn->nodes()) {
1015  auto var_dSize = __current_bn->variable(node).domainSize();
1016 
1017  if (var_dSize != 2) {
1018  unsigned long b, c;
1019  superiorPow((unsigned long)var_dSize, b, c);
1020  Size nb_bits{Size(b)};
1021 
1022  std::string bit_name;
1023  std::vector< NodeId > bits(nb_bits);
1024 
1025  for (Size bit = 0; bit < nb_bits; bit++) {
1026  bit_name = __current_bn->variable(node).name() + " - bit - ";
1027  std::stringstream ss;
1028  ss << bit;
1029  bit_name += ss.str();
1030 
1031  LabelizedVariable var_bit(bit_name, "node " + bit_name, 2);
1032  NodeId iD = __bin_bn->add(var_bit);
1033 
1034  bits[bit] = iD;
1035  } // end of : for each bit
1036 
1037  __var_bits.insert(node, bits);
1038 
1039  } // end of : if variable is not binary
1040  else {
1041  std::string bit_name = __current_bn->variable(node).name();
1042  LabelizedVariable var_bit(bit_name, "node " + bit_name, 2);
1043  NodeId iD = __bin_bn->add(var_bit);
1044 
1045  __var_bits.insert(node, std::vector< NodeId >(1, iD));
1046  }
1047 
1048  } // end of : for each original variable
1049 
1050  for (auto node : __current_bn->nodes()) {
1051  NodeSet parents = __current_bn->parents(node);
1052 
1053  if (!parents.empty()) {
1054  for (auto par : __current_bn->parents(node)) {
1055  for (Size parent_bit = 0, spbits = Size(__var_bits[par].size());
1056  parent_bit < spbits;
1057  parent_bit++)
1058  for (Size var_bit = 0, mbits = Size(__var_bits[node].size());
1059  var_bit < mbits;
1060  var_bit++)
1061  __bin_bn->addArc(__var_bits[par][parent_bit],
1062  __var_bits[node][var_bit]);
1063  }
1064  }
1065 
1066  // arcs with one's bits
1067  auto bitsize = __var_bits[node].size();
1068 
1069  for (Size bit_c = 1; bit_c < bitsize; bit_c++)
1070  for (Size bit_p = 0; bit_p < bit_c; bit_p++)
1071  __bin_bn->addArc(__var_bits[node][bit_p], __var_bits[node][bit_c]);
1072 
1073  } // end of : for each original variable
1074 
1075  __bin_bn->endTopologyTransformation();
1076 
1077  // binarization of cpts
1078 
1079  auto varsize = __current_bn->size();
1080 
1081  for (Size var = 0; var < varsize; var++) {
1082  auto bitsize = __var_bits[var].size();
1083 
1084  for (Size i = 0; i < bitsize; i++) {
1085  Potential< GUM_SCALAR > const* potential(
1086  &__bin_bn->cpt(__var_bits[var][i]));
1087  Instantiation ins(potential);
1088  ins.setFirst();
1089 
1090  auto entry_size = potential->domainSize() / 2;
1091  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
1092  entry_size);
1093 
1094  Size old_conf = 0;
1095 
1096  for (Size conf = 0; conf < entry_size; conf++) {
1097  std::vector< std::vector< GUM_SCALAR > > pvar_cpt;
1098  auto verticessize = (*__credalNet_current_cpt)[var][old_conf].size();
1099 
1100  for (Size old_distri = 0; old_distri < verticessize; old_distri++) {
1101  const std::vector< GUM_SCALAR >& vertex =
1102  (*__credalNet_current_cpt)[var][old_conf][old_distri];
1103  auto vertexsize = vertex.size();
1104 
1105  std::vector< Idx > incc(vertexsize, 0);
1106 
1107  for (Size preced = 0; preced < i; preced++) {
1108  auto bit_pos =
1109  ins.pos(__bin_bn->variable(__var_bits[var][preced]));
1110  auto val = ins.val(bit_pos);
1111 
1112  Size pas = Size(int2Pow((unsigned long)preced));
1113  Size elem;
1114 
1115  if (val == 0)
1116  elem = 0;
1117  else
1118  elem = pas;
1119 
1120  while (elem < vertexsize) {
1121  incc[elem]++;
1122  elem++;
1123 
1124  if (elem % pas == 0) elem += pas;
1125  }
1126  }
1127 
1128  Size pas = Size(int2Pow((unsigned long)i));
1129 
1130  std::vector< GUM_SCALAR > distri(2, 0);
1131  int pos = 1;
1132 
1133  for (Size elem = 0; elem < vertexsize; elem++) {
1134  if (elem % pas == 0) pos = -pos;
1135 
1136  if (incc[elem] == i)
1137  (pos < 0) ? (distri[0] += vertex[elem])
1138  : (distri[1] += vertex[elem]);
1139  }
1140 
1141  if (i > 0) {
1142  GUM_SCALAR den = distri[0] + distri[1];
1143 
1144  if (den == 0) {
1145  distri[0] = 0;
1146  distri[1] = 0;
1147  } else {
1148  distri[0] /= den;
1149  distri[1] /= den;
1150  }
1151  }
1152 
1153  pvar_cpt.push_back(distri);
1154 
1155  } // end of old distris
1156 
1157  // get min/max approx, 2 vertices
1158  std::vector< std::vector< GUM_SCALAR > > vertices(
1159  2, std::vector< GUM_SCALAR >(2, 1));
1160  vertices[1][1] = 0;
1161 
1162  auto new_verticessize = pvar_cpt.size();
1163 
1164  for (Size v = 0; v < new_verticessize; v++) {
1165  if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1166 
1167  if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1168  }
1169 
1170  vertices[0][0] = 1 - vertices[0][1];
1171  vertices[1][0] = 1 - vertices[1][1];
1172 
1173  pvar_cpt = vertices;
1174 
1175  var_cpt[conf] = pvar_cpt;
1176 
1177  ++ins;
1178  ++ins;
1179 
1180  old_conf++;
1181 
1182  if (old_conf == (*__credalNet_current_cpt)[var].size()) old_conf = 0;
1183 
1184  } // end of new parent conf
1185 
1186  __credalNet_bin_cpt->insert(__var_bits[var][i], var_cpt);
1187 
1188  } // end of bit i
1189 
1190  } // end of old variable
1191 
1192  __bin_bn->beginTopologyTransformation();
1193 
1194  /* indicatrices variables */
1195  auto old_varsize = __var_bits.size();
1196 
1197  for (Size i = 0; i < old_varsize; i++) {
1198  auto bitsize = __var_bits[i].size();
1199 
1200  // binary variable
1201  if (bitsize == 1) continue;
1202 
1203  auto old_card = __src_bn.variable(i).domainSize();
1204 
1205  for (Size mod = 0; mod < old_card; mod++) {
1206  std::string s;
1207  s = "I-";
1208  std::stringstream ss;
1209  ss << __src_bn.variable(i).name();
1210  ss << "-";
1211  ss << mod;
1212  s += ss.str();
1213 
1214  LabelizedVariable var(s, "node " + s, 2);
1215  const NodeId indic = __bin_bn->add(var);
1216 
1217  // arcs from one's bits
1218  for (Size bit = 0; bit < bitsize; bit++)
1219  __bin_bn->addArc(__var_bits[i][bit], indic);
1220 
1221  // cpt
1222  Size num = Size(int2Pow(long(bitsize)));
1223 
1224  std::vector< std::vector< std::vector< GUM_SCALAR > > > icpt(num);
1225 
1226  for (Size entry = 0; entry < num; entry++) {
1227  std::vector< std::vector< GUM_SCALAR > > vertices(
1228  1, std::vector< GUM_SCALAR >(2, 0));
1229 
1230  if (i == entry)
1231  vertices[0][1] = 1;
1232  else
1233  vertices[0][0] = 1;
1234 
1235  icpt[entry] = vertices;
1236  }
1237 
1238  __credalNet_bin_cpt->insert(indic, icpt);
1239 
1240  __bin_nodeType->insert(indic, NodeType::Indic);
1241  } // end of each modality, i.e. as many indicatrice
1242  }
1243 
1244  __bin_bn->endTopologyTransformation();
1245 
1246  if (this->__current_bn != nullptr) delete this->__current_bn;
1247 
1248  this->__current_bn = __bin_bn;
1249 
1250  if (this->__credalNet_current_cpt != nullptr)
1251  delete this->__credalNet_current_cpt;
1252 
1253  this->__credalNet_current_cpt = __credalNet_bin_cpt;
1254 
1255  if (this->__current_nodeType != nullptr) delete this->__current_nodeType;
1256 
1257  this->__current_nodeType = __bin_nodeType;
1258 
1259  __sort_varType(); // will fill __bin_nodeType except for NodeType::Indic
1260  // variables
1261  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
NodeProperty< std::vector< NodeId > > __var_bits
Corresponding bits of each variable.
Definition: credalNet.h:638
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
unsigned long int2Pow(unsigned long exponent)
Specialized base 2 pow function with integer.
Definition: pow_inl.h:46
void __sort_varType()
Set the NodeType of each node
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:52
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

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

References gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

442  {
443  GUM_SCALAR epsi_min = 1.;
444  GUM_SCALAR epsi_max = 0.;
445  GUM_SCALAR epsi_moy = 0.;
446  GUM_SCALAR epsi_den = 0.;
447 
448  for (auto node : src_bn().nodes()) {
449  const Potential< GUM_SCALAR >* const potential(&__src_bn.cpt(node));
450 
451  Potential< GUM_SCALAR >* const potential_min(
452  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_min.cpt(node)));
453  Potential< GUM_SCALAR >* const potential_max(
454  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_max.cpt(node)));
455 
456  Size var_dSize = __src_bn.variable(node).domainSize();
457  Size entry_size = potential->domainSize() / var_dSize;
458 
459  Instantiation ins(potential);
460  Instantiation ins_min(potential_min);
461  Instantiation ins_max(potential_max);
462 
463  ins.setFirst();
464  ins_min.setFirst();
465  ins_max.setFirst();
466 
467  std::vector< GUM_SCALAR > vertex(var_dSize);
468 
469  for (Size entry = 0; entry < entry_size; entry++) {
470  GUM_SCALAR den;
471 
472  if (oneNet)
473  den = 0;
474  else
475  den = potential_max->get(ins_max);
476 
477  Size nbm = 0;
478 
479  for (Size modality = 0; modality < var_dSize; modality++) {
480  vertex[modality] = potential->get(ins);
481 
482  if (oneNet) {
483  den += vertex[modality];
484 
485  if (vertex[modality] < 1 && vertex[modality] > 0)
486  GUM_ERROR(OperationNotAllowed,
487  "bnToCredal : the BayesNet contains "
488  "probabilities and not event counts "
489  "although user precised oneNet = "
490  << oneNet);
491  }
492 
493  if (vertex[modality] > 0) nbm++;
494 
495  ++ins;
496  }
497 
499  if (!oneNet) {
500  GUM_SCALAR sum = 0;
501 
502  for (auto modality = vertex.cbegin(), theEnd = vertex.cend();
503  modality != theEnd;
504  ++modality) {
505  sum += *modality;
506  }
507 
508  if (std::fabs(1. - sum) > __epsRedund) {
509  GUM_ERROR(CPTNoSumTo1,
510  __src_bn.variable(node).name() << "(" << __epsRedund << ")"
511  << " " << entry << std::endl
512  << vertex << std::endl
513  << ins << std::endl);
514  }
515  }
516 
518 
519  GUM_SCALAR epsilon;
520 
521  if (beta == 0)
522  epsilon = 0;
523  else if (den == 0 || beta == 1)
524  epsilon = GUM_SCALAR(1.0);
525  else
526  epsilon = GUM_SCALAR(std::pow(beta, std::log1p(den)));
527 
528  epsi_moy += epsilon;
529  epsi_den += 1;
530 
531  if (epsilon > epsi_max) epsi_max = epsilon;
532 
533  if (epsilon < epsi_min) epsi_min = epsilon;
534 
535  GUM_SCALAR min, max;
536 
537  for (Size modality = 0; modality < var_dSize; modality++) {
538  if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
539  min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
540 
541  if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
542 
543  max = GUM_SCALAR(min + epsilon);
544  } else { // if ( ( vertex[modality] == 0 && keepZeroes ) || (
545  // vertex[modality] > 0 && nbm <= 1 ) || ( vertex[modality] == 0
546  // && nbm <= 1 ) ) {
547  min = vertex[modality];
548 
549  if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
550 
551  max = min;
552  }
553 
554  potential_min->set(ins_min, min);
555  potential_max->set(ins_max, max);
556 
557  ++ins_min;
558  ++ins_max;
559  } // end of : for each modality
560 
561  } // end of : for each entry
562 
563  } // end of : for each variable
564 
565  __epsilonMin = epsi_min;
566  __epsilonMax = epsi_max;
567  __epsilonMoy = (GUM_SCALAR)epsi_moy / (GUM_SCALAR)epsi_den;
568 
570  }
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:594
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
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:590
GUM_SCALAR __epsilonMin
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:586
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
const BayesNet< GUM_SCALAR > & src_bn() const
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
GUM_SCALAR __epsRedund
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:599
+ Here is the call graph for this function:

◆ computeCPTMinMax()

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

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

1303  {
1304  __binCptMin.resize(current_bn().size());
1305  __binCptMax.resize(current_bn().size());
1306 
1307  for (auto node : current_bn().nodes()) {
1308  auto pConf = credalNet_currentCpt()[node].size();
1309  std::vector< GUM_SCALAR > min(pConf);
1310  std::vector< GUM_SCALAR > max(pConf);
1311 
1312  for (Size pconf = 0; pconf < pConf; pconf++) {
1313  GUM_SCALAR v1, v2;
1314  v1 = credalNet_currentCpt()[node][pconf][0][1];
1315 
1316  if (credalNet_currentCpt()[node][pconf].size() > 1)
1317  v2 = credalNet_currentCpt()[node][pconf][1][1];
1318  else
1319  v2 = v1;
1320 
1321  GUM_SCALAR delta = v1 - v2;
1322  min[pconf] = (delta >= 0) ? v2 : v1;
1323  max[pconf] = (delta >= 0) ? v1 : v2;
1324  }
1325 
1326  __binCptMin[node] = min;
1327  __binCptMax[node] = max;
1328  }
1329 
1330  __hasComputedCPTMinMax = true;
1331  }
std::vector< std::vector< GUM_SCALAR > > __binCptMax
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:662
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:647
const BayesNet< GUM_SCALAR > & current_bn() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
std::vector< std::vector< GUM_SCALAR > > __binCptMin
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:654
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const

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

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

1265  {
1266  if (__credalNet_current_cpt != nullptr) return *__credalNet_current_cpt;
1267 
1268  return __credalNet_src_cpt;
1269  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
+ Here is the caller graph for this function:

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

1273  {
1274  return __credalNet_src_cpt;
1275  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631

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

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

1404  {
1405  if (__current_bn != nullptr) return *__current_bn;
1406 
1407  return __src_bn;
1408  }
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
+ Here is the caller graph for this function:

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

Referenced by gum::credal::CNLoopyPropagation< GUM_SCALAR >::_makeInferenceByOrderedArcs(), and gum::credal::CNLoopyPropagation< GUM_SCALAR >::_makeInferenceByRandomOrder().

1279  {
1280  if (__current_nodeType != nullptr) return (*(__current_nodeType))[id];
1281 
1282  return __original_nodeType[id];
1283  }
NodeProperty< NodeType > __original_nodeType
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:641
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:643
+ Here is the caller graph for this function:

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

393  {
394  return __src_bn.variable(id).domainSize();
395  }
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619

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

1351  {
1352  return __epsilonMax;
1353  }
GUM_SCALAR __epsilonMax
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:590

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

1356  {
1357  return __epsilonMoy;
1358  }
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:594

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

1346  {
1347  return __epsilonMin;
1348  }
GUM_SCALAR __epsilonMin
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:586

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

Definition at line 271 of file credalNet_tpl.h.

References gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

275  {
276  Potential< GUM_SCALAR >* const potential_min(
277  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_min.cpt(id)));
278  Potential< GUM_SCALAR >* const potential_max(
279  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_max.cpt(id)));
280 
281  auto var_dSize = __src_bn.variable(id).domainSize();
282 
283  if (lower.size() != var_dSize || upper.size() != var_dSize)
284  GUM_ERROR(
285  SizeError,
286  "setCPT : variable modalities in cpts does not match for node id : "
287  << id << " with sizes of constraints : ( " << lower.size() << " || "
288  << upper.size() << " ) != " << var_dSize);
289 
290  auto entry_size = potential_min->domainSize() / var_dSize;
291 
292  if (entry >= entry_size)
293  GUM_ERROR(SizeError,
294  "setCPT : entry is greater or equal than entry size "
295  "(entries start at 0 up to entry_size - 1) : "
296  << entry << " >= " << entry_size);
297 
298  Instantiation min(potential_min);
299  Instantiation max(potential_max);
300  min.setFirst();
301  max.setFirst();
302 
303  Idx pos = 0;
304 
305  while (pos != entry) {
306  ++min;
307  ++max;
308  ++pos;
309  }
310 
311  for (Size i = 0; i < var_dSize; i++) {
312  potential_min->set(min, lower[i]);
313  potential_max->set(max, upper[i]);
314  ++min;
315  ++max;
316  }
317  }
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

References gum::DiscreteVariable::domainSize(), gum::Instantiation::forgetMaster(), GUM_ERROR, gum::Instantiation::nbrDim(), gum::Instantiation::reorder(), gum::Instantiation::val(), gum::Instantiation::variable(), gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence(), and gum::Instantiation::variablesSequence().

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

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

Definition at line 255 of file credalNet_tpl.h.

References GUM_ERROR.

258  {
259  try {
260  __src_bn_min.cpt(id).fillWith(lower);
261  __src_bn_max.cpt(id).fillWith(upper);
262  } catch (const SizeError&) {
263  GUM_ERROR(
264  SizeError,
265  "fillConstraints : sizes does not match in fillWith for node id : "
266  << id);
267  }
268  }
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624

◆ get_CPT_max()

template<typename GUM_SCALAR >
const std::vector< std::vector< GUM_SCALAR > > & gum::credal::CredalNet< GUM_SCALAR >::get_CPT_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 1341 of file credalNet_tpl.h.

1341  {
1342  return __binCptMax;
1343  }
std::vector< std::vector< GUM_SCALAR > > __binCptMax
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:662

◆ get_CPT_min()

template<typename GUM_SCALAR >
const std::vector< std::vector< GUM_SCALAR > > & gum::credal::CredalNet< GUM_SCALAR >::get_CPT_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 1335 of file credalNet_tpl.h.

1335  {
1336  return __binCptMin;
1337  }
std::vector< std::vector< GUM_SCALAR > > __binCptMin
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:654

◆ hasComputedCPTMinMax()

template<typename GUM_SCALAR >
const bool gum::credal::CredalNet< GUM_SCALAR >::hasComputedCPTMinMax ( ) const
Returns
Returns TRUE if this CredalNet has called computeCPTMinMax() 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 1297 of file credalNet_tpl.h.

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

1297  {
1298  return __hasComputedCPTMinMax;
1299  }
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:647
+ Here is the caller graph for this function:

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

References GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

622  {
623  for (auto node : __src_bn.nodes()) {
624  const Potential< GUM_SCALAR >* const potential(&__src_bn.cpt(node));
625 
626  Potential< GUM_SCALAR >* const potential_min(
627  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_min.cpt(node)));
628  Potential< GUM_SCALAR >* const potential_max(
629  const_cast< Potential< GUM_SCALAR >* const >(&__src_bn_max.cpt(node)));
630 
631  Size var_dSize = __src_bn.variable(node).domainSize();
632  Size entry_size = potential->domainSize() / var_dSize;
633 
634  Instantiation ins(potential);
635  Instantiation ins_min(potential_min);
636  Instantiation ins_max(potential_max);
637 
638  ins.setFirst();
639  ins_min.setFirst();
640  ins_max.setFirst();
641 
642  std::vector< GUM_SCALAR > vertex(var_dSize);
643 
644  for (Size entry = 0; entry < entry_size; entry++) {
645  GUM_SCALAR den = 0;
646  Size nbm = 0;
647 
648  for (Size modality = 0; modality < var_dSize; modality++) {
649  vertex[modality] = potential->get(ins);
650 
651  if (vertex[modality] < 1 && vertex[modality] > 0)
652  GUM_ERROR(OperationNotAllowed,
653  "idmLearning : the BayesNet contains "
654  "probabilities and not event counts.");
655 
656  den += vertex[modality];
657 
658  if (vertex[modality] > 0) nbm++;
659 
660  ++ins;
661  }
662 
663  if (nbm > 1 || !keepZeroes) den += s;
664 
665  GUM_SCALAR min, max;
666 
667  for (Size modality = 0; modality < var_dSize; modality++) {
668  min = vertex[modality];
669  max = min;
670 
671  if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
672 
673  min = GUM_SCALAR(min * 1.0 / den);
674  max = GUM_SCALAR(max * 1.0 / den);
675 
676  potential_min->set(ins_min, min);
677  potential_max->set(ins_max, max);
678 
679  ++ins_min;
680  ++ins_max;
681  } // end of : for each modality
682 
683  } // end of : for each entry
684 
685  } // end of : for each variable
686 
687  __epsilonMin = GUM_SCALAR(s);
688  __epsilonMax = GUM_SCALAR(s);
689  __epsilonMoy = GUM_SCALAR(s);
691  }
GUM_SCALAR __epsilonMoy
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:594
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
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:590
GUM_SCALAR __epsilonMin
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:586
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

388  {
389  return Instantiation(__src_bn.cpt(id));
390  }
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619

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

References gum::DiscreteVariable::domainSize(), gum::credal::LRSWrapper< GUM_SCALAR >::fillH(), gum::credal::LRSWrapper< GUM_SCALAR >::getOutput(), gum::credal::LRSWrapper< GUM_SCALAR >::H2V(), gum::credal::LRSWrapper< GUM_SCALAR >::nextHInput(), gum::Instantiation::setFirst(), gum::credal::LRSWrapper< GUM_SCALAR >::setUpH(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

792  {
793  if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
794 
795  __credalNet_src_cpt.resize(__src_bn.size());
796 
797  LRSWrapper< GUM_SCALAR > lrsWrapper;
798 
799  for (auto node : __src_bn.nodes()) {
800  const Potential< GUM_SCALAR >* const potential_min(
801  &__src_bn_min.cpt(node));
802  const Potential< GUM_SCALAR >* const potential_max(
803  &__src_bn_max.cpt(node));
804 
805  Size var_dSize = __src_bn.variable(node).domainSize();
806  Size entry_size = potential_min->domainSize() / var_dSize;
807 
808  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
809  entry_size);
810 
811  Instantiation ins_min(potential_min);
812  Instantiation ins_max(potential_max);
813 
814  ins_min.setFirst();
815  ins_max.setFirst();
816 
817  lrsWrapper.setUpH(var_dSize);
818 
819  for (Size entry = 0; entry < entry_size; entry++) {
820  for (Size modality = 0; modality < var_dSize; modality++) {
821  lrsWrapper.fillH(
822  potential_min->get(ins_min), potential_max->get(ins_max), modality);
823  ++ins_min;
824  ++ins_max;
825  }
826 
827  lrsWrapper.H2V();
828  var_cpt[entry] = lrsWrapper.getOutput();
829  lrsWrapper.nextHInput();
830  }
831 
832  __credalNet_src_cpt.insert(node, var_cpt);
833 
834  } // end of : for each variable (node)
835 
836  // get precise/credal/vacuous status of each variable
837  __sort_varType();
838  __separatelySpecified = true;
839  }
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:616
void __sort_varType()
Set the NodeType of each node
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

References gum::DiscreteVariable::domainSize(), gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

843  {
844  if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
845 
846  __credalNet_src_cpt.resize(__src_bn.size());
847 
848  for (auto node : __src_bn.nodes()) {
849  const Potential< GUM_SCALAR >* const potential_min(
850  &__src_bn_min.cpt(node));
851  const Potential< GUM_SCALAR >* const potential_max(
852  &__src_bn_max.cpt(node));
853 
854  auto var_dSize = __src_bn.variable(node).domainSize();
855  auto entry_size = potential_min->domainSize() / var_dSize;
856 
857  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
858  entry_size);
859 
860  Instantiation ins_min(potential_min);
861  Instantiation ins_max(potential_max);
862 
863  ins_min.setFirst();
864  ins_max.setFirst();
865 
866  // use iterator
867  for (Size entry = 0; entry < entry_size; entry++) {
868  std::vector< std::vector< GUM_SCALAR > > vertices;
869  std::vector< GUM_SCALAR > vertex(var_dSize); // if not interval
870 
871  std::vector< std::vector< GUM_SCALAR > > inequalities(
872  var_dSize * 2, std::vector< GUM_SCALAR >(var_dSize + 1, 0));
873 
874  std::vector< GUM_SCALAR > sum_ineq1(var_dSize + 1, -1);
875  std::vector< GUM_SCALAR > sum_ineq2(var_dSize + 1, 1);
876  sum_ineq1[0] = 1;
877  sum_ineq2[0] = -1;
878 
879  bool isInterval = false;
880 
881  for (Size modality = 0; modality < var_dSize; modality++) {
882  inequalities[modality * 2][0] = -potential_min->get(ins_min);
883  inequalities[modality * 2 + 1][0] = potential_max->get(ins_max);
884  inequalities[modality * 2][modality + 1] = 1;
885  inequalities[modality * 2 + 1][modality + 1] = -1;
886 
887  vertex[modality] = inequalities[modality * 2 + 1][0];
888 
889  if (!isInterval
890  && (-inequalities[modality * 2][0]
891  != inequalities[modality * 2 + 1][0]))
892  isInterval = true;
893 
894  ++ins_min;
895  ++ins_max;
896  }
897 
898  inequalities.push_back(sum_ineq1);
899  inequalities.push_back(sum_ineq2);
900 
901  if (!isInterval) {
902  vertices.push_back(vertex);
903  } else {
904  try {
905  __H2Vlrs(inequalities, vertices);
906  //__H2Vcdd ( inequalities, vertices );
907  } catch (const std::exception& err) {
908  std::cout << err.what() << std::endl;
909  throw;
910  }
911 
912  } // end of : is interval
913 
914  if (entry == 0 && vertices.size() >= 2) {
915  auto tmp = vertices[0];
916  vertices[0] = vertices[1];
917  vertices[1] = tmp;
918  }
919 
920  var_cpt[entry] = vertices;
921 
922  } // end of : for each entry
923 
924  __credalNet_src_cpt.insert(node, var_cpt);
925  // std::cout << __src_bn.variable(node_idIt).name() << std::endl;
926  // std::cout << var_cpt << std::endl;
927 
928  } // end of : for each variable (node)
929 
930  // get precise/credal/vacuous status of each variable
931  __sort_varType();
932  __separatelySpecified = true;
933  }
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:616
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_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

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

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

1292  {
1293  return __separatelySpecified;
1294  }
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:616
+ Here is the caller graph for this function:

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

References gum::DiscreteVariable::domainSize(), GUM_ERROR, gum::Instantiation::setFirst(), and gum::MultiDimDecorator< GUM_SCALAR >::variable().

573  {
574  for (auto node : __src_bn.nodes()) {
575  const Potential< GUM_SCALAR >* const potential(&__src_bn.cpt(node));
576 
577  auto var_dSize = __src_bn.variable(node).domainSize();
578  auto entry_size = potential->domainSize() / var_dSize;
579 
580  Instantiation ins(potential);
581 
582  ins.setFirst();
583 
584  std::vector< GUM_SCALAR > vertex(var_dSize);
585 
586  for (Size entry = 0; entry < entry_size; entry++) {
587  GUM_SCALAR den = 0;
588  bool zeroes = false;
589  Instantiation ins_prev = ins;
590 
591  for (Size modality = 0; modality < var_dSize; modality++) {
592  vertex[modality] = potential->get(ins);
593 
594  if (vertex[modality] < 1 && vertex[modality] > 0)
595  GUM_ERROR(OperationNotAllowed,
596  "lagrangeNormalization : the BayesNet "
597  "contains probabilities and not event "
598  "counts.");
599 
600  den += vertex[modality];
601 
602  if (!zeroes && vertex[modality] == 0) { zeroes = true; }
603 
604  ++ins;
605  }
606 
607  if (zeroes) {
608  ins = ins_prev;
609 
610  for (Size modality = 0; modality < var_dSize; modality++) {
611  potential->set(ins, potential->get(ins) + 1);
612  ++ins;
613  }
614  }
615 
616  } // end of : for each entry
617 
618  } // end of : for each variable
619  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

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

1287  {
1288  return __original_nodeType[id];
1289  }
NodeProperty< NodeType > __original_nodeType
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:641

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

References GUM_ERROR, GUM_SHOWERROR, and gum::BIFWriter< GUM_SCALAR >::write().

942  {
943  BIFWriter< GUM_SCALAR > writer;
944 
945  std::string minfilename = min_path; //"min.bif";
946  std::string maxfilename = max_path; //"max.bif";
947  std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
948  std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
949 
950  if (!min_file.good())
951  GUM_ERROR(
952  IOError,
953  "bnToCredal() : could not open stream : min_file : " << minfilename);
954 
955  if (!max_file.good()) {
956  min_file.close();
957  GUM_ERROR(
958  IOError,
959  "bnToCredal() : could not open stream : min_file : " << maxfilename);
960  }
961 
962  try {
963  writer.write(min_file, __src_bn_min);
964  writer.write(max_file, __src_bn_max);
965  } catch (Exception& err) {
966  GUM_SHOWERROR(err);
967  min_file.close();
968  max_file.close();
969  throw(err);
970  }
971 
972  min_file.close();
973  max_file.close();
974  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:58
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:622
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:624
+ Here is the call graph for this function:

◆ 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.

References gum::DiscreteVariable::domainSize(), GUM_ERROR, and gum::MultiDimDecorator< GUM_SCALAR >::variable().

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

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

References gum::DiscreteVariable::domainSize(), gum::Instantiation::forgetMaster(), GUM_ERROR, gum::Instantiation::nbrDim(), gum::Instantiation::reorder(), gum::Instantiation::val(), gum::MultiDimDecorator< GUM_SCALAR >::variable(), gum::Instantiation::variable(), and gum::Instantiation::variablesSequence().

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

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

References gum::DiscreteVariable::domainSize(), GUM_ERROR, and gum::MultiDimDecorator< GUM_SCALAR >::variable().

66  {
67  const Potential< GUM_SCALAR >* const potential(&__src_bn.cpt(id));
68 
69  auto var_dSize = __src_bn.variable(id).domainSize();
70  auto entry_size = potential->domainSize() / var_dSize;
71 
72  if (cpt.size() != entry_size)
73  GUM_ERROR(SizeError,
74  "setCPTs : entry sizes of cpts does not match for node id : "
75  << id << " : " << cpt.size() << " != " << entry_size);
76 
77  for (const auto& cset : cpt) {
78  if (cset.size() == 0)
79  GUM_ERROR(
80  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 << " : "
90  << vertex.size() << " != " << 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:631
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:

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

1411  {
1412  return __src_bn;
1413  }
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619

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

References gum::BayesNet< GUM_SCALAR >::cpt(), gum::MultiDimDecorator< GUM_SCALAR >::domainSize(), gum::Instantiation::erase(), gum::Instantiation::forgetMaster(), gum::DAGmodel::nodes(), gum::Instantiation::setFirst(), and gum::BayesNet< GUM_SCALAR >::variable().

1361  {
1362  std::stringstream output;
1363  const BayesNet< GUM_SCALAR >* __current_bn;
1364  const NodeProperty<
1365  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1367 
1368  if (this->__current_bn == nullptr)
1369  __current_bn = &this->__src_bn;
1370  else
1371  __current_bn = this->__current_bn;
1372 
1373  if (this->__credalNet_current_cpt == nullptr)
1374  __credalNet_current_cpt = &this->__credalNet_src_cpt;
1375  else
1376  __credalNet_current_cpt = this->__credalNet_current_cpt;
1377 
1378  for (auto node : __current_bn->nodes()) {
1379  const Potential< GUM_SCALAR >* potential(&__current_bn->cpt(node));
1380  auto pconfs =
1381  potential->domainSize() / __current_bn->variable(node).domainSize();
1382 
1383  output << "\n" << __current_bn->variable(node) << "\n";
1384 
1385  Instantiation ins(potential);
1386  ins.forgetMaster();
1387  ins.erase(__current_bn->variable(node));
1388  ins.setFirst();
1389 
1390  for (Size pconf = 0; pconf < pconfs; pconf++) {
1391  output << ins << " : ";
1392  output << (*__credalNet_current_cpt)[node][pconf] << "\n";
1393 
1394  if (pconf < pconfs - 1) ++ins;
1395  }
1396  }
1397 
1398  output << "\n";
1399 
1400  return output.str();
1401  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:627
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:635
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:631
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:619
+ Here is the call graph for this function:

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 662 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 654 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 635 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 631 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 627 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 643 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 581 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 608 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 604 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 590 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 586 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 594 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 599 of file credalNet.h.

◆ __hasComputedCPTMinMax

template<typename GUM_SCALAR>
bool gum::credal::CredalNet< GUM_SCALAR >::__hasComputedCPTMinMax
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 647 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 641 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 611 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 579 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 616 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 619 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 624 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 622 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 638 of file credalNet.h.


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