61 template <
typename GUM_SCALAR >
65 std::string name = node;
66 auto ds = default_domain_size;
68 long range_max = long(ds) - 1;
69 std::vector< std::string > labels;
70 std::vector< GUM_SCALAR > ticks;
72 if (*(node.rbegin()) ==
']') {
73 auto posBrack = node.find(
'[');
74 if (posBrack != std::string::npos) {
75 name = node.substr(0, posBrack);
76 const auto& s_args = node.substr(posBrack + 1, node.size() - posBrack - 2);
77 const auto& args =
split(s_args,
",");
78 if (args.size() == 0) {
80 }
else if (args.size() == 1) {
81 ds =
static_cast< Size >(std::stoi(args[0]));
83 range_max = long(ds) - 1;
84 }
else if (args.size() == 2) {
85 range_min = std::stol(args[0]);
86 range_max = std::stol(args[1]);
87 if (1 + range_max - range_min < 2) {
90 ds =
static_cast< Size >(1 + range_max - range_min);
92 for (
const auto& tick: args) {
93 ticks.push_back(static_cast< GUM_SCALAR >(std::atof(tick.c_str())));
95 ds =
static_cast< Size >(args.size() - 1);
98 }
else if (*(node.rbegin()) ==
'}') {
99 auto posBrack = node.find(
'{');
100 if (posBrack != std::string::npos) {
101 name = node.substr(0, posBrack);
102 labels =
split(node.substr(posBrack + 1, node.size() - posBrack - 2),
"|");
103 if (labels.size() < 2) {
109 ds =
static_cast< Size >(labels.size());
115 }
else if (ds == 1) {
117 "Only one value for variable " << name
118 <<
" (2 at least are needed).");
126 if (!labels.empty()) {
128 }
else if (!ticks.empty()) {
138 template <
typename GUM_SCALAR >
145 for (
const auto& chaine:
split(dotlike,
";")) {
147 bool notfirst =
false;
148 for (
const auto& souschaine:
split(chaine,
"->")) {
150 for (
const auto& node:
split(souschaine,
"<-")) {
151 auto idVar =
build_node(bn, node, domainSize);
172 template <
typename GUM_SCALAR >
177 template <
typename GUM_SCALAR >
183 template <
typename GUM_SCALAR >
185 IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
188 __copyPotentials(source);
191 template <
typename GUM_SCALAR >
194 if (
this != &source) {
199 __copyPotentials(source);
205 template <
typename GUM_SCALAR >
208 for (
const auto p: __probaMap) {
213 template <
typename GUM_SCALAR >
216 return __varMap.get(
id);
219 template <
typename GUM_SCALAR >
222 const std::string& new_name) {
223 __varMap.changeName(
id, new_name);
226 template <
typename GUM_SCALAR >
228 NodeId id,
const std::string& old_label,
const std::string& new_label) {
229 if (variable(
id).varType() != VarType::Labelized) {
239 template <
typename GUM_SCALAR >
241 return __varMap.get(var);
244 template <
typename GUM_SCALAR >
260 template <
typename GUM_SCALAR >
262 unsigned int nbrmod) {
265 "Variable " << name <<
"needs more than " << nbrmod
273 template <
typename GUM_SCALAR >
276 NodeId proposedId = dag().nextNodeId();
279 res = add(var, aContent, proposedId);
284 template <
typename GUM_SCALAR >
291 res = add(var, ptr,
id);
301 template <
typename GUM_SCALAR >
306 __varMap.insert(
id, var);
307 this->_dag.addNodeWithId(
id);
310 (*cpt) << variable(
id);
311 __probaMap.insert(
id, cpt);
315 template <
typename GUM_SCALAR >
320 template <
typename GUM_SCALAR >
326 template <
typename GUM_SCALAR >
329 return *(__probaMap[varId]);
332 template <
typename GUM_SCALAR >
337 template <
typename GUM_SCALAR >
339 erase(__varMap.get(var));
342 template <
typename GUM_SCALAR >
344 if (__varMap.exists(varId)) {
346 const NodeSet& children = this->children(varId);
348 for (
const auto c: children) {
349 __probaMap[c]->erase(variable(varId));
352 delete __probaMap[varId];
354 __probaMap.erase(varId);
355 __varMap.erase(varId);
356 this->_dag.eraseNode(varId);
360 template <
typename GUM_SCALAR >
362 if (!this->empty()) {
363 auto l = this->nodes();
364 for (
const auto no: l) {
370 template <
typename GUM_SCALAR >
372 if (this->_dag.existsArc(tail, head)) {
374 "The arc (" << tail <<
"," << head <<
") already exists.")
377 this->_dag.addArc(tail, head);
379 (*(__probaMap[head])) << variable(tail);
382 template <
typename GUM_SCALAR >
384 const std::string& head) {
386 addArc(this->idFromName(tail), this->idFromName(head));
389 "The arc " << tail <<
"->" << head <<
" already exists.")
393 template <
typename GUM_SCALAR >
395 if (__varMap.exists(arc.
tail()) && __varMap.exists(arc.
head())) {
397 this->_dag.eraseArc(arc);
399 (*(__probaMap[head])) >> variable(tail);
403 template <
typename GUM_SCALAR >
405 eraseArc(
Arc(tail, head));
408 template <
typename GUM_SCALAR >
411 if (!__varMap.exists(arc.
tail()) || !__varMap.exists(arc.
head())
412 || !dag().existsArc(arc)) {
435 beginTopologyTransformation();
437 for (
const auto node: this->parents(tail))
439 for (
const auto node: this->parents(head))
445 for (
const auto p: new_parents) {
446 if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
451 new_parents.erase(tail);
453 for (
const auto p: new_parents) {
454 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
457 endTopologyTransformation();
461 del_vars << &(variable(tail));
463 prod.
margSumOut(del_vars).putFirst(&variable(head));
466 cpt_head = std::move(new_cpt_head);
469 (prod / cpt_head).putFirst(&variable(tail))};
471 cpt_tail = std::move(new_cpt_tail);
474 template <
typename GUM_SCALAR >
476 reverseArc(
Arc(tail, head));
483 template <
typename GUM_SCALAR >
488 template <
typename GUM_SCALAR >
495 template <
typename GUM_SCALAR >
501 template <
typename GUM_SCALAR >
509 template <
typename GUM_SCALAR >
517 template <
typename GUM_SCALAR >
522 template <
typename GUM_SCALAR >
527 template <
typename GUM_SCALAR >
532 template <
typename GUM_SCALAR >
543 template <
typename GUM_SCALAR >
545 GUM_SCALAR external_weight) {
546 return addNoisyORCompound(var, external_weight);
549 template <
typename GUM_SCALAR >
555 template <
typename GUM_SCALAR >
557 GUM_SCALAR external_weight) {
561 template <
typename GUM_SCALAR >
563 GUM_SCALAR external_weight) {
567 template <
typename GUM_SCALAR >
569 GUM_SCALAR external_weight) {
573 template <
typename GUM_SCALAR >
575 GUM_SCALAR external_weight,
577 return addNoisyORCompound(var, external_weight,
id);
580 template <
typename GUM_SCALAR >
582 GUM_SCALAR external_weight,
587 template <
typename GUM_SCALAR >
589 GUM_SCALAR external_weight,
594 template <
typename GUM_SCALAR >
601 template <
typename GUM_SCALAR >
603 GUM_SCALAR external_weight,
608 template <
typename GUM_SCALAR >
611 GUM_SCALAR causalWeight) {
619 CImodel->causalWeight(variable(tail), causalWeight);
622 "Head variable (" << variable(head).name()
623 <<
") is not a CIModel variable !");
627 template <
typename GUM_SCALAR >
635 template <
typename GUM_SCALAR >
637 for (
const auto node: nodes())
638 __probaMap[node]->beginMultipleChanges();
642 template <
typename GUM_SCALAR >
644 for (
const auto node: nodes())
645 __probaMap[node]->endMultipleChanges();
649 template <
typename GUM_SCALAR >
652 for (
const auto& elt: __probaMap) {
660 template <
typename GUM_SCALAR >
669 for (
gum::Idx i = 0; i < src.second->nbrDim(); i++) {
670 (*copy_array) << variableFromName(src.second->variable(i).name());
673 copy_array->
copyFrom(*(src.second));
676 __probaMap.insert(src.first, copy_array);
680 template <
typename GUM_SCALAR >
682 for (
const auto node: nodes())
686 template <
typename GUM_SCALAR >
690 generator.
generateCPT(cpt(node).pos(variable(node)), cpt(node));
693 template <
typename GUM_SCALAR >
696 if (cpt(
id).nbrDim() != newPot->
nbrDim()) {
698 "cannot exchange potentials with different " 699 "dimensions for variable with id " 703 for (
Idx i = 0; i < cpt(
id).nbrDim(); i++) {
704 if (&cpt(
id).variable(i) != &(newPot->
variable(i))) {
706 "cannot exchange potentials because, for variable with id " 707 <<
id <<
", dimension " << i <<
" differs. ");
711 _unsafeChangePotential(
id, newPot);
714 template <
typename GUM_SCALAR >
717 delete __probaMap[id];
718 __probaMap[id] = newPot;
721 template <
typename GUM_SCALAR >
724 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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
bool hasUniqueElts(std::vector< T > const &x)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Container used to map discrete variables with nodes.
BayesNet()
Default constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size default_domain_size)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void clear()
clear the whole Bayesnet *
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void endMultipleChanges() final
Default implementation of MultiDimContainer::set().
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this DAGModel.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
<agrum/multidim/multiDimImplementation.h>
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size Idx
Type for indexes.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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)