45 template <
typename GUM_SCALAR >
50 template <
typename GUM_SCALAR >
53 this->setProperty(
"name", name);
56 template <
typename GUM_SCALAR >
62 template <
typename GUM_SCALAR >
65 if (
this != &source) { DAGmodel::operator=(source); }
70 template <
typename GUM_SCALAR >
75 template <
typename GUM_SCALAR >
79 for (
auto node : nodes()) {
82 for (
auto parent : parents(node))
83 q *= variable(parent).domainSize();
85 dim += (variable(node).domainSize() - 1) * q;
91 template <
typename GUM_SCALAR >
94 for (
auto node : nodes()) {
95 auto v = variable(node).domainSize();
96 if (v > res) { res = v; }
101 template <
typename GUM_SCALAR >
103 GUM_SCALAR res = 1.0;
104 for (
auto node : nodes()) {
105 auto v = cpt(node).min();
106 if (v < res) { res = v; }
111 template <
typename GUM_SCALAR >
113 GUM_SCALAR res = 1.0;
114 for (
auto node : nodes()) {
115 auto v = cpt(node).max();
116 if (v > res) { res = v; }
121 template <
typename GUM_SCALAR >
123 GUM_SCALAR res = 1.0;
124 for (
auto node : nodes()) {
125 auto v = cpt(node).minNonZero();
126 if (v < res) { res = v; }
131 template <
typename GUM_SCALAR >
133 GUM_SCALAR res = 0.0;
134 for (
auto node : nodes()) {
135 auto v = cpt(node).maxNonOne();
136 if (v > res) { res = v; }
141 template <
typename GUM_SCALAR >
144 double dSize = log10DomainSize();
146 for (
auto node : nodes())
147 param += cpt(node).content()->realSize();
149 double compressionRatio = log10(1.0 * param) - dSize;
152 s <<
"BN{nodes: " << size() <<
", arcs: " << dag().sizeArcs() <<
", ";
155 s <<
"domainSize: 10^" << dSize;
157 s <<
"domainSize: " << std::round(std::pow(10.0, dSize));
159 s <<
", parameters: " << param <<
", compression ratio: ";
161 if (compressionRatio > -3)
162 s << trunc(100.0 - std::pow(10.0, compressionRatio + 2.0));
164 s <<
"100-10^" << compressionRatio + 2.0;
171 template <
typename GUM_SCALAR >
173 std::stringstream output;
174 output <<
"digraph \"";
179 bn_name = this->property(
"name");
180 }
catch (
NotFound&) { bn_name =
"no_name"; }
182 output << bn_name <<
"\" {" << std::endl;
183 output <<
" graph [bgcolor=transparent,label=\"" << bn_name <<
"\"];" 185 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl
188 for (
auto node : nodes())
189 output <<
"\"" << variable(node).name() <<
"\" [comment=\"" << node <<
":" 190 << variable(node).toStringWithDescription() <<
"\"];" << std::endl;
194 std::string tab =
" ";
196 for (
auto node : nodes()) {
197 if (children(node).size() > 0) {
198 for (
auto child : children(node)) {
199 output << tab <<
"\"" << variable(node).name() <<
"\" -> " 200 <<
"\"" << variable(child).name() <<
"\";" << std::endl;
202 }
else if (parents(node).size() == 0) {
203 output << tab <<
"\"" << variable(node).name() <<
"\";" << std::endl;
207 output <<
"}" << std::endl;
215 template <
typename GUM_SCALAR >
218 auto value = (GUM_SCALAR)1.0;
222 for (
auto node : nodes()) {
223 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
return (GUM_SCALAR)0; }
234 template <
typename GUM_SCALAR >
237 auto value = (GUM_SCALAR)0.0;
241 for (
auto node : nodes()) {
242 if ((tmp = cpt(node)[i]) == (GUM_SCALAR)0) {
243 return (GUM_SCALAR)(-std::numeric_limits< double >::infinity());
246 value += log2(cpt(node)[i]);
252 template <
typename GUM_SCALAR >
254 if (size() != from.
size()) {
return false; }
256 if (sizeArcs() != from.
sizeArcs()) {
return false; }
261 for (
auto node : nodes()) {
263 alignment.
insert(&variable(node),
271 for (
auto node : nodes()) {
274 if (cpt(node).nbrDim() != from.
cpt(fromnode).nbrDim()) {
return false; }
276 if (cpt(node).domainSize() != from.
cpt(fromnode).domainSize()) {
284 for (
Idx indice = 0; indice < cpt(node).nbrDim(); ++indice) {
286 j.chgVal(*(alignment.
second(p)), i.
val(*p));
289 if (std::pow(cpt(node).
get(i) - from.
cpt(fromnode).get(j), (GUM_SCALAR)2)
290 > (GUM_SCALAR)1e-6) {
299 template <
typename GUM_SCALAR >
305 template <
typename GUM_SCALAR >
311 NodeSet& alreadyVisitedDn)
const {
312 if (alreadyVisitedUp.
contains(node))
return;
313 alreadyVisitedUp << node;
318 for (
auto fath : _dag.parents(node))
319 __minimalCondSetVisitUp(
320 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
321 for (
auto chil : _dag.children(node))
322 __minimalCondSetVisitDn(
323 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
328 template <
typename GUM_SCALAR >
334 NodeSet& alreadyVisitedDn)
const {
335 if (alreadyVisitedDn.
contains(node))
return;
336 alreadyVisitedDn << node;
340 for (
auto fath : _dag.parents(node))
341 __minimalCondSetVisitUp(
342 fath, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
344 for (
auto chil : _dag.children(node))
345 __minimalCondSetVisitDn(
346 chil, soids, minimal, alreadyVisitedUp, alreadyVisitedDn);
351 template <
typename GUM_SCALAR >
359 alreadyVisitedDn << target;
360 alreadyVisitedUp << target;
362 for (
auto fath : _dag.parents(target))
363 __minimalCondSetVisitUp(
364 fath, soids, res, alreadyVisitedUp, alreadyVisitedDn);
365 for (
auto chil : _dag.children(target))
366 __minimalCondSetVisitDn(
367 chil, soids, res, alreadyVisitedUp, alreadyVisitedDn);
371 template <
typename GUM_SCALAR >
375 for (
auto node : targets) {
376 res += minimalCondSet(node, soids);
381 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.
Abstract class for generating Conditional Probability Tables.
Virtual base class for PGMs using a DAG.
class for NoisyOR-net implementation as multiDim
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.
Class representing Bayesian networks.
class for multiDimNoisyORCompound
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
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.
Header of the Potential class.
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.
class for NoisyAND-net implementation as multiDim
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.