48 template <
typename GUM_SCALAR >
53 template <
typename GUM_SCALAR >
56 this->setProperty(
"name", name);
59 template <
typename GUM_SCALAR >
65 template <
typename GUM_SCALAR >
68 if (
this != &source) { DAGmodel::operator=(source); }
73 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
82 for (
auto node: nodes()) {
85 for (
auto parent: parents(node))
86 q *= variable(parent).domainSize();
88 dim += (variable(node).domainSize() - 1) * q;
94 template <
typename GUM_SCALAR >
97 for (
auto node: nodes()) {
98 auto v = variable(node).domainSize();
99 if (v > res) { res = v; }
104 template <
typename GUM_SCALAR >
106 GUM_SCALAR res = 1.0;
107 for (
auto node: nodes()) {
108 auto v = cpt(node).min();
109 if (v < res) { res = v; }
114 template <
typename GUM_SCALAR >
116 GUM_SCALAR res = 1.0;
117 for (
auto node: nodes()) {
118 auto v = cpt(node).max();
119 if (v > res) { res = v; }
124 template <
typename GUM_SCALAR >
126 GUM_SCALAR res = 1.0;
127 for (
auto node: nodes()) {
128 auto v = cpt(node).minNonZero();
129 if (v < res) { res = v; }
134 template <
typename GUM_SCALAR >
136 GUM_SCALAR res = 0.0;
137 for (
auto node: nodes()) {
138 auto v = cpt(node).maxNonOne();
139 if (v > res) { res = v; }
144 template <
typename GUM_SCALAR >
147 double dSize = log10DomainSize();
149 for (
auto node: nodes())
150 param += cpt(node).content()->realSize();
153 s <<
"BN{nodes: " << size() <<
", arcs: " << dag().sizeArcs() <<
", ";
156 s <<
"domainSize: 10^" << dSize;
158 s <<
"domainSize: " << std::round(std::pow(10.0, dSize));
160 s <<
", dim: " << param <<
"}";
165 template <
typename GUM_SCALAR >
167 std::stringstream output;
168 output <<
"digraph \"";
173 bn_name = this->property(
"name");
174 }
catch (
NotFound&) { bn_name =
"no_name"; }
176 output << bn_name <<
"\" {" << std::endl;
177 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" 179 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl
182 for (
auto node: nodes())
183 output <<
"\"" << variable(node).name() <<
"\" [comment=\"" << node <<
":" 184 << variable(node).toStringWithDescription() <<
"\"];" << std::endl;
188 std::string tab =
" ";
190 for (
auto node: nodes()) {
191 if (children(node).size() > 0) {
192 for (
auto child: children(node)) {
193 output << tab <<
"\"" << variable(node).name() <<
"\" -> " 194 <<
"\"" << variable(child).name() <<
"\";" << std::endl;
196 }
else if (parents(node).size() == 0) {
197 output << tab <<
"\"" << variable(node).name() <<
"\";" << std::endl;
201 output <<
"}" << std::endl;
209 template <
typename GUM_SCALAR >
212 auto value = (GUM_SCALAR)1.0;
216 for (
auto node: nodes()) {
217 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
228 template <
typename GUM_SCALAR >
231 auto value = (GUM_SCALAR)0.0;
235 for (
auto node: nodes()) {
236 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
237 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
240 value += log2(cpt(node)[i]);
246 template <
typename GUM_SCALAR >
248 if (size() != from.
size()) {
return false; }
250 if (sizeArcs() != from.
sizeArcs()) {
return false; }
255 for (
auto node: nodes()) {
257 alignment.
insert(&variable(node),
265 for (
auto node: nodes()) {
268 if (cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
270 if (cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
278 for (
Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
280 j.chgVal(*(alignment.
second(p)), i.
val(*p));
283 if (std::pow(cpt(node).
get(i) - from.
cpt(fromnode).get(j), (GUM_SCALAR)2)
284 > (GUM_SCALAR)1e-6) {
293 template <
typename GUM_SCALAR >
299 template <
typename GUM_SCALAR >
305 NodeSet& alreadyVisitedDn)
const {
306 if (alreadyVisitedUp.
contains(node))
return;
307 alreadyVisitedUp << node;
312 for (
auto fath: _dag.parents(node))
313 __minimalCondSetVisitUp(
314 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
315 for (
auto chil: _dag.children(node))
316 __minimalCondSetVisitDn(
317 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
322 template <
typename GUM_SCALAR >
328 NodeSet& alreadyVisitedDn)
const {
329 if (alreadyVisitedDn.
contains(node))
return;
330 alreadyVisitedDn << node;
334 for (
auto fath: _dag.parents(node))
335 __minimalCondSetVisitUp(
336 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
338 for (
auto chil: _dag.children(node))
339 __minimalCondSetVisitDn(
340 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
345 template <
typename GUM_SCALAR >
353 alreadyVisitedDn << target;
354 alreadyVisitedUp << target;
356 for (
auto fath: _dag.parents(target))
357 __minimalCondSetVisitUp(
358 fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
359 for (
auto chil: _dag.children(target))
360 __minimalCondSetVisitDn(
361 chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
365 template <
typename GUM_SCALAR >
369 for (
auto node: targets) {
370 res += minimalCondSet(node, soids);
375 template <
typename GUM_SCALAR >
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Virtual base class for PGMs using a DAG.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void inc()
Operator increment.
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
bool operator==(const TiXmlString &a, const TiXmlString &b)
Set of pairs of elements with fast search for both elements.
Class for assigning/browsing values to tuples of discrete variables.
std::string toString() const
INLINE std::ostream & operator<<(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn)
Prints map's DAG in output using the Graphviz-dot format.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
bool operator!=(const TiXmlString &a, const TiXmlString &b)
void setFirst()
Assign the first values to the tuple of the Instantiation.
IBayesNet()
Default constructor.
Size Idx
Type for indexes.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Size NodeId
Type for node ids.
bool end() const
Returns true if the Instantiation reached the end.