aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
classBayesNet.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 Headers of ClassBayesNet<GUM_SCALAR>.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_CLASS_BAYESNET_H
29 #define GUM_CLASS_BAYESNET_H
30 
31 #include <list>
32 
33 #include <agrum/BN/IBayesNet.h>
34 
35 #include <agrum/PRM/PRM.h>
36 namespace gum {
37  namespace prm {
38 
39  // clang-format off
40  /**
41 * @class ClassBayesNet
42 * @headerfile classBayesNet.h <agrum/PRM/classBayesNet.h>
43  * @brief This class decorates a gum::prm::Class<GUM_SCALAR> has an
44  * IBaseBayesNet.
45  * @tparam GUM_SCALAR The type of scalar to use.
46  *
47  * This class filters PRMAttribute and PRMAggregate in a way it can be
48  * interpreted as a IBayesNet.
49  *
50  * SlotChains and PRMReferenceSlot are not represented.
51  *
52  * Remember that a ClassBayesNet<GUM_SCALAR> does not contain input nodes
53  * parents and output nodes children. Thus you should be careful when using
54  * one of the BayesNetInference over a ClassBayesNet<GUM_SCALAR> since some
55  * variables are missing in the DAG but not in the nodes CPT.
56  */
57  // clang-format on
58  template < typename GUM_SCALAR >
59  class ClassBayesNet: public IBayesNet< GUM_SCALAR > {
60  public:
61  // ========================================================================
62  /// @name Constructors & destructor.
63  // ========================================================================
64  /// @{
65 
66  /// Default constructor.
67  /// @param c The Class<GUM_SCALAR> decorated by this
68  /// ClassBayesNet<GUM_SCALAR>.
69  ClassBayesNet< GUM_SCALAR >(const PRMClass< GUM_SCALAR >& c);
70 
71  /// Copy constructor.
72  ClassBayesNet< GUM_SCALAR >(const ClassBayesNet< GUM_SCALAR >& from);
73 
74  /// Copy operator.
75  ClassBayesNet< GUM_SCALAR >&
76  operator=(const ClassBayesNet< GUM_SCALAR >& from);
77 
78  /// Destructor.
79  virtual ~ClassBayesNet< GUM_SCALAR >();
80 
81  /// @}
82  // ===========================================================================
83  /// @name Variable manipulation methods.
84  // ===========================================================================
85  /// @{
86 
87  /**
88  * @brief Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.
89  *
90  * If the node is an PRMAggregate, this will raise an OperationNotAllowed
91  * since PRMAggregate's CPT are define at instantiation only.
92  *
93  * @param varId a variable id.
94  * @return the Potential of varId.
95  *
96  * @throw NotFound raised if varId does not match any variable in this
97  * IBayesNet.
98  * @throw OperationNotAllowed raised if varId is an PRMAggregate.
99  */
100  virtual const Potential< GUM_SCALAR >& cpt(NodeId varId) const;
101 
102  /// See gum::IBaseBayesNet::variableNodeMap().
103  virtual const VariableNodeMap& variableNodeMap() const;
104 
105  /// See gum::IBaseBayesNet::variable().
106  virtual const DiscreteVariable& variable(NodeId id) const;
107 
108  /// See gum::IBaseBayesNet::nodeId().
109  virtual NodeId nodeId(const DiscreteVariable& var) const;
110 
111  /// See gum::IBaseBayesNet::idFromName().
112  virtual NodeId idFromName(const std::string& name) const;
113 
114  /// See gum::IBaseBayesNet::variableFromName().
115  virtual const DiscreteVariable&
116  variableFromName(const std::string& name) const;
117 
118  /// See gum::IBaseBayesNet::modalities().
119  const NodeProperty< Size >& modalities() const;
120 
121  /// @}
122  // ===========================================================================
123  /// @name Graphical methods
124  // ===========================================================================
125  /// @{
126  /// @return Returns a dot representation of this IBayesNet.
127  virtual std::string toDot() const;
128 
129  /// @}
130  private:
131  /// Mapping between DiscreteVariable and their NodeId
134 
135  /// Private getter with type checking in case the id is not a formal
136  /// PRMAttribute.
137  /// @throw NotFound Raised if id is not a formal attribute.
138  const PRMClassElement< GUM_SCALAR >& get__(NodeId id) const;
139 
140  /// Private getter with type checking in case the id is not a formal
141  /// PRMAttribute.
142  /// @throw NotFound Raised if id is not a formal attribute.
143  const PRMClassElement< GUM_SCALAR >& get__(const std::string& name) const;
144 
145  /// The PRMClassElementContainer decorated by this.
147 
149 
150  void init__(const PRMClass< GUM_SCALAR >& c);
151  };
152 
153 
154 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
155  extern template class ClassBayesNet< double >;
156 #endif
157 
158 
159  } /* namespace prm */
160 } /* namespace gum */
161 
162 #include <agrum/PRM/classBayesNet_tpl.h>
163 
164 #endif /* GUM_CLASS_BAYESNET_H */
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
void init__(const PRMClass< GUM_SCALAR > &c)
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
const PRMClassElement< GUM_SCALAR > & get__(const std::string &name) const
Private getter with type checking in case the id is not a formal PRMAttribute.
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
virtual NodeId nodeId(const DiscreteVariable &var) const
See gum::IBaseBayesNet::nodeId().
virtual std::string toDot() const
const PRMClassElement< GUM_SCALAR > & get__(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute.
const PRMClass< GUM_SCALAR > * class__
The PRMClassElementContainer decorated by this.
NodeProperty< Size > modalities__
ClassBayesNet< GUM_SCALAR > & operator=(const ClassBayesNet< GUM_SCALAR > &from)
Copy operator.
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
HashTable< const DiscreteVariable *, const PRMClassElement< GUM_SCALAR > *> varNodeMap__
Mapping between DiscreteVariable and their NodeId.
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.