31 template <
typename GUM_SCALAR >
39 template <
typename GUM_SCALAR >
43 for (
auto node :
nodes())
49 template <
typename GUM_SCALAR >
54 template <
typename GUM_SCALAR >
59 template <
typename GUM_SCALAR >
65 template <
typename GUM_SCALAR >
75 template <
typename GUM_SCALAR >
86 template <
typename GUM_SCALAR >
90 "Not implemented yet. please use referent bayesnet method");
93 template <
typename GUM_SCALAR >
98 return __bn.variable(
id);
101 template <
typename GUM_SCALAR >
112 template <
typename GUM_SCALAR >
123 template <
typename GUM_SCALAR >
125 const std::string& name)
const {
131 return __bn.variable(
id);
136 template <
typename GUM_SCALAR >
142 template <
typename GUM_SCALAR >
144 if (!
__bn.dag().existsNode(
id))
151 for (
auto pa : this->
__bn.parents(
id)) {
156 for (
auto son : this->
__bn.children(
id))
161 template <
typename GUM_SCALAR >
166 for (
auto pa : this->
__bn.parents(
id))
170 template <
typename GUM_SCALAR >
178 template <
typename GUM_SCALAR >
184 template <
typename GUM_SCALAR >
190 template <
typename GUM_SCALAR >
199 for (
Idx i = 1; i < pot->nbrDim(); i++) {
200 NodeId parent =
__bn.idFromName(pot->variable(i).name());
211 template <
typename GUM_SCALAR >
214 if (!
dag().existsNode(
id))
219 "The potential is not a marginal for __bn.variable <" 225 for (
Idx i = 1; i < pot->
nbrDim(); i++) {
228 "Variable <" << pot->
variable(i).name()
229 <<
"> is not in the parents of node " << id);
235 template <
typename GUM_SCALAR >
241 template <
typename GUM_SCALAR >
257 template <
typename GUM_SCALAR >
270 "The potential is not a marginal for __bn.variable <" 271 <<
__bn.variable(
id).name() <<
">");
277 template <
typename GUM_SCALAR >
282 const auto&
cpt = this->
cpt(
id);
285 for (
Idx i = 1; i <
cpt.nbrDim(); i++) {
289 return (this->
parents(
id) == cpt_parents);
292 template <
typename GUM_SCALAR >
294 for (
auto node :
nodes())
300 template <
typename GUM_SCALAR >
302 std::stringstream output;
303 output <<
"digraph \"";
307 static std::string inFragmentStyle =
"fillcolor=\"#ffffaa\"," 309 "fontcolor=\"#000000\"";
310 static std::string styleWithLocalCPT =
"fillcolor=\"#ffddaa\"," 312 "fontcolor=\"#000000\"";
313 static std::string notConsistantStyle =
"fillcolor=\"#ff0000\"," 315 "fontcolor=\"#ffff00\"";
316 static std::string outFragmentStyle =
"fillcolor=\"#f0f0f0\"," 318 "fontcolor=\"#000000\"";
321 bn_name =
__bn.property(
"name");
322 }
catch (
NotFound&) { bn_name =
"no_name"; }
324 bn_name =
"Fragment of " + bn_name;
326 output << bn_name <<
"\" {" << std::endl;
327 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" 329 output <<
" node [style=filled];" << std::endl << std::endl;
331 for (
auto node :
__bn.nodes()) {
332 output <<
"\"" <<
__bn.variable(node).name() <<
"\" [comment=\"" << node
333 <<
":" <<
__bn.variable(node) <<
", \"";
337 output << notConsistantStyle;
339 output << styleWithLocalCPT;
341 output << inFragmentStyle;
343 output << outFragmentStyle;
345 output <<
"];" << std::endl;
350 std::string tab =
" ";
352 for (
auto node :
__bn.nodes()) {
353 if (
__bn.children(node).size() > 0) {
354 for (
auto child :
__bn.children(node)) {
355 output << tab <<
"\"" <<
__bn.variable(node).name() <<
"\" -> " 356 <<
"\"" <<
__bn.variable(child).name() <<
"\" [";
358 if (
dag().existsArc(
Arc(node, child)))
359 output << inFragmentStyle;
361 output << outFragmentStyle;
363 output <<
"];" << std::endl;
368 output <<
"}" << std::endl;
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.
virtual NodeId nodeId(const DiscreteVariable &var) const override
Return id node from discrete var pointer.
Abstract Base class for all diGraph Listener.
virtual void whenNodeDeleted(const void *src, NodeId id) noexcept override
the action to take when a node has just been removed from the graph
void uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
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.
virtual const DiscreteVariable & variableFromName(const std::string &name) const override
Getter by name.
void _installCPT(NodeId id, const Potential< GUM_SCALAR > *pot) noexcept
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
BayesNetFragment()=delete
void installCPT(NodeId id, const Potential< GUM_SCALAR > *pot)
install a local cpt for a node into the fragment.
virtual void whenArcAdded(const void *src, NodeId from, NodeId to) noexcept override
the action to take when a new arc is inserted into the graph
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
virtual const VariableNodeMap & variableNodeMap() const override
Returns a constant reference to the VariableNodeMap of this BN.
Container used to map discrete variables with nodes.
void _installArc(NodeId from, NodeId to) noexcept
Class representing Bayesian networks.
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.
gum is the global namespace for all aGrUM entities
virtual ~BayesNetFragment()
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const override
Returns the CPT of a variable.
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
DAG _dag
The DAG of this Directed Graphical Model.
Header of the Potential class.
virtual const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variabe given it's node id.
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
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 void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
virtual void whenArcDeleted(const void *src, NodeId from, NodeId to) noexcept override
the action to take when an arc has just been removed from the graph
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
virtual std::string toDot() const override
creates a dot representing the whole referred BN hilighting the fragment.
void uninstallNode(NodeId id) noexcept
uninstall a node referenced by its nodeId
bool isInstalledNode(NodeId id) const noexcept
check if a certain NodeId exists in the fragment
void installMarginal(NodeId id, const Potential< GUM_SCALAR > *pot)
install a local marginal for a node into the fragment.
bool checkConsistency() const noexcept
returns true if all nodes in the fragment are consistent
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
virtual NodeId idFromName(const std::string &name) const override
Getter by name.
Size Idx
Type for indexes.
virtual void whenNodeAdded(const void *src, NodeId id) noexcept override
the action to take when a new node is inserted into the graph
void _uninstallArc(NodeId from, NodeId to) noexcept
void _uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
const std::string & name() const
returns the name of the variable
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)
Class representing Fragment of Bayesian networks.