60 template <
typename GUM_SCALAR >
64 std::string name = node;
66 std::vector< std::string > labels;
69 auto posBrack = node.find(
'[');
70 if (posBrack != std::string::npos) {
71 if (*(node.rbegin()) !=
']')
74 name = node.substr(0, posBrack);
75 ds =
static_cast< Size >(
76 std::stoi(node.substr(posBrack + 1, node.size() - posBrack - 2)));
81 posBrack = node.find(
'{');
82 if (posBrack != std::string::npos) {
83 if (*(node.rbegin()) !=
'}')
86 name = node.substr(0, posBrack);
87 labels =
split(node.substr(posBrack + 1, node.size() - posBrack - 2),
"|");
88 if (labels.size() < 2) {
94 ds =
static_cast< Size >(labels.size());
100 }
else if (ds == 1) {
102 "Only one value for variable " << name
103 <<
" (2 at least are needed).");
111 if (labels.empty()) {
115 for (
const auto& label : labels) {
125 template <
typename GUM_SCALAR >
132 for (
const auto& chaine :
split(dotlike,
";")) {
134 bool notfirst =
false;
135 for (
const auto& souschaine :
split(chaine,
"->")) {
137 for (
const auto& node :
split(souschaine,
"<-")) {
138 auto idVar =
build_node(bn, node, domainSize);
159 template <
typename GUM_SCALAR >
164 template <
typename GUM_SCALAR >
170 template <
typename GUM_SCALAR >
172 IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
175 __copyPotentials(source);
178 template <
typename GUM_SCALAR >
181 if (
this != &source) {
186 __copyPotentials(source);
192 template <
typename GUM_SCALAR >
195 for (
const auto p : __probaMap) {
200 template <
typename GUM_SCALAR >
203 return __varMap.get(
id);
206 template <
typename GUM_SCALAR >
209 const std::string& new_name) {
210 __varMap.changeName(
id, new_name);
213 template <
typename GUM_SCALAR >
215 NodeId id,
const std::string& old_label,
const std::string& new_label) {
216 if (variable(
id).varType() != VarType::Labelized) {
226 template <
typename GUM_SCALAR >
228 return __varMap.get(var);
231 template <
typename GUM_SCALAR >
247 template <
typename GUM_SCALAR >
249 unsigned int nbrmod) {
252 "Variable " << name <<
"needs more than " << nbrmod
260 template <
typename GUM_SCALAR >
263 NodeId proposedId = dag().nextNodeId();
266 res = add(var, aContent, proposedId);
271 template <
typename GUM_SCALAR >
278 res = add(var, ptr,
id);
288 template <
typename GUM_SCALAR >
293 __varMap.insert(
id, var);
294 this->_dag.addNodeWithId(
id);
297 (*cpt) << variable(
id);
298 __probaMap.insert(
id, cpt);
302 template <
typename GUM_SCALAR >
307 template <
typename GUM_SCALAR >
313 template <
typename GUM_SCALAR >
316 return *(__probaMap[varId]);
319 template <
typename GUM_SCALAR >
324 template <
typename GUM_SCALAR >
326 erase(__varMap.get(var));
329 template <
typename GUM_SCALAR >
331 if (__varMap.exists(varId)) {
333 const NodeSet& children = this->children(varId);
335 for (
const auto c : children) {
336 __probaMap[c]->erase(variable(varId));
339 delete __probaMap[varId];
341 __probaMap.erase(varId);
342 __varMap.erase(varId);
343 this->_dag.eraseNode(varId);
347 template <
typename GUM_SCALAR >
349 if (this->_dag.existsArc(tail, head)) {
351 "The arc (" << tail <<
"," << head <<
") already exists.")
354 this->_dag.addArc(tail, head);
356 (*(__probaMap[head])) << variable(tail);
359 template <
typename GUM_SCALAR >
361 const std::string& head) {
363 addArc(this->idFromName(tail), this->idFromName(head));
366 "The arc " << tail <<
"->" << head <<
" already exists.")
370 template <
typename GUM_SCALAR >
372 if (__varMap.exists(arc.
tail()) && __varMap.exists(arc.
head())) {
374 this->_dag.eraseArc(arc);
376 (*(__probaMap[head])) >> variable(tail);
380 template <
typename GUM_SCALAR >
382 eraseArc(
Arc(tail, head));
385 template <
typename GUM_SCALAR >
388 if (!__varMap.exists(arc.
tail()) || !__varMap.exists(arc.
head())
389 || !dag().existsArc(arc)) {
412 beginTopologyTransformation();
414 for (
const auto node : this->parents(tail))
416 for (
const auto node : this->parents(head))
422 for (
const auto p : new_parents) {
423 if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
428 new_parents.erase(tail);
430 for (
const auto p : new_parents) {
431 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
434 endTopologyTransformation();
438 del_vars << &(variable(tail));
440 prod.
margSumOut(del_vars).putFirst(&variable(head));
443 cpt_head = std::move(new_cpt_head);
446 (prod / cpt_head).putFirst(&variable(tail))};
448 cpt_tail = std::move(new_cpt_tail);
451 template <
typename GUM_SCALAR >
453 reverseArc(
Arc(tail, head));
460 template <
typename GUM_SCALAR >
465 template <
typename GUM_SCALAR >
472 template <
typename GUM_SCALAR >
478 template <
typename GUM_SCALAR >
486 template <
typename GUM_SCALAR >
494 template <
typename GUM_SCALAR >
499 template <
typename GUM_SCALAR >
504 template <
typename GUM_SCALAR >
509 template <
typename GUM_SCALAR >
520 template <
typename GUM_SCALAR >
522 GUM_SCALAR external_weight) {
523 return addNoisyORCompound(var, external_weight);
526 template <
typename GUM_SCALAR >
532 template <
typename GUM_SCALAR >
534 GUM_SCALAR external_weight) {
538 template <
typename GUM_SCALAR >
540 GUM_SCALAR external_weight) {
544 template <
typename GUM_SCALAR >
546 GUM_SCALAR external_weight) {
550 template <
typename GUM_SCALAR >
552 GUM_SCALAR external_weight,
554 return addNoisyORCompound(var, external_weight,
id);
557 template <
typename GUM_SCALAR >
559 GUM_SCALAR external_weight,
564 template <
typename GUM_SCALAR >
566 GUM_SCALAR external_weight,
571 template <
typename GUM_SCALAR >
578 template <
typename GUM_SCALAR >
580 GUM_SCALAR external_weight,
585 template <
typename GUM_SCALAR >
588 GUM_SCALAR causalWeight) {
596 CImodel->causalWeight(variable(tail), causalWeight);
599 "Head variable (" << variable(head).name()
600 <<
") is not a CIModel variable !");
604 template <
typename GUM_SCALAR >
612 template <
typename GUM_SCALAR >
614 for (
const auto node : nodes())
615 __probaMap[node]->beginMultipleChanges();
619 template <
typename GUM_SCALAR >
621 for (
const auto node : nodes())
622 __probaMap[node]->endMultipleChanges();
626 template <
typename GUM_SCALAR >
629 for (
const auto& elt : __probaMap) {
637 template <
typename GUM_SCALAR >
646 for (
gum::Idx i = 0; i < src.second->nbrDim(); i++) {
647 (*copy_array) << variableFromName(src.second->variable(i).name());
650 copy_array->
copyFrom(*(src.second));
653 __probaMap.insert(src.first, copy_array);
657 template <
typename GUM_SCALAR >
659 for (
const auto node : nodes())
663 template <
typename GUM_SCALAR >
667 generator.
generateCPT(cpt(node).pos(variable(node)), cpt(node));
670 template <
typename GUM_SCALAR >
673 if (cpt(
id).nbrDim() != newPot->
nbrDim()) {
675 "cannot exchange potentials with different " 676 "dimensions for variable with id " 680 for (
Idx i = 0; i < cpt(
id).nbrDim(); i++) {
681 if (&cpt(
id).variable(i) != &(newPot->
variable(i))) {
683 "cannot exchange potentials because, for variable with id " 684 <<
id <<
", dimension " << i <<
" differs. ");
688 _unsafeChangePotential(
id, newPot);
691 template <
typename GUM_SCALAR >
694 delete __probaMap[id];
695 __probaMap[id] = newPot;
698 template <
typename GUM_SCALAR >
701 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)
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size domainSize)
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
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.
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.
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 bn with a dotlike syntax : 'a->b->c;b->d;'.
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)