55 template < GUM_Numeric GUM_SCALAR >
61 template < GUM_Numeric GUM_SCALAR >
65 for (
auto node:
nodes())
71 template < GUM_Numeric GUM_SCALAR >
76 template < GUM_Numeric GUM_SCALAR >
81 template < GUM_Numeric GUM_SCALAR >
86 template < GUM_Numeric GUM_SCALAR >
94 template < GUM_Numeric GUM_SCALAR >
99 else return _bn_.cpt(
id);
102 template < GUM_Numeric GUM_SCALAR >
104 return this->
_bn_.variableNodeMap();
107 template < GUM_Numeric GUM_SCALAR >
111 return _bn_.variable(
id);
114 template < GUM_Numeric GUM_SCALAR >
123 template < GUM_Numeric GUM_SCALAR >
132 template < GUM_Numeric GUM_SCALAR >
139 return _bn_.variable(
id);
144 template < GUM_Numeric GUM_SCALAR >
149 template < GUM_Numeric GUM_SCALAR >
151 if (!
_bn_.internalDag().existsNode(
id))
155 this->
dag_.addNodeWithId(
id);
158 for (
auto pa: this->
_bn_.parents(
id)) {
163 for (
auto son: this->
_bn_.children(
id))
168 template < GUM_Numeric GUM_SCALAR >
173 for (
auto pa: this->
_bn_.parents(
id))
177 template < GUM_Numeric GUM_SCALAR >
181 this->
dag_.eraseNode(
id);
185 template < GUM_Numeric GUM_SCALAR >
187 this->
dag_.eraseArc(
Arc(from, to));
190 template < GUM_Numeric GUM_SCALAR >
192 this->
dag_.addArc(from, to);
195 template < GUM_Numeric GUM_SCALAR >
199 for (
auto node_it =
parents.beginSafe(); node_it !=
parents.endSafe();
203 for (
Idx i = 1; i < pot.nbrDim(); i++) {
204 NodeId parent =
_bn_.idFromName(pot.variable(i).name());
215 template < GUM_Numeric GUM_SCALAR >
220 if (&(pot.variable(0)) != &(
variable(
id))) {
222 "The tensor is not a marginal for _bn_.variable <" <<
variable(
id).name() <<
">")
227 for (
Idx i = 1; i < pot.nbrDim(); i++) {
228 if (!
parents.contains(
_bn_.idFromName(pot.variable(i).name())))
230 "Variable <" << pot.variable(i).name() <<
"> is not in the parents of node "
237 template < GUM_Numeric GUM_SCALAR >
243 template < GUM_Numeric GUM_SCALAR >
249 const Tensor< GUM_SCALAR >& pot =
cpt(
id);
251 for (
Idx i = 1; i < pot.nbrDim(); i++) {
252 NodeId parent =
_bn_.idFromName(pot.variable(i).name());
259 template < GUM_Numeric GUM_SCALAR >
265 if (pot.nbrDim() > 1) {
269 if (&(pot.variable(0)) != &(
_bn_.variable(
id))) {
271 "The tensor is not a marginal for _bn_.variable <" <<
_bn_.variable(
id).name()
278 template < GUM_Numeric GUM_SCALAR >
283 const auto& node_cpt = this->
cpt(
id);
286 for (
Idx i = 1; i < node_cpt.nbrDim(); i++) {
287 cpt_parents.
insert(
_bn_.idFromName(node_cpt.variable(i).name()));
290 return (this->
parents(
id) == cpt_parents);
293 template < GUM_Numeric GUM_SCALAR >
295 for (
auto node:
nodes())
301 template < GUM_Numeric GUM_SCALAR >
303 std::stringstream output;
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\"";
320 bn_name =
_bn_.propertyWithDefault(
"name",
"no_name");
322 bn_name =
"Fragment of " + bn_name;
324 output << std::format(
"digraph \"{}\" {{\n", bn_name);
325 output << std::format(
" graph [bgcolor=transparent,label=\"{}\"];\n", bn_name);
326 output <<
" node [style=filled];" << std::endl << std::endl;
328 for (
auto node:
_bn_.nodes()) {
329 output <<
"\"" <<
_bn_.variable(node).name() <<
"\" [comment=\"" << node <<
":"
330 <<
_bn_.variable(node) <<
", \"";
334 output << notConsistantStyle;
335 }
else if (
_localCPTs_.exists(node)) output << styleWithLocalCPT;
336 else output << inFragmentStyle;
337 }
else output << outFragmentStyle;
339 output <<
"];" << std::endl;
344 std::string tab =
" ";
346 for (
auto node:
_bn_.nodes()) {
347 if (
_bn_.children(node).size() > 0) {
348 for (
auto child:
_bn_.children(node)) {
349 output << std::format(
" \"{}\" -> \"{}\" [",
350 _bn_.variable(node).name(),
351 _bn_.variable(child).name());
354 else output << outFragmentStyle;
356 output <<
"];" << std::endl;
361 output <<
"}" << std::endl;
366 template < GUM_Numeric GUM_SCALAR >
372 for (
const auto nod:
nodes()) {
376 res.
addArc(arc.tail(), arc.head());
378 for (
const auto nod:
nodes()) {
379 res.
cpt(nod).fillWith(
cpt(nod));
385 template < GUM_Numeric GUM_SCALAR >
390 template < GUM_Numeric GUM_SCALAR >
395 template < GUM_Numeric GUM_SCALAR >
400 template < GUM_Numeric GUM_SCALAR >
405 template < GUM_Numeric GUM_SCALAR >
410 template < GUM_Numeric GUM_SCALAR >
415 template < GUM_Numeric GUM_SCALAR >
417 const Tensor< GUM_SCALAR >& pot) {
421 template < GUM_Numeric GUM_SCALAR >
423 const Tensor< GUM_SCALAR >& pot) {
427 template < GUM_Numeric GUM_SCALAR >
432 template < GUM_Numeric GUM_SCALAR >
Class representing Fragment of Bayesian networks.
Class representing Bayesian networks.
The base class for all directed edges.
void installNode(NodeId id)
install a node referenced by its nodeId
void installMarginal(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
const DiscreteVariable & variableFromName(std::string_view name) const final
Getter by name.
void uninstallArc_(NodeId from, NodeId to)
void whenNodeDeleted(const void *src, NodeId id) final
the action to take when a node has just been removed from the graph
void installCPT_(NodeId id, const Tensor< GUM_SCALAR > &pot)
const IBayesNet< GUM_SCALAR > & _bn_
The referred BayesNet.
gum::BayesNet< GUM_SCALAR > toBN() const
create a brand new BayesNet from a fragment.
void installArc_(NodeId from, NodeId to)
void whenNodeAdded(const void *src, NodeId id) final
the action to take when a new node is inserted into the graph
void whenArcDeleted(const void *src, NodeId from, NodeId to) final
the action to take when an arc has just been removed from the graph
NodeId idFromName(std::string_view name) const final
Getter by name.
const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it's node id.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
void whenArcAdded(const void *src, NodeId from, NodeId to) final
the action to take when a new arc is inserted into the graph
NodeProperty< const Tensor< GUM_SCALAR > * > _localCPTs_
Mapping between the variable's id and their CPT specific to this Fragment.
bool checkConsistency(NodeId id) const
returns true if the nodeId's (local or not) cpt is consistent with its parents in the fragment
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
std::string toDot() const final
creates a dot representing the whole referred BN hilighting the fragment.
~BayesNetFragment() override
void installAscendants(NodeId id)
install a node and all its ascendants
NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
void uninstallCPT(NodeId id)
uninstall a local CPT.
const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this BN.
BayesNetFragment()=delete
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void uninstallCPT_(NodeId id)
uninstall a local CPT.
void installCPT(NodeId id, const Tensor< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
Class representing a Bayesian network.
const Tensor< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void addArc(NodeId tail, NodeId head)
Add an arc in the BN, and update arc.head's CPT.
NodeId add(const DiscreteVariable &var)
Add a variable to the gum::BayesNet.
DAG dag_
The DAG of this Directed Graphical Model.
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const final
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
const DAG & internalDag() const
Returns a const reference to the internal (unnamed) DAG. O(1), no copy. Use for stable references or ...
DiGraphListener(const DiGraph *g)
default constructor
Base class for discrete random variable.
IBayesNet()
Default constructor.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
void insert(const Key &k)
Inserts a new element into the set.
aGrUM's Tensor is a multi-dimensional array with tensor operators.
Container used to map discrete variables with nodes.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Header of the Tensor class.