aGrUM 3.1.1
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-2026 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-2026 *
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 < GUM_Numeric GUM_SCALAR >
88 class MarkovRandomField: public IMarkovRandomField< GUM_SCALAR > {
89 public:
112 static MarkovRandomField< GUM_SCALAR > fastPrototype(std::string_view dotlike, Size domainSize);
113 static MarkovRandomField< GUM_SCALAR > fastPrototype(std::string_view dotlike,
114 std::string_view domain = "[2]");
115
121 static MarkovRandomField< GUM_SCALAR > fromBN(const BayesNet< GUM_SCALAR >& bn);
122
123 // ===========================================================================
125 // ===========================================================================
127
131 MarkovRandomField();
132
138 explicit MarkovRandomField(std::string_view name);
139
143 ~MarkovRandomField() override;
144
148 MarkovRandomField(const MarkovRandomField< GUM_SCALAR >& source);
149
151 // ===========================================================================
153 // ===========================================================================
155
162 MarkovRandomField< GUM_SCALAR >& operator=(const MarkovRandomField< GUM_SCALAR >& source);
163
170 MarkovRandomField< GUM_SCALAR >& operator=(MarkovRandomField< GUM_SCALAR >&& source) noexcept;
171
173 // ===========================================================================
175 // ===========================================================================
177
185 const Tensor< GUM_SCALAR >& factor(const NodeSet& varIds) const final;
186
187 virtual const Tensor< GUM_SCALAR >&
188 factor(const std::vector< std::string >& varnames) const final;
189
195 const NodeSet& smallestFactorFromNode(NodeId node) const final;
196
201 const FactorTable< GUM_SCALAR >& factors() const final;
202
217 NodeId add(const DiscreteVariable& var);
218
237 NodeId add(std::string_view 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(std::string_view name);
279
290 void erase(const DiscreteVariable& var);
291
302
307 const DiscreteVariable& variable(std::string_view name) const;
308
318 void changeVariableName(NodeId id, std::string_view new_name);
319
323 void changeVariableName(std::string_view name, std::string_view new_name);
324
335 void changeVariableLabel(NodeId id, std::string_view old_label, std::string_view new_label);
336
340 void changeVariableLabel(std::string_view name,
341 std::string_view old_label,
342 std::string_view new_label);
343
345
346 // ===========================================================================
348 // ===========================================================================
350
359 const Tensor< GUM_SCALAR >& addFactor(const std::vector< std::string >& varnames);
360
371 const Tensor< GUM_SCALAR >& addFactor(const NodeSet& vars);
372
382 const Tensor< GUM_SCALAR >& addFactor(const Tensor< GUM_SCALAR >& factor);
383
390 void eraseFactor(const NodeSet& vars);
391
392 void eraseFactor(const std::vector< std::string >& varnames);
394
395
397 void generateFactors() const;
398
400 void generateFactor(const NodeSet& vars) const;
401
404 void beginTopologyTransformation();
405
406 void endTopologyTransformation();
407
408 private:
409 bool _topologyTransformationInProgress_;
410
412 void _clearFactors_();
413
415 void _copyFactors_(const MarkovRandomField< GUM_SCALAR >& source);
416
418 void _rebuildGraph_();
419
421 FactorTable< GUM_SCALAR > _factors_;
422
423 Tensor< GUM_SCALAR >& _addFactor_(const std::vector< NodeId >& ordered_nodes);
424
425 void _eraseFactor_(const NodeSet& vars);
426
427 public:
428 using IMarkovRandomField< GUM_SCALAR >::graph;
429 using IMarkovRandomField< GUM_SCALAR >::size;
430 using IMarkovRandomField< GUM_SCALAR >::nodes;
431 using IMarkovRandomField< GUM_SCALAR >::log10DomainSize;
432 using DiscreteGraphicalModel::idFromName;
433 using DiscreteGraphicalModel::variableNodeMap;
434 using DiscreteGraphicalModel::variable;
435 using DiscreteGraphicalModel::nodeId;
436 using DiscreteGraphicalModel::variableFromName;
437 };
438
440 template < GUM_Numeric GUM_SCALAR >
441 std::ostream& operator<<(std::ostream& output, const MarkovRandomField< GUM_SCALAR >& bn);
442
443
444#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
445
446 extern template class MarkovRandomField< double >;
447
448#endif
449
450} /* namespace gum */
451
453
454#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.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46