57 template <
typename GUM_SCALAR >
61 std::string name = node;
63 std::vector< std::string > labels;
66 auto posBrack = node.find(
'[');
67 if (posBrack != std::string::npos) {
68 if (*(node.rbegin()) !=
']')
71 name = node.substr(0, posBrack);
72 ds =
static_cast< Size >(
73 std::stoi(node.substr(posBrack + 1, node.size() - posBrack - 2)));
78 posBrack = node.find(
'{');
79 if (posBrack != std::string::npos) {
80 if (*(node.rbegin()) !=
'}')
83 name = node.substr(0, posBrack);
84 labels =
split(node.substr(posBrack + 1, node.size() - posBrack - 2),
"|");
85 if (labels.size() < 2) {
91 ds =
static_cast< Size >(labels.size());
99 "Only one value for variable " << name
100 <<
" (2 at least are needed).");
108 if (labels.empty()) {
112 for (
const auto& label : labels) {
122 template <
typename GUM_SCALAR >
129 for (
const auto& chaine :
split(dotlike,
";")) {
131 bool notfirst =
false;
132 for (
const auto& souschaine :
split(chaine,
"->")) {
134 for (
const auto& node :
split(souschaine,
"<-")) {
135 auto idVar =
build_node(bn, node, domainSize);
156 template <
typename GUM_SCALAR >
161 template <
typename GUM_SCALAR >
167 template <
typename GUM_SCALAR >
169 IBayesNet< GUM_SCALAR >(source), __varMap(source.__varMap) {
172 __copyPotentials(source);
175 template <
typename GUM_SCALAR >
178 if (
this != &source) {
183 __copyPotentials(source);
189 template <
typename GUM_SCALAR >
192 for (
const auto p : __probaMap) {
197 template <
typename GUM_SCALAR >
200 return __varMap.get(
id);
203 template <
typename GUM_SCALAR >
206 const std::string& new_name) {
207 __varMap.changeName(
id, new_name);
210 template <
typename GUM_SCALAR >
212 NodeId id,
const std::string& old_label,
const std::string& new_label) {
213 if (variable(
id).varType() != VarType::Labelized) {
223 template <
typename GUM_SCALAR >
225 return __varMap.get(var);
228 template <
typename GUM_SCALAR >
244 template <
typename GUM_SCALAR >
246 unsigned int nbrmod) {
249 "Variable " << name <<
"needs more than " << nbrmod
257 template <
typename GUM_SCALAR >
260 NodeId proposedId = dag().nextNodeId();
263 res = add(var, aContent, proposedId);
268 template <
typename GUM_SCALAR >
275 res = add(var, ptr,
id);
285 template <
typename GUM_SCALAR >
290 __varMap.insert(
id, var);
291 this->_dag.addNodeWithId(
id);
294 (*cpt) << variable(
id);
295 __probaMap.insert(
id, cpt);
299 template <
typename GUM_SCALAR >
304 template <
typename GUM_SCALAR >
310 template <
typename GUM_SCALAR >
313 return *(__probaMap[varId]);
316 template <
typename GUM_SCALAR >
321 template <
typename GUM_SCALAR >
323 erase(__varMap.get(var));
326 template <
typename GUM_SCALAR >
328 if (__varMap.exists(varId)) {
330 const NodeSet& children = this->children(varId);
332 for (
const auto c : children) {
333 __probaMap[c]->erase(variable(varId));
336 delete __probaMap[varId];
338 __probaMap.erase(varId);
339 __varMap.erase(varId);
340 this->_dag.eraseNode(varId);
344 template <
typename GUM_SCALAR >
346 this->_dag.
addArc(tail, head);
348 (*(__probaMap[head])) << variable(tail);
351 template <
typename GUM_SCALAR >
353 if (__varMap.exists(arc.
tail()) && __varMap.exists(arc.
head())) {
355 this->_dag.eraseArc(arc);
357 (*(__probaMap[head])) >> variable(tail);
361 template <
typename GUM_SCALAR >
363 eraseArc(
Arc(tail, head));
366 template <
typename GUM_SCALAR >
369 if (!__varMap.exists(arc.
tail()) || !__varMap.exists(arc.
head())
370 || !dag().existsArc(arc)) {
393 beginTopologyTransformation();
395 for (
const auto node : this->parents(tail))
397 for (
const auto node : this->parents(head))
403 for (
const auto p : new_parents) {
404 if ((p != tail) && !dag().existsArc(p, tail)) { addArc(p, tail); }
409 new_parents.erase(tail);
411 for (
const auto p : new_parents) {
412 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
415 endTopologyTransformation();
419 del_vars << &(variable(tail));
422 cpt_head = std::move(new_cpt_head);
426 cpt_tail = std::move(new_cpt_tail);
429 template <
typename GUM_SCALAR >
431 reverseArc(
Arc(tail, head));
438 template <
typename GUM_SCALAR >
443 template <
typename GUM_SCALAR >
450 template <
typename GUM_SCALAR >
456 template <
typename GUM_SCALAR >
464 template <
typename GUM_SCALAR >
472 template <
typename GUM_SCALAR >
477 template <
typename GUM_SCALAR >
482 template <
typename GUM_SCALAR >
487 template <
typename GUM_SCALAR >
498 template <
typename GUM_SCALAR >
500 GUM_SCALAR external_weight) {
501 return addNoisyORCompound(var, external_weight);
504 template <
typename GUM_SCALAR >
510 template <
typename GUM_SCALAR >
512 GUM_SCALAR external_weight) {
516 template <
typename GUM_SCALAR >
518 GUM_SCALAR external_weight) {
522 template <
typename GUM_SCALAR >
524 GUM_SCALAR external_weight) {
528 template <
typename GUM_SCALAR >
530 GUM_SCALAR external_weight,
532 return addNoisyORCompound(var, external_weight,
id);
535 template <
typename GUM_SCALAR >
537 GUM_SCALAR external_weight,
542 template <
typename GUM_SCALAR >
544 GUM_SCALAR external_weight,
549 template <
typename GUM_SCALAR >
556 template <
typename GUM_SCALAR >
558 GUM_SCALAR external_weight,
563 template <
typename GUM_SCALAR >
566 GUM_SCALAR causalWeight) {
574 CImodel->causalWeight(variable(tail), causalWeight);
577 "Head variable (" << variable(head).name()
578 <<
") is not a CIModel variable !");
582 template <
typename GUM_SCALAR >
590 template <
typename GUM_SCALAR >
592 for (
const auto node : nodes())
593 __probaMap[node]->beginMultipleChanges();
597 template <
typename GUM_SCALAR >
599 for (
const auto node : nodes())
600 __probaMap[node]->endMultipleChanges();
604 template <
typename GUM_SCALAR >
607 for (
const auto& elt : __probaMap) {
615 template <
typename GUM_SCALAR >
624 for (
gum::Idx i = 0; i < src.second->nbrDim(); i++) {
625 (*copy_array) << variableFromName(src.second->variable(i).name());
628 copy_array->
copyFrom(*(src.second));
631 __probaMap.insert(src.first, copy_array);
635 template <
typename GUM_SCALAR >
637 for (
const auto node : nodes())
640 template <
typename GUM_SCALAR >
644 generator.
generateCPT(cpt(node).pos(variable(node)), cpt(node));
647 template <
typename GUM_SCALAR >
650 if (cpt(
id).nbrDim() != newPot->
nbrDim()) {
652 "cannot exchange potentials with different " 653 "dimensions for variable with id " 657 for (
Idx i = 0; i < cpt(
id).nbrDim(); i++) {
658 if (&cpt(
id).variable(i) != &(newPot->
variable(i))) {
660 "cannot exchange potentials because, for variable with id " 661 <<
id <<
", dimension " << i <<
" differs. ");
665 _unsafeChangePotential(
id, newPot);
668 template <
typename GUM_SCALAR >
671 delete __probaMap[id];
672 __probaMap[id] = newPot;
675 template <
typename GUM_SCALAR >
678 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.
Abstract class for generating Conditional Probability Tables.
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)
class for NoisyOR-net implementation as multiDim
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
Container used to map discrete variables with nodes.
BayesNet()
Default constructor.
Class representing Bayesian networks.
class for LOGIT implementation as multiDim
Class representing Bayesian networks.
class for multiDimNoisyORCompound
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
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;'.
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
virtual Size domainSize() const =0
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...
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.
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
std::string toString() const
class for NoisyAND-net implementation as multiDim
<agrum/multidim/multiDimImplementation.h>
Header of gumRangeVariable.
Size Idx
Type for indexes.
Utilities for manipulating strings.
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.
Base class for labelized discrete random variables.
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)