59 template <
typename GUM_SCALAR >
63 std::string name = node;
64 auto ds = default_domain_size;
66 long range_max = long(ds) - 1;
67 std::vector< std::string > labels;
68 std::vector< GUM_SCALAR > ticks;
70 if (*(node.rbegin()) ==
']') {
71 auto posBrack = node.find(
'[');
72 if (posBrack != std::string::npos) {
73 name = node.substr(0, posBrack);
74 const auto& s_args = node.substr(posBrack + 1, node.size() - posBrack - 2);
75 const auto& args =
split(s_args,
",");
76 if (args.size() == 0) {
78 }
else if (args.size() == 1) {
79 ds =
static_cast< Size >(std::stoi(args[0]));
81 range_max = long(ds) - 1;
82 }
else if (args.size() == 2) {
83 range_min = std::stol(args[0]);
84 range_max = std::stol(args[1]);
85 if (1 + range_max - range_min < 2) {
88 ds =
static_cast< Size >(1 + range_max - range_min);
90 for (
const auto& tick: args) {
91 ticks.push_back(static_cast< GUM_SCALAR >(std::atof(tick.c_str())));
93 ds =
static_cast< Size >(args.size() - 1);
96 }
else if (*(node.rbegin()) ==
'}') {
97 auto posBrack = node.find(
'{');
98 if (posBrack != std::string::npos) {
99 name = node.substr(0, posBrack);
100 labels =
split(node.substr(posBrack + 1, node.size() - posBrack - 2),
"|");
101 if (labels.size() < 2) {
107 ds =
static_cast< Size >(labels.size());
113 }
else if (ds == 1) {
115 "Only one value for variable " << name
116 <<
" (2 at least are needed).");
122 idVar = mn.idFromName(name);
124 if (!labels.empty()) {
126 }
else if (!ticks.empty()) {
129 idVar = mn.add(
RangeVariable(name, name, range_min, range_max));
136 template <
typename GUM_SCALAR >
137 MarkovNet< GUM_SCALAR >
138 MarkovNet< GUM_SCALAR >::fastPrototype(
const std::string& dotlike,
173 template <
typename GUM_SCALAR >
175 GUM_CONSTRUCTOR(MarkovNet);
178 template <
typename GUM_SCALAR >
179 INLINE MarkovNet< GUM_SCALAR >::MarkovNet(std::string name) :
181 GUM_CONSTRUCTOR(MarkovNet);
184 template <
typename GUM_SCALAR >
185 MarkovNet< GUM_SCALAR >::MarkovNet(
const MarkovNet< GUM_SCALAR >& source) :
187 GUM_CONS_CPY(MarkovNet);
189 __copyFactors(source);
192 template <
typename GUM_SCALAR >
193 MarkovNet< GUM_SCALAR >&
194 MarkovNet< GUM_SCALAR >::operator=(
const MarkovNet< GUM_SCALAR >& source) {
195 if (
this != &source) {
197 __varMap = source.__varMap;
198 __copyFactors(source);
204 template <
typename GUM_SCALAR >
205 MarkovNet< GUM_SCALAR >::~MarkovNet() {
207 GUM_DESTRUCTOR(MarkovNet);
210 template <
typename GUM_SCALAR >
212 MarkovNet< GUM_SCALAR >::variable(
NodeId id)
const {
213 return __varMap.get(
id);
216 template <
typename GUM_SCALAR >
218 MarkovNet< GUM_SCALAR >::changeVariableName(
NodeId id,
219 const std::string& new_name) {
220 __varMap.changeName(
id, new_name);
223 template <
typename GUM_SCALAR >
224 INLINE
void MarkovNet< GUM_SCALAR >::changeVariableLabel(
225 NodeId id,
const std::string& old_label,
const std::string& new_label) {
235 template <
typename GUM_SCALAR >
238 return __varMap.get(var);
241 template <
typename GUM_SCALAR >
243 MarkovNet< GUM_SCALAR >::factor(
const NodeSet& varIds)
const {
244 return *__factors[varIds];
247 template <
typename GUM_SCALAR >
252 template <
typename GUM_SCALAR >
253 INLINE
NodeId MarkovNet< GUM_SCALAR >::add(
const std::string& name,
254 unsigned int nbrmod) {
257 "Variable " << name <<
"needs more than " << nbrmod
265 template <
typename GUM_SCALAR >
270 template <
typename GUM_SCALAR >
273 __varMap.insert(
id, var);
274 this->_graph.addNodeWithId(
id);
278 template <
typename GUM_SCALAR >
280 MarkovNet< GUM_SCALAR >::idFromName(
const std::string& name)
const {
281 return __varMap.idFromName(name);
284 template <
typename GUM_SCALAR >
286 MarkovNet< GUM_SCALAR >::variableFromName(
const std::string& name)
const {
287 return __varMap.variableFromName(name);
290 template <
typename GUM_SCALAR >
291 INLINE
const VariableNodeMap& MarkovNet< GUM_SCALAR >::variableNodeMap()
const {
295 template <
typename GUM_SCALAR >
297 erase(__varMap.get(var));
300 template <
typename GUM_SCALAR >
301 void MarkovNet< GUM_SCALAR >::erase(
NodeId varId) {
320 template <
typename GUM_SCALAR >
321 void MarkovNet< GUM_SCALAR >::clear() {
322 if (!this->empty()) {
323 auto l = this->nodes();
324 for (
const auto no: l) {
331 template <
typename GUM_SCALAR >
333 const MarkovNet< GUM_SCALAR >& mn) {
334 output << mn.toString();
338 template <
typename GUM_SCALAR >
340 MarkovNet< GUM_SCALAR >::addFactor(
const gum::NodeSet& vars) {
341 if (vars.
size() == 0) {
345 if (__factors.exists(vars)) {
349 for (
const auto v: vars) {
350 factor->
add(variable(v));
352 __factors.insert(vars, factor);
354 for (
const auto var1: vars)
355 for (
const auto var2: vars)
356 if (var1 != var2) this->_graph.addEdge(var1, var2);
361 template <
typename GUM_SCALAR >
363 const std::vector< std::string >& varnames) {
365 for (
const auto name: varnames) {
366 vars.
insert(idFromName(name));
368 return addFactor(vars);
371 template <
typename GUM_SCALAR >
374 if (factor.
nbrDim() == 0) {
379 for (
Idx i = 0; i < factor.
nbrDim(); i++) {
382 if (__factors.exists(key)) {
386 __factors.insert(key, p);
391 template <
typename GUM_SCALAR >
392 INLINE
void MarkovNet< GUM_SCALAR >::generateFactors()
const {
396 template <
typename GUM_SCALAR >
397 INLINE
void MarkovNet< GUM_SCALAR >::generateFactor(
const NodeSet& vars)
const {
405 template <
typename GUM_SCALAR >
406 void MarkovNet< GUM_SCALAR >::changeFactor(
const NodeSet& vars,
429 template <
typename GUM_SCALAR >
430 void MarkovNet< GUM_SCALAR >::__clearFactors() {
431 for (
const auto& c: __factors) {
437 template <
typename GUM_SCALAR >
438 void MarkovNet< GUM_SCALAR >::__copyFactors(
439 const MarkovNet< GUM_SCALAR >& source) {
441 for (
const auto& factor: source.factors()) {
442 addFactor(*factor.second);
aGrUM's Potential is a multi-dimensional array with tensor operators.
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
void changeLabel(Idx pos, const std::string &aLabel) const
change a label for this index
bool hasUniqueElts(std::vector< T > const &x)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Class for discretized random variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Container used to map discrete variables with nodes.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
NodeId build_node(gum::BayesNet< GUM_SCALAR > &bn, std::string node, gum::Size default_domain_size)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Base class for discrete random variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
The class for generic Hash Tables.
Representation of a setA Set is a structure that contains arbitrary elements.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map's DAG in output using the Graphviz-dot format.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Idx posLabel(const std::string &label) const
return the pos from label
IMarkovNet< GUM_SCALAR > & operator=(const IMarkovNet< GUM_SCALAR > &source)
Copy operator.
Defines a discrete random variable over an integer interval.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
NodeId nextNodeId()
Returns the next value of an unique counter for PRM's node id.
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Size Idx
Type for indexes.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Class representing the minimal interface for Bayesian Network.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size size() const noexcept
Returns the number of elements in the set.
const std::string & name() const
returns the name of the variable
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Size NodeId
Type for node ids.
void insert(const Key &k)
Inserts a new element into the set.
#define GUM_ERROR(type, msg)