60 template < GUM_Numeric GUM_SCALAR >
65 template < GUM_Numeric GUM_SCALAR >
71 template < GUM_Numeric GUM_SCALAR >
77 template < GUM_Numeric GUM_SCALAR >
85 template < GUM_Numeric GUM_SCALAR >
90 template < GUM_Numeric GUM_SCALAR >
94 res += f.second->domainSize();
99 template < GUM_Numeric GUM_SCALAR >
102 for (
auto node:
nodes()) {
103 auto v =
variable(node).domainSize();
104 if (v > res) { res = v; }
109 template < GUM_Numeric GUM_SCALAR >
111 GUM_SCALAR res = 1.0;
113 auto v = elt.second->min();
114 if (v < res) { res = v; }
119 template < GUM_Numeric GUM_SCALAR >
121 GUM_SCALAR res = 1.0;
123 auto v = elt.second->max();
124 if (v > res) { res = v; }
129 template < GUM_Numeric GUM_SCALAR >
131 GUM_SCALAR res = 1.0;
133 auto v = elt.second->minNonZero();
134 if (v < res) { res = v; }
139 template < GUM_Numeric GUM_SCALAR >
141 GUM_SCALAR res = 0.0;
143 auto v = elt.second->maxNonOne();
144 if (v > res) { res = v; }
149 template < GUM_Numeric GUM_SCALAR >
155 param +=
factor.second->content()->realSize();
160 if (dSize > 6) s << std::format(
"domainSize: 10^{}", dSize);
161 else s << std::format(
"domainSize: {}", std::round(std::pow(10.0, dSize)));
163 s << std::format(
", dim: {}}}", param);
168 template < GUM_Numeric GUM_SCALAR >
170 std::stringstream output;
174 output << std::format(
"graph \"{}\" {{\n", mn_name);
175 output << std::format(
" graph [bgcolor=transparent,label=\"{}\"];\n", mn_name);
176 output <<
" node [style=filled fillcolor=\"#ffffaa\"];" << std::endl << std::endl;
178 for (
auto node:
nodes())
179 output << std::format(
" \"{}\" [comment=\"{}:{}\"];\n",
182 variable(node).toStringWithDescription());
186 std::string tab =
" ";
188 for (
auto node:
nodes()) {
192 output << std::format(
" \"{}\" -- \"{}\";\n",
198 output << std::format(
" \"{}\";\n",
variable(node).name());
202 output <<
"}" << std::endl;
207 template < GUM_Numeric GUM_SCALAR >
209 std::stringstream output;
212 output << std::format(
"graph FG_{} {{\n", mn_name);
213 output <<
" layout=neato;" << std::endl;
214 output << std::format(
" graph [bgcolor=transparent,label=\"factor graph for {}\"];\n",
218 output <<
" node [shape=rectangle,margin=0.04,width=0,height=0, "
219 "style=filled,color=\"coral\"];"
221 for (
auto nod:
nodes()) {
222 output << std::format(
"\"{}\";\n",
variable(nod).name());
227 output <<
"node[shape = point,width = 0.1,height = 0.1,style = filled,color = "
230 for (
const auto& kv:
factors()) {
232 for (
NodeId nod: kv.first) {
233 output <<
"#" <<
variable(nod).name();
235 output <<
"\";" << std::endl;
239 output <<
" edge[len = 0.7];" << std::endl;
240 for (
const auto& kv:
factors()) {
241 std::string clicname =
"\"f";
242 for (
NodeId nod: kv.first) {
248 for (
NodeId nod: kv.first)
249 output << std::format(
" {} -- \"{}\";\n", clicname,
variable(nod).name());
251 output <<
"}" << std::endl;
256 template < GUM_Numeric GUM_SCALAR >
258 if (
size() != from.
size()) {
return false; }
265 for (
auto node:
nodes()) {
267 if (!from.
exists(v1.name()))
return false;
269 if (v1 != v2)
return false;
274 for (
const auto& elt:
factors()) {
275 const auto& key = elt.first;
276 const auto&
factor = *elt.second;
279 for (
const auto n: key)
282 if (!from.
factors().exists(fromkey)) {
return false; }
284 const auto& fromfactor = from.
factor(fromkey);
289 for (
Idx indice = 0; indice <
factor.nbrDim(); ++indice) {
294 if (std::pow(
factor.get(i) - fromfactor.get(j), (GUM_SCALAR)2) > (GUM_SCALAR)1e-6) {
302 template < GUM_Numeric GUM_SCALAR >
308 template < GUM_Numeric GUM_SCALAR >
311 if (!this->
exists(name)) {
318 template < GUM_Numeric GUM_SCALAR >
322 NodeSet& alreadyVisited)
const {
323 if (alreadyVisited.
contains(node))
return;
324 alreadyVisited << node;
329 for (
auto neig:
graph_.neighbours(node))
334 template < GUM_Numeric GUM_SCALAR >
341 alreadyVisited << target;
343 for (
auto neig:
graph_.neighbours(target))
348 template < GUM_Numeric GUM_SCALAR >
352 for (
auto node: targets) {
Class representing Markov random fields.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
Set of pairs of elements with fast search for both elements.
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const DiscreteVariable & variableFromName(std::string_view name) const override
Returns a constant reference over a variable given its name.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
Base class for discrete random variable.
void setProperty(std::string_view name, std::string_view value)
Add or change a property of this GraphicalModel.
double log10DomainSize() const
const std::string & propertyWithDefault(std::string_view name, const std::string &byDefault) const
Return the value of the property name of this GraphicalModel.
Class representing the minimal interface for Markov random field.
virtual std::string toDot() const
IMarkovRandomField()
Default constructor.
GUM_SCALAR minParam() const
GUM_SCALAR maxParam() const
virtual const Tensor< GUM_SCALAR > & factor(const NodeSet &varIds) const =0
Returns the factor of a set of variable.
GUM_SCALAR minNonZeroParam() const
~IMarkovRandomField() override
Destructor.
Size maxVarDomainSize() const
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
Size dim() const
Returns the dimension (the number of free parameters) in this bayes net.
bool operator==(const IMarkovRandomField< GUM_SCALAR > &from) const
This operator compares 2 BNs !
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
IMarkovRandomField< GUM_SCALAR > & operator=(const IMarkovRandomField< GUM_SCALAR > &source)
Copy operator.
GUM_SCALAR maxNonOneParam() const
std::string toString() 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 named copy of the internal undirected graph: each node id is assigned the name of the corre...
UGmodel & operator=(const UGmodel &source)
Private copy operator.
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
UndiGraph graph_
The DAG of this Directed Graphical Model.
UndiGraph graph() const
Returns a named copy of the internal undirected graph: each node id is assigned the name of the corre...
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.
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
Header of the Tensor class.