aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
classBayesNet_tpl.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 Inline implementation of ClassBayesNet.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #include <agrum/PRM/classBayesNet.h> // to ease IDE parser
29 #include <agrum/tools/core/inline.h>
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
35  void ClassBayesNet< GUM_SCALAR >::init__(const PRMClass< GUM_SCALAR >& c) {
36  for (const auto node: c.containerDag().nodes()) {
37  try {
38  // Adding the attribute
41  const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
42  this->dag_.addNodeWithId(elt.id());
43  this->varNodeMap__.insert(&(elt.type().variable()), &elt);
44  }
45  } catch (NotFound&) {
46  // Not an attribute
47  }
48  }
49 
50  for (const auto& arc: c.containerDag().arcs()) {
51  try {
52  this->dag_.addArc(arc.tail(), arc.head());
53  } catch (InvalidNode&) {
54  // Not added means not an attribute
55  }
56  }
57  }
58 
59  template < typename GUM_SCALAR >
61  const PRMClass< GUM_SCALAR >& c) :
62  IBayesNet< GUM_SCALAR >(),
63  class__(&c) {
65  init__(c);
66  }
67 
68  template < typename GUM_SCALAR >
70  const ClassBayesNet< GUM_SCALAR >& from) :
74  }
75 
76  template < typename GUM_SCALAR >
79  }
80 
81  template < typename GUM_SCALAR >
83  const ClassBayesNet< GUM_SCALAR >& from) {
84  if (this != &from) {
86 
88  }
89 
90  return *this;
91  }
92 
93  template < typename GUM_SCALAR >
94  INLINE const Potential< GUM_SCALAR >&
96  return get__(varId).cpf();
97  }
98 
99  template < typename GUM_SCALAR >
100  INLINE const VariableNodeMap&
102  GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.");
103  }
104 
105  template < typename GUM_SCALAR >
106  INLINE const DiscreteVariable&
108  return get__(id).type().variable();
109  }
110 
111  template < typename GUM_SCALAR >
112  INLINE NodeId
114  return varNodeMap__[&var]->id();
115  }
116 
117  template < typename GUM_SCALAR >
118  INLINE NodeId
120  return get__(name).id();
121  }
122 
123  template < typename GUM_SCALAR >
125  const std::string& name) const {
126  return get__(name).type().variable();
127  }
128 
129  template < typename GUM_SCALAR >
132  if (this->dag_.exists(id)) {
133  return class__->get(id);
134  } else {
135  GUM_ERROR(NotFound, "no element found with that id.");
136  }
137  }
138 
139  template < typename GUM_SCALAR >
142  try {
143  return class__->get(name);
144  } catch (NotFound&) {
145  GUM_ERROR(NotFound, "no element found with that id.");
146  }
147  }
148 
149  template < typename GUM_SCALAR >
150  INLINE const NodeProperty< Size >&
152  if (modalities__.empty()) {
153  for (const auto node: this->nodes()) {
155  }
156  }
157 
158  return modalities__;
159  }
160 
161  template < typename GUM_SCALAR >
163  std::string tab = " ";
165  output << "digraph \"";
166  output << class__->name() << "\" {" << std::endl;
167 
168  for (const auto node: this->nodes()) {
169  if (this->children(node).size() > 0)
170  for (const auto chi: this->children(node)) {
171  output << tab << "\"" << variable(node).name() << "\" -> ";
172  output << "\"" << variable(chi).name() << "\";" << std::endl;
173  }
174  else if (this->parents(node).size() == 0) {
175  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
176  }
177  }
178 
179  output << "}" << std::endl;
180  return output.str();
181  }
182 
183  } /* namespace prm */
184 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)