56 template < GUM_Numeric GUM_SCALAR >
62 template < GUM_Numeric GUM_SCALAR >
66 for (
auto node:
nodes())
72 template < GUM_Numeric GUM_SCALAR >
77 template < GUM_Numeric GUM_SCALAR >
82 template < GUM_Numeric GUM_SCALAR >
87 template < GUM_Numeric GUM_SCALAR >
95 template < GUM_Numeric GUM_SCALAR >
100 else return _bn_.cpt(
id);
103 template < GUM_Numeric GUM_SCALAR >
105 return this->
_bn_.variableNodeMap();
108 template < GUM_Numeric GUM_SCALAR >
112 return _bn_.variable(
id);
115 template < GUM_Numeric GUM_SCALAR >
124 template < GUM_Numeric GUM_SCALAR >
133 template < GUM_Numeric GUM_SCALAR >
140 return _bn_.variable(
id);
145 template < GUM_Numeric GUM_SCALAR >
150 template < GUM_Numeric GUM_SCALAR >
152 if (!
_bn_.internalDag().existsNode(
id))
156 this->
dag_.addNodeWithId(
id);
159 for (
auto pa: this->
_bn_.parents(
id)) {
164 for (
auto son: this->
_bn_.children(
id))
169 template < GUM_Numeric GUM_SCALAR >
174 for (
auto pa: this->
_bn_.parents(
id))
178 template < GUM_Numeric GUM_SCALAR >
182 this->
dag_.eraseNode(
id);
186 template < GUM_Numeric GUM_SCALAR >
188 this->
dag_.eraseArc(
Arc(from, to));
191 template < GUM_Numeric GUM_SCALAR >
193 this->
dag_.addArc(from, to);
196 template < GUM_Numeric GUM_SCALAR >
200 for (
auto node_it =
parents.beginSafe(); node_it !=
parents.endSafe();
204 for (
Idx i = 1; i < pot.nbrDim(); i++) {
205 NodeId parent =
_bn_.idFromName(pot.variable(i).name());
216 template < GUM_Numeric GUM_SCALAR >
221 if (&(pot.variable(0)) != &(
variable(
id))) {
223 "The tensor is not a marginal for _bn_.variable <" <<
variable(
id).name() <<
">")
228 for (
Idx i = 1; i < pot.nbrDim(); i++) {
231 "Variable <" << pot.variable(i).name() <<
"> is not in the parents of node "
238 template < GUM_Numeric GUM_SCALAR >
244 template < GUM_Numeric GUM_SCALAR >
250 const Tensor< GUM_SCALAR >& pot =
cpt(
id);
252 for (
Idx i = 1; i < pot.nbrDim(); i++) {
253 NodeId parent =
_bn_.idFromName(pot.variable(i).name());
260 template < GUM_Numeric GUM_SCALAR >
266 if (pot.nbrDim() > 1) {
270 if (&(pot.variable(0)) != &(
_bn_.variable(
id))) {
272 "The tensor is not a marginal for _bn_.variable <" <<
_bn_.variable(
id).name()
279 template < GUM_Numeric GUM_SCALAR >
284 const auto& node_cpt = this->
cpt(
id);
287 for (
Idx i = 1; i < node_cpt.nbrDim(); i++) {
288 cpt_parents.
insert(
_bn_.idFromName(node_cpt.variable(i).name()));
291 return (this->
parents(
id) == cpt_parents);
294 template < GUM_Numeric GUM_SCALAR >
296 for (
auto node:
nodes())
302 template < GUM_Numeric GUM_SCALAR >
311 static constexpr NodeColors inFragment{
"#ffffaa",
"#000000",
"#000000"};
312 static constexpr NodeColors withLocalCPT{
"#ffddaa",
"#000000",
"#000000"};
313 static constexpr NodeColors notConsistent{
"#ff0000",
"#000000",
"#ffff00"};
314 static constexpr NodeColors outFragment{
"#f0f0f0",
"#f0f0f0",
"#000000"};
317 const auto title =
"Fragment of " +
_bn_.propertyWithDefault(
"name",
"no_name");
321 for (
const auto node:
_bn_.nodes()) {
322 const NodeColors* colors = &outFragment;
325 else if (
_localCPTs_.exists(node)) colors = &withLocalCPT;
326 else colors = &inFragment;
329 const auto& name =
_bn_.variable(node).name();
333 "(" + std::to_string(node) +
") " + name);
337 writer.
node(name, attributes);
340 for (
const auto node:
_bn_.nodes()) {
341 for (
const auto child:
_bn_.children(node)) {
345 std::to_string(node) +
" → "
346 + std::to_string(child));
352 writer.
edge(
_bn_.variable(node).name(),
_bn_.variable(child).name(), attributes);
359 template < GUM_Numeric GUM_SCALAR >
365 for (
const auto nod:
nodes()) {
369 res.
addArc(arc.tail(), arc.head());
371 for (
const auto nod:
nodes()) {
372 res.
cpt(nod).fillWith(
cpt(nod));
378 template < GUM_Numeric GUM_SCALAR >
383 template < GUM_Numeric GUM_SCALAR >
388 template < GUM_Numeric GUM_SCALAR >
393 template < GUM_Numeric GUM_SCALAR >
398 template < GUM_Numeric GUM_SCALAR >
403 template < GUM_Numeric GUM_SCALAR >
408 template < GUM_Numeric GUM_SCALAR >
410 const Tensor< GUM_SCALAR >& pot) {
414 template < GUM_Numeric GUM_SCALAR >
416 const Tensor< GUM_SCALAR >& pot) {
420 template < GUM_Numeric GUM_SCALAR >
425 template < GUM_Numeric GUM_SCALAR >
Class representing Fragment of Bayesian networks.
Class representing Bayesian networks.
Arc(NodeId tail, NodeId head)
basic constructor. Creates tail -> head.
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
~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.
virtual std::string toDot() const
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'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
The options given to a toDot(const DotOptions&).
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
#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 ...
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
gum is the global namespace for all aGrUM entities
static DotStyle read()
the style of the graphs of the section notebook
Header of the Tensor class.