aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphicalModel_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Interface-like class encapsulating basic functionalities for both a
25  *IBayesNet and IMarkovNet
26  *
27  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 #include <algorithm>
30 #include <iterator>
31 
32 #include <agrum/tools/core/math/math_utils.h>
33 #include <agrum/tools/graphicalModels/graphicalModel.h>
34 
35 namespace gum {
36 
37  INLINE
38  const std::string& GraphicalModel::property(const std::string& name) const {
39  try {
40  return properties__()[name];
41  } catch (NotFound&) {
42  std::string msg = "The following property does not exists: ";
43  GUM_ERROR(NotFound, msg + name);
44  }
45  }
46 
47  INLINE
48  HashTable< std::string, std::string >& GraphicalModel::properties__() const {
49  if (propertiesMap__ == nullptr) {
50  propertiesMap__ = new HashTable< std::string, std::string >();
51  }
52 
53  return *propertiesMap__;
54  }
55 
56  INLINE
57  const std::string&
58  GraphicalModel::propertyWithDefault(const std::string& name,
59  const std::string& byDefault) const {
60  try {
61  return properties__()[name];
62  } catch (NotFound&) { return byDefault; }
63  }
64 
65  INLINE
66  void GraphicalModel::setProperty(const std::string& name,
67  const std::string& value) {
68  try {
69  properties__()[name] = value;
70  } catch (NotFound&) { properties__().insert(name, value); }
71  }
72 
73 
74  INLINE
75  double GraphicalModel::log10DomainSize() const {
76  double dSize = 0.0;
77 
78  for (const auto node: nodes()) {
79  dSize += std::log10(variable(node).domainSize());
80  }
81 
82  return dSize;
83  }
84 
85  INLINE
86  Instantiation GraphicalModel::completeInstantiation() const {
87  Instantiation I;
88 
89  for (const auto node: nodes())
90  I << variable(node);
91 
92  return I;
93  }
94 
95  INLINE
96  bool GraphicalModel::empty() const { return size() == 0; }
97 
98  INLINE
99  std::vector< std::string >
100  GraphicalModel::names(const std::vector< NodeId >& ids) const {
101  std::vector< std::string > res;
102  const VariableNodeMap& v = variableNodeMap();
103  std::transform(ids.cbegin(),
104  ids.cend(),
105  std::back_inserter(res),
106  [v](NodeId n) { return v[n].name(); });
107  return res;
108  }
109 
110  INLINE
111  std::vector< std::string > GraphicalModel::names(const NodeSet& ids) const {
112  const VariableNodeMap& v = variableNodeMap();
113  std::vector< std::string > res;
114  for (auto n: ids) {
115  res.push_back(v.name(n));
116  }
117  return res;
118  }
119 
120  INLINE
121  std::vector< NodeId >
122  GraphicalModel::ids(const std::vector< std::string >& names) const {
123  std::vector< NodeId > res;
124  const VariableNodeMap& v = variableNodeMap();
125  std::transform(names.cbegin(),
126  names.cend(),
127  std::back_inserter(res),
128  [v](const std::string& n) { return v.idFromName(n); });
129  return res;
130  }
131 } /* namespace gum */