61 template <
typename GUM_SCALAR >
66 template <
typename GUM_SCALAR >
72 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
86 template <
typename GUM_SCALAR >
91 template <
typename GUM_SCALAR >
95 res += f.second->domainSize();
100 template <
typename GUM_SCALAR >
103 for (
auto node:
nodes()) {
104 auto v =
variable(node).domainSize();
105 if (v > res) { res = v; }
110 template <
typename GUM_SCALAR >
112 GUM_SCALAR res = 1.0;
114 auto v = elt.second->min();
115 if (v < res) { res = v; }
120 template <
typename GUM_SCALAR >
122 GUM_SCALAR res = 1.0;
124 auto v = elt.second->max();
125 if (v > res) { res = v; }
130 template <
typename GUM_SCALAR >
132 GUM_SCALAR res = 1.0;
134 auto v = elt.second->minNonZero();
135 if (v < res) { res = v; }
140 template <
typename GUM_SCALAR >
142 GUM_SCALAR res = 0.0;
144 auto v = elt.second->maxNonOne();
145 if (v > res) { res = v; }
150 template <
typename GUM_SCALAR >
156 param +=
factor.second->content()->realSize();
159 s <<
"MRF{nodes: " <<
size() <<
", edges: " <<
graph().sizeEdges() <<
", ";
161 if (dSize > 6) s <<
"domainSize: 10^" << dSize;
162 else s <<
"domainSize: " << std::round(std::pow(10.0, dSize));
164 s <<
", dim: " << param <<
"}";
169 template <
typename GUM_SCALAR >
171 std::stringstream output;
172 output <<
"graph \"";
178 }
catch (
NotFound const&) { mn_name =
"no_name"; }
180 output << mn_name <<
"\" {" << std::endl;
181 output <<
" graph [bgcolor=transparent,label=\"" << mn_name <<
"\"];" << std::endl;
182 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
184 for (
auto node:
nodes())
185 output <<
" \"" <<
variable(node).name() <<
"\" [comment=\"" << node <<
":"
186 <<
variable(node).toStringWithDescription() <<
"\"];" << std::endl;
190 std::string tab =
" ";
192 for (
auto node:
nodes()) {
196 output << tab <<
"\"" <<
variable(node).name() <<
"\" -- "
197 <<
"\"" <<
variable(nei).name() <<
"\";" << std::endl;
201 output << tab <<
"\"" <<
variable(node).name() <<
"\";" << std::endl;
205 output <<
"}" << std::endl;
210 template <
typename GUM_SCALAR >
212 std::stringstream output;
216 }
catch (
NotFound const&) { mn_name =
"no_name"; }
218 output <<
"graph FG_" << mn_name <<
" {" << std::endl;
219 output <<
" layout=neato;" << std::endl;
220 output <<
" graph [bgcolor=transparent,label=\"factor graph for " << mn_name <<
"\"];"
224 output <<
" node [shape=rectangle,margin=0.04,width=0,height=0, "
225 "style=filled,color=\"coral\"];"
227 for (
auto nod:
nodes()) {
228 output <<
"\"" <<
variable(nod).name() <<
"\";" << std::endl;
233 output <<
"node[shape = point,width = 0.1,height = 0.1,style = filled,color = "
236 for (
const auto& kv:
factors()) {
238 for (
NodeId nod: kv.first) {
239 output <<
"#" <<
variable(nod).name();
241 output <<
"\";" << std::endl;
245 output <<
" edge[len = 0.7];" << std::endl;
246 for (
const auto& kv:
factors()) {
247 std::string clicname =
"\"f";
248 for (
NodeId nod: kv.first) {
254 for (
NodeId nod: kv.first)
255 output <<
" " << clicname <<
" -- \"" <<
variable(nod).name() <<
"\";" << std::endl;
257 output <<
"}" << std::endl;
262 template <
typename GUM_SCALAR >
264 if (
size() != from.
size()) {
return false; }
271 for (
auto node:
nodes()) {
275 if (v1 != v2)
return false;
284 for (
const auto& elt:
factors()) {
285 const auto& key = elt.first;
286 const auto&
factor = *elt.second;
289 for (
const auto n: key)
292 if (!from.
factors().exists(fromkey)) {
return false; }
294 const auto& fromfactor = from.
factor(fromkey);
299 for (
Idx indice = 0; indice <
factor.nbrDim(); ++indice) {
304 if (std::pow(
factor.get(i) - fromfactor.get(j), (GUM_SCALAR)2) > (GUM_SCALAR)1e-6) {
312 template <
typename GUM_SCALAR >
317 template <
typename GUM_SCALAR >
324 template <
typename GUM_SCALAR >
335 template <
typename GUM_SCALAR >
339 NodeSet& alreadyVisited)
const {
340 if (alreadyVisited.
contains(node))
return;
341 alreadyVisited << node;
346 for (
auto neig:
graph_.neighbours(node))
351 template <
typename GUM_SCALAR >
358 alreadyVisited << target;
360 for (
auto neig:
graph_.neighbours(target))
365 template <
typename GUM_SCALAR >
369 for (
auto node: targets) {
Class representing Markov random fields.
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.
Set of pairs of elements with fast search for both elements.
Base class for discrete random variable.
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
double log10DomainSize() const
Class representing the minimal interface for Markov random field.
IMarkovRandomField()
Default constructor.
virtual std::string toDot() const
virtual const DiscreteVariable & variableFromName(const std::string &name) const =0
Getter by name.
GUM_SCALAR minParam() const
virtual const Tensor< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
bool operator!=(const IMarkovRandomField< GUM_SCALAR > &from) const
std::string toString() const
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.
virtual const FactorTable< GUM_SCALAR > & factors() const =0
Returns the set of factors as a IMarkovRandomField::FactorTable.
virtual std::string toDotAsFactorGraph() const
void _minimalCondSetVisit_(NodeId node, const NodeSet &soids, NodeSet &minimal, NodeSet &alreadyVisited) const
GUM_SCALAR maxNonOneParam() const
bool operator==(const IMarkovRandomField< GUM_SCALAR > &from) const
This operator compares 2 BNs !
virtual ~IMarkovRandomField()
Destructor.
GUM_SCALAR maxParam() const
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
IMarkovRandomField< GUM_SCALAR > & operator=(const IMarkovRandomField< GUM_SCALAR > &source)
Copy operator.
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
Size maxVarDomainSize() const
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
GUM_SCALAR minNonZeroParam() const
virtual const NodeSet & smallestFactorFromNode(NodeId node) const =0
Returns the smallest factor that contains this variable.
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Exception : the element we looked for cannot be found.
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.
UGmodel()
Default constructor.
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
UGmodel & operator=(const UGmodel &source)
Private copy operator.
UndiGraph graph_
The DAG of this Directed Graphical Model.
const UndiGraph & graph() const
Returns a constant reference to the dag of this Bayes Net.
const NodeSet & neighbours(const NodeId id) const
returns the neighbours of a node as set of nodes
Size sizeEdges() 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.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
bool operator==(const HashTableIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
Header of the Tensor class.