aGrUM  0.13.2
BayesNetFragment.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #ifndef GUM_BAYES_NET_FRAGMENT_H
28 #define GUM_BAYES_NET_FRAGMENT_H
29 
30 #include <agrum/agrum.h>
31 
32 #include <agrum/BN/IBayesNet.h>
34 
35 #include <agrum/BN/BayesNet.h>
36 
37 namespace gum {
69  template < typename GUM_SCALAR >
71  : public IBayesNet< GUM_SCALAR >
72  , public gum::DiGraphListener {
73  private:
76 
80 
81  public:
84  BayesNetFragment() = delete;
85  BayesNetFragment(const BayesNetFragment< GUM_SCALAR >& fragment) = delete;
87 
88  explicit BayesNetFragment(const IBayesNet< GUM_SCALAR >& bn);
89 
90  virtual ~BayesNetFragment();
92 
95 
97 
99  virtual void whenNodeAdded(const void* src, NodeId id) noexcept override;
100 
102 
104  virtual void whenNodeDeleted(const void* src, NodeId id) noexcept override;
105 
107 
110  virtual void
111  whenArcAdded(const void* src, NodeId from, NodeId to) noexcept override;
112 
114 
117  virtual void
118  whenArcDeleted(const void* src, NodeId from, NodeId to) noexcept override;
120 
123 
129  virtual const Potential< GUM_SCALAR >& cpt(NodeId varId) const override;
130 
134  virtual const VariableNodeMap& variableNodeMap() const override;
135 
141  virtual const DiscreteVariable& variable(NodeId id) const override;
142 
148  virtual NodeId nodeId(const DiscreteVariable& var) const override;
149 
155  virtual NodeId idFromName(const std::string& name) const override;
156 
162  virtual const DiscreteVariable&
163  variableFromName(const std::string& name) const override;
164 
169  virtual std::string toDot() const override;
170 
172 
175 
179  bool isInstalledNode(NodeId id) const noexcept;
180 
187  void installNode(NodeId id);
188 
195  void installAscendants(NodeId id);
196 
202  void uninstallNode(NodeId id) noexcept;
203 
214  void installMarginal(NodeId id, const Potential< GUM_SCALAR >* pot);
215 
230  void installCPT(NodeId id, const Potential< GUM_SCALAR >* pot);
231 
239  void uninstallCPT(NodeId id) noexcept;
240 
247  bool checkConsistency(NodeId id) const;
248 
252  bool checkConsistency() const noexcept;
253 
255 
256  using IBayesNet< GUM_SCALAR >::nodes;
257  using IBayesNet< GUM_SCALAR >::dag;
258 
259  protected:
260  // remove an arc
261  void _uninstallArc(NodeId from, NodeId to) noexcept;
262 
263  // add an arc
264  void _installArc(NodeId from, NodeId to) noexcept;
265 
266  // install a CPT, create or delete arcs. Checks are made in public methods
267  // In particular, it is assumed that all the variables in the pot are in the
268  // fragment
269  void _installCPT(NodeId id, const Potential< GUM_SCALAR >* pot) noexcept;
270 
275  void _uninstallCPT(NodeId id) noexcept;
276  };
277 
278  extern template class BayesNetFragment< float >;
279  extern template class BayesNetFragment< double >;
280 
281 } // namespace gum
282 
283 #include <agrum/BN/BayesNetFragment_tpl.h>
284 
285 #endif // GUM_BAYES_NET_FRAGMENT_H
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
virtual NodeId nodeId(const DiscreteVariable &var) const override
Return id node from discrete var pointer.
Abstract Base class for all diGraph Listener.
void uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
NodeProperty< const Potential< GUM_SCALAR > * > __localCPTs
Mapping between the variable&#39;s id and their CPT specific to this Fragment.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:61
virtual const DiscreteVariable & variableFromName(const std::string &name) const override
Getter by name.
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
void _installCPT(NodeId id, const Potential< GUM_SCALAR > *pot) noexcept
void installCPT(NodeId id, const Potential< GUM_SCALAR > *pot)
install a local cpt for a node into the fragment.
virtual const VariableNodeMap & variableNodeMap() const override
Returns a constant reference to the VariableNodeMap of this BN.
Container used to map discrete variables with nodes.
void _installArc(NodeId from, NodeId to) noexcept
virtual void whenArcAdded(const void *src, NodeId from, NodeId to) noexceptoverride
the action to take when a new arc is inserted into the graph
Class representing Bayesian networks.
Class representing Bayesian networks.
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:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Base classes for oriented graph listeners.
The class for generic Hash Tables.
Definition: hashTable.h:676
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const override
Returns the CPT of a variable.
const IBayesNet< GUM_SCALAR > & __bn
The referred BayesNet.
virtual void whenArcDeleted(const void *src, NodeId from, NodeId to) noexceptoverride
the action to take when an arc has just been removed from the graph
virtual const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variabe given it&#39;s node id.
Portion of a BN identified by the list of nodes and a BayesNet.
void installNode(NodeId id)
install a node referenced by its nodeId
virtual std::string toDot() const override
creates a dot representing the whole referred BN hilighting the fragment.
void uninstallNode(NodeId id) noexcept
uninstall a node referenced by its nodeId
bool isInstalledNode(NodeId id) const noexcept
check if a certain NodeId exists in the fragment
void installMarginal(NodeId id, const Potential< GUM_SCALAR > *pot)
install a local marginal for a node into the fragment.
bool checkConsistency() const noexcept
returns true if all nodes in the fragment are consistent
virtual NodeId idFromName(const std::string &name) const override
Getter by name.
virtual void whenNodeAdded(const void *src, NodeId id) noexceptoverride
the action to take when a new node is inserted into the graph
void _uninstallArc(NodeId from, NodeId to) noexcept
void _uninstallCPT(NodeId id) noexcept
uninstall a local CPT.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:113
virtual void whenNodeDeleted(const void *src, NodeId id) noexceptoverride
the action to take when a node has just been removed from the graph