aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
IMarkovNet_tpl.h
Go to the documentation of this file.
1 
30 #include <limits>
31 
32 #include <agrum/MN/IMarkovNet.h>
34 
35 namespace gum {
36 
37  // IMarkovNet
38 
39  template < typename GUM_SCALAR >
41  GUM_CONSTRUCTOR(IMarkovNet);
42  }
43 
44  template < typename GUM_SCALAR >
45  INLINE IMarkovNet< GUM_SCALAR >::IMarkovNet(std::string name) : UGmodel() {
46  GUM_CONSTRUCTOR(IMarkovNet);
47  this->setProperty("name", name);
48  }
49 
50  template < typename GUM_SCALAR >
52  UGmodel(source) {
53  GUM_CONS_CPY(IMarkovNet);
54  }
55 
56  template < typename GUM_SCALAR >
59  if (this != &source) { UGmodel::operator=(source); }
60 
61  return *this;
62  }
63 
64  template < typename GUM_SCALAR >
66  GUM_DESTRUCTOR(IMarkovNet);
67  }
68 
69  template < typename GUM_SCALAR >
71  Size res = 0;
72  for (auto f: factors()) {
73  res += f.second->domainSize();
74  }
75  return res;
76  }
77 
78  template < typename GUM_SCALAR >
80  /*
81  Size res = 0;
82  for (auto node: nodes()) {
83  auto v = variable(node).domainSize();
84  if (v > res) { res = v; }
85  }
86  return res;*/
87 
88  GUM_ERROR(FatalError, "Not Implemented Yet")
89  }
90 
91  template < typename GUM_SCALAR >
93  /*
94  GUM_SCALAR res = 1.0;
95  for (auto node: nodes()) {
96  auto v = factor(node).min();
97  if (v < res) { res = v; }
98  }
99  return res;*/
100 
101  GUM_ERROR(FatalError, "Not Implemented Yet")
102  }
103 
104  template < typename GUM_SCALAR >
106  /*
107  GUM_SCALAR res = 1.0;
108  for (auto node: nodes()) {
109  auto v = factor(node).max();
110  if (v > res) { res = v; }
111  }
112  return res;*/
113 
114  GUM_ERROR(FatalError, "Not Implemented Yet")
115  }
116 
117  template < typename GUM_SCALAR >
119  /*
120  GUM_SCALAR res = 1.0;
121  for (auto node: nodes()) {
122  auto v = factor(node).minNonZero();
123  if (v < res) { res = v; }
124  }
125  return res;
126  */
127 
128  GUM_ERROR(FatalError, "Not Implemented Yet")
129  }
130 
131  template < typename GUM_SCALAR >
133  /*
134  GUM_SCALAR res = 0.0;
135  for (auto node: nodes()) {
136  auto v = factor(node).maxNonOne();
137  if (v > res) { res = v; }
138  }
139  return res;
140  */
141  GUM_ERROR(FatalError, "Not Implemented Yet")
142  }
143 
144  template < typename GUM_SCALAR >
145  INLINE std::string IMarkovNet< GUM_SCALAR >::toString() const {
146  Size param = 0;
147  double dSize = log10DomainSize();
148 
149  for (auto factor: factors())
150  param += factor.second->content()->realSize();
151 
152  std::stringstream s;
153  s << "MN{nodes: " << size() << ", edges: " << graph().sizeEdges() << ", ";
154 
155  if (dSize > 6)
156  s << "domainSize: 10^" << dSize;
157  else
158  s << "domainSize: " << std::round(std::pow(10.0, dSize));
159 
160  s << ", dim: " << param << "}";
161 
162  return s.str();
163  }
164 
165  template < typename GUM_SCALAR >
166  std::string IMarkovNet< GUM_SCALAR >::toDot() const {
167  return "Not Implemented Yet"; /*
168  std::stringstream output;
169  output << "digraph \"";
170 
171  std::string bn_name;
172 
173  try {
174  bn_name = this->property("name");
175  } catch (NotFound&) { bn_name = "no_name"; }
176 
177  output << bn_name << "\" {" << std::endl;
178  output << " graph [bgcolor=transparent,label=\"" << bn_name << "\"];"
179  << std::endl;
180  output << " node [style=filled fillcolor=\"#ffffaa\"];" << std::endl
181  << std::endl;
182 
183  for (auto node: nodes())
184  output << "\"" << variable(node).name() << "\" [comment=\"" << node << ":"
185  << variable(node).toStringWithDescription() << "\"];" << std::endl;
186 
187  output << std::endl;
188 
189  std::string tab = " ";
190 
191  for (auto node: nodes()) {
192  if (children(node).size() > 0) {
193  for (auto child: children(node)) {
194  output << tab << "\"" << variable(node).name() << "\" -> "
195  << "\"" << variable(child).name() << "\";" << std::endl;
196  }
197  } else if (parents(node).size() == 0) {
198  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
199  }
200  }
201 
202  output << "}" << std::endl;
203 
204  return output.str();*/
205  }
206 
207  template < typename GUM_SCALAR >
209  return "Not Implemented Yet";
210  /*
211  if (size() != from.size()) { return false; }
212 
213  if (sizeEdges() != from.sizeEdges()) { return false; }
214 
215  // alignment of variables between the 2 BNs
216  Bijection< const DiscreteVariable*, const DiscreteVariable* > alignment;
217 
218  for (auto node: nodes()) {
219  try {
220  alignment.insert(&variable(node),
221  &from.variableFromName(variable(node).name()));
222  } catch (NotFound&) {
223  // a name is not found in from
224  return false;
225  }
226  }
227 
228  for (auto node: nodes()) {
229  NodeId fromnode = from.idFromName(variable(node).name());
230 
231  if (factor(node).nbrDim() != from.factor(fromnode).nbrDim()) { return
232  false; }
233 
234  Instantiation i(factor(node));
235  Instantiation j(from.factor(fromnode));
236 
237  for (i.setFirst(); !i.end(); i.inc()) {
238  for (Idx indice = 0; indice < factor(node).nbrDim(); ++indice) {
239  const DiscreteVariable* p = &(i.variable(indice));
240  j.chgVal(*(alignment.second(p)), i.val(*p));
241  }
242 
243  if (std::pow(factor(node).get(i) - from.factor(fromnode).get(j),
244  (GUM_SCALAR)2) > (GUM_SCALAR)1e-6) { return false;
245  }
246  }
247  }
248 
249  return true;
250  */
251  }
252 
253  template < typename GUM_SCALAR >
255  return !this->operator==(from);
256  }
257 
258  template < typename GUM_SCALAR >
259  INLINE std::ostream& operator<<(std::ostream& output,
260  const IMarkovNet< GUM_SCALAR >& bn) {
261  output << bn.toString();
262  return output;
263  }
264 
265 } /* namespace gum */
UGmodel & operator=(const UGmodel &source)
Private copy operator.
Definition: UGmodel.cpp:38
GUM_SCALAR minNonZeroParam() const
virtual ~IMarkovNet()
Destructor.
GUM_SCALAR maxNonOneParam() const
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Virtual base class for PGMs using a undirected graph.
Definition: UGmodel.h:46
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
double log10DomainSize() const
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:626
virtual std::string toDot() const
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovNet::FactorTable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
IMarkovNet()
Default constructor.
IMarkovNet< GUM_SCALAR > & operator=(const IMarkovNet< GUM_SCALAR > &source)
Copy operator.
bool operator!=(const IMarkovNet< GUM_SCALAR > &from) const
GUM_SCALAR minParam() const
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
Definition: UGmodel_inl.h:36
Size maxVarDomainSize() const
std::string toString() const
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
Class representing the minimal interface for Bayesian Network.
Definition: IMarkovNet.h:64
Size sizeEdges() const
indicates the number of edges stored within the EdgeGraphPart
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
GUM_SCALAR maxParam() const
virtual Size size() const final
Returns the number of variables in this Directed Graphical Model.
Definition: UGmodel_inl.h:39
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual const Potential< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
bool operator==(const IMarkovNet< GUM_SCALAR > &from) const
This operator compares 2 BNs !