aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
instanceBayesNet_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 InstanceBayesNet.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #include <agrum/PRM/instanceBayesNet.h> // to ease IDE parser
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
35  void
36  InstanceBayesNet< GUM_SCALAR >::init__(const PRMInstance< GUM_SCALAR >& i) {
37  for (const auto node: i.type().containerDag().nodes()) {
38  try {
39  // Adding the attribute
40  const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
41  this->dag_.addNodeWithId(attr.id());
43  } catch (NotFound&) {
44  // Not an attribute
45  }
46  }
47 
48  for (const auto& arc: i.type().containerDag().arcs()) {
49  try {
50  this->dag_.addArc(arc.tail(), arc.head());
51  } catch (InvalidNode&) {
52  // Not added means not an attribute
53  }
54  }
55  }
56 
57  template < typename GUM_SCALAR >
59  const PRMInstance< GUM_SCALAR >& i) :
60  IBayesNet< GUM_SCALAR >(),
61  inst__(&i) {
63  init__(i);
64  }
65 
66  template < typename GUM_SCALAR >
68  const InstanceBayesNet& from) :
72  }
73 
74  template < typename GUM_SCALAR >
77  }
78 
79  template < typename GUM_SCALAR >
82  if (this != &from) {
84 
86  }
87 
88  return *this;
89  }
90 
91  template < typename GUM_SCALAR >
92  INLINE const Potential< GUM_SCALAR >&
94  return get__(varId).cpf();
95  }
96 
97  template < typename GUM_SCALAR >
98  INLINE const VariableNodeMap&
100  GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet");
101  }
102 
103  template < typename GUM_SCALAR >
104  INLINE const DiscreteVariable&
106  return get__(id).type().variable();
107  }
108 
109  template < typename GUM_SCALAR >
110  INLINE NodeId
112  return varNodeMap__[&var]->id();
113  }
114 
115  template < typename GUM_SCALAR >
116  INLINE NodeId
118  return get__(name).id();
119  }
120 
121  template < typename GUM_SCALAR >
122  INLINE const DiscreteVariable&
124  const std::string& name) const {
125  return get__(name).type().variable();
126  }
127 
128  template < typename GUM_SCALAR >
131  return inst__->get(id);
132  }
133 
134  template < typename GUM_SCALAR >
137  try {
138  return inst__->get(name);
139  } catch (NotFound&) {
140  GUM_ERROR(NotFound, "no element found with that name");
141  }
142  }
143 
144  template < typename GUM_SCALAR >
145  INLINE const NodeProperty< Size >&
147  if (modalities__.empty()) {
148  for (const auto node: this->nodes()) {
150  }
151  }
152 
153  return modalities__;
154  }
155 
156  template < typename GUM_SCALAR >
158  std::string tab = " ";
160  output << "digraph \"";
161  output << inst__->name() << "\" {" << std::endl;
162 
163  for (const auto node: this->nodes()) {
164  if (this->children(node).size() > 0) {
165  const NodeSet& children = this->children(node);
166 
167  for (const auto chi: children) {
168  output << tab << "\"" << variable(node).name() << "\" -> ";
169  output << "\"" << variable(chi).name() << "\";" << std::endl;
170  }
171  } else if (this->parents(node).size() == 0) {
172  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
173  }
174  }
175 
176  output << "}" << std::endl;
177  return output.str();
178  }
179 
180  } /* namespace prm */
181 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)