34 template <
typename GUM_SCALAR >
42 template <
typename GUM_SCALAR >
46 for (
auto node:
nodes())
52 template <
typename GUM_SCALAR >
57 template <
typename GUM_SCALAR >
62 template <
typename GUM_SCALAR >
68 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
90 template <
typename GUM_SCALAR >
94 "Not implemented yet. please use referent bayesnet method");
97 template <
typename GUM_SCALAR >
103 return __bn.variable(
id);
106 template <
typename GUM_SCALAR >
117 template <
typename GUM_SCALAR >
128 template <
typename GUM_SCALAR >
130 const std::string& name)
const {
136 return __bn.variable(
id);
141 template <
typename GUM_SCALAR >
146 template <
typename GUM_SCALAR >
148 if (!
__bn.dag().existsNode(
id))
155 for (
auto pa: this->
__bn.parents(
id)) {
160 for (
auto son: this->
__bn.children(
id))
165 template <
typename GUM_SCALAR >
170 for (
auto pa: this->
__bn.parents(
id))
174 template <
typename GUM_SCALAR >
182 template <
typename GUM_SCALAR >
188 template <
typename GUM_SCALAR >
193 template <
typename GUM_SCALAR >
214 template <
typename GUM_SCALAR >
217 if (!
dag().existsNode(
id))
222 "The potential is not a marginal for __bn.variable <" 231 "Variable <" << pot.
variable(i).name()
232 <<
"> is not in the parents of node " << id);
238 template <
typename GUM_SCALAR >
244 template <
typename GUM_SCALAR >
260 template <
typename GUM_SCALAR >
273 "The potential is not a marginal for __bn.variable <" 274 <<
__bn.variable(
id).name() <<
">");
280 template <
typename GUM_SCALAR >
285 const auto&
cpt = this->
cpt(
id);
288 for (
Idx i = 1; i <
cpt.nbrDim(); i++) {
292 return (this->
parents(
id) == cpt_parents);
295 template <
typename GUM_SCALAR >
297 for (
auto node:
nodes())
303 template <
typename GUM_SCALAR >
305 std::stringstream output;
306 output <<
"digraph \"";
310 static std::string inFragmentStyle =
"fillcolor=\"#ffffaa\"," 312 "fontcolor=\"#000000\"";
313 static std::string styleWithLocalCPT =
"fillcolor=\"#ffddaa\"," 315 "fontcolor=\"#000000\"";
316 static std::string notConsistantStyle =
"fillcolor=\"#ff0000\"," 318 "fontcolor=\"#ffff00\"";
319 static std::string outFragmentStyle =
"fillcolor=\"#f0f0f0\"," 321 "fontcolor=\"#000000\"";
324 bn_name =
__bn.property(
"name");
325 }
catch (
NotFound&) { bn_name =
"no_name"; }
327 bn_name =
"Fragment of " + bn_name;
329 output << bn_name <<
"\" {" << std::endl;
330 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" 332 output <<
" node [style=filled];" << std::endl << std::endl;
334 for (
auto node:
__bn.nodes()) {
335 output <<
"\"" <<
__bn.variable(node).name() <<
"\" [comment=\"" << node
336 <<
":" <<
__bn.variable(node) <<
", \"";
340 output << notConsistantStyle;
342 output << styleWithLocalCPT;
344 output << inFragmentStyle;
346 output << outFragmentStyle;
348 output <<
"];" << std::endl;
353 std::string tab =
" ";
355 for (
auto node:
__bn.nodes()) {
356 if (
__bn.children(node).size() > 0) {
357 for (
auto child:
__bn.children(node)) {
358 output << tab <<
"\"" <<
__bn.variable(node).name() <<
"\" -> " 359 <<
"\"" <<
__bn.variable(child).name() <<
"\" [";
361 if (
dag().existsArc(
Arc(node, child)))
362 output << inFragmentStyle;
364 output << outFragmentStyle;
366 output <<
"];" << std::endl;
371 output <<
"}" << std::endl;
376 template <
typename GUM_SCALAR >
382 for (
const auto nod:
nodes()) {
385 for (
const auto arc:
dag().
arcs()) {
386 res.
addArc(arc.tail(), arc.head());
388 for (
const auto nod:
nodes()) {
389 res.
cpt(nod).fillWith(
cpt(nod));
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
aGrUM's Potential is a multi-dimensional array with tensor operators.
Class representing a Bayesian Network.
Abstract Base class for all diGraph Listener.
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
gum::BayesNet< GUM_SCALAR > toBN() const
create a brand new BayesNet from a fragment.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
void _installArc(NodeId from, NodeId to)
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable's id and their CPT specific to this Fragment.
void installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
BayesNetFragment()=delete
virtual void whenNodeDeleted(const void *src, NodeId id) final
the action to take when a node has just been removed from the graph
virtual NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
Container used to map discrete variables with nodes.
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it's node id.
Base class for discrete random variable.
void installAscendants(NodeId id)
install a node and all its ascendants
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual ~BayesNetFragment()
virtual void whenNodeAdded(const void *src, NodeId id) final
the action to take when a new node is inserted into the graph
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
DAG _dag
The DAG of this Directed Graphical Model.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void installMarginal(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
virtual const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this BN.
void _installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
virtual void whenArcAdded(const void *src, NodeId from, NodeId to) final
the action to take when a new arc is inserted into the graph
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
virtual const DiscreteVariable & variableFromName(const std::string &name) const final
Getter by name.
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
Portion of a BN identified by the list of nodes and a BayesNet.
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
void installNode(NodeId id)
install a node referenced by its nodeId
void _uninstallCPT(NodeId id)
uninstall a local CPT.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
Size Idx
Type for indexes.
void uninstallCPT(NodeId id)
uninstall a local CPT.
const std::string & name() const
returns the name of the variable
virtual void whenArcDeleted(const void *src, NodeId from, NodeId to) final
the action to take when an arc has just been removed from the graph
virtual void eraseNode(const NodeId id)
remove a node and its adjacent arcs from the graph
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Size NodeId
Type for node ids.
void insert(const Key &k)
Inserts a new element into the set.
#define GUM_ERROR(type, msg)
virtual std::string toDot() const final
creates a dot representing the whole referred BN hilighting the fragment.
void _uninstallArc(NodeId from, NodeId to)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.