76 template <
typename GUM_SCALAR >
78 const std::string& node,
79 const std::string& default_domain) {
85 }
catch (gum::NotFound
const&) { res = bn.
add(*v); }
89 template <
typename GUM_SCALAR >
92 return fastPrototype(dotlike,
"[" + std::to_string(domainSize) +
"]");
95 template <
typename GUM_SCALAR >
97 const std::string& domain) {
102 bool notfirst =
false;
103 for (
const auto& souschaine:
split(chaine,
"->")) {
105 for (
auto& node:
split(souschaine,
"<-")) {
127 template <
typename GUM_SCALAR >
132 template <
typename GUM_SCALAR >
137 template <
typename GUM_SCALAR >
145 template <
typename GUM_SCALAR >
147 if (
this != &source) {
158 template <
typename GUM_SCALAR >
166 template <
typename GUM_SCALAR >
171 template <
typename GUM_SCALAR >
176 template <
typename GUM_SCALAR >
178 const std::string& old_label,
179 const std::string& new_label) {
189 template <
typename GUM_SCALAR >
194 template <
typename GUM_SCALAR >
198 return add(var, ptr);
205 template <
typename GUM_SCALAR >
207 unsigned int default_nbrmod) {
213 template < typename GUM_SCALAR >
218 return add(var, aContent, proposedId);
221 template <
typename GUM_SCALAR >
226 return add(var, ptr,
id);
233 template <
typename GUM_SCALAR >
238 this->
dag_.addNodeWithId(
id);
240 auto cpt =
new Tensor< GUM_SCALAR >(aContent);
246 template <
typename GUM_SCALAR >
251 template <
typename GUM_SCALAR >
254 return _varMap_.variableFromName(name);
257 template <
typename GUM_SCALAR >
262 template <
typename GUM_SCALAR >
267 template <
typename GUM_SCALAR >
272 template <
typename GUM_SCALAR >
284 this->
dag_.eraseNode(varId);
288 template <
typename GUM_SCALAR >
290 if (!this->
empty()) {
291 auto l = this->
nodes();
292 for (
const auto no: l) {
298 template <
typename GUM_SCALAR >
300 if (this->
dag_.existsArc(tail, head)) {
304 this->
dag_.addArc(tail, head);
309 template <
typename GUM_SCALAR >
318 template <
typename GUM_SCALAR >
323 this->
dag_.eraseArc(arc);
329 template <
typename GUM_SCALAR >
334 template <
typename GUM_SCALAR >
356 Tensor< GUM_SCALAR > prod{cpt(tail) * cpt(head)};
360 beginTopologyTransformation();
362 for (
const auto node: this->parents(tail))
363 new_parents.insert(node);
364 for (
const auto node: this->parents(head))
365 new_parents.insert(node);
370 for (
const auto p: new_parents) {
376 new_parents.erase(tail);
378 for (
const auto p: new_parents) {
379 if ((p != head) && !dag().existsArc(p, head)) { addArc(p, head); }
382 endTopologyTransformation();
387 Tensor< GUM_SCALAR > new_cpt_head = prod.sumOut(del_vars).putFirst(&
variable(head));
389 auto& cpt_head =
const_cast< Tensor< GUM_SCALAR >&
>(
cpt(head));
390 cpt_head = std::move(new_cpt_head);
392 Tensor< GUM_SCALAR > new_cpt_tail{(prod / cpt_head).putFirst(&
variable(tail))};
393 auto& cpt_tail =
const_cast< Tensor< GUM_SCALAR >&
>(cpt(tail));
394 cpt_tail = std::move(new_cpt_tail);
397 template <
typename GUM_SCALAR >
405 template <
typename GUM_SCALAR >
410 template <
typename GUM_SCALAR >
417 template <
typename GUM_SCALAR >
422 template <
typename GUM_SCALAR >
429 template <
typename GUM_SCALAR >
436 template <
typename GUM_SCALAR >
441 template <
typename GUM_SCALAR >
446 template <
typename GUM_SCALAR >
451 template <
typename GUM_SCALAR >
458 template <
typename GUM_SCALAR >
466 template <
typename GUM_SCALAR >
468 GUM_SCALAR external_weight) {
472 template <
typename GUM_SCALAR >
474 GUM_SCALAR external_weight) {
478 template <
typename GUM_SCALAR >
480 GUM_SCALAR external_weight) {
484 template <
typename GUM_SCALAR >
486 GUM_SCALAR external_weight) {
490 template <
typename GUM_SCALAR >
492 GUM_SCALAR external_weight) {
496 template <
typename GUM_SCALAR >
498 GUM_SCALAR external_weight,
503 template <
typename GUM_SCALAR >
505 GUM_SCALAR external_weight,
510 template <
typename GUM_SCALAR >
512 GUM_SCALAR external_weight,
517 template <
typename GUM_SCALAR >
519 GUM_SCALAR external_weight,
524 template <
typename GUM_SCALAR >
526 GUM_SCALAR external_weight,
531 template <
typename GUM_SCALAR >
539 CImodel->causalWeight(
variable(tail), causalWeight);
542 "Head variable (" <<
variable(head).name() <<
") is not a CIModel variable !")
546 template <
typename GUM_SCALAR >
547 INLINE std::ostream&
operator<<(std::ostream& output,
const BayesNet< GUM_SCALAR >& bn) {
548 output << bn.toString();
553 template <
typename GUM_SCALAR >
555 for (
const auto node:
nodes())
560 template <
typename GUM_SCALAR >
562 for (
const auto node:
nodes())
567 template <
typename GUM_SCALAR >
578 template <
typename GUM_SCALAR >
582 for (
const auto& src: source._probaMap_) {
584 auto copy_array =
new Tensor< GUM_SCALAR >();
585 copy_array->beginMultipleChanges();
586 for (
gum::Idx i = 0; i < src.second->nbrDim(); i++) {
589 copy_array->endMultipleChanges();
590 copy_array->copyFrom(*(src.second));
597 template <
typename GUM_SCALAR >
599 for (
const auto node:
nodes())
603 template <
typename GUM_SCALAR >
610 template <
typename GUM_SCALAR >
612 if (
cpt(
id).nbrDim() != newPot->nbrDim()) {
614 "cannot exchange tensors with different "
615 "dimensions for variable with id "
619 for (
Idx i = 0; i <
cpt(
id).nbrDim(); i++) {
622 "cannot exchange tensors because, for variable with id " <<
id <<
", dimension "
623 << i <<
" differs. ")
627 _unsafeChangeTensor_(
id, newPot);
630 template <
typename GUM_SCALAR >
636 template <
typename GUM_SCALAR >
641 template <
typename GUM_SCALAR >
642 BayesNet< GUM_SCALAR >
649 "Cannot have both an observation and an intervention on the same variable")
672 const std::string& nam = interventions.
variable(i).
name();
678 const auto v1 = bn.
idFromName(this->variable(par).name());
685 interventions.
val(i)));
693 cpt_changed.
insert(child);
695 .fillWith(this->
cpt(child).extract(all));
Class representing Bayesian networks.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
The base class for all directed edges.
GUM_NODISCARD NodeId head() const
returns the head of the arc
GUM_NODISCARD NodeId tail() const
returns the tail of the arc
Exception base for argument error.
Class representing a Bayesian network.
NodeId addNoisyORNet(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
NodeId addLogit(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a Logit implementation.
NodeId addAMPLITUDE(const DiscreteVariable &var)
Others aggregators.
NodeId addMEDIAN(const DiscreteVariable &var)
Others aggregators.
void endTopologyTransformation()
terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void changeVariableLabel(NodeId id, const std::string &old_label, const std::string &new_label)
Changes a variable's label in the gum::BayesNet.
void changeVariableName(NodeId id, const std::string &new_name)
Changes a variable's name in the gum::BayesNet.
NodeId addNoisyOR(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
void beginTopologyTransformation()
When inserting/removing arcs, node CPTs change their dimension with a cost in time.
NodeId addFORALL(const DiscreteVariable &var, Idx value=1)
Others aggregators.
NodeId addEXISTS(const DiscreteVariable &var, Idx value=1)
Others aggregators.
void erase(NodeId varId)
Remove a variable from the gum::BayesNet.
NodeId addOR(const DiscreteVariable &var)
Add a variable, it's associate node and an OR implementation.
const DiscreteVariable & variableFromName(const std::string &name) const final
Returns a variable given its name in the gum::BayesNet.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
NodeId addAND(const DiscreteVariable &var)
Add a variable, it's associate node and an AND implementation.
void generateCPTs() const
randomly generates CPTs for a given structure
NodeId addCOUNT(const DiscreteVariable &var, Idx value=1)
Others aggregators.
void clear()
clear the whole Bayes net *
BayesNet()
Default constructor.
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
void changeTensor(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
void addWeightedArc(NodeId tail, NodeId head, GUM_SCALAR causalWeight)
Add an arc in the BN, and update arc.head's CPT.
void reverseArc(NodeId tail, NodeId head)
Reverses an arc while preserving the same joint distribution.
NodeId addMIN(const DiscreteVariable &var)
Others aggregators.
BayesNet< GUM_SCALAR > contextualize(const gum::Instantiation &observations, const gum::Instantiation &interventions) const
create a contextual BN from this and a set of hard observations and hard interventions.
const VariableNodeMap & variableNodeMap() const final
Returns a map between variables and nodes of this gum::BayesNet.
void _copyTensors_(const BayesNet< GUM_SCALAR > &source)
copy of tensors from a BN to another, using names of vars as ref.
static BayesNet< GUM_SCALAR > fastPrototype(const std::string &dotlike, Size domainSize)
Create a Bayesian network with a dot-like syntax which specifies:
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
void eraseArc(const Arc &arc)
Removes an arc in the BN, and update head's CTP.
NodeId addSUM(const DiscreteVariable &var)
Others aggregators.
virtual ~BayesNet()
Destructor.
NodeProperty< Tensor< GUM_SCALAR > * > _probaMap_
Mapping between the variable's id and their CPT.
VariableNodeMap _varMap_
the map between variable and id
NodeId addNoisyAND(const DiscreteVariable &var, GUM_SCALAR external_weight, NodeId id)
Add a variable, its associate node and a noisyAND implementation.
NodeId nodeId(const DiscreteVariable &var) const final
Returns a variable's id in the gum::BayesNet.
NodeId addMAX(const DiscreteVariable &var)
Others aggregators.
void generateCPT(NodeId node) const
randomly generate CPT for a given node in a given structure
void _clearTensors_()
clear all tensors
void _unsafeChangeTensor_(NodeId id, Tensor< GUM_SCALAR > *newPot)
change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.
NodeId idFromName(const std::string &name) const final
Returns a variable's id given its name in the gum::BayesNet.
BayesNet< GUM_SCALAR > & operator=(const BayesNet< GUM_SCALAR > &source)
Copy operator.
NodeId addNoisyORCompound(const DiscreteVariable &var, GUM_SCALAR external_weight)
Add a variable, it's associate node and a gum::noisyOR implementation.
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
DAG dag_
The DAG of this Directed Graphical Model.
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Base class for discrete random variable.
virtual Size domainSize() const =0
Exception : a similar element already exists.
Base class for all aGrUM's exceptions.
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
virtual bool empty() const
Return true if this graphical model is empty.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
IBayesNet()
Default constructor.
Class for assigning/browsing values to tuples of discrete variables.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Idx val(Idx i) const
Returns the current value of the variable at position i.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Exception : there is something wrong with an arc.
Idx posLabel(const std::string &label) const
return the pos from label
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
Multidimensional matrix stored as an array in memory.
abstract class for Conditional Indepency Models
<agrum/base/multidim/multiDimImplementation.h>
Noisy AND representation.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
<agrum/BN/generator/simpleCPTGenerator.h>
Exception : problem with size.
static Tensor< GUM_SCALAR > deterministicTensor(const DiscreteVariable &var, Idx value)
Container used to map discrete variables with nodes.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
std::string remove_newline(const std::string &s)
remove all newlines in a string
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
class for LOGIT implementation as multiDim
class for NoisyAND-net implementation as multiDim
class for multiDimNoisyORCompound
class for NoisyOR-net implementation as multiDim
gum is the global namespace for all aGrUM entities
Set< const DiscreteVariable * > VariableSet
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
std::unique_ptr< DiscreteVariable > fastVariable(std::string var_description, Size default_domain_size)
Create a pointer on a Discrete Variable from a "fast" syntax.
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, const std::string &node, const std::string &default_domain)
Abstract class for generating Conditional Probability Tables.
Utilities for manipulating strings.