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

Class template representing a Credal Network. More...

#include <agrum/CN/credalNet.h>

+ Collaboration diagram for gum::credal::CredalNet< GUM_SCALAR >:

Public Member Functions

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

Public Types

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

Detailed Description

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

Class template representing a Credal Network.

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

Definition at line 88 of file credalNet.h.

Member Enumeration Documentation

◆ NodeType

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

NodeType to speed-up computations in some algorithms.

Enumerator
Precise 
Credal 
Vacuous 
Indic 

Definition at line 91 of file credalNet.h.

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

Constructor & Destructor Documentation

◆ CredalNet() [1/3]

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

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

node by node, arc by arc, manually filling potentials.

Definition at line 31 of file credalNet_tpl.h.

31  {
32  initParams__();
33 
34  src_bn__ = BayesNet< GUM_SCALAR >();
35  src_bn_min__ = BayesNet< GUM_SCALAR >();
36  src_bn_max__ = BayesNet< GUM_SCALAR >();
37 
38  GUM_CONSTRUCTOR(CredalNet);
39  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
void initParams__()
Initialize private constant variables after the Constructor has been called.

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

405  {
406  initParams__();
407  initCNNets__(src_min_num, src_max_den);
408 
409  GUM_CONSTRUCTOR(CredalNet);
410  }
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
void initCNNets__(const std::string &src_min_num, const std::string &src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
void initParams__()
Initialize private constant variables after the Constructor has been called.

◆ CredalNet() [3/3]

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

Constructor for interval defined credal network which takes 2 BayesNet.

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

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

Definition at line 413 of file credalNet_tpl.h.

414  {
415  initParams__();
416  initCNNets__(src_min_num, src_max_den);
417 
418  GUM_CONSTRUCTOR(CredalNet);
419  }
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31
void initCNNets__(const std::string &src_min_num, const std::string &src_max_den)
Initialize private BayesNet variables after the Constructor has been called.
void initParams__()
Initialize private constant variables after the Constructor has been called.

◆ ~CredalNet()

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

Destructor.

Definition at line 422 of file credalNet_tpl.h.

422  {
423  if (current_bn__ != nullptr) delete current_bn__;
424 
425  if (credalNet_current_cpt__ != nullptr) delete credalNet_current_cpt__;
426 
427  if (current_nodeType__ != nullptr) delete current_nodeType__;
428 
429  GUM_DESTRUCTOR(CredalNet);
430  }
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * credalNet_current_cpt__
This CredalNet up-to-date CPTs.
Definition: credalNet.h:610
NodeProperty< NodeType > * current_nodeType__
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:618
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
Definition: credalNet_tpl.h:31

Member Function Documentation

◆ 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_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

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_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ approximatedBinarization()

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

Approximate binarization.

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

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

Definition at line 981 of file credalNet_tpl.h.

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

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

1497  {
1498  const BayesNet< GUM_SCALAR >* current_bn__;
1499 
1500  if (this->current_bn__ == nullptr)
1501  current_bn__ = &this->src_bn__;
1502  else
1503  current_bn__ = this->current_bn__;
1504 
1505  for (auto node: current_bn__->nodes())
1506  dest.add(current_bn__->variable(node));
1507 
1508  dest.beginTopologyTransformation();
1509 
1510  for (auto node: current_bn__->nodes()) {
1511  for (auto parent_idIt: current_bn__->cpt(node).variablesSequence()) {
1512  if (current_bn__->nodeId(*parent_idIt) != node)
1513  dest.addArc(current_bn__->nodeId(*parent_idIt), node);
1514  } // end of : for each parent in order of appearence
1515  } // end of : for each variable
1516 
1517  dest.endTopologyTransformation();
1518  }
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

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

◆ computeBinaryCPTMinMax()

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

Used with binary networks to speed-up L2U inference.

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

Definition at line 1307 of file credalNet_tpl.h.

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

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

1269  {
1270  if (credalNet_current_cpt__ != nullptr) return *credalNet_current_cpt__;
1271 
1272  return credalNet_src_cpt__;
1273  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * credalNet_current_cpt__
This CredalNet up-to-date CPTs.
Definition: credalNet.h:610
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606

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

1277  {
1278  return credalNet_src_cpt__;
1279  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606

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

1408  {
1409  if (current_bn__ != nullptr) return *current_bn__;
1410 
1411  return src_bn__;
1412  }
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1283  {
1284  if (current_nodeType__ != nullptr) return (*(current_nodeType__))[id];
1285 
1286  return original_nodeType__[id];
1287  }
NodeProperty< NodeType > original_nodeType__
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:616
NodeProperty< NodeType > * current_nodeType__
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:618

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

397  {
398  return src_bn__.variable(id).domainSize();
399  }
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1355  {
1356  return epsilonMax__;
1357  }
GUM_SCALAR epsilonMax__
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:565

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

1360  {
1361  return epsilonMoy__;
1362  }
GUM_SCALAR epsilonMoy__
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:569

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

1350  {
1351  return epsilonMin__;
1352  }
GUM_SCALAR epsilonMin__
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:561

◆ fillConstraint() [1/2]

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::fillConstraint ( const NodeId id,
const Idx entry,
const std::vector< GUM_SCALAR > &  lower,
const std::vector< GUM_SCALAR > &  upper 
)

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

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

You need to call intervalToCredal when done filling all constraints.

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

Definition at line 275 of file credalNet_tpl.h.

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

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

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

◆ fillConstraints()

template<typename GUM_SCALAR >
void gum::credal::CredalNet< GUM_SCALAR >::fillConstraints ( const NodeId id,
const std::vector< GUM_SCALAR > &  lower,
const std::vector< GUM_SCALAR > &  upper 
)

Set the interval constraints of the credal sets of a given node (all instantiations )

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

You need to call intervalToCredal when done filling all constraints.

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

Definition at line 259 of file credalNet_tpl.h.

262  {
263  try {
264  src_bn_min__.cpt(id).fillWith(lower);
265  src_bn_max__.cpt(id).fillWith(upper);
266  } catch (const SizeError&) {
267  GUM_ERROR(
268  SizeError,
269  "fillConstraints : sizes does not match in fillWith for node id : "
270  << id);
271  }
272  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

1485  {
1486  Size vertices_size = 0;
1487 
1488  for (auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd;
1489  ++entry) {
1490  if (entry->size() > vertices_size) vertices_size = Size(entry->size());
1491  }
1492 
1493  return int(vertices_size);
1494  }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ get_binaryCPT_max()

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

Used with binary networks to speed-up L2U inference.

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

Definition at line 1345 of file credalNet_tpl.h.

1345  {
1346  return binCptMax__;
1347  }
std::vector< std::vector< GUM_SCALAR > > binCptMax__
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:637

◆ get_binaryCPT_min()

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

Used with binary networks to speed-up L2U inference.

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

Definition at line 1339 of file credalNet_tpl.h.

1339  {
1340  return binCptMin__;
1341  }
std::vector< std::vector< GUM_SCALAR > > binCptMin__
Used with binary networks to speed-up L2U inference.
Definition: credalNet.h:629

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

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

◆ hasComputedBinaryCPTMinMax()

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

Definition at line 1301 of file credalNet_tpl.h.

1301  {
1303  }
bool hasComputedBinaryCPTMinMax__
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:622

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

616  {
617  for (auto node: src_bn__.nodes()) {
618  const Potential< GUM_SCALAR >* const potential(&src_bn__.cpt(node));
619 
620  Potential< GUM_SCALAR >* const potential_min(
621  const_cast< Potential< GUM_SCALAR >* const >(&src_bn_min__.cpt(node)));
622  Potential< GUM_SCALAR >* const potential_max(
623  const_cast< Potential< GUM_SCALAR >* const >(&src_bn_max__.cpt(node)));
624 
625  Size var_dSize = src_bn__.variable(node).domainSize();
626  Size entry_size = potential->domainSize() / var_dSize;
627 
628  Instantiation ins(potential);
629  Instantiation ins_min(potential_min);
630  Instantiation ins_max(potential_max);
631 
632  ins.setFirst();
633  ins_min.setFirst();
634  ins_max.setFirst();
635 
636  std::vector< GUM_SCALAR > vertex(var_dSize);
637 
638  for (Size entry = 0; entry < entry_size; entry++) {
639  GUM_SCALAR den = 0;
640  Size nbm = 0;
641 
642  for (Size modality = 0; modality < var_dSize; modality++) {
643  vertex[modality] = potential->get(ins);
644 
645  if (vertex[modality] < 1 && vertex[modality] > 0)
646  GUM_ERROR(OperationNotAllowed,
647  "idmLearning : the BayesNet contains "
648  "probabilities and not event counts.");
649 
650  den += vertex[modality];
651 
652  if (vertex[modality] > 0) nbm++;
653 
654  ++ins;
655  }
656 
657  if (nbm > 1 || !keepZeroes) den += s;
658 
659  GUM_SCALAR min, max;
660 
661  for (Size modality = 0; modality < var_dSize; modality++) {
662  min = vertex[modality];
663  max = min;
664 
665  if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
666 
667  min = GUM_SCALAR(min * 1.0 / den);
668  max = GUM_SCALAR(max * 1.0 / den);
669 
670  potential_min->set(ins_min, min);
671  potential_max->set(ins_max, max);
672 
673  ++ins_min;
674  ++ins_max;
675  } // end of : for each modality
676 
677  } // end of : for each entry
678 
679  } // end of : for each variable
680 
681  epsilonMin__ = GUM_SCALAR(s);
682  epsilonMax__ = GUM_SCALAR(s);
683  epsilonMoy__ = GUM_SCALAR(s);
685  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
GUM_SCALAR epsilonMax__
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:565
GUM_SCALAR epsilonMoy__
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:569
void intervalToCredal__()
Computes the vertices of each credal set according to their interval definition (does not use lrs)...
GUM_SCALAR epsilonMin__
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:561
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

1452  {
1453  BIFReader< GUM_SCALAR > reader(&src_bn__, src_min_num);
1454  std::string other;
1455 
1456  if (src_max_den.compare("") != 0)
1457  other = src_max_den;
1458  else
1459  other = src_min_num;
1460 
1461  BIFReader< GUM_SCALAR > reader_min(&src_bn_min__, src_min_num);
1462  BIFReader< GUM_SCALAR > reader_max(&src_bn_max__, other);
1463 
1464  reader.proceed();
1465  reader_min.proceed();
1466  reader_max.proceed();
1467  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1472  {
1473  src_bn__ = src_min_num;
1474  src_bn_min__ = src_min_num;
1475 
1476  if (src_max_den.size() > 0)
1477  src_bn_max__ = src_max_den;
1478  else
1479  src_bn_max__ = src_min_num;
1480  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1424  {
1425  epsilonMin__ = 0;
1426  epsilonMax__ = 0;
1427  epsilonMoy__ = 0;
1428 
1429  epsRedund__ = GUM_SCALAR(1e-6);
1430 
1431  // farey algorithm
1432  epsF__ = GUM_SCALAR(1e-6);
1433  denMax__ = GUM_SCALAR(1e6); // beware LRSWrapper
1434 
1435  // continued fractions, beware LRSWrapper
1436  // decimal paces (epsC__ * precisionC__ == 1)
1437  precisionC__ = GUM_SCALAR(1e6);
1438  deltaC__ = 5;
1439 
1440  // old custom algorithm
1441  precision__ = GUM_SCALAR(1e6); // beware LRSWrapper
1442 
1443  current_bn__ = nullptr;
1444  credalNet_current_cpt__ = nullptr;
1445  current_nodeType__ = nullptr;
1446 
1448  }
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
GUM_SCALAR precision__
Precision used by frac__.
Definition: credalNet.h:586
GUM_SCALAR epsilonMax__
The highest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:565
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * credalNet_current_cpt__
This CredalNet up-to-date CPTs.
Definition: credalNet.h:610
GUM_SCALAR epsilonMoy__
The average perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:569
GUM_SCALAR epsRedund__
Value under which a decimal number is considered to be zero when computing redundant vertices...
Definition: credalNet.h:574
bool hasComputedBinaryCPTMinMax__
Used by L2U, to know if lower and upper probabilities over the second modality has been stored in ord...
Definition: credalNet.h:622
NodeProperty< NodeType > * current_nodeType__
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:618
GUM_SCALAR epsilonMin__
The lowest perturbation of the BayesNet provided as input for this CredalNet.
Definition: credalNet.h:561
GUM_SCALAR denMax__
Highest possible denominator allowed when using farey__.
Definition: credalNet.h:583
GUM_SCALAR deltaC__
5 by default, used by fracC__ as number of decimals.
Definition: credalNet.h:556
GUM_SCALAR precisionC__
1e6 by default, used by fracC__ as precision.
Definition: credalNet.h:554
GUM_SCALAR epsF__
Value under which a decimal number is considered to be zero when using farey__.
Definition: credalNet.h:579

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

392  {
393  return Instantiation(src_bn__.cpt(id));
394  }
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

786  {
787  if (!credalNet_src_cpt__.empty()) credalNet_src_cpt__.clear();
788 
789  credalNet_src_cpt__.resize(src_bn__.size());
790 
791  LRSWrapper< GUM_SCALAR > lrsWrapper;
792 
793  for (auto node: src_bn__.nodes()) {
794  const Potential< GUM_SCALAR >* const potential_min(
795  &src_bn_min__.cpt(node));
796  const Potential< GUM_SCALAR >* const potential_max(
797  &src_bn_max__.cpt(node));
798 
799  Size var_dSize = src_bn__.variable(node).domainSize();
800  Size entry_size = potential_min->domainSize() / var_dSize;
801 
802  std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
803  entry_size);
804 
805  Instantiation ins_min(potential_min);
806  Instantiation ins_max(potential_max);
807 
808  ins_min.setFirst();
809  ins_max.setFirst();
810 
811  lrsWrapper.setUpH(var_dSize);
812 
813  for (Size entry = 0; entry < entry_size; entry++) {
814  for (Size modality = 0; modality < var_dSize; modality++) {
815  if (potential_min->get(ins_min) > potential_max->get(ins_max)) {
816  GUM_ERROR(CPTError,
817  "For variable " << src_bn__.variable(node).name()
818  << " (at " << ins_min
819  << "), the min is greater than the max : "
820  << potential_min->get(ins_min) << ">"
821  << potential_max->get(ins_max) << ".");
822  }
823  lrsWrapper.fillH(potential_min->get(ins_min),
824  potential_max->get(ins_max),
825  modality);
826  ++ins_min;
827  ++ins_max;
828  }
829 
830  lrsWrapper.H2V();
831  var_cpt[entry] = lrsWrapper.getOutput();
832  lrsWrapper.nextHInput();
833  }
834 
835  credalNet_src_cpt__.insert(node, var_cpt);
836 
837  } // end of : for each variable (node)
838 
839  // get precise/credal/vacuous status of each variable
840  sort_varType__();
841  separatelySpecified__ = true;
842  }
void sort_varType__()
Set the NodeType of each node
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
bool separatelySpecified__
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:591
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

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

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

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

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

1296  {
1297  return separatelySpecified__;
1298  }
bool separatelySpecified__
TRUE if this CredalNet is separately and interval specified, FALSE otherwise.
Definition: credalNet.h:591

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

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

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

1291  {
1292  return original_nodeType__[id];
1293  }
NodeProperty< NodeType > original_nodeType__
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:616

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

946  {
947  BIFWriter< GUM_SCALAR > writer;
948 
949  std::string minfilename = min_path; //"min.bif";
950  std::string maxfilename = max_path; //"max.bif";
951  std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
952  std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
953 
954  if (!min_file.good())
955  GUM_ERROR(
956  IOError,
957  "bnToCredal() : could not open stream : min_file : " << minfilename);
958 
959  if (!max_file.good()) {
960  min_file.close();
961  GUM_ERROR(
962  IOError,
963  "bnToCredal() : could not open stream : min_file : " << maxfilename);
964  }
965 
966  try {
967  writer.write(min_file, src_bn_min__);
968  writer.write(max_file, src_bn_max__);
969  } catch (Exception& err) {
970  GUM_SHOWERROR(err);
971  min_file.close();
972  max_file.close();
973  throw(err);
974  }
975 
976  min_file.close();
977  max_file.close();
978  }
BayesNet< GUM_SCALAR > src_bn_max__
BayesNet used to store upper probabilities.
Definition: credalNet.h:599
#define GUM_SHOWERROR(e)
Definition: exceptions.h:60
BayesNet< GUM_SCALAR > src_bn_min__
BayesNet used to store lower probabilities.
Definition: credalNet.h:597
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

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,
154  std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
155 
156  if (node_cpt[entry].size() != 0)
157  GUM_ERROR(DuplicateElement,
158  "setCPT : vertices of entry id "
159  << entry << " already set to : " << node_cpt[entry]
160  << ", cannot insert : " << cpt);
161 
162  node_cpt[entry] = cpt;
163 
165  }
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

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

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

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

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:606
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ sort_varType__()

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

Set the NodeType of each node

Definition at line 1766 of file credalNet_tpl.h.

1766  {
1767  NodeProperty< NodeType >* current_nodeType__;
1768  const NodeProperty<
1769  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1771 
1772  const BayesNet< GUM_SCALAR >* current_bn__;
1773 
1774  if (this->current_bn__ == nullptr)
1775  current_bn__ = &src_bn__;
1776  else
1777  current_bn__ = this->current_bn__;
1778 
1779  if (this->credalNet_current_cpt__ == nullptr)
1780  credalNet_current_cpt__ = &credalNet_src_cpt__;
1781  else
1782  credalNet_current_cpt__ = this->credalNet_current_cpt__;
1783 
1784  if (this->current_nodeType__ == nullptr)
1785  current_nodeType__ = &original_nodeType__;
1786  else
1787  current_nodeType__ = this->current_nodeType__;
1788 
1789  /*if ( ! current_nodeType__->empty() )
1790  current_nodeType__->clear();*/
1791 
1792  for (auto node: current_bn__->nodes()) {
1793  // indicatrices are already present
1794  if (current_nodeType__->exists(node)) continue;
1795 
1796  bool precise = true, vacuous = true;
1797 
1798  for (auto entry = (*credalNet_current_cpt__)[node].cbegin(),
1799  theEnd2 = (*credalNet_current_cpt__)[node].cend();
1800  entry != theEnd2;
1801  ++entry) {
1802  auto vertices = entry->size();
1803  auto var_dSize = (*entry)[0].size();
1804 
1805  if (precise && vertices > 1) precise = false;
1806 
1807  if (vacuous && vertices == var_dSize) {
1808  std::vector< bool > elem(var_dSize, false);
1809 
1810  for (auto vertex = entry->cbegin(), vEnd = entry->cend();
1811  vertex != vEnd;
1812  ++vertex) {
1813  for (auto probability = vertex->cbegin(), pEnd = vertex->cend();
1814  probability != pEnd;
1815  ++probability) {
1816  if (*probability == 1) {
1817  elem[probability - vertex->begin()] = true;
1818  break;
1819  }
1820  } // end of : for each modality
1821 
1822  break; // not vacuous
1823  } // end of : for each vertex
1824 
1825  for (auto /*std::vector< bool >::const_iterator*/ probability
1826  = elem.cbegin();
1827  probability != elem.cend();
1828  ++probability)
1829  if (*probability == false) vacuous = false;
1830 
1831  } // end of : if vertices == dSize
1832  else
1833  vacuous = false;
1834 
1835  if (vacuous == false && precise == false) {
1836  current_nodeType__->insert(node, NodeType::Credal);
1837  break;
1838  }
1839 
1840  } // end of : for each parents entry
1841 
1842  if (vacuous)
1843  current_nodeType__->insert(node, NodeType::Vacuous);
1844  else if (precise)
1845  current_nodeType__->insert(node, NodeType::Precise);
1846 
1847  } // end of : for each variable
1848  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
NodeProperty< NodeType > original_nodeType__
The NodeType of each node from the ORIGINAL network.
Definition: credalNet.h:616
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * credalNet_current_cpt__
This CredalNet up-to-date CPTs.
Definition: credalNet.h:610
NodeProperty< NodeType > * current_nodeType__
The NodeType of each node from the up-to-date network.
Definition: credalNet.h:618
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1415  {
1416  return src_bn__;
1417  }
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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

1365  {
1366  std::stringstream output;
1367  const BayesNet< GUM_SCALAR >* current_bn__;
1368  const NodeProperty<
1369  std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1371 
1372  if (this->current_bn__ == nullptr)
1373  current_bn__ = &this->src_bn__;
1374  else
1375  current_bn__ = this->current_bn__;
1376 
1377  if (this->credalNet_current_cpt__ == nullptr)
1378  credalNet_current_cpt__ = &this->credalNet_src_cpt__;
1379  else
1380  credalNet_current_cpt__ = this->credalNet_current_cpt__;
1381 
1382  for (auto node: current_bn__->nodes()) {
1383  const Potential< GUM_SCALAR >* potential(&current_bn__->cpt(node));
1384  auto pconfs
1385  = potential->domainSize() / current_bn__->variable(node).domainSize();
1386 
1387  output << "\n" << current_bn__->variable(node) << "\n";
1388 
1389  Instantiation ins(potential);
1390  ins.forgetMaster();
1391  ins.erase(current_bn__->variable(node));
1392  ins.setFirst();
1393 
1394  for (Size pconf = 0; pconf < pconfs; pconf++) {
1395  output << ins << " : ";
1396  output << (*credalNet_current_cpt__)[node][pconf] << "\n";
1397 
1398  if (pconf < pconfs - 1) ++ins;
1399  }
1400  }
1401 
1402  output << "\n";
1403 
1404  return output.str();
1405  }
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
BayesNet< GUM_SCALAR > * current_bn__
Up-to-date BayesNet (used as a DAG).
Definition: credalNet.h:602
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > * credalNet_current_cpt__
This CredalNet up-to-date CPTs.
Definition: credalNet.h:610
NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > credalNet_src_cpt__
This CredalNet original CPTs.
Definition: credalNet.h:606
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
BayesNet< GUM_SCALAR > src_bn__
Original BayesNet (used as a DAG).
Definition: credalNet.h:594

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 637 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 629 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 610 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 606 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 602 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 618 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 556 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 583 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 579 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 565 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 561 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 569 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 574 of file credalNet.h.

◆ hasComputedBinaryCPTMinMax__

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

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

Definition at line 622 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 616 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 586 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 554 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 591 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 594 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 599 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 597 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 613 of file credalNet.h.


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