aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
instanceBayesNet_tpl.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 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 InstanceBayesNet< GUM_SCALAR >::_init_(const PRMInstance< GUM_SCALAR >& i) {
36  for (const auto node: i.type().containerDag().nodes()) {
37  try {
38  // Adding the attribute
39  const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
40  this->dag_.addNodeWithId(attr.id());
42  } catch (NotFound&) {
43  // Not an attribute
44  }
45  }
46 
47  for (const auto& arc: i.type().containerDag().arcs()) {
48  try {
49  this->dag_.addArc(arc.tail(), arc.head());
50  } catch (InvalidNode&) {
51  // Not added means not an attribute
52  }
53  }
54  }
55 
56  template < typename GUM_SCALAR >
58  IBayesNet< GUM_SCALAR >(), _inst_(&i) {
60  _init_(i);
61  }
62 
63  template < typename GUM_SCALAR >
67  }
68 
69  template < typename GUM_SCALAR >
72  }
73 
74  template < typename GUM_SCALAR >
77  if (this != &from) {
79 
81  }
82 
83  return *this;
84  }
85 
86  template < typename GUM_SCALAR >
88  return _get_(varId).cpf();
89  }
90 
91  template < typename GUM_SCALAR >
93  GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet")
94  }
95 
96  template < typename GUM_SCALAR >
98  return _get_(id).type().variable();
99  }
100 
101  template < typename GUM_SCALAR >
103  return _varNodeMap_[&var]->id();
104  }
105 
106  template < typename GUM_SCALAR >
108  return _get_(name).id();
109  }
110 
111  template < typename GUM_SCALAR >
112  INLINE const DiscreteVariable&
114  return _get_(name).type().variable();
115  }
116 
117  template < typename GUM_SCALAR >
120  return _inst_->get(id);
121  }
122 
123  template < typename GUM_SCALAR >
126  try {
127  return _inst_->get(name);
128  } catch (NotFound&) { GUM_ERROR(NotFound, "no element found with that name") }
129  }
130 
131  template < typename GUM_SCALAR >
133  if (_modalities_.empty()) {
134  for (const auto node: this->nodes()) {
136  }
137  }
138 
139  return _modalities_;
140  }
141 
142  template < typename GUM_SCALAR >
144  std::string tab = " ";
146  output << "digraph \"";
147  output << _inst_->name() << "\" {" << std::endl;
148 
149  for (const auto node: this->nodes()) {
150  if (this->children(node).size() > 0) {
151  const NodeSet& children = this->children(node);
152 
153  for (const auto chi: children) {
154  output << tab << "\"" << variable(node).name() << "\" -> ";
155  output << "\"" << variable(chi).name() << "\";" << std::endl;
156  }
157  } else if (this->parents(node).size() == 0) {
158  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
159  }
160  }
161 
162  output << "}" << std::endl;
163  return output.str();
164  }
165 
166  } /* namespace prm */
167 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)