aGrUM  0.16.0
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 89 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 92 of file credalNet.h.

92 : 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 31 of file credalNet_tpl.h.

31  {
32  __initParams();
33 
34  __src_bn = BayesNet< GUM_SCALAR >();
35  __src_bn_min = BayesNet< GUM_SCALAR >();
36  __src_bn_max = BayesNet< GUM_SCALAR >();
37 
38  GUM_CONSTRUCTOR(CredalNet);
39  }
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:624
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:626

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

References GUM_SHOWERROR.

403  {
404  try {
405  __initParams();
406  __initCNNets(src_min_num, src_max_den);
407  } catch (Exception& err) {
408  GUM_SHOWERROR(err);
409  throw(err);
410  }
411 
412  GUM_CONSTRUCTOR(CredalNet);
413  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
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:31

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

References GUM_SHOWERROR.

417  {
418  try {
419  __initParams();
420  __initCNNets(src_min_num, src_max_den);
421  } catch (Exception& err) {
422  GUM_SHOWERROR(err);
423  throw(err);
424  }
425 
426  GUM_CONSTRUCTOR(CredalNet);
427  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
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:31

◆ ~CredalNet()

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

Destructor.

Definition at line 430 of file credalNet_tpl.h.

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

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 1512 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().

1512  {
1513  const BayesNet< GUM_SCALAR >* __current_bn;
1514 
1515  if (this->__current_bn == nullptr)
1516  __current_bn = &this->__src_bn;
1517  else
1518  __current_bn = this->__current_bn;
1519 
1520  for (auto node : __current_bn->nodes())
1521  dest.add(__current_bn->variable(node));
1522 
1523  dest.beginTopologyTransformation();
1524 
1525  for (auto node : __current_bn->nodes()) {
1526  for (auto parent_idIt : __current_bn->cpt(node).variablesSequence()) {
1527  if (__current_bn->nodeId(*parent_idIt) != node)
1528  dest.addArc(__current_bn->nodeId(*parent_idIt), node);
1529  } // end of : for each parent in order of appearence
1530  } // end of : for each variable
1531 
1532  dest.endTopologyTransformation();
1533  }
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:629
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
+ 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 1498 of file credalNet_tpl.h.

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

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

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

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

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

1450  {
1451  BIFReader< GUM_SCALAR > reader(&__src_bn, src_min_num);
1452  std::string other;
1453 
1454  if (src_max_den.compare("") != 0)
1455  other = src_max_den;
1456  else
1457  other = src_min_num;
1458 
1459  BIFReader< GUM_SCALAR > reader_min(&__src_bn_min, src_min_num);
1460  BIFReader< GUM_SCALAR > reader_max(&__src_bn_max, other);
1461 
1462  try {
1463  reader.proceed();
1464  } catch (Exception& err) {
1465  GUM_SHOWERROR(err);
1466  throw(err);
1467  }
1468 
1469  try {
1470  reader_min.proceed();
1471  } catch (Exception& err) {
1472  GUM_SHOWERROR(err);
1473  throw(err);
1474  }
1475 
1476  try {
1477  reader_max.proceed();
1478  } catch (Exception& err) {
1479  GUM_SHOWERROR(err);
1480  throw(err);
1481  }
1482  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:624
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:626
+ 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 1485 of file credalNet_tpl.h.

References gum::DAGmodel::size().

1487  {
1488  __src_bn = src_min_num;
1489  __src_bn_min = src_min_num;
1490 
1491  if (src_max_den.size() > 0)
1492  __src_bn_max = src_max_den;
1493  else
1494  __src_bn_max = src_min_num;
1495  }
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:624
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:626
+ 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 1422 of file credalNet_tpl.h.

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

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

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

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

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

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

◆ addVariable()

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

Adds a discrete node into the network.

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

Definition at line 42 of file credalNet_tpl.h.

References GUM_ERROR.

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

◆ 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 979 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().

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

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

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

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

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

1267  {
1268  if (__credalNet_current_cpt != nullptr) return *__credalNet_current_cpt;
1269 
1270  return __credalNet_src_cpt;
1271  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:637
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:633
+ 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 1275 of file credalNet_tpl.h.

1275  {
1276  return __credalNet_src_cpt;
1277  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:633

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

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

1406  {
1407  if (__current_bn != nullptr) return *__current_bn;
1408 
1409  return __src_bn;
1410  }
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:629
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
+ 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 1281 of file credalNet_tpl.h.

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

1281  {
1282  if (__current_nodeType != nullptr) return (*(__current_nodeType))[id];
1283 
1284  return __original_nodeType[id];
1285  }
NodeProperty< NodeType > __original_nodeType
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:643
NodeProperty< NodeType > * __current_nodeType
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:645
+ 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 395 of file credalNet_tpl.h.

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

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

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

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

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

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

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

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

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

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

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

References GUM_ERROR.

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

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

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

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

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

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

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

1299  {
1300  return __hasComputedCPTMinMax;
1301  }
bool __hasComputedCPTMinMax
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:649
+ 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 624 of file credalNet_tpl.h.

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

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

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

◆ 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 794 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().

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

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

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

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

1294  {
1295  return __separatelySpecified;
1296  }
bool __separatelySpecified
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:618
+ 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 575 of file credalNet_tpl.h.

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

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

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

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

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

944  {
945  BIFWriter< GUM_SCALAR > writer;
946 
947  std::string minfilename = min_path; //"min.bif";
948  std::string maxfilename = max_path; //"max.bif";
949  std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
950  std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
951 
952  if (!min_file.good())
953  GUM_ERROR(
954  IOError,
955  "bnToCredal() : could not open stream : min_file : " << minfilename);
956 
957  if (!max_file.good()) {
958  min_file.close();
959  GUM_ERROR(
960  IOError,
961  "bnToCredal() : could not open stream : min_file : " << maxfilename);
962  }
963 
964  try {
965  writer.write(min_file, __src_bn_min);
966  writer.write(max_file, __src_bn_max);
967  } catch (Exception& err) {
968  GUM_SHOWERROR(err);
969  min_file.close();
970  max_file.close();
971  throw(err);
972  }
973 
974  min_file.close();
975  max_file.close();
976  }
#define GUM_SHOWERROR(e)
Definition: exceptions.h:61
BayesNet< GUM_SCALAR > __src_bn_min
BayesNet used to store lower probabilities.
Definition: credalNet.h:624
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
BayesNet< GUM_SCALAR > __src_bn_max
BayesNet used to store upper probabilities.
Definition: credalNet.h:626
+ 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 112 of file credalNet_tpl.h.

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

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

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

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

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

1413  {
1414  return __src_bn;
1415  }
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621

◆ 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 1363 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().

1363  {
1364  std::stringstream output;
1365  const BayesNet< GUM_SCALAR >* __current_bn;
1366  const NodeProperty<
1367  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1369 
1370  if (this->__current_bn == nullptr)
1371  __current_bn = &this->__src_bn;
1372  else
1373  __current_bn = this->__current_bn;
1374 
1375  if (this->__credalNet_current_cpt == nullptr)
1376  __credalNet_current_cpt = &this->__credalNet_src_cpt;
1377  else
1378  __credalNet_current_cpt = this->__credalNet_current_cpt;
1379 
1380  for (auto node : __current_bn->nodes()) {
1381  const Potential< GUM_SCALAR >* potential(&__current_bn->cpt(node));
1382  auto pconfs =
1383  potential->domainSize() / __current_bn->variable(node).domainSize();
1384 
1385  output << "\n" << __current_bn->variable(node) << "\n";
1386 
1387  Instantiation ins(potential);
1388  ins.forgetMaster();
1389  ins.erase(__current_bn->variable(node));
1390  ins.setFirst();
1391 
1392  for (Size pconf = 0; pconf < pconfs; pconf++) {
1393  output << ins << " : ";
1394  output << (*__credalNet_current_cpt)[node][pconf] << "\n";
1395 
1396  if (pconf < pconfs - 1) ++ins;
1397  }
1398  }
1399 
1400  output << "\n";
1401 
1402  return output.str();
1403  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
BayesNet< GUM_SCALAR > * __current_bn
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:629
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * __credalNet_current_cpt
This CredalNet up-to-date CPTs.
Definition: credalNet.h:637
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > __credalNet_src_cpt
This CredalNet original CPTs.
Definition: credalNet.h:633
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
BayesNet< GUM_SCALAR > __src_bn
Original BayesNet (used as a DAG).
Definition: credalNet.h:621
+ 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 664 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 656 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 637 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 633 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 629 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 645 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 583 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 610 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 606 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 592 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 588 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 596 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 601 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 649 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 643 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 613 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 581 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 618 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 621 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 626 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 624 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 640 of file credalNet.h.


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