aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphicalModel_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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& GraphicalModel::propertyWithDefault(const std::string& name,
58  const std::string& byDefault) const {
59  try {
60  return _properties_()[name];
61  } catch (NotFound&) { return byDefault; }
62  }
63 
64  INLINE
65  void GraphicalModel::setProperty(const std::string& name, const std::string& value) {
66  try {
67  _properties_()[name] = value;
68  } catch (NotFound&) { _properties_().insert(name, value); }
69  }
70 
71 
72  INLINE
73  double GraphicalModel::log10DomainSize() const {
74  double dSize = 0.0;
75 
76  for (const auto node: nodes()) {
77  dSize += std::log10(variable(node).domainSize());
78  }
79 
80  return dSize;
81  }
82 
83  INLINE
84  Instantiation GraphicalModel::completeInstantiation() const {
85  Instantiation I;
86 
87  for (const auto node: nodes())
88  I << variable(node);
89 
90  return I;
91  }
92 
93  INLINE
94  bool GraphicalModel::empty() const { return size() == 0; }
95 
96  INLINE
97  std::vector< std::string > GraphicalModel::names(const std::vector< NodeId >& ids) const {
98  std::vector< std::string > res;
99  const VariableNodeMap& v = variableNodeMap();
100  std::transform(ids.cbegin(), ids.cend(), std::back_inserter(res), [v](NodeId n) {
101  return v[n].name();
102  });
103  return res;
104  }
105 
106  INLINE
107  std::vector< std::string > GraphicalModel::names(const NodeSet& ids) const {
108  const VariableNodeMap& v = variableNodeMap();
109  std::vector< std::string > res;
110  for (auto n: ids) {
111  res.push_back(v.name(n));
112  }
113  return res;
114  }
115 
116  INLINE
117  std::vector< NodeId > GraphicalModel::ids(const std::vector< std::string >& names) const {
118  std::vector< NodeId > res;
119  const VariableNodeMap& v = variableNodeMap();
120  std::transform(names.cbegin(),
121  names.cend(),
122  std::back_inserter(res),
123  [v](const std::string& n) { return v.idFromName(n); });
124  return res;
125  }
126 } /* namespace gum */