aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
graphicalModel_inl.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#pragma once
41
42
50#include <algorithm>
51#include <iterator>
52
54
56
57namespace gum {
58
59 INLINE
60 const std::string& GraphicalModel::property(const std::string& name) const {
61 try {
62 return _properties_()[name];
63 } catch (NotFound const&) {
64 std::string msg = "The following property does not exists: ";
65 GUM_ERROR(NotFound, msg + name)
66 }
67 }
68
69 INLINE
73
74 INLINE
75 const std::string& GraphicalModel::propertyWithDefault(const std::string& name,
76 const std::string& byDefault) const {
77 return (_propertiesMap_.exists(name)) ? _propertiesMap_[name] : byDefault;
78 }
79
80 INLINE
81 std::vector< std::string > GraphicalModel::properties() const {
82 std::vector< std::string > prop;
83 for (const auto& [p, v]: _propertiesMap_)
84 prop.push_back(p);
85 return prop;
86 }
87
88 INLINE
89 void GraphicalModel::setProperty(const std::string& name, const std::string& value) {
90 if (_propertiesMap_.exists(name)) _propertiesMap_[name] = value;
91 else _propertiesMap_.insert(name, value);
92 }
94 INLINE
96 double dSize = 0.0;
97
98 for (const auto node: nodes()) {
99 dSize += std::log10(variable(node).domainSize());
100 }
101
102 return dSize;
103 }
104
105 INLINE
108
109 for (const auto node: nodes())
110 I << variable(node);
111
112 return I;
113 }
114
115 INLINE
116 bool GraphicalModel::empty() const { return size() == 0; }
117
118 INLINE
119 std::vector< std::string > GraphicalModel::names(const std::vector< NodeId >& ids) const {
120 std::vector< std::string > res;
121 const VariableNodeMap& v = variableNodeMap();
122 std::transform(ids.cbegin(), ids.cend(), std::back_inserter(res), [v](NodeId n) {
123 return v[n].name();
124 });
125 return res;
126 }
127
128 INLINE
129 std::vector< std::string > GraphicalModel::names(const NodeSet& ids) const {
130 const VariableNodeMap& v = variableNodeMap();
131 std::vector< std::string > res;
132 for (auto n: ids) {
133 res.push_back(v.name(n));
134 }
135 return res;
136 }
137
138 INLINE
139 std::vector< NodeId > GraphicalModel::ids(const std::vector< std::string >& names) const {
140 std::vector< NodeId > res;
142 std::transform(names.cbegin(),
143 names.cend(),
144 std::back_inserter(res),
145 [v](const std::string& n) { return v.idFromName(n); });
146 return res;
148
149 INLINE
150 VariableSet GraphicalModel::variables(const std::vector< std::string >& l) const {
151 VariableSet s;
152 const VariableNodeMap& v = variableNodeMap();
153 for (const auto& name: l) {
154 s.insert(&v.variableFromName(name));
155 }
156 return s;
157 }
158
159 INLINE
161 VariableSet s;
162 const VariableNodeMap& v = variableNodeMap();
163 for (const auto& node: l) {
164 s.insert(&v.get(node));
166 return s;
167 }
168
169} /* namespace gum */
const HashTable< std::string, std::string > & _properties_() const
Return the properties of this Directed Graphical Model.
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it's node id.
void setProperty(const std::string &name, const std::string &value)
Add or change a property of this GraphicalModel.
virtual const VariableNodeMap & variableNodeMap() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
HashTable< std::string, std::string > _propertiesMap_
The properties of this Directed Graphical Model.
const std::string & property(const std::string &name) const
Return the value of the property name of this GraphicalModel.
const std::string & propertyWithDefault(const std::string &name, const std::string &byDefault) const
Return the value of the property name of this GraphicalModel.
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
virtual const NodeGraphPart & nodes() const =0
Returns a constant reference to the VariableNodeMap of this Graphical Model.
std::vector< std::string > names(const std::vector< NodeId > &ids) const
transform a vector of NodeId in a vector of names
gum::VariableSet variables(const std::vector< std::string > &l) const
transform a vector of names into a VariableeSet
double log10DomainSize() const
std::vector< std::string > properties() const
List of all the properties.
Instantiation completeInstantiation() const
Get an instantiation over all the variables of the model.
virtual bool empty() const
Return true if this graphical model is empty.
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.
Class for assigning/browsing values to tuples of discrete variables.
Exception : the element we looked for cannot be found.
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
Container used to map discrete variables with nodes.
const DiscreteVariable & variableFromName(const std::string &name) const
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it's node id.
const std::string & name(NodeId id) const
Returns the name of a variable given its id.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Class representing probabilistic DAG model.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet