aGrUM 3.1.1
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-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#pragma once
42
43
51#include <algorithm>
52#include <iterator>
53
55
57
58namespace gum {
59 INLINE
60 const std::string& GraphicalModel::property(std::string_view name) const {
61 auto p = _properties_().tryGet(name);
62 if (!p) { GUM_ERROR(NotFound, "The following property does not exists: " << name) }
63 return *p;
64 }
65
66 INLINE
70
71 INLINE
72 const std::string& GraphicalModel::propertyWithDefault(std::string_view name,
73 const std::string& byDefault) const {
74 auto p = _propertiesMap_.tryGet(name);
75 return p ? *p : byDefault;
76 }
77
78 INLINE
79 std::vector< std::string > GraphicalModel::properties() const {
80 std::vector< std::string > prop;
81 for (const auto& [p, v]: _propertiesMap_)
82 prop.push_back(p);
83 return prop;
84 }
85
86 INLINE
87 void GraphicalModel::setProperty(std::string_view name, std::string_view value) {
88 if (auto p = _propertiesMap_.tryGet(name)) *p = value;
89 else _propertiesMap_.insert(std::string(name), std::string(value));
90 }
91
92 INLINE
94 double dSize = 0.0;
95
96 for (const auto node: nodes()) {
97 dSize += std::log10(variable(node).domainSize());
98 }
99
100 return dSize;
101 }
102
103 INLINE
106
107 for (const auto node: nodes())
108 I << variable(node);
110 return I;
111 }
112
113 INLINE
114 bool GraphicalModel::empty() const { return size() == 0; }
115
116 INLINE
117 std::vector< std::string > GraphicalModel::names(const std::vector< NodeId >& ids) const {
118 std::vector< std::string > res;
120
121 std::ranges::transform(ids, std::back_inserter(res), [&v](const NodeId n) {
122 return v[n].name();
123 });
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;
141 const VariableNodeMap& v = variableNodeMap();
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;
147 }
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;
163 for (const auto& node: l) {
164 s.insert(&v.get(node));
165 }
166 return s;
167 }
169 INLINE
170 bool GraphicalModel::existsProperty(std::string_view name) const {
171 return _propertiesMap_.exists(name);
172 }
173
174 INLINE
176 for (auto id: g)
177 g.setName(id, variable(id).name());
178 }
179} /* 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.
const std::string & property(std::string_view name) const
Return the value of the property name of this GraphicalModel.
void _nameNodes_(NodeGraphPart &g) const
Names every node of g using variable(id).name() for each node id in g.
void setProperty(std::string_view name, std::string_view 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.
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 the number of variables in this Directed 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 names of property in the Graphical model.
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.
bool existsProperty(std::string_view name) const
check wether a property exists in this GraphicalModel
const std::string & propertyWithDefault(std::string_view name, const std::string &byDefault) const
Return the value of the property name of this GraphicalModel.
virtual Size size() const =0
Returns the number of variables in this Directed Graphical Model.
optional_ref< Val > tryGet(const Key &key)
Returns a pointer to the value associated with a given key, or nullptr if the key does not exist.
Class for assigning/browsing values to tuples of discrete variables.
Class for node sets in graph.
void setName(NodeId id, const std::string &name)
sets the name of node id
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:510
Container used to map discrete variables with nodes.
const DiscreteVariable & variableFromName(std::string_view 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:76
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