59 template <
typename GUM_SCALAR >
63 std::string name = node;
64 auto ds = default_domain_size;
66 long range_max = long(ds) - 1;
67 std::vector< std::string > labels;
68 std::vector< GUM_SCALAR > ticks;
70 if (*(node.rbegin()) ==
']') {
71 auto posBrack = node.find(
'[');
72 if (posBrack != std::string::npos) {
73 name = node.substr(0, posBrack);
74 const auto& s_args = node.substr(posBrack + 1, node.size() - posBrack - 2);
75 const auto& args =
split(s_args,
",");
76 if (args.size() == 0) {
78 }
else if (args.size() == 1) {
79 ds =
static_cast< Size >(std::stoi(args[0]));
81 range_max = long(ds) - 1;
82 }
else if (args.size() == 2) {
83 range_min = std::stol(args[0]);
84 range_max = std::stol(args[1]);
85 if (1 + range_max - range_min < 2) {
88 ds =
static_cast< Size >(1 + range_max - range_min);
90 for (
const auto& tick: args) {
91 ticks.push_back(static_cast< GUM_SCALAR >(std::atof(tick.c_str())));
93 ds =
static_cast< Size >(args.size() - 1);
96 }
else if (*(node.rbegin()) ==
'}') {
97 auto posBrack = node.find(
'{');
98 if (posBrack != std::string::npos) {
99 name = node.substr(0, posBrack);
100 labels =
split(node.substr(posBrack + 1, node.size() - posBrack - 2),
"|");
101 if (labels.size() < 2) {
107 ds =
static_cast< Size >(labels.size());
113 }
else if (ds == 1) {
115 "Only one value for variable " << name
116 <<
" (2 at least are needed).");
124 if (!labels.empty()) {
126 }
else if (!ticks.empty()) {
136 template <
typename GUM_SCALAR >
143 for (
const auto& chaine:
split(dotlike,
";")) {
145 bool notfirst =
false;
146 for (
const auto& souschaine:
split(chaine,
"->")) {
148 for (
const auto& node:
split(souschaine,
"<-")) {
149 auto idVar =
build_node(bn, node, domainSize);
170 template <
typename GUM_SCALAR >
175 template <
typename GUM_SCALAR >
181 template <
typename GUM_SCALAR >
183 IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
186 __copyPotentials(source);
189 template <
typename GUM_SCALAR >
192 if (
this != &source) {
197 __copyPotentials(source);
203 template <
typename GUM_SCALAR >
206 for (
const auto p: __probaMap) {
211 template <
typename GUM_SCALAR >
214 return __varMap.get(
id);
217 template <
typename GUM_SCALAR >
220 const std::string& new_name) {
221 __varMap.changeName(
id, new_name);
224 template <
typename GUM_SCALAR >
226 NodeId id,
const std::string& old_label,
const std::string& new_label) {
227 if (variable(
id).varType() != VarType::Labelized) {
237 template <
typename GUM_SCALAR >
239 return __varMap.get(var);
242 template <
typename GUM_SCALAR >
258 template <
typename GUM_SCALAR >
260 unsigned int nbrmod) {
263 "Variable " << name <<
"needs more than " << nbrmod
271 template <
typename GUM_SCALAR >
274 NodeId proposedId = dag().nextNodeId();
277 res = add(var, aContent, proposedId);
282 template <
typename GUM_SCALAR >
289 res = add(var, ptr,
id);
299 template <
typename GUM_SCALAR >
304 __varMap.insert(
id, var);
305 this->_dag.addNodeWithId(
id);
308 (*cpt) << variable(
id);
309 __probaMap.insert(
id, cpt);
313 template <
typename GUM_SCALAR >
318 template <
typename GUM_SCALAR >
324 template <
typename GUM_SCALAR >
327 return *(__probaMap[varId]);
330 template <
typename GUM_SCALAR >
335 template <
typename GUM_SCALAR >
337 erase(__varMap.get(var));
340 template <
typename GUM_SCALAR >
342 if (__varMap.exists(varId)) {
344 const NodeSet& children = this->children(varId);
346 for (
const auto c: children) {
347 __probaMap[c]->erase(variable(varId));
350 delete __probaMap[varId];
352 __probaMap.erase(varId);
353 __varMap.erase(varId);
354 this->_dag.eraseNode(varId);
358 template <
typename GUM_SCALAR >
360 if (!this->empty()) {
361 auto l = this->nodes();
362 for (
const auto no: l) {
368 template <
typename GUM_SCALAR >
370 if (this->_dag.existsArc(tail, head)) {
372 "The arc (" << tail <<
"," << head <<
") already exists.")
375 this->_dag.addArc(tail, head);
377 (*(__probaMap[head])) << variable(tail);
380 template <
typename GUM_SCALAR >
382 const std::string& head) {
384 addArc(this->idFromName(tail), this->idFromName(head));
387 "The arc " << tail <<
"->" << head <<
" already exists.")
391 template <
typename GUM_SCALAR >
393 if (__varMap.exists(arc.
tail()) && __varMap.exists(arc.
head())) {
395 this->_dag.eraseArc(arc);
397 (*(__probaMap[head])) >> variable(tail);
401 template <
typename GUM_SCALAR >
403 eraseArc(
Arc(tail, head));
406 template <
typename GUM_SCALAR >
409 if (!__varMap.exists(arc.
tail()) || !__varMap.exists(arc.
head())
410 || !dag().existsArc(arc)) {
433 beginTopologyTransformation();
435 for (
const auto node: this->parents(tail))
437 for (
const auto node: this->parents(head))
443 for (
const auto p: new_parents) {
444 if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
449 new_parents.erase(tail);
451 for (
const auto p: new_parents) {
452 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
455 endTopologyTransformation();
459 del_vars << &(variable(tail));
461 prod.
margSumOut(del_vars).putFirst(&variable(head));
464 cpt_head = std::move(new_cpt_head);
467 (prod / cpt_head).putFirst(&variable(tail))};
469 cpt_tail = std::move(new_cpt_tail);
472 template <
typename GUM_SCALAR >
474 reverseArc(
Arc(tail, head));
481 template <
typename GUM_SCALAR >
486 template <
typename GUM_SCALAR >
493 template <
typename GUM_SCALAR >
499 template <
typename GUM_SCALAR >
507 template <
typename GUM_SCALAR >
515 template <
typename GUM_SCALAR >
520 template <
typename GUM_SCALAR >
525 template <
typename GUM_SCALAR >
530 template <
typename GUM_SCALAR >
541 template <
typename GUM_SCALAR >
543 GUM_SCALAR external_weight) {
544 return addNoisyORCompound(var, external_weight);
547 template <
typename GUM_SCALAR >
553 template <
typename GUM_SCALAR >
555 GUM_SCALAR external_weight) {
559 template <
typename GUM_SCALAR >
561 GUM_SCALAR external_weight) {
565 template <
typename GUM_SCALAR >
567 GUM_SCALAR external_weight) {
571 template <
typename GUM_SCALAR >
573 GUM_SCALAR external_weight,
575 return addNoisyORCompound(var, external_weight,
id);
578 template <
typename GUM_SCALAR >
580 GUM_SCALAR external_weight,
585 template <
typename GUM_SCALAR >
587 GUM_SCALAR external_weight,
592 template <
typename GUM_SCALAR >
599 template <
typename GUM_SCALAR >
601 GUM_SCALAR external_weight,
606 template <
typename GUM_SCALAR >
609 GUM_SCALAR causalWeight) {
617 CImodel->causalWeight(variable(tail), causalWeight);
620 "Head variable (" << variable(head).name()
621 <<
") is not a CIModel variable !");
625 template <
typename GUM_SCALAR >
633 template <
typename GUM_SCALAR >
635 for (
const auto node: nodes())
636 __probaMap[node]->beginMultipleChanges();
640 template <
typename GUM_SCALAR >
642 for (
const auto node: nodes())
643 __probaMap[node]->endMultipleChanges();
647 template <
typename GUM_SCALAR >
650 for (
const auto& elt: __probaMap) {
658 template <
typename GUM_SCALAR >
667 for (
gum::Idx i = 0; i < src.second->nbrDim(); i++) {
668 (*copy_array) << variableFromName(src.second->variable(i).name());
671 copy_array->
copyFrom(*(src.second));
674 __probaMap.insert(src.first, copy_array);
678 template <
typename GUM_SCALAR >
680 for (
const auto node: nodes())
684 template <
typename GUM_SCALAR >
688 generator.
generateCPT(cpt(node).pos(variable(node)), cpt(node));
691 template <
typename GUM_SCALAR >
694 if (cpt(
id).nbrDim() != newPot->
nbrDim()) {
696 "cannot exchange potentials with different " 697 "dimensions for variable with id " 701 for (
Idx i = 0; i < cpt(
id).nbrDim(); i++) {
702 if (&cpt(
id).variable(i) != &(newPot->
variable(i))) {
704 "cannot exchange potentials because, for variable with id " 705 <<
id <<
", dimension " << i <<
" differs. ");
709 _unsafeChangePotential(
id, newPot);
712 template <
typename GUM_SCALAR >
715 delete __probaMap[id];
716 __probaMap[id] = newPot;
719 template <
typename GUM_SCALAR >
722 changePotential(idFromName(name), newPot);
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
aGrUM's Potential is a multi-dimensional array with tensor operators.
Class representing a Bayesian Network.
virtual void beginMultipleChanges() final
Default implementation of MultiDimContainer::set().
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
bool hasUniqueElts(std::vector< T > const &x)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
VariableNodeMap __varMap
the map between variable and id
Class for discretized random variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Container used to map discrete variables with nodes.
BayesNet()
Default constructor.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size default_domain_size)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void clear()
clear the whole Bayes net *
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
NodeId head() const
returns the head of the arc
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize=2)
Create a Bayesian network with a dot-like syntax which specifies:
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
virtual Size domainSize() const =0
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
abstract class for Conditional Indepency Models
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Base class for all aGrUM's exceptions.
Idx posLabel(const std::string &label) const
return the pos from label
Multidimensional matrix stored as an array in memory.
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
<agrum/BN/generator/simpleCPTGenerator.h>
NodeProperty< Potential< GUM_SCALAR > *> __probaMap
Mapping between the variable's id and their CPT.
Noisy AND representation.
void generateCPTs() const
randomly generates CPTs for a given structure
Defines a discrete random variable over an integer interval.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
INLINE std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map's DAG in output using the Graphviz-dot format.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
std::string toString() const
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
<agrum/tools/multidim/multiDimImplementation.h>
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Size Idx
Type for indexes.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
NodeId idFromName(const std::string &name) const final
Returns a variable's id given its name in the gum::BayesNet.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Size NodeId
Type for node ids.
void insert(const Key &k)
Inserts a new element into the set.
void generateCPT(const Idx &varId, const Potential< GUM_SCALAR > &cpt) override
Generates a CPT using floats.
NodeId tail() const
returns the tail of the arc
#define GUM_ERROR(type, msg)