aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
BayesNetFragment.h
Go to the documentation of this file.
1 
30 #ifndef GUM_BAYES_NET_FRAGMENT_H
31 #define GUM_BAYES_NET_FRAGMENT_H
32 
33 #include <agrum/agrum.h>
34 
35 #include <agrum/BN/IBayesNet.h>
37 
38 #include <agrum/BN/BayesNet.h>
39 
40 namespace gum {
72  template < typename GUM_SCALAR >
74  public IBayesNet< GUM_SCALAR >,
75  public gum::DiGraphListener {
76  private:
79 
83 
84  public:
87  BayesNetFragment() = delete;
88  BayesNetFragment(const BayesNetFragment< GUM_SCALAR >& fragment) = delete;
90 
91  explicit BayesNetFragment(const IBayesNet< GUM_SCALAR >& bn);
92 
93  virtual ~BayesNetFragment();
95 
98 
100 
102  virtual void whenNodeAdded(const void* src, NodeId id) final;
103 
105 
107  virtual void whenNodeDeleted(const void* src, NodeId id) final;
108 
110 
113  virtual void whenArcAdded(const void* src, NodeId from, NodeId to) final;
114 
116 
119  virtual void whenArcDeleted(const void* src, NodeId from, NodeId to) final;
121 
124 
130  virtual const Potential< GUM_SCALAR >& cpt(NodeId varId) const final;
131  virtual const Potential< GUM_SCALAR >& cpt(const std::string& name) const {
132  return cpt(idFromName(name));
133  };
134 
138  virtual const VariableNodeMap& variableNodeMap() const final;
139 
145  virtual const DiscreteVariable& variable(NodeId id) const final;
146  virtual const DiscreteVariable& variable(const std::string& name) const final {
147  return variable(idFromName(name));
148  };
149 
155  virtual NodeId nodeId(const DiscreteVariable& var) const final;
156 
162  virtual NodeId idFromName(const std::string& name) const final;
163 
169  virtual const DiscreteVariable&
170  variableFromName(const std::string& name) const final;
171 
176  virtual std::string toDot() const final;
177 
179 
182 
186  bool isInstalledNode(NodeId id) const;
187  bool isInstalledNode(const std::string& name) const {
188  return isInstalledNode(idFromName(name));
189  };
190 
197  void installNode(NodeId id);
198  void installNode(const std::string& name) {
199  installNode(__bn.idFromName(name));
200  }
201 
208  void installAscendants(NodeId id);
209  void installAscendants(const std::string& name) {
210  installAscendants(__bn.idFromName(name));
211  }
212 
218  void uninstallNode(NodeId id);
219  void uninstallNode(const std::string& name) {
220  uninstallNode(idFromName(name));
221  }
222 
233  void installMarginal(NodeId id, const Potential< GUM_SCALAR >& pot);
234  void installMarginal(const std::string& name,
235  const Potential< GUM_SCALAR >& pot) {
236  installMarginal(__bn.idFromName(name), pot);
237  }
238 
251  void installCPT(NodeId id, const Potential< GUM_SCALAR >& pot);
252  void installCPT(const std::string& name, const Potential< GUM_SCALAR >& pot) {
253  installCPT(__bn.idFromName(name), pot);
254  };
255 
263  void uninstallCPT(NodeId id);
264  void uninstallCPT(const std::string& name) { uninstallCPT(idFromName(name)); }
265 
272  bool checkConsistency(NodeId id) const;
273  bool checkConsistency(const std::string& name) const {
274  return checkConsistency(idFromName(name));
275  }
276 
282  bool checkConsistency() const;
283 
285 
286 
292 
295 
296  protected:
297  // remove an arc
298  void _uninstallArc(NodeId from, NodeId to);
299 
300  // add an arc
301  void _installArc(NodeId from, NodeId to);
302 
303  // install a CPT BY COPY, create or delete arcs. Checks are made in public
304  // methods In particular, it is assumed that all the variables in the pot are
305  // in the fragment
306  void _installCPT(NodeId id, const Potential< GUM_SCALAR >& pot);
307 
312  void _uninstallCPT(NodeId id);
313  };
314 
315 
316 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
317  extern template class BayesNetFragment< double >;
318 #endif
319 
320 } // namespace gum
321 
323 
324 #endif // GUM_BAYES_NET_FRAGMENT_H
virtual const Potential< GUM_SCALAR > & cpt(const std::string &name) const
Returns the CPT of a variable.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Class representing a Bayesian Network.
Definition: BayesNet.h:78
Abstract Base class for all diGraph Listener.
gum::BayesNet< GUM_SCALAR > toBN() const
create a brand new BayesNet from a fragment.
void _installArc(NodeId from, NodeId to)
NodeProperty< const Potential< GUM_SCALAR > *> __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
void installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local cpt BY COPYfor a node into the fragment.
virtual void whenNodeDeleted(const void *src, NodeId id) final
the action to take when a node has just been removed from the graph
void installNode(const std::string &name)
check if a certain NodeId exists in the fragment
virtual NodeId nodeId(const DiscreteVariable &var) const final
Return id node from discrete var pointer.
Container used to map discrete variables with nodes.
void uninstallNode(NodeId id)
uninstall a node referenced by its nodeId
virtual const DiscreteVariable & variable(const std::string &name) const final
Returns the CPT of a variable.
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
bool checkConsistency(const std::string &name) const
check if a certain NodeId exists in the fragment
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual const DiscreteVariable & variable(NodeId id) const final
Returns a constant reference over a variabe given it&#39;s node id.
Base class for discrete random variable.
void installAscendants(NodeId id)
install a node and all its ascendants
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual void whenNodeAdded(const void *src, NodeId id) final
the action to take when a new node is inserted into the graph
The class for generic Hash Tables.
Definition: hashTable.h:679
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
void installMarginal(NodeId id, const Potential< GUM_SCALAR > &pot)
install a local marginal BY COPY for a node into the fragment.
virtual const VariableNodeMap & variableNodeMap() const final
Returns a constant reference to the VariableNodeMap of this BN.
void _installCPT(NodeId id, const Potential< GUM_SCALAR > &pot)
virtual void whenArcAdded(const void *src, NodeId from, NodeId to) final
the action to take when a new arc is inserted into the graph
virtual const DiscreteVariable & variableFromName(const std::string &name) const final
Getter by name.
virtual NodeId idFromName(const std::string &name) const final
Getter by name.
Portion of a BN identified by the list of nodes and a BayesNet.
void installAscendants(const std::string &name)
check if a certain NodeId exists in the fragment
void installNode(NodeId id)
install a node referenced by its nodeId
void _uninstallCPT(NodeId id)
uninstall a local CPT.
bool isInstalledNode(NodeId id) const
check if a certain NodeId exists in the fragment
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const final
Returns the CPT of a variable.
void installMarginal(const std::string &name, const Potential< GUM_SCALAR > &pot)
check if a certain NodeId exists in the fragment
bool checkConsistency() const
returns true if all nodes in the fragment are consistent
bool isInstalledNode(const std::string &name) const
check if a certain NodeId exists in the fragment
void installCPT(const std::string &name, const Potential< GUM_SCALAR > &pot)
check if a certain NodeId exists in the fragment
void uninstallNode(const std::string &name)
check if a certain NodeId exists in the fragment
void uninstallCPT(const std::string &name)
check if a certain NodeId exists in the fragment
void uninstallCPT(NodeId id)
uninstall a local CPT.
virtual void whenArcDeleted(const void *src, NodeId from, NodeId to) final
the action to take when an arc has just been removed from the graph
Size NodeId
Type for node ids.
Definition: graphElements.h:98
virtual std::string toDot() const final
creates a dot representing the whole referred BN hilighting the fragment.
void _uninstallArc(NodeId from, NodeId to)