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();
152 double compressionRatio = log10(1.0 * param) - dSize;
155 s <<
"BN{nodes: " << size() <<
", arcs: " << dag().sizeArcs() <<
", ";
158 s <<
"domainSize: 10^" << dSize;
160 s <<
"domainSize: " << std::round(std::pow(10.0, dSize));
162 s <<
", parameters: " << param <<
", compression ratio: ";
164 if (compressionRatio > -3)
165 s << trunc(100.0 - std::pow(10.0, compressionRatio + 2.0));
167 s <<
"100-10^" << compressionRatio + 2.0;
174 template <
typename GUM_SCALAR >
176 std::stringstream output;
177 output <<
"digraph \"";
182 bn_name = this->property(
"name");
183 }
catch (
NotFound&) { bn_name =
"no_name"; }
185 output << bn_name <<
"\" {" << std::endl;
186 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" 188 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl
191 for (
auto node : nodes())
192 output <<
"\"" << variable(node).name() <<
"\" [comment=\"" << node <<
":" 193 << variable(node).toStringWithDescription() <<
"\"];" << std::endl;
197 std::string tab =
" ";
199 for (
auto node : nodes()) {
200 if (children(node).size() > 0) {
201 for (
auto child : children(node)) {
202 output << tab <<
"\"" << variable(node).name() <<
"\" -> " 203 <<
"\"" << variable(child).name() <<
"\";" << std::endl;
205 }
else if (parents(node).size() == 0) {
206 output << tab <<
"\"" << variable(node).name() <<
"\";" << std::endl;
210 output <<
"}" << std::endl;
218 template <
typename GUM_SCALAR >
221 auto value = (GUM_SCALAR)1.0;
225 for (
auto node : nodes()) {
226 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
237 template <
typename GUM_SCALAR >
240 auto value = (GUM_SCALAR)0.0;
244 for (
auto node : nodes()) {
245 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
246 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
249 value += log2(cpt(node)[i]);
255 template <
typename GUM_SCALAR >
257 if (size() != from.
size()) {
return false; }
259 if (sizeArcs() != from.
sizeArcs()) {
return false; }
264 for (
auto node : nodes()) {
266 alignment.
insert(&variable(node),
274 for (
auto node : nodes()) {
277 if (cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
279 if (cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
287 for (
Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
289 j.chgVal(*(alignment.
second(p)), i.
val(*p));
292 if (std::pow(cpt(node).
get(i) - from.
cpt(fromnode).get(j), (GUM_SCALAR)2)
293 > (GUM_SCALAR)1e-6) {
302 template <
typename GUM_SCALAR >
308 template <
typename GUM_SCALAR >
314 NodeSet& alreadyVisitedDn)
const {
315 if (alreadyVisitedUp.
contains(node))
return;
316 alreadyVisitedUp << node;
321 for (
auto fath : _dag.parents(node))
322 __minimalCondSetVisitUp(
323 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
324 for (
auto chil : _dag.children(node))
325 __minimalCondSetVisitDn(
326 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
331 template <
typename GUM_SCALAR >
337 NodeSet& alreadyVisitedDn)
const {
338 if (alreadyVisitedDn.
contains(node))
return;
339 alreadyVisitedDn << node;
343 for (
auto fath : _dag.parents(node))
344 __minimalCondSetVisitUp(
345 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
347 for (
auto chil : _dag.children(node))
348 __minimalCondSetVisitDn(
349 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
354 template <
typename GUM_SCALAR >
362 alreadyVisitedDn << target;
363 alreadyVisitedUp << target;
365 for (
auto fath : _dag.parents(target))
366 __minimalCondSetVisitUp(
367 fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
368 for (
auto chil : _dag.children(target))
369 __minimalCondSetVisitDn(
370 chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
374 template <
typename GUM_SCALAR >
378 for (
auto node : targets) {
379 res += minimalCondSet(node, soids);
384 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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Virtual base class for PGMs using a DAG.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.