aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
MarkovRandomField.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49#ifndef GUM_MARKOV_RANDOM_FIELD_H
50#define GUM_MARKOV_RANDOM_FIELD_H
51
52#include <utility>
53
54#include <agrum/agrum.h>
55
56#include <agrum/BN/BayesNet.h>
58
59namespace gum {
60
87 template < typename GUM_SCALAR >
88 class MarkovRandomField: public IMarkovRandomField< GUM_SCALAR > {
89 public:
112 static MarkovRandomField< GUM_SCALAR > fastPrototype(const std::string& dotlike,
113 Size domainSize);
114 static MarkovRandomField< GUM_SCALAR > fastPrototype(const std::string& dotlike,
115 const std::string& domain = "[2]");
116
122 static MarkovRandomField< GUM_SCALAR > fromBN(const BayesNet< GUM_SCALAR >& bn);
123
124 // ===========================================================================
126 // ===========================================================================
128
132 MarkovRandomField();
133
139 explicit MarkovRandomField(std::string name);
140
144 virtual ~MarkovRandomField();
145
149 MarkovRandomField(const MarkovRandomField< GUM_SCALAR >& source);
150
152 // ===========================================================================
154 // ===========================================================================
156
163 MarkovRandomField< GUM_SCALAR >& operator=(const MarkovRandomField< GUM_SCALAR >& source);
164
166 // ===========================================================================
168 // ===========================================================================
170
178 virtual const Tensor< GUM_SCALAR >& factor(const NodeSet& varIds) const final;
179
180 virtual const Tensor< GUM_SCALAR >&
181 factor(const std::vector< std::string >& varnames) const final;
182
188 virtual const NodeSet& smallestFactorFromNode(NodeId node) const final;
189
194 virtual const FactorTable< GUM_SCALAR >& factors() const final;
195
201 virtual const VariableNodeMap& variableNodeMap() const final;
202
217 NodeId add(const DiscreteVariable& var);
218
237 NodeId add(const std::string& fast_description, unsigned int default_nbrmod = 2);
238
256 NodeId add(const DiscreteVariable& var, NodeId id);
257
261 void clear();
262
273 void erase(NodeId varId);
274
278 void erase(const std::string& name);
279
290 void erase(const DiscreteVariable& var);
291
302 const DiscreteVariable& variable(NodeId id) const final;
303
310 const DiscreteVariable& variable(const std::string& name) const {
311 return variable(idFromName(name));
312 };
313
323 void changeVariableName(NodeId id, const std::string& new_name);
324
328 void changeVariableName(const std::string& name, const std::string& new_name) {
329 changeVariableName(idFromName(name), new_name);
330 }
331
342 void changeVariableLabel(NodeId id, const std::string& old_label, const std::string& new_label);
343
347 void changeVariableLabel(const std::string& name,
348 const std::string& old_label,
349 const std::string& new_label) {
350 changeVariableLabel(idFromName(name), old_label, new_label);
351 }
352
361 NodeId nodeId(const DiscreteVariable& var) const final;
362
371 NodeId idFromName(const std::string& name) const final;
372
382 const DiscreteVariable& variableFromName(const std::string& name) const final;
384
385 // ===========================================================================
387 // ===========================================================================
389
398 const Tensor< GUM_SCALAR >& addFactor(const std::vector< std::string >& varnames);
399
410 const Tensor< GUM_SCALAR >& addFactor(const NodeSet& vars);
411
421 const Tensor< GUM_SCALAR >& addFactor(const Tensor< GUM_SCALAR >& factor);
422
429 void eraseFactor(const NodeSet& vars);
430
431 void eraseFactor(const std::vector< std::string >& varnames);
433
434
436 void generateFactors() const;
437
439 void generateFactor(const NodeSet& vars) const;
440
443 void beginTopologyTransformation();
444
445 void endTopologyTransformation();
446
447 private:
448 bool _topologyTransformationInProgress_;
449
451 void _clearFactors_();
452
454 void _copyFactors_(const MarkovRandomField< GUM_SCALAR >& source);
455
457 void _rebuildGraph_();
459 VariableNodeMap _varMap_;
460
462 FactorTable< GUM_SCALAR > _factors_;
463
464 Tensor< GUM_SCALAR >& _addFactor_(const std::vector< NodeId >& ordered_nodes);
465
466 void _eraseFactor_(const NodeSet& vars);
467
468 public:
469 using IMarkovRandomField< GUM_SCALAR >::graph;
470 using IMarkovRandomField< GUM_SCALAR >::size;
471 using IMarkovRandomField< GUM_SCALAR >::nodes;
472 using IMarkovRandomField< GUM_SCALAR >::log10DomainSize;
473 };
474
476 template < typename GUM_SCALAR >
477 std::ostream& operator<<(std::ostream& output, const MarkovRandomField< GUM_SCALAR >& bn);
478
479
480#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
481
482 extern template class MarkovRandomField< double >;
483
484#endif
485
486} /* namespace gum */
487
489
490#endif /* GUM_MARKOV_RANDOM_FIELD_H */
Class representing Bayesian networks.
Class representing Markov random fields.
Template implementation of BN/MarkovRandomField.h class.
Class representing the minimal interface for Markov random field.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913