aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
classBayesNet.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 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 >& operator=(const ClassBayesNet< GUM_SCALAR >& from);
76 
77  /// Destructor.
78  virtual ~ClassBayesNet< GUM_SCALAR >();
79 
80  /// @}
81  // ===========================================================================
82  /// @name Variable manipulation methods.
83  // ===========================================================================
84  /// @{
85 
86  /**
87  * @brief Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.
88  *
89  * If the node is an PRMAggregate, this will raise an OperationNotAllowed
90  * since PRMAggregate's CPT are define at instantiation only.
91  *
92  * @param varId a variable id.
93  * @return the Potential of varId.
94  *
95  * @throw NotFound raised if varId does not match any variable in this
96  * IBayesNet.
97  * @throw OperationNotAllowed raised if varId is an PRMAggregate.
98  */
99  virtual const Potential< GUM_SCALAR >& cpt(NodeId varId) const;
100 
101  /// See gum::IBaseBayesNet::variableNodeMap().
102  virtual const VariableNodeMap& variableNodeMap() const;
103 
104  /// See gum::IBaseBayesNet::variable().
105  virtual const DiscreteVariable& variable(NodeId id) const;
106 
107  /// See gum::IBaseBayesNet::nodeId().
108  virtual NodeId nodeId(const DiscreteVariable& var) const;
109 
110  /// See gum::IBaseBayesNet::idFromName().
111  virtual NodeId idFromName(const std::string& name) const;
112 
113  /// See gum::IBaseBayesNet::variableFromName().
114  virtual const DiscreteVariable& variableFromName(const std::string& name) const;
115 
116  /// See gum::IBaseBayesNet::modalities().
117  const NodeProperty< Size >& modalities() const;
118 
119  /// @}
120  // ===========================================================================
121  /// @name Graphical methods
122  // ===========================================================================
123  /// @{
124  /// @return Returns a dot representation of this IBayesNet.
125  virtual std::string toDot() const;
126 
127  /// @}
128  private:
129  /// Mapping between DiscreteVariable and their NodeId
131 
132  /// Private getter with type checking in case the id is not a formal
133  /// PRMAttribute.
134  /// @throw NotFound Raised if id is not a formal attribute.
135  const PRMClassElement< GUM_SCALAR >& _get_(NodeId id) const;
136 
137  /// Private getter with type checking in case the id is not a formal
138  /// PRMAttribute.
139  /// @throw NotFound Raised if id is not a formal attribute.
140  const PRMClassElement< GUM_SCALAR >& _get_(const std::string& name) const;
141 
142  /// The PRMClassElementContainer decorated by this.
144 
146 
147  void _init_(const PRMClass< GUM_SCALAR >& c);
148  };
149 
150 
151 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
152  extern template class ClassBayesNet< double >;
153 #endif
154 
155 
156  } /* namespace prm */
157 } /* namespace gum */
158 
159 #include <agrum/PRM/classBayesNet_tpl.h>
160 
161 #endif /* GUM_CLASS_BAYESNET_H */
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
void _init_(const PRMClass< GUM_SCALAR > &c)
HashTable< const DiscreteVariable *, const PRMClassElement< GUM_SCALAR > *> _varNodeMap_
Mapping between DiscreteVariable and their NodeId.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
const PRMClassElement< GUM_SCALAR > & _get_(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute.
const PRMClassElement< GUM_SCALAR > & _get_(const std::string &name) const
Private getter with type checking in case the id is not a formal PRMAttribute.
NodeProperty< Size > _modalities_
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
ClassBayesNet< GUM_SCALAR > & operator=(const ClassBayesNet< GUM_SCALAR > &from)
Copy operator.
const PRMClass< GUM_SCALAR > * _class_
The PRMClassElementContainer decorated by this.
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.