28 template <
typename GUM_SCALAR >
39 template <
typename GUM_SCALAR >
44 NodeId a = __src_bn.add(var);
45 NodeId b = __src_bn_min.add(var);
46 NodeId c = __src_bn_max.add(var);
48 if (a != b || a != c )
50 "addVariable : not the same id over all networks : " 51 << a <<
", " << b <<
", " << c);
56 template <
typename GUM_SCALAR >
58 __src_bn.addArc(tail, head);
59 __src_bn_min.addArc(tail, head);
60 __src_bn_max.addArc(tail, head);
63 template <
typename GUM_SCALAR >
66 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt) {
70 auto entry_size = potential->domainSize() / var_dSize;
72 if (cpt.size() != entry_size)
74 "setCPTs : entry sizes of cpts does not match for node id : " 75 <<
id <<
" : " << cpt.size() <<
" != " << entry_size);
77 for (
const auto& cset : cpt) {
81 "setCPTs : vertices in credal set does not match for node id : " 82 <<
id <<
" with 0 vertices");
84 for (
const auto& vertex : cset) {
85 if (vertex.size() != var_dSize)
87 "setCPTs : variable modalities in cpts does " 88 "not match for node id : " 89 <<
id <<
" with vertex " << vertex <<
" : " 90 << vertex.size() <<
" != " << var_dSize);
94 for (
const auto& prob : vertex) {
98 if (std::fabs(sum - 1) > 1e-6)
100 "setCPTs : a vertex coordinates does not " 101 "sum to one for node id : " 102 <<
id <<
" with vertex " << vertex);
106 __credalNet_src_cpt.insert(
id, cpt);
109 template <
typename GUM_SCALAR >
113 const std::vector< std::vector< GUM_SCALAR > >& cpt) {
117 auto entry_size = potential->domainSize() / var_dSize;
119 if (entry >= entry_size)
121 "setCPT : entry is greater or equal than entry size " 122 "(entries start at 0 up to entry_size - 1) : " 123 << entry <<
" >= " << entry_size);
128 for (
const auto& vertex : cpt) {
129 if (vertex.size() != var_dSize)
131 "setCPT : variable modalities in cpts does not " 132 "match for node id : " 133 <<
id <<
" with vertex " << vertex <<
" at entry " << entry
134 <<
" : " << vertex.size() <<
" != " << var_dSize);
138 for (
const auto& prob : vertex) {
142 if (std::fabs(sum - 1) > 1e-6)
145 "setCPT : a vertex coordinates does not sum to one for node id : " 146 <<
id <<
" at entry " << entry <<
" with vertex " << vertex);
150 auto& node_cpt = __credalNet_src_cpt.getWithDefault(
151 id, std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
153 if (node_cpt[entry].size() != 0)
155 "setCPT : vertices of entry id " 156 << entry <<
" already set to : " << node_cpt[entry]
157 <<
", cannot insert : " << cpt);
159 node_cpt[entry] = cpt;
164 template <
typename GUM_SCALAR >
168 const std::vector< std::vector< GUM_SCALAR > >& cpt) {
172 auto entry_size = potential->domainSize() / var_dSize;
193 "setCPT : instantiation : " 194 << ins <<
" is not valid for node id " <<
id 195 <<
" which accepts instantiations such as (order is not " 200 Idx entry = 0, jump = 1;
202 for (
Idx i = 0, end = ins.
nbrDim(); i < end; i++) {
203 if (__src_bn.nodeId(ins.
variable(i)) ==
id)
continue;
205 entry += ins.
val(i) * jump;
210 if (entry >= entry_size)
212 "setCPT : entry is greater or equal than entry size " 213 "(entries start at 0 up to entry_size - 1) : " 214 << entry <<
" >= " << entry_size);
219 for (
const auto& vertex : cpt) {
220 if (vertex.size() != var_dSize)
222 "setCPT : variable modalities in cpts does not " 223 "match for node id : " 224 <<
id <<
" with vertex " << vertex <<
" at entry " << entry
225 <<
" : " << vertex.size() <<
" != " << var_dSize);
229 for (
const auto& prob : vertex) {
233 if (std::fabs(sum - 1) > 1e-6)
236 "setCPT : a vertex coordinates does not sum to one for node id : " 237 <<
id <<
" at entry " << entry <<
" with vertex " << vertex);
240 auto& node_cpt = __credalNet_src_cpt.getWithDefault(
241 id, std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
243 if (node_cpt[entry].size() != 0)
245 "setCPT : vertices of entry : " 246 << ins <<
" id " << entry <<
" already set to : " 247 << node_cpt[entry] <<
", cannot insert : " << cpt);
249 node_cpt[entry] = cpt;
254 template <
typename GUM_SCALAR >
257 const std::vector< GUM_SCALAR >& lower,
258 const std::vector< GUM_SCALAR >& upper) {
260 __src_bn_min.cpt(
id).fillWith(lower);
261 __src_bn_max.cpt(
id).fillWith(upper);
265 "fillConstraints : sizes does not match in fillWith for node id : " 270 template <
typename GUM_SCALAR >
274 const std::vector< GUM_SCALAR >& lower,
275 const std::vector< GUM_SCALAR >& upper) {
281 auto var_dSize = __src_bn.variable(
id).domainSize();
283 if (lower.size() != var_dSize || upper.size() != var_dSize)
286 "setCPT : variable modalities in cpts does not match for node id : " 287 <<
id <<
" with sizes of constraints : ( " << lower.size() <<
" || " 288 << upper.size() <<
" ) != " << var_dSize);
290 auto entry_size = potential_min->
domainSize() / var_dSize;
292 if (entry >= entry_size)
294 "setCPT : entry is greater or equal than entry size " 295 "(entries start at 0 up to entry_size - 1) : " 296 << entry <<
" >= " << entry_size);
305 while (pos != entry) {
311 for (
Size i = 0; i < var_dSize; i++) {
312 potential_min->
set(min, lower[i]);
313 potential_max->
set(max, upper[i]);
319 template <
typename GUM_SCALAR >
323 const std::vector< GUM_SCALAR >& lower,
324 const std::vector< GUM_SCALAR >& upper) {
349 "setCPT : instantiation : " 350 << ins <<
" is not valid for node id " <<
id 351 <<
" which accepts instantiations such as (order is not " 356 Idx entry = 0, jump = 1;
358 for (
Idx i = 0, end = ins.
nbrDim(); i < end; i++) {
359 if (__src_bn.nodeId(ins.
variable(i)) ==
id)
continue;
361 entry += ins.
val(i) * jump;
381 fillConstraint(
id, entry, lower, upper);
387 template <
typename GUM_SCALAR >
392 template <
typename GUM_SCALAR >
394 return __src_bn.variable(
id).domainSize();
399 template <
typename GUM_SCALAR >
401 const std::string& src_max_den) {
404 __initCNNets(src_min_num, src_max_den);
413 template <
typename GUM_SCALAR >
418 __initCNNets(src_min_num, src_max_den);
427 template <
typename GUM_SCALAR >
429 if (__current_bn !=
nullptr)
delete __current_bn;
431 if (__credalNet_current_cpt !=
nullptr)
delete __credalNet_current_cpt;
433 if (__current_nodeType !=
nullptr)
delete __current_nodeType;
439 template <
typename GUM_SCALAR >
442 const bool keepZeroes) {
443 GUM_SCALAR epsi_min = 1.;
444 GUM_SCALAR epsi_max = 0.;
445 GUM_SCALAR epsi_moy = 0.;
446 GUM_SCALAR epsi_den = 0.;
448 for (
auto node : src_bn().nodes()) {
456 Size var_dSize = __src_bn.variable(node).domainSize();
457 Size entry_size = potential->domainSize() / var_dSize;
467 std::vector< GUM_SCALAR > vertex(var_dSize);
469 for (
Size entry = 0; entry < entry_size; entry++) {
475 den = potential_max->
get(ins_max);
479 for (
Size modality = 0; modality < var_dSize; modality++) {
480 vertex[modality] = potential->get(ins);
483 den += vertex[modality];
485 if (vertex[modality] < 1 && vertex[modality] > 0)
487 "bnToCredal : the BayesNet contains " 488 "probabilities and not event counts " 489 "although user precised oneNet = " 493 if (vertex[modality] > 0) nbm++;
502 for (
auto modality = vertex.cbegin(), theEnd = vertex.cend();
508 if (std::fabs(1. - sum) > __epsRedund) {
510 __src_bn.variable(node).name() <<
"(" << __epsRedund <<
")" 511 <<
" " << entry << std::endl
512 << vertex << std::endl
513 << ins << std::endl);
523 else if (den == 0 || beta == 1)
524 epsilon = GUM_SCALAR(1.0);
526 epsilon = GUM_SCALAR(std::pow(beta, std::log1p(den)));
531 if (epsilon > epsi_max) epsi_max = epsilon;
533 if (epsilon < epsi_min) epsi_min = epsilon;
537 for (
Size modality = 0; modality < var_dSize; modality++) {
538 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
539 min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
541 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
543 max = GUM_SCALAR(min + epsilon);
547 min = vertex[modality];
549 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
554 potential_min->
set(ins_min, min);
555 potential_max->
set(ins_max, max);
565 __epsilonMin = epsi_min;
566 __epsilonMax = epsi_max;
567 __epsilonMoy = (GUM_SCALAR)epsi_moy / (GUM_SCALAR)epsi_den;
569 __intervalToCredal();
572 template <
typename GUM_SCALAR >
574 for (
auto node : __src_bn.nodes()) {
578 auto entry_size = potential->domainSize() / var_dSize;
584 std::vector< GUM_SCALAR > vertex(var_dSize);
586 for (
Size entry = 0; entry < entry_size; entry++) {
591 for (
Size modality = 0; modality < var_dSize; modality++) {
592 vertex[modality] = potential->get(ins);
594 if (vertex[modality] < 1 && vertex[modality] > 0)
596 "lagrangeNormalization : the BayesNet " 597 "contains probabilities and not event " 600 den += vertex[modality];
602 if (!zeroes && vertex[modality] == 0) { zeroes =
true; }
610 for (
Size modality = 0; modality < var_dSize; modality++) {
611 potential->set(ins, potential->get(ins) + 1);
621 template <
typename GUM_SCALAR >
623 for (
auto node : __src_bn.nodes()) {
631 Size var_dSize = __src_bn.variable(node).domainSize();
632 Size entry_size = potential->domainSize() / var_dSize;
642 std::vector< GUM_SCALAR > vertex(var_dSize);
644 for (
Size entry = 0; entry < entry_size; entry++) {
648 for (
Size modality = 0; modality < var_dSize; modality++) {
649 vertex[modality] = potential->get(ins);
651 if (vertex[modality] < 1 && vertex[modality] > 0)
653 "idmLearning : the BayesNet contains " 654 "probabilities and not event counts.");
656 den += vertex[modality];
658 if (vertex[modality] > 0) nbm++;
663 if (nbm > 1 || !keepZeroes) den += s;
667 for (
Size modality = 0; modality < var_dSize; modality++) {
668 min = vertex[modality];
671 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
673 min = GUM_SCALAR(min * 1.0 / den);
674 max = GUM_SCALAR(max * 1.0 / den);
676 potential_min->
set(ins_min, min);
677 potential_max->
set(ins_max, max);
687 __epsilonMin = GUM_SCALAR(s);
688 __epsilonMax = GUM_SCALAR(s);
689 __epsilonMoy = GUM_SCALAR(s);
690 __intervalToCredal();
694 template <
typename GUM_SCALAR >
696 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
698 __credalNet_src_cpt.resize(__src_bn.size());
700 for (
auto node : __src_bn.nodes()) {
702 &__src_bn_min.cpt(node));
704 &__src_bn_max.cpt(node));
707 Size entry_size = potential_min->domainSize() / var_dSize;
709 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
718 std::vector< GUM_SCALAR > lower(var_dSize);
719 std::vector< GUM_SCALAR > upper(var_dSize);
721 for (
Size entry = 0; entry < entry_size; entry++) {
722 for (
Size modality = 0; modality < var_dSize;
723 modality++, ++ins_min, ++ins_max) {
724 lower[modality] = potential_min->get(ins_min);
725 upper[modality] = potential_max->get(ins_max);
728 bool all_equals =
true;
729 std::vector< std::vector< GUM_SCALAR > > vertices;
731 for (
Size modality = 0; modality < var_dSize; modality++) {
732 if (std::fabs(upper[modality] - lower[modality]) < 1e-6)
continue;
735 std::vector< GUM_SCALAR > vertex(var_dSize);
736 vertex[modality] = upper[modality];
738 for (
Size mod = 0; mod < var_dSize; mod++) {
739 if (modality != mod) vertex[mod] = lower[mod];
742 GUM_SCALAR total = 0;
744 auto vsize = vertex.size();
746 for (
Size i = 0; i < vsize; i++)
749 if (std::fabs(total - 1.) > 1e-6)
751 __src_bn.variable(node).name() <<
" " << entry << std::endl
752 << vertex << std::endl);
754 vertices.push_back(vertex);
758 std::vector< GUM_SCALAR > vertex(var_dSize);
760 for (
Size modality = 0; modality < var_dSize; modality++)
761 vertex[modality] = lower[modality];
763 GUM_SCALAR total = 0.;
765 auto vsize = vertex.size();
767 for (
Size i = 0; i < vsize; i++)
770 if (std::fabs(total - 1.) > 1e-6)
772 __src_bn.variable(node).name() <<
" " << entry << std::endl
773 << vertex << std::endl);
775 vertices.push_back(vertex);
778 var_cpt[entry] = vertices;
781 __credalNet_src_cpt.insert(node, var_cpt);
787 __separatelySpecified =
true;
791 template <
typename GUM_SCALAR >
793 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
795 __credalNet_src_cpt.resize(__src_bn.size());
799 for (
auto node : __src_bn.nodes()) {
801 &__src_bn_min.cpt(node));
803 &__src_bn_max.cpt(node));
806 Size entry_size = potential_min->domainSize() / var_dSize;
808 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
817 lrsWrapper.
setUpH(var_dSize);
819 for (
Size entry = 0; entry < entry_size; entry++) {
820 for (
Size modality = 0; modality < var_dSize; modality++) {
822 potential_min->get(ins_min), potential_max->get(ins_max), modality);
832 __credalNet_src_cpt.insert(node, var_cpt);
838 __separatelySpecified =
true;
842 template <
typename GUM_SCALAR >
844 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
846 __credalNet_src_cpt.resize(__src_bn.size());
848 for (
auto node : __src_bn.nodes()) {
850 &__src_bn_min.cpt(node));
852 &__src_bn_max.cpt(node));
855 auto entry_size = potential_min->domainSize() / var_dSize;
857 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
867 for (
Size entry = 0; entry < entry_size; entry++) {
868 std::vector< std::vector< GUM_SCALAR > > vertices;
869 std::vector< GUM_SCALAR > vertex(var_dSize);
871 std::vector< std::vector< GUM_SCALAR > > inequalities(
872 var_dSize * 2, std::vector< GUM_SCALAR >(var_dSize + 1, 0));
874 std::vector< GUM_SCALAR > sum_ineq1(var_dSize + 1, -1);
875 std::vector< GUM_SCALAR > sum_ineq2(var_dSize + 1, 1);
879 bool isInterval =
false;
881 for (
Size modality = 0; modality < var_dSize; modality++) {
882 inequalities[modality * 2][0] = -potential_min->get(ins_min);
883 inequalities[modality * 2 + 1][0] = potential_max->get(ins_max);
884 inequalities[modality * 2][modality + 1] = 1;
885 inequalities[modality * 2 + 1][modality + 1] = -1;
887 vertex[modality] = inequalities[modality * 2 + 1][0];
890 && (-inequalities[modality * 2][0]
891 != inequalities[modality * 2 + 1][0]))
898 inequalities.push_back(sum_ineq1);
899 inequalities.push_back(sum_ineq2);
902 vertices.push_back(vertex);
905 __H2Vlrs(inequalities, vertices);
907 }
catch (
const std::exception& err) {
908 std::cout << err.what() << std::endl;
914 if (entry == 0 && vertices.size() >= 2) {
915 auto tmp = vertices[0];
916 vertices[0] = vertices[1];
920 var_cpt[entry] = vertices;
924 __credalNet_src_cpt.insert(node, var_cpt);
932 __separatelySpecified =
true;
939 template <
typename GUM_SCALAR >
942 const std::string& max_path)
const {
945 std::string minfilename = min_path;
946 std::string maxfilename = max_path;
947 std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
948 std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
950 if (!min_file.good())
953 "bnToCredal() : could not open stream : min_file : " << minfilename);
955 if (!max_file.good()) {
959 "bnToCredal() : could not open stream : min_file : " << maxfilename);
963 writer.
write(min_file, __src_bn_min);
964 writer.
write(max_file, __src_bn_max);
976 template <
typename GUM_SCALAR >
984 std::vector< std::vector< std::vector< GUM_SCALAR > > > >();
992 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
993 __credalNet_current_cpt;
995 if (this->__current_bn ==
nullptr)
996 __current_bn = &this->__src_bn;
998 __current_bn = this->__current_bn;
1000 if (this->__credalNet_current_cpt ==
nullptr)
1001 __credalNet_current_cpt = &this->__credalNet_src_cpt;
1003 __credalNet_current_cpt = this->__credalNet_current_cpt;
1010 if (!__var_bits.empty()) __var_bits.
clear();
1014 for (
auto node : __current_bn->nodes()) {
1015 auto var_dSize = __current_bn->variable(node).domainSize();
1017 if (var_dSize != 2) {
1022 std::string bit_name;
1023 std::vector< NodeId > bits(nb_bits);
1025 for (
Size bit = 0; bit < nb_bits; bit++) {
1026 bit_name = __current_bn->variable(node).name() +
" - bit - ";
1027 std::stringstream ss;
1029 bit_name += ss.str();
1037 __var_bits.insert(node, bits);
1041 std::string bit_name = __current_bn->variable(node).name();
1045 __var_bits.insert(node, std::vector< NodeId >(1, iD));
1050 for (
auto node : __current_bn->nodes()) {
1051 NodeSet parents = __current_bn->parents(node);
1053 if (!parents.
empty()) {
1054 for (
auto par : __current_bn->parents(node)) {
1055 for (
Size parent_bit = 0, spbits =
Size(__var_bits[par].size());
1056 parent_bit < spbits;
1058 for (
Size var_bit = 0, mbits =
Size(__var_bits[node].size());
1061 __bin_bn->
addArc(__var_bits[par][parent_bit],
1062 __var_bits[node][var_bit]);
1067 auto bitsize = __var_bits[node].size();
1069 for (
Size bit_c = 1; bit_c < bitsize; bit_c++)
1070 for (
Size bit_p = 0; bit_p < bit_c; bit_p++)
1071 __bin_bn->
addArc(__var_bits[node][bit_p], __var_bits[node][bit_c]);
1079 auto varsize = __current_bn->
size();
1081 for (
Size var = 0; var < varsize; var++) {
1082 auto bitsize = __var_bits[var].size();
1084 for (
Size i = 0; i < bitsize; i++) {
1086 &__bin_bn->
cpt(__var_bits[var][i]));
1090 auto entry_size = potential->
domainSize() / 2;
1091 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
1096 for (
Size conf = 0; conf < entry_size; conf++) {
1097 std::vector< std::vector< GUM_SCALAR > > pvar_cpt;
1098 auto verticessize = (*__credalNet_current_cpt)[var][old_conf].size();
1100 for (
Size old_distri = 0; old_distri < verticessize; old_distri++) {
1101 const std::vector< GUM_SCALAR >& vertex =
1102 (*__credalNet_current_cpt)[var][old_conf][old_distri];
1103 auto vertexsize = vertex.size();
1105 std::vector< Idx > incc(vertexsize, 0);
1107 for (
Size preced = 0; preced < i; preced++) {
1109 ins.
pos(__bin_bn->
variable(__var_bits[var][preced]));
1110 auto val = ins.val(bit_pos);
1120 while (elem < vertexsize) {
1124 if (elem % pas == 0) elem += pas;
1130 std::vector< GUM_SCALAR > distri(2, 0);
1133 for (
Size elem = 0; elem < vertexsize; elem++) {
1134 if (elem % pas == 0) pos = -pos;
1136 if (incc[elem] == i)
1137 (pos < 0) ? (distri[0] += vertex[elem])
1138 : (distri[1] += vertex[elem]);
1142 GUM_SCALAR den = distri[0] + distri[1];
1153 pvar_cpt.push_back(distri);
1158 std::vector< std::vector< GUM_SCALAR > > vertices(
1159 2, std::vector< GUM_SCALAR >(2, 1));
1162 auto new_verticessize = pvar_cpt.size();
1164 for (
Size v = 0; v < new_verticessize; v++) {
1165 if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1167 if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1170 vertices[0][0] = 1 - vertices[0][1];
1171 vertices[1][0] = 1 - vertices[1][1];
1173 pvar_cpt = vertices;
1175 var_cpt[conf] = pvar_cpt;
1182 if (old_conf == (*__credalNet_current_cpt)[var].size()) old_conf = 0;
1186 __credalNet_bin_cpt->insert(__var_bits[var][i], var_cpt);
1195 auto old_varsize = __var_bits.size();
1197 for (
Size i = 0; i < old_varsize; i++) {
1198 auto bitsize = __var_bits[i].size();
1201 if (bitsize == 1)
continue;
1203 auto old_card = __src_bn.variable(i).domainSize();
1205 for (
Size mod = 0; mod < old_card; mod++) {
1208 std::stringstream ss;
1209 ss << __src_bn.variable(i).name();
1215 const NodeId indic = __bin_bn->
add(var);
1218 for (
Size bit = 0; bit < bitsize; bit++)
1219 __bin_bn->
addArc(__var_bits[i][bit], indic);
1224 std::vector< std::vector< std::vector< GUM_SCALAR > > > icpt(num);
1226 for (
Size entry = 0; entry < num; entry++) {
1227 std::vector< std::vector< GUM_SCALAR > > vertices(
1228 1, std::vector< GUM_SCALAR >(2, 0));
1235 icpt[entry] = vertices;
1238 __credalNet_bin_cpt->insert(indic, icpt);
1240 __bin_nodeType->insert(indic, NodeType::Indic);
1246 if (this->__current_bn !=
nullptr)
delete this->__current_bn;
1248 this->__current_bn = __bin_bn;
1250 if (this->__credalNet_current_cpt !=
nullptr)
1251 delete this->__credalNet_current_cpt;
1253 this->__credalNet_current_cpt = __credalNet_bin_cpt;
1255 if (this->__current_nodeType !=
nullptr)
delete this->__current_nodeType;
1257 this->__current_nodeType = __bin_nodeType;
1263 template <
typename GUM_SCALAR >
1266 if (__credalNet_current_cpt !=
nullptr)
return *__credalNet_current_cpt;
1268 return __credalNet_src_cpt;
1271 template <
typename GUM_SCALAR >
1274 return __credalNet_src_cpt;
1277 template <
typename GUM_SCALAR >
1280 if (__current_nodeType !=
nullptr)
return (*(__current_nodeType))[id];
1282 return __original_nodeType[id];
1285 template <
typename GUM_SCALAR >
1288 return __original_nodeType[id];
1291 template <
typename GUM_SCALAR >
1293 return __separatelySpecified;
1296 template <
typename GUM_SCALAR >
1298 return __hasComputedCPTMinMax;
1302 template <
typename GUM_SCALAR >
1304 __binCptMin.resize(current_bn().size());
1305 __binCptMax.resize(current_bn().size());
1307 for (
auto node : current_bn().nodes()) {
1308 auto pConf = credalNet_currentCpt()[node].size();
1309 std::vector< GUM_SCALAR > min(pConf);
1310 std::vector< GUM_SCALAR > max(pConf);
1312 for (
Size pconf = 0; pconf < pConf; pconf++) {
1314 v1 = credalNet_currentCpt()[node][pconf][0][1];
1316 if (credalNet_currentCpt()[node][pconf].size() > 1)
1317 v2 = credalNet_currentCpt()[node][pconf][1][1];
1321 GUM_SCALAR delta = v1 - v2;
1322 min[pconf] = (delta >= 0) ? v2 : v1;
1323 max[pconf] = (delta >= 0) ? v1 : v2;
1326 __binCptMin[node] = min;
1327 __binCptMax[node] = max;
1330 __hasComputedCPTMinMax =
true;
1333 template <
typename GUM_SCALAR >
1334 const std::vector< std::vector< GUM_SCALAR > >&
1339 template <
typename GUM_SCALAR >
1340 const std::vector< std::vector< GUM_SCALAR > >&
1345 template <
typename GUM_SCALAR >
1347 return __epsilonMin;
1350 template <
typename GUM_SCALAR >
1352 return __epsilonMax;
1355 template <
typename GUM_SCALAR >
1357 return __epsilonMoy;
1360 template <
typename GUM_SCALAR >
1362 std::stringstream output;
1365 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1366 __credalNet_current_cpt;
1368 if (this->__current_bn ==
nullptr)
1369 __current_bn = &this->__src_bn;
1371 __current_bn = this->__current_bn;
1373 if (this->__credalNet_current_cpt ==
nullptr)
1374 __credalNet_current_cpt = &this->__credalNet_src_cpt;
1376 __credalNet_current_cpt = this->__credalNet_current_cpt;
1378 for (
auto node : __current_bn->
nodes()) {
1383 output <<
"\n" << __current_bn->
variable(node) <<
"\n";
1390 for (
Size pconf = 0; pconf < pconfs; pconf++) {
1391 output << ins <<
" : ";
1392 output << (*__credalNet_current_cpt)[node][pconf] <<
"\n";
1394 if (pconf < pconfs - 1) ++ins;
1400 return output.str();
1403 template <
typename GUM_SCALAR >
1405 if (__current_bn !=
nullptr)
return *__current_bn;
1410 template <
typename GUM_SCALAR >
1419 template <
typename GUM_SCALAR >
1425 __epsRedund = GUM_SCALAR(1e-6);
1428 __epsF = GUM_SCALAR(1e-6);
1429 __denMax = GUM_SCALAR(1e6);
1433 __precisionC = GUM_SCALAR(1e6);
1437 __precision = GUM_SCALAR(1e6);
1439 __current_bn =
nullptr;
1440 __credalNet_current_cpt =
nullptr;
1441 __current_nodeType =
nullptr;
1443 __hasComputedCPTMinMax =
false;
1446 template <
typename GUM_SCALAR >
1448 const std::string& src_max_den) {
1452 if (src_max_den.compare(
"") != 0)
1453 other = src_max_den;
1455 other = src_min_num;
1482 template <
typename GUM_SCALAR >
1486 __src_bn = src_min_num;
1487 __src_bn_min = src_min_num;
1489 if (src_max_den.
size() > 0)
1490 __src_bn_max = src_max_den;
1492 __src_bn_max = src_min_num;
1495 template <
typename GUM_SCALAR >
1497 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt)
1499 Size vertices_size = 0;
1501 for (
auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd;
1503 if (entry->size() > vertices_size) vertices_size =
Size(entry->size());
1506 return int(vertices_size);
1509 template <
typename GUM_SCALAR >
1513 if (this->__current_bn ==
nullptr)
1514 __current_bn = &this->__src_bn;
1516 __current_bn = this->__current_bn;
1518 for (
auto node : __current_bn->
nodes())
1523 for (
auto node : __current_bn->
nodes()) {
1524 for (
auto parent_idIt : __current_bn->
cpt(node).variablesSequence()) {
1525 if (__current_bn->
nodeId(*parent_idIt) != node)
1585 template <
typename GUM_SCALAR >
1587 const std::vector< std::vector< GUM_SCALAR > >& h_rep,
1588 std::vector< std::vector< GUM_SCALAR > >& v_rep)
const {
1592 std::string sinefile =
1599 std::ofstream h_file(sinefile.c_str(), std::ios::out | std::ios::trunc);
1603 "__H2Vlrs : could not open lrs input file : " << sinefile);
1605 h_file <<
"H - representation\n";
1606 h_file <<
"begin\n";
1607 h_file << h_rep.size() <<
' ' << h_rep[0].size() <<
" rational\n";
1609 for (
auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1610 for (
auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2;
1616 num, den, ((*it2 > 0) ? *it2 : -*it2), int64_t(__denMax), __epsF);
1618 h_file << ((*it2 > 0) ? num : -num) <<
'/' << den <<
' ';
1631 std::string soft_name =
"lrs";
1632 std::string extfile(sinefile);
1635 args[0] =
new char[soft_name.size()];
1636 args[1] =
new char[sinefile.size()];
1637 args[2] =
new char[extfile.size()];
1639 strcpy(args[0], soft_name.c_str());
1640 strcpy(args[1], sinefile.c_str());
1641 strcpy(args[2], extfile.c_str());
1644 int old_cout, new_cout;
1648 new_cout = open(
"/dev/null", O_WRONLY);
1664 std::ifstream v_file(extfile.c_str() , std::ios::in);
1669 std::string line, tmp;
1671 GUM_SCALAR probability;
1673 std::string::size_type pos;
1674 bool keep_going =
true;
1677 std::vector< GUM_SCALAR > vertex;
1679 v_file.ignore(256,
'l');
1681 while (v_file.good() && keep_going) {
1682 getline(v_file, line);
1684 if (line.size() == 0)
1686 else if (line.compare(
"end") == 0) {
1696 }
else if (line[1] !=
'1') {
1698 "__H2Vlrs : reading something other than a vertex from " 1699 "lrs output file : ");
1702 line = line.substr(2);
1703 cstr =
new char[line.size() + 1];
1704 strcpy(cstr, line.c_str());
1706 p = strtok(cstr,
" ");
1708 while (p !=
nullptr) {
1711 if (tmp.compare(
"1") == 0 || tmp.compare(
"0") == 0)
1712 probability = GUM_SCALAR(atof(tmp.c_str()));
1714 pos = tmp.find(
"/");
1716 GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1717 / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1720 vertex.push_back(probability);
1721 p = strtok(
nullptr,
" ");
1727 bool is_redund =
false;
1729 #pragma omp parallel 1734 auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1735 auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1737 for (
auto p = begin_pos; p < end_pos; p++) {
1738 #pragma omp flush(is_redund) 1740 if (is_redund)
break;
1742 bool thread_redund =
true;
1744 auto vsize = vertex.size();
1746 for (
Size modality = 0; modality < vsize; modality++) {
1747 if (std::fabs(vertex[modality] - v_rep[p][modality]) > __epsRedund) {
1748 thread_redund =
false;
1753 if (thread_redund) {
1755 #pragma omp flush(is_redund) 1760 if (!is_redund) v_rep.push_back(vertex);
1768 if (std::remove(sinefile.c_str()) != 0)
1771 if (std::remove(extfile.c_str()) != 0)
1775 template <
typename GUM_SCALAR >
1779 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1780 __credalNet_current_cpt;
1784 if (this->__current_bn ==
nullptr)
1785 __current_bn = &__src_bn;
1787 __current_bn = this->__current_bn;
1789 if (this->__credalNet_current_cpt ==
nullptr)
1790 __credalNet_current_cpt = &__credalNet_src_cpt;
1792 __credalNet_current_cpt = this->__credalNet_current_cpt;
1794 if (this->__current_nodeType ==
nullptr)
1795 __current_nodeType = &__original_nodeType;
1797 __current_nodeType = this->__current_nodeType;
1802 for (
auto node : __current_bn->
nodes()) {
1804 if (__current_nodeType->
exists(node))
continue;
1806 bool precise =
true, vacuous =
true;
1808 for (
auto entry = (*__credalNet_current_cpt)[node].cbegin(),
1809 theEnd2 = (*__credalNet_current_cpt)[node].cend();
1812 auto vertices = entry->
size();
1813 auto var_dSize = (*entry)[0].size();
1815 if (precise && vertices > 1) precise =
false;
1817 if (vacuous && vertices == var_dSize) {
1818 std::vector< bool > elem(var_dSize,
false);
1820 for (
auto vertex = entry->cbegin(), vEnd = entry->cend();
1823 for (
auto probability = vertex->cbegin(), pEnd = vertex->cend();
1824 probability != pEnd;
1826 if (*probability == 1) {
1827 elem[probability - vertex->begin()] =
true;
1835 for (
auto probability =
1837 probability != elem.cend();
1839 if (*probability ==
false) vacuous =
false;
1845 if (vacuous ==
false && precise ==
false) {
1846 __current_nodeType->
insert(node, NodeType::Credal);
1853 __current_nodeType->
insert(node, NodeType::Vacuous);
1855 __current_nodeType->
insert(node, NodeType::Precise);
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
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 )
aGrUM's Potential is a multi-dimensional array with tensor operators.
Class representing a Bayesian Network.
void __bnCopy(BayesNet< GUM_SCALAR > &bn_dest)
const bool isSeparatelySpecified() const
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...
std::string toString() const
virtual Size domainSize() const final
Returns the product of the variables domain size.
bool empty() const noexcept
Indicates whether the set is the empty set.
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
unsigned int getThreadNumber()
Get the calling thread id.
std::string getUniqueFileName()
Returns a path to a unique file name.
void fillConstraints(const NodeId &id, const std::vector< GUM_SCALAR > &lower, const std::vector< GUM_SCALAR > &upper)
Set the interval constraints of the credal sets of a given node ( all instantiations ) ...
void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes a Bayesian Network in the output stream using the BIF format.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
void __initParams()
Initialize private constant variables after the Constructor has been called.
unsigned long int2Pow(unsigned long exponent)
Specialized base 2 pow function with integer.
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
NodeType currentNodeType(const NodeId &id) const
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 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 ) ...
bool forgetMaster()
Deassociate the master MultiDimAdressable, if any.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void __sort_varType()
Set the NodeType of each node
void reorder(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
gum is the global namespace for all aGrUM entities
void computeCPTMinMax()
Used with binary networks to speed-up L2U inference.
const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
The class for generic Hash Tables.
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_max() const
Used with binary networks to speed-up L2U inference.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
Instantiation instantiation(const NodeId &id)
Get an Instantiation from a node id, usefull to fill the constraints of the network ...
Idx val(Idx i) const
Returns the current value of the variable at position i.
void approximatedBinarization()
Approximate binarization.
Size proceed() final
parse.
void lagrangeNormalization()
Normalize counts of a BayesNet storing counts of each events such that no probability is 0...
virtual Size domainSize() const =0
Size size() const
Returns the number of variables in this Directed Graphical Model.
void fillH(const GUM_SCALAR &min, const GUM_SCALAR &max, const Size &modal)
Creates the H-representation of min <= p(X=modal | .) <= max and add it to the problem input __input...
Class template representing a Credal Network.
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
void setUpH(const Size &card)
Sets up an H-representation.
NodeId addVariable(const std::string &name, const Size &card)
Adds a discrete node into the network.
void intervalToCredalWithFiles()
Writes a IBayesNet in the BIF format.
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_srcCpt() const
const GUM_SCALAR & epsilonMean() const
void nextHInput()
Reset the wrapper for next computation for a H-representation with the same variable cardinality and ...
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
int __find_dNode_card(const std::vector< std::vector< std::vector< GUM_SCALAR > > > &var_cpt) const
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...
void __H2Vlrs(const std::vector< std::vector< GUM_SCALAR > > &h_rep, std::vector< std::vector< GUM_SCALAR > > &v_rep) const
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Base class for all aGrUM's exceptions.
NodeType
NodeType to speed-up computations in some algorithms.
const GUM_SCALAR & epsilonMin() const
NodeType nodeType(const NodeId &id) const
const std::vector< std::vector< GUM_SCALAR > > & get_CPT_min() const
Used with binary networks to speed-up L2U inference.
void __intervalToCredal()
Computes the vertices of each credal set according to their interval definition (does not use lrs)...
CredalNet()
Constructor used to create a CredalNet step by step, i.e.
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
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 of templatized reader of BIF files for Bayesian Networks.
const BayesNet< GUM_SCALAR > & current_bn() const
Size domainSize(const NodeId &id)
Get the cardinality of a node
Class for assigning/browsing values to tuples of discrete variables.
virtual Idx pos(const DiscreteVariable &var) const final
Returns the index of a variable.
void intervalToCredal()
Computes the vertices of each credal set according to their interval definition (uses lrs)...
Class representing Credal Networks.
void clear()
Removes all the elements in the hash table.
const bool hasComputedCPTMinMax() const
void H2V()
H-representation to V-representation.
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 poin...
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable's id in the gum::BayesNet.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
const matrix & getOutput() const
Get the output matrix solution of the problem.
Size Idx
Type for indexes.
Utilities for manipulating strings.
const GUM_SCALAR & epsilonMax() const
void erase(const DiscreteVariable &v) final
Removes a variable from the Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
void addArc(const NodeId &tail, const NodeId &head)
Adds an arc between two nodes.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
const BayesNet< GUM_SCALAR > & src_bn() const
const NodeProperty< std::vector< std::vector< std::vector< GUM_SCALAR > > > > & credalNet_currentCpt() const
void idmLearning(const Idx s=0, const bool keepZeroes=false)
Learns parameters from a BayesNet storing counts of events.
Size NodeId
Type for node ids.
#define GUM_ERROR(type, msg)