30 template <
typename GUM_SCALAR >
41 template <
typename GUM_SCALAR >
46 NodeId a = __src_bn.add(var);
47 NodeId b = __src_bn_min.add(var);
48 NodeId c = __src_bn_max.add(var);
50 if (a != b || a != c )
52 "addVariable : not the same id over all networks : " 53 << a <<
", " << b <<
", " << c);
58 template <
typename GUM_SCALAR >
60 __src_bn.addArc(tail, head);
61 __src_bn_min.addArc(tail, head);
62 __src_bn_max.addArc(tail, head);
65 template <
typename GUM_SCALAR >
68 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& cpt) {
72 auto entry_size = potential->domainSize() / var_dSize;
74 if (cpt.size() != entry_size)
76 "setCPTs : entry sizes of cpts does not match for node id : " 77 <<
id <<
" : " << cpt.size() <<
" != " << entry_size);
79 for (
const auto& cset : cpt) {
83 "setCPTs : vertices in credal set does not match for node id : " 84 <<
id <<
" with 0 vertices");
86 for (
const auto& vertex : cset) {
87 if (vertex.size() != var_dSize)
89 "setCPTs : variable modalities in cpts does " 90 "not match for node id : " 91 <<
id <<
" with vertex " << vertex <<
" : " 92 << vertex.size() <<
" != " << var_dSize);
96 for (
const auto& prob : vertex) {
100 if (std::fabs(sum - 1) > 1e-6)
102 "setCPTs : a vertex coordinates does not " 103 "sum to one for node id : " 104 <<
id <<
" with vertex " << vertex);
108 __credalNet_src_cpt.insert(
id, cpt);
111 template <
typename GUM_SCALAR >
115 const std::vector< std::vector< GUM_SCALAR > >& cpt) {
119 auto entry_size = potential->domainSize() / var_dSize;
121 if (entry >= entry_size)
123 "setCPT : entry is greater or equal than entry size " 124 "(entries start at 0 up to entry_size - 1) : " 125 << entry <<
" >= " << entry_size);
130 for (
const auto& vertex : cpt) {
131 if (vertex.size() != var_dSize)
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);
140 for (
const auto& prob : vertex) {
144 if (std::fabs(sum - 1) > 1e-6)
147 "setCPT : a vertex coordinates does not sum to one for node id : " 148 <<
id <<
" at entry " << entry <<
" with vertex " << vertex);
152 auto& node_cpt = __credalNet_src_cpt.getWithDefault(
153 id, std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
155 if (node_cpt[entry].size() != 0)
157 "setCPT : vertices of entry id " 158 << entry <<
" already set to : " << node_cpt[entry]
159 <<
", cannot insert : " << cpt);
161 node_cpt[entry] = cpt;
166 template <
typename GUM_SCALAR >
170 const std::vector< std::vector< GUM_SCALAR > >& cpt) {
174 auto entry_size = potential->domainSize() / var_dSize;
195 "setCPT : instantiation : " 196 << ins <<
" is not valid for node id " <<
id 197 <<
" which accepts instantiations such as (order is not " 202 Idx entry = 0, jump = 1;
204 for (
Idx i = 0, end = ins.
nbrDim(); i < end; i++) {
205 if (__src_bn.nodeId(ins.
variable(i)) ==
id)
continue;
207 entry += ins.
val(i) * jump;
212 if (entry >= entry_size)
214 "setCPT : entry is greater or equal than entry size " 215 "(entries start at 0 up to entry_size - 1) : " 216 << entry <<
" >= " << entry_size);
221 for (
const auto& vertex : cpt) {
222 if (vertex.size() != var_dSize)
224 "setCPT : variable modalities in cpts does not " 225 "match for node id : " 226 <<
id <<
" with vertex " << vertex <<
" at entry " << entry
227 <<
" : " << vertex.size() <<
" != " << var_dSize);
231 for (
const auto& prob : vertex) {
235 if (std::fabs(sum - 1) > 1e-6)
238 "setCPT : a vertex coordinates does not sum to one for node id : " 239 <<
id <<
" at entry " << entry <<
" with vertex " << vertex);
242 auto& node_cpt = __credalNet_src_cpt.getWithDefault(
243 id, std::vector< std::vector< std::vector< GUM_SCALAR > > >(entry_size));
245 if (node_cpt[entry].size() != 0)
247 "setCPT : vertices of entry : " 248 << ins <<
" id " << entry <<
" already set to : " 249 << node_cpt[entry] <<
", cannot insert : " << cpt);
251 node_cpt[entry] = cpt;
256 template <
typename GUM_SCALAR >
259 const std::vector< GUM_SCALAR >& lower,
260 const std::vector< GUM_SCALAR >& upper) {
262 __src_bn_min.cpt(
id).fillWith(lower);
263 __src_bn_max.cpt(
id).fillWith(upper);
267 "fillConstraints : sizes does not match in fillWith for node id : " 272 template <
typename GUM_SCALAR >
276 const std::vector< GUM_SCALAR >& lower,
277 const std::vector< GUM_SCALAR >& upper) {
283 auto var_dSize = __src_bn.variable(
id).domainSize();
285 if (lower.size() != var_dSize || upper.size() != var_dSize)
288 "setCPT : variable modalities in cpts does not match for node id : " 289 <<
id <<
" with sizes of constraints : ( " << lower.size() <<
" || " 290 << upper.size() <<
" ) != " << var_dSize);
292 auto entry_size = potential_min->
domainSize() / var_dSize;
294 if (entry >= entry_size)
296 "setCPT : entry is greater or equal than entry size " 297 "(entries start at 0 up to entry_size - 1) : " 298 << entry <<
" >= " << entry_size);
307 while (pos != entry) {
313 for (
Size i = 0; i < var_dSize; i++) {
314 potential_min->
set(min, lower[i]);
315 potential_max->
set(max, upper[i]);
321 template <
typename GUM_SCALAR >
325 const std::vector< GUM_SCALAR >& lower,
326 const std::vector< GUM_SCALAR >& upper) {
351 "setCPT : instantiation : " 352 << ins <<
" is not valid for node id " <<
id 353 <<
" which accepts instantiations such as (order is not " 358 Idx entry = 0, jump = 1;
360 for (
Idx i = 0, end = ins.
nbrDim(); i < end; i++) {
361 if (__src_bn.nodeId(ins.
variable(i)) ==
id)
continue;
363 entry += ins.
val(i) * jump;
383 fillConstraint(
id, entry, lower, upper);
389 template <
typename GUM_SCALAR >
394 template <
typename GUM_SCALAR >
396 return __src_bn.variable(
id).domainSize();
401 template <
typename GUM_SCALAR >
403 const std::string& src_max_den) {
406 __initCNNets(src_min_num, src_max_den);
415 template <
typename GUM_SCALAR >
420 __initCNNets(src_min_num, src_max_den);
429 template <
typename GUM_SCALAR >
431 if (__current_bn !=
nullptr)
delete __current_bn;
433 if (__credalNet_current_cpt !=
nullptr)
delete __credalNet_current_cpt;
435 if (__current_nodeType !=
nullptr)
delete __current_nodeType;
441 template <
typename GUM_SCALAR >
444 const bool keepZeroes) {
445 GUM_SCALAR epsi_min = 1.;
446 GUM_SCALAR epsi_max = 0.;
447 GUM_SCALAR epsi_moy = 0.;
448 GUM_SCALAR epsi_den = 0.;
450 for (
auto node : src_bn().nodes()) {
458 Size var_dSize = __src_bn.variable(node).domainSize();
459 Size entry_size = potential->domainSize() / var_dSize;
469 std::vector< GUM_SCALAR > vertex(var_dSize);
471 for (
Size entry = 0; entry < entry_size; entry++) {
477 den = potential_max->
get(ins_max);
481 for (
Size modality = 0; modality < var_dSize; modality++) {
482 vertex[modality] = potential->get(ins);
485 den += vertex[modality];
487 if (vertex[modality] < 1 && vertex[modality] > 0)
489 "bnToCredal : the BayesNet contains " 490 "probabilities and not event counts " 491 "although user precised oneNet = " 495 if (vertex[modality] > 0) nbm++;
504 for (
auto modality = vertex.cbegin(), theEnd = vertex.cend();
510 if (std::fabs(1. - sum) > __epsRedund) {
512 __src_bn.variable(node).name() <<
"(" << __epsRedund <<
")" 513 <<
" " << entry << std::endl
514 << vertex << std::endl
515 << ins << std::endl);
525 else if (den == 0 || beta == 1)
526 epsilon = GUM_SCALAR(1.0);
528 epsilon = GUM_SCALAR(std::pow(beta, std::log1p(den)));
533 if (epsilon > epsi_max) epsi_max = epsilon;
535 if (epsilon < epsi_min) epsi_min = epsilon;
539 for (
Size modality = 0; modality < var_dSize; modality++) {
540 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) {
541 min = GUM_SCALAR((1. - epsilon) * vertex[modality]);
543 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
545 max = GUM_SCALAR(min + epsilon);
549 min = vertex[modality];
551 if (oneNet) min = GUM_SCALAR(min * 1.0 / den);
556 potential_min->
set(ins_min, min);
557 potential_max->
set(ins_max, max);
567 __epsilonMin = epsi_min;
568 __epsilonMax = epsi_max;
569 __epsilonMoy = (GUM_SCALAR)epsi_moy / (GUM_SCALAR)epsi_den;
571 __intervalToCredal();
574 template <
typename GUM_SCALAR >
576 for (
auto node : __src_bn.nodes()) {
580 auto entry_size = potential->domainSize() / var_dSize;
586 std::vector< GUM_SCALAR > vertex(var_dSize);
588 for (
Size entry = 0; entry < entry_size; entry++) {
593 for (
Size modality = 0; modality < var_dSize; modality++) {
594 vertex[modality] = potential->get(ins);
596 if (vertex[modality] < 1 && vertex[modality] > 0)
598 "lagrangeNormalization : the BayesNet " 599 "contains probabilities and not event " 602 den += vertex[modality];
604 if (!zeroes && vertex[modality] == 0) { zeroes =
true; }
612 for (
Size modality = 0; modality < var_dSize; modality++) {
613 potential->set(ins, potential->get(ins) + 1);
623 template <
typename GUM_SCALAR >
625 for (
auto node : __src_bn.nodes()) {
633 Size var_dSize = __src_bn.variable(node).domainSize();
634 Size entry_size = potential->domainSize() / var_dSize;
644 std::vector< GUM_SCALAR > vertex(var_dSize);
646 for (
Size entry = 0; entry < entry_size; entry++) {
650 for (
Size modality = 0; modality < var_dSize; modality++) {
651 vertex[modality] = potential->get(ins);
653 if (vertex[modality] < 1 && vertex[modality] > 0)
655 "idmLearning : the BayesNet contains " 656 "probabilities and not event counts.");
658 den += vertex[modality];
660 if (vertex[modality] > 0) nbm++;
665 if (nbm > 1 || !keepZeroes) den += s;
669 for (
Size modality = 0; modality < var_dSize; modality++) {
670 min = vertex[modality];
673 if ((vertex[modality] > 0 && nbm > 1) || !keepZeroes) { max += s; }
675 min = GUM_SCALAR(min * 1.0 / den);
676 max = GUM_SCALAR(max * 1.0 / den);
678 potential_min->
set(ins_min, min);
679 potential_max->
set(ins_max, max);
689 __epsilonMin = GUM_SCALAR(s);
690 __epsilonMax = GUM_SCALAR(s);
691 __epsilonMoy = GUM_SCALAR(s);
692 __intervalToCredal();
696 template <
typename GUM_SCALAR >
698 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
700 __credalNet_src_cpt.resize(__src_bn.size());
702 for (
auto node : __src_bn.nodes()) {
704 &__src_bn_min.cpt(node));
706 &__src_bn_max.cpt(node));
709 Size entry_size = potential_min->domainSize() / var_dSize;
711 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
720 std::vector< GUM_SCALAR > lower(var_dSize);
721 std::vector< GUM_SCALAR > upper(var_dSize);
723 for (
Size entry = 0; entry < entry_size; entry++) {
724 for (
Size modality = 0; modality < var_dSize;
725 modality++, ++ins_min, ++ins_max) {
726 lower[modality] = potential_min->get(ins_min);
727 upper[modality] = potential_max->get(ins_max);
730 bool all_equals =
true;
731 std::vector< std::vector< GUM_SCALAR > > vertices;
733 for (
Size modality = 0; modality < var_dSize; modality++) {
734 if (std::fabs(upper[modality] - lower[modality]) < 1e-6)
continue;
737 std::vector< GUM_SCALAR > vertex(var_dSize);
738 vertex[modality] = upper[modality];
740 for (
Size mod = 0; mod < var_dSize; mod++) {
741 if (modality != mod) vertex[mod] = lower[mod];
744 GUM_SCALAR total = 0;
746 auto vsize = vertex.size();
748 for (
Size i = 0; i < vsize; i++)
751 if (std::fabs(total - 1.) > 1e-6)
753 __src_bn.variable(node).name() <<
" " << entry << std::endl
754 << vertex << std::endl);
756 vertices.push_back(vertex);
760 std::vector< GUM_SCALAR > vertex(var_dSize);
762 for (
Size modality = 0; modality < var_dSize; modality++)
763 vertex[modality] = lower[modality];
765 GUM_SCALAR total = 0.;
767 auto vsize = vertex.size();
769 for (
Size i = 0; i < vsize; i++)
772 if (std::fabs(total - 1.) > 1e-6)
774 __src_bn.variable(node).name() <<
" " << entry << std::endl
775 << vertex << std::endl);
777 vertices.push_back(vertex);
780 var_cpt[entry] = vertices;
783 __credalNet_src_cpt.insert(node, var_cpt);
789 __separatelySpecified =
true;
793 template <
typename GUM_SCALAR >
795 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
797 __credalNet_src_cpt.resize(__src_bn.size());
801 for (
auto node : __src_bn.nodes()) {
803 &__src_bn_min.cpt(node));
805 &__src_bn_max.cpt(node));
808 Size entry_size = potential_min->domainSize() / var_dSize;
810 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
819 lrsWrapper.
setUpH(var_dSize);
821 for (
Size entry = 0; entry < entry_size; entry++) {
822 for (
Size modality = 0; modality < var_dSize; modality++) {
824 potential_min->get(ins_min), potential_max->get(ins_max), modality);
834 __credalNet_src_cpt.insert(node, var_cpt);
840 __separatelySpecified =
true;
844 template <
typename GUM_SCALAR >
846 if (!__credalNet_src_cpt.empty()) __credalNet_src_cpt.clear();
848 __credalNet_src_cpt.resize(__src_bn.size());
850 for (
auto node : __src_bn.nodes()) {
852 &__src_bn_min.cpt(node));
854 &__src_bn_max.cpt(node));
857 auto entry_size = potential_min->domainSize() / var_dSize;
859 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
869 for (
Size entry = 0; entry < entry_size; entry++) {
870 std::vector< std::vector< GUM_SCALAR > > vertices;
871 std::vector< GUM_SCALAR > vertex(var_dSize);
873 std::vector< std::vector< GUM_SCALAR > > inequalities(
874 var_dSize * 2, std::vector< GUM_SCALAR >(var_dSize + 1, 0));
876 std::vector< GUM_SCALAR > sum_ineq1(var_dSize + 1, -1);
877 std::vector< GUM_SCALAR > sum_ineq2(var_dSize + 1, 1);
881 bool isInterval =
false;
883 for (
Size modality = 0; modality < var_dSize; modality++) {
884 inequalities[modality * 2][0] = -potential_min->get(ins_min);
885 inequalities[modality * 2 + 1][0] = potential_max->get(ins_max);
886 inequalities[modality * 2][modality + 1] = 1;
887 inequalities[modality * 2 + 1][modality + 1] = -1;
889 vertex[modality] = inequalities[modality * 2 + 1][0];
892 && (-inequalities[modality * 2][0]
893 != inequalities[modality * 2 + 1][0]))
900 inequalities.push_back(sum_ineq1);
901 inequalities.push_back(sum_ineq2);
904 vertices.push_back(vertex);
907 __H2Vlrs(inequalities, vertices);
909 }
catch (
const std::exception& err) {
910 std::cout << err.what() << std::endl;
916 if (entry == 0 && vertices.size() >= 2) {
917 auto tmp = vertices[0];
918 vertices[0] = vertices[1];
922 var_cpt[entry] = vertices;
926 __credalNet_src_cpt.insert(node, var_cpt);
934 __separatelySpecified =
true;
941 template <
typename GUM_SCALAR >
944 const std::string& max_path)
const {
947 std::string minfilename = min_path;
948 std::string maxfilename = max_path;
949 std::ofstream min_file(minfilename.c_str(), std::ios::out | std::ios::trunc);
950 std::ofstream max_file(maxfilename.c_str(), std::ios::out | std::ios::trunc);
952 if (!min_file.good())
955 "bnToCredal() : could not open stream : min_file : " << minfilename);
957 if (!max_file.good()) {
961 "bnToCredal() : could not open stream : min_file : " << maxfilename);
965 writer.
write(min_file, __src_bn_min);
966 writer.
write(max_file, __src_bn_max);
978 template <
typename GUM_SCALAR >
986 std::vector< std::vector< std::vector< GUM_SCALAR > > > >();
994 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
995 __credalNet_current_cpt;
997 if (this->__current_bn ==
nullptr)
998 __current_bn = &this->__src_bn;
1000 __current_bn = this->__current_bn;
1002 if (this->__credalNet_current_cpt ==
nullptr)
1003 __credalNet_current_cpt = &this->__credalNet_src_cpt;
1005 __credalNet_current_cpt = this->__credalNet_current_cpt;
1012 if (!__var_bits.empty()) __var_bits.
clear();
1016 for (
auto node : __current_bn->nodes()) {
1017 auto var_dSize = __current_bn->variable(node).domainSize();
1019 if (var_dSize != 2) {
1024 std::string bit_name;
1025 std::vector< NodeId > bits(nb_bits);
1027 for (
Size bit = 0; bit < nb_bits; bit++) {
1028 bit_name = __current_bn->variable(node).name() +
" - bit - ";
1029 std::stringstream ss;
1031 bit_name += ss.str();
1039 __var_bits.insert(node, bits);
1043 std::string bit_name = __current_bn->variable(node).name();
1047 __var_bits.insert(node, std::vector< NodeId >(1, iD));
1052 for (
auto node : __current_bn->nodes()) {
1053 NodeSet parents = __current_bn->parents(node);
1055 if (!parents.
empty()) {
1056 for (
auto par : __current_bn->parents(node)) {
1057 for (
Size parent_bit = 0, spbits =
Size(__var_bits[par].size());
1058 parent_bit < spbits;
1060 for (
Size var_bit = 0, mbits =
Size(__var_bits[node].size());
1063 __bin_bn->
addArc(__var_bits[par][parent_bit],
1064 __var_bits[node][var_bit]);
1069 auto bitsize = __var_bits[node].size();
1071 for (
Size bit_c = 1; bit_c < bitsize; bit_c++)
1072 for (
Size bit_p = 0; bit_p < bit_c; bit_p++)
1073 __bin_bn->
addArc(__var_bits[node][bit_p], __var_bits[node][bit_c]);
1081 auto varsize = __current_bn->
size();
1083 for (
Size var = 0; var < varsize; var++) {
1084 auto bitsize = __var_bits[var].size();
1086 for (
Size i = 0; i < bitsize; i++) {
1088 &__bin_bn->
cpt(__var_bits[var][i]));
1092 auto entry_size = potential->
domainSize() / 2;
1093 std::vector< std::vector< std::vector< GUM_SCALAR > > > var_cpt(
1098 for (
Size conf = 0; conf < entry_size; conf++) {
1099 std::vector< std::vector< GUM_SCALAR > > pvar_cpt;
1100 auto verticessize = (*__credalNet_current_cpt)[var][old_conf].size();
1102 for (
Size old_distri = 0; old_distri < verticessize; old_distri++) {
1103 const std::vector< GUM_SCALAR >& vertex =
1104 (*__credalNet_current_cpt)[var][old_conf][old_distri];
1105 auto vertexsize = vertex.size();
1107 std::vector< Idx > incc(vertexsize, 0);
1109 for (
Size preced = 0; preced < i; preced++) {
1111 ins.
pos(__bin_bn->
variable(__var_bits[var][preced]));
1112 auto val = ins.val(bit_pos);
1122 while (elem < vertexsize) {
1126 if (elem % pas == 0) elem += pas;
1132 std::vector< GUM_SCALAR > distri(2, 0);
1135 for (
Size elem = 0; elem < vertexsize; elem++) {
1136 if (elem % pas == 0) pos = -pos;
1138 if (incc[elem] == i)
1139 (pos < 0) ? (distri[0] += vertex[elem])
1140 : (distri[1] += vertex[elem]);
1144 GUM_SCALAR den = distri[0] + distri[1];
1155 pvar_cpt.push_back(distri);
1160 std::vector< std::vector< GUM_SCALAR > > vertices(
1161 2, std::vector< GUM_SCALAR >(2, 1));
1164 auto new_verticessize = pvar_cpt.size();
1166 for (
Size v = 0; v < new_verticessize; v++) {
1167 if (pvar_cpt[v][1] < vertices[0][1]) vertices[0][1] = pvar_cpt[v][1];
1169 if (pvar_cpt[v][1] > vertices[1][1]) vertices[1][1] = pvar_cpt[v][1];
1172 vertices[0][0] = 1 - vertices[0][1];
1173 vertices[1][0] = 1 - vertices[1][1];
1175 pvar_cpt = vertices;
1177 var_cpt[conf] = pvar_cpt;
1184 if (old_conf == (*__credalNet_current_cpt)[var].size()) old_conf = 0;
1188 __credalNet_bin_cpt->insert(__var_bits[var][i], var_cpt);
1197 auto old_varsize = __var_bits.size();
1199 for (
Size i = 0; i < old_varsize; i++) {
1200 auto bitsize = __var_bits[i].size();
1203 if (bitsize == 1)
continue;
1205 auto old_card = __src_bn.variable(i).domainSize();
1207 for (
Size mod = 0; mod < old_card; mod++) {
1210 std::stringstream ss;
1211 ss << __src_bn.variable(i).name();
1217 const NodeId indic = __bin_bn->
add(var);
1220 for (
Size bit = 0; bit < bitsize; bit++)
1221 __bin_bn->
addArc(__var_bits[i][bit], indic);
1226 std::vector< std::vector< std::vector< GUM_SCALAR > > > icpt(num);
1228 for (
Size entry = 0; entry < num; entry++) {
1229 std::vector< std::vector< GUM_SCALAR > > vertices(
1230 1, std::vector< GUM_SCALAR >(2, 0));
1237 icpt[entry] = vertices;
1240 __credalNet_bin_cpt->insert(indic, icpt);
1242 __bin_nodeType->insert(indic, NodeType::Indic);
1248 if (this->__current_bn !=
nullptr)
delete this->__current_bn;
1250 this->__current_bn = __bin_bn;
1252 if (this->__credalNet_current_cpt !=
nullptr)
1253 delete this->__credalNet_current_cpt;
1255 this->__credalNet_current_cpt = __credalNet_bin_cpt;
1257 if (this->__current_nodeType !=
nullptr)
delete this->__current_nodeType;
1259 this->__current_nodeType = __bin_nodeType;
1265 template <
typename GUM_SCALAR >
1268 if (__credalNet_current_cpt !=
nullptr)
return *__credalNet_current_cpt;
1270 return __credalNet_src_cpt;
1273 template <
typename GUM_SCALAR >
1276 return __credalNet_src_cpt;
1279 template <
typename GUM_SCALAR >
1282 if (__current_nodeType !=
nullptr)
return (*(__current_nodeType))[id];
1284 return __original_nodeType[id];
1287 template <
typename GUM_SCALAR >
1290 return __original_nodeType[id];
1293 template <
typename GUM_SCALAR >
1295 return __separatelySpecified;
1298 template <
typename GUM_SCALAR >
1300 return __hasComputedCPTMinMax;
1304 template <
typename GUM_SCALAR >
1306 __binCptMin.resize(current_bn().size());
1307 __binCptMax.resize(current_bn().size());
1309 for (
auto node : current_bn().nodes()) {
1310 auto pConf = credalNet_currentCpt()[node].size();
1311 std::vector< GUM_SCALAR > min(pConf);
1312 std::vector< GUM_SCALAR > max(pConf);
1314 for (
Size pconf = 0; pconf < pConf; pconf++) {
1316 v1 = credalNet_currentCpt()[node][pconf][0][1];
1318 if (credalNet_currentCpt()[node][pconf].size() > 1)
1319 v2 = credalNet_currentCpt()[node][pconf][1][1];
1323 GUM_SCALAR delta = v1 - v2;
1324 min[pconf] = (delta >= 0) ? v2 : v1;
1325 max[pconf] = (delta >= 0) ? v1 : v2;
1328 __binCptMin[node] = min;
1329 __binCptMax[node] = max;
1332 __hasComputedCPTMinMax =
true;
1335 template <
typename GUM_SCALAR >
1336 const std::vector< std::vector< GUM_SCALAR > >&
1341 template <
typename GUM_SCALAR >
1342 const std::vector< std::vector< GUM_SCALAR > >&
1347 template <
typename GUM_SCALAR >
1349 return __epsilonMin;
1352 template <
typename GUM_SCALAR >
1354 return __epsilonMax;
1357 template <
typename GUM_SCALAR >
1359 return __epsilonMoy;
1362 template <
typename GUM_SCALAR >
1364 std::stringstream output;
1367 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1368 __credalNet_current_cpt;
1370 if (this->__current_bn ==
nullptr)
1371 __current_bn = &this->__src_bn;
1373 __current_bn = this->__current_bn;
1375 if (this->__credalNet_current_cpt ==
nullptr)
1376 __credalNet_current_cpt = &this->__credalNet_src_cpt;
1378 __credalNet_current_cpt = this->__credalNet_current_cpt;
1380 for (
auto node : __current_bn->
nodes()) {
1385 output <<
"\n" << __current_bn->
variable(node) <<
"\n";
1392 for (
Size pconf = 0; pconf < pconfs; pconf++) {
1393 output << ins <<
" : ";
1394 output << (*__credalNet_current_cpt)[node][pconf] <<
"\n";
1396 if (pconf < pconfs - 1) ++ins;
1402 return output.str();
1405 template <
typename GUM_SCALAR >
1407 if (__current_bn !=
nullptr)
return *__current_bn;
1412 template <
typename GUM_SCALAR >
1421 template <
typename GUM_SCALAR >
1427 __epsRedund = GUM_SCALAR(1e-6);
1430 __epsF = GUM_SCALAR(1e-6);
1431 __denMax = GUM_SCALAR(1e6);
1435 __precisionC = GUM_SCALAR(1e6);
1439 __precision = GUM_SCALAR(1e6);
1441 __current_bn =
nullptr;
1442 __credalNet_current_cpt =
nullptr;
1443 __current_nodeType =
nullptr;
1445 __hasComputedCPTMinMax =
false;
1448 template <
typename GUM_SCALAR >
1450 const std::string& src_max_den) {
1454 if (src_max_den.compare(
"") != 0)
1455 other = src_max_den;
1457 other = src_min_num;
1484 template <
typename GUM_SCALAR >
1488 __src_bn = src_min_num;
1489 __src_bn_min = src_min_num;
1491 if (src_max_den.
size() > 0)
1492 __src_bn_max = src_max_den;
1494 __src_bn_max = src_min_num;
1497 template <
typename GUM_SCALAR >
1499 const std::vector< std::vector< std::vector< GUM_SCALAR > > >& var_cpt)
1501 Size vertices_size = 0;
1503 for (
auto entry = var_cpt.cbegin(), theEnd = var_cpt.cend(); entry != theEnd;
1505 if (entry->size() > vertices_size) vertices_size =
Size(entry->size());
1508 return int(vertices_size);
1511 template <
typename GUM_SCALAR >
1515 if (this->__current_bn ==
nullptr)
1516 __current_bn = &this->__src_bn;
1518 __current_bn = this->__current_bn;
1520 for (
auto node : __current_bn->
nodes())
1525 for (
auto node : __current_bn->
nodes()) {
1526 for (
auto parent_idIt : __current_bn->
cpt(node).variablesSequence()) {
1527 if (__current_bn->
nodeId(*parent_idIt) != node)
1587 template <
typename GUM_SCALAR >
1589 const std::vector< std::vector< GUM_SCALAR > >& h_rep,
1590 std::vector< std::vector< GUM_SCALAR > >& v_rep)
const {
1594 std::string sinefile =
1601 std::ofstream h_file(sinefile.c_str(), std::ios::out | std::ios::trunc);
1605 "__H2Vlrs : could not open lrs input file : " << sinefile);
1607 h_file <<
"H - representation\n";
1608 h_file <<
"begin\n";
1609 h_file << h_rep.size() <<
' ' << h_rep[0].size() <<
" rational\n";
1611 for (
auto it = h_rep.cbegin(), theEnd = h_rep.cend(); it != theEnd; ++it) {
1612 for (
auto it2 = it->cbegin(), theEnd2 = it->cend(); it2 != theEnd2;
1618 num, den, ((*it2 > 0) ? *it2 : -*it2), int64_t(__denMax), __epsF);
1620 h_file << ((*it2 > 0) ? num : -num) <<
'/' << den <<
' ';
1633 std::string soft_name =
"lrs";
1634 std::string extfile(sinefile);
1637 args[0] =
new char[soft_name.size()];
1638 args[1] =
new char[sinefile.size()];
1639 args[2] =
new char[extfile.size()];
1641 strcpy(args[0], soft_name.c_str());
1642 strcpy(args[1], sinefile.c_str());
1643 strcpy(args[2], extfile.c_str());
1646 int old_cout, new_cout;
1650 new_cout = open(
"/dev/null", O_WRONLY);
1666 std::ifstream v_file(extfile.c_str() , std::ios::in);
1671 std::string line, tmp;
1673 GUM_SCALAR probability;
1675 std::string::size_type pos;
1676 bool keep_going =
true;
1679 std::vector< GUM_SCALAR > vertex;
1681 v_file.ignore(256,
'l');
1683 while (v_file.good() && keep_going) {
1684 getline(v_file, line);
1686 if (line.size() == 0)
1688 else if (line.compare(
"end") == 0) {
1698 }
else if (line[1] !=
'1') {
1700 "__H2Vlrs : reading something other than a vertex from " 1701 "lrs output file : ");
1704 line = line.substr(2);
1705 cstr =
new char[line.size() + 1];
1706 strcpy(cstr, line.c_str());
1708 p = strtok(cstr,
" ");
1710 while (p !=
nullptr) {
1713 if (tmp.compare(
"1") == 0 || tmp.compare(
"0") == 0)
1714 probability = GUM_SCALAR(atof(tmp.c_str()));
1716 pos = tmp.find(
"/");
1718 GUM_SCALAR(atof(tmp.substr(0, pos).c_str())
1719 / atof(tmp.substr(pos + 1, tmp.size()).c_str()));
1722 vertex.push_back(probability);
1723 p = strtok(
nullptr,
" ");
1729 bool is_redund =
false;
1731 #pragma omp parallel 1736 auto begin_pos = (this_thread + 0) * v_rep.size() / num_threads;
1737 auto end_pos = (this_thread + 1) * v_rep.size() / num_threads;
1739 for (
auto p = begin_pos; p < end_pos; p++) {
1740 #pragma omp flush(is_redund) 1742 if (is_redund)
break;
1744 bool thread_redund =
true;
1746 auto vsize = vertex.size();
1748 for (
Size modality = 0; modality < vsize; modality++) {
1749 if (std::fabs(vertex[modality] - v_rep[p][modality]) > __epsRedund) {
1750 thread_redund =
false;
1755 if (thread_redund) {
1757 #pragma omp flush(is_redund) 1762 if (!is_redund) v_rep.push_back(vertex);
1770 if (std::remove(sinefile.c_str()) != 0)
1773 if (std::remove(extfile.c_str()) != 0)
1777 template <
typename GUM_SCALAR >
1781 std::vector< std::vector< std::vector< GUM_SCALAR > > > >*
1782 __credalNet_current_cpt;
1786 if (this->__current_bn ==
nullptr)
1787 __current_bn = &__src_bn;
1789 __current_bn = this->__current_bn;
1791 if (this->__credalNet_current_cpt ==
nullptr)
1792 __credalNet_current_cpt = &__credalNet_src_cpt;
1794 __credalNet_current_cpt = this->__credalNet_current_cpt;
1796 if (this->__current_nodeType ==
nullptr)
1797 __current_nodeType = &__original_nodeType;
1799 __current_nodeType = this->__current_nodeType;
1804 for (
auto node : __current_bn->
nodes()) {
1806 if (__current_nodeType->
exists(node))
continue;
1808 bool precise =
true, vacuous =
true;
1810 for (
auto entry = (*__credalNet_current_cpt)[node].cbegin(),
1811 theEnd2 = (*__credalNet_current_cpt)[node].cend();
1814 auto vertices = entry->
size();
1815 auto var_dSize = (*entry)[0].size();
1817 if (precise && vertices > 1) precise =
false;
1819 if (vacuous && vertices == var_dSize) {
1820 std::vector< bool > elem(var_dSize,
false);
1822 for (
auto vertex = entry->cbegin(), vEnd = entry->cend();
1825 for (
auto probability = vertex->cbegin(), pEnd = vertex->cend();
1826 probability != pEnd;
1828 if (*probability == 1) {
1829 elem[probability - vertex->begin()] =
true;
1837 for (
auto probability =
1839 probability != elem.cend();
1841 if (*probability ==
false) vacuous =
false;
1847 if (vacuous ==
false && precise ==
false) {
1848 __current_nodeType->
insert(node, NodeType::Credal);
1855 __current_nodeType->
insert(node, NodeType::Vacuous);
1857 __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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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)...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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)