aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMInference.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 PRMInference.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_PRM_INFERENCE_H
30 #define GUM_PRM_INFERENCE_H
31 
32 #include <string>
33 
34 #include <agrum/tools/core/hashTable.h>
35 #include <agrum/tools/multidim/potential.h>
36 
37 #include <agrum/PRM/PRM.h>
38 
39 namespace gum {
40  namespace prm {
41  /**
42  * @class PRMInference
43  * @headerfile PRMInference.h <agrum/PRM/PRMInference.h>
44  * @brief This abstract class is used as base class for all inference class
45  * on PRM<GUM_SCALAR>.
46  *
47  * The main purpose of this class is to give a common interface between all
48  * inference algorithms and to handle evidences.
49  */
50  template < typename GUM_SCALAR >
51  class PRMInference {
52  public:
53  /// Code alias.
54  typedef std::pair< const PRMInstance< GUM_SCALAR >*,
55  const PRMAttribute< GUM_SCALAR >* >
57 
58  /// Code alias.
59  typedef NodeProperty< const Potential< GUM_SCALAR >* > EMap;
60 
61  /// Code alias.
62  typedef
63  typename NodeProperty< const Potential< GUM_SCALAR >* >::iterator_safe
65 
66  /// Code alias.
67  typedef typename NodeProperty<
69 
70  // ========================================================================
71  /// @name Constructor & destructor.
72  // ========================================================================
73  /// @{
74 
75  /// Default constructor.
76  PRMInference(const PRM< GUM_SCALAR >& prm,
77  const PRMSystem< GUM_SCALAR >& system);
78 
79  /// Copy constructor.
80  PRMInference(const PRMInference& source);
81 
82  /// Destructor.
83  virtual ~PRMInference();
84 
85  /// Copy operator.
86  PRMInference& operator=(const PRMInference& source);
87 
88  /// @}
89  // ========================================================================
90  /// @name Getters & setters.
91  // ========================================================================
92  /// @{
93 
94  /// Returns the name of the current inference algorithm
95  virtual std::string name() const = 0;
96 
97  /// @}
98  // ========================================================================
99  // ========================================================================
100  /// @name Query methods.
101  // ========================================================================
102  /// @{
103 
104  /**
105  * Compute the posterior of the formal attribute pointed by chain and
106  * stores it in m.
107  *
108  * @param chain A string of the form instance.attribute.
109  * @param m An empty CPF which will be filed by the posterior of chain.
110  * @throw NotFound Raised if chain is invalid.
111  * @throw TypeError Raised if chain does not point to an
112  *PRMAttribute<GUM_SCALAR>.
113  * @throw OperationNotAllowed Raise if m is not empty.
114  */
115  void posterior(const Chain& chain, Potential< GUM_SCALAR >& m);
116 
117  /**
118  * Compute the joint probability of the formals attributes pointed by
119  * chains and stores it in m.
120  *
121  * @param chains A Set of strings of the form instance.attribute.
122  * @param j An empty CPF which will be filed by the joint probability
123  * over chains.
124  * @throw NotFound Raised if some chain in chains does not point to a
125  * formal attribute.
126  * @throw OperationNotAllowed Raise if m is not empty.
127  */
128  void joint(const std::vector< Chain >& chains, Potential< GUM_SCALAR >& j);
129 
130  /// @}
131  // ========================================================================
132  /// @name Evidence handling.
133  // ========================================================================
134  /// @{
135 
136  /// Returns EMap of evidences over i.
137  /// @throw NotFound if i has no evidence.
138  EMap& evidence(const PRMInstance< GUM_SCALAR >& i);
139 
140  /// Returns EMap of evidences over i.
141  /// @throw NotFound if i has no evidence.
142  EMap& evidence(const PRMInstance< GUM_SCALAR >* i);
143 
144  /// Returns EMap of evidences over i.
145  /// @throw NotFound if i has no evidence.
146  const EMap& evidence(const PRMInstance< GUM_SCALAR >& i) const;
147 
148  /// Returns EMap of evidences over i.
149  /// @throw NotFound if i has no evidence.
150  const EMap& evidence(const PRMInstance< GUM_SCALAR >* i) const;
151 
152  /// Returns true if i has evidence.
153  bool hasEvidence(const PRMInstance< GUM_SCALAR >& i) const;
154 
155  /// Returns EMap of evidences over i.
156  bool hasEvidence(const PRMInstance< GUM_SCALAR >* i) const;
157 
158  /// Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
159  bool hasEvidence(const Chain& chain) const;
160 
161  /// Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
162  bool hasEvidence() const;
163 
164  /// Add an evidence to the given instance's elt.
165  /// @param chain The variable being observed.
166  /// @param p The Potential added (by copy) as evidence.
167  ///
168  /// @throw NotFound Raised if elt does not belong to i.
169  /// @throw OperationNotAllowed Raised if p is inconsistent with elt.
170  void addEvidence(const Chain& chain, const Potential< GUM_SCALAR >& p);
171 
172  /// Remove evidence on the given instance's elt.
173  /// @param chain The variable being observed.
174  ///
175  /// @throw NotFound Raised if the given names are not found.
176  /// @throw TypeError Raised if the elt is not an PRMAttribute<GUM_SCALAR>.
177  void removeEvidence(const Chain& chain);
178 
179  /// Remove all evidences.
180  void clearEvidence();
181 
182  /// @}
183  protected:
184  // ========================================================================
185  /// @name Protected members.
186  // ========================================================================
187  /// @{
188 
189  /// This method is called whenever an evidence is added, but AFTER
190  /// any processing made by PRMInference.
191  virtual void evidenceAdded_(const Chain& chain) = 0;
192 
193  /// This method is called whenever an evidence is removed, but BEFORE
194  /// any processing made by PRMInference.
195  virtual void evidenceRemoved_(const Chain& chain) = 0;
196 
197  /// @brief Generic method to compute the posterior of given element.
198  /// @param chain
199  /// @param m CPF filled with the posterior of elt. It is initialized
200  /// properly.
201  virtual void posterior_(const Chain& chain, Potential< GUM_SCALAR >& m) = 0;
202 
203  /// @brief Generic method to compute the posterior of given element.
204  /// @param queries Set of pairs of PRMInstance<GUM_SCALAR> and
205  /// PRMAttribute<GUM_SCALAR>.
206  /// @param j CPF filled with the joint probability of queries. It is
207  /// initialized properly.
208  virtual void joint_(const std::vector< Chain >& queries,
209  Potential< GUM_SCALAR >& j)
210  = 0;
211 
212  /// The PRM<GUM_SCALAR> on which inference is done.
213  PRM< GUM_SCALAR > const* prm_;
214 
215  /// The Model on which inference is done.
217 
218  /// @}
219 
220  private:
221  // ========================================================================
222  /// @name Private evidence handling methods and members.
223  // ========================================================================
224  /// @{
225 
226  /// Code alias.
227  typedef typename HashTable< const PRMInstance< GUM_SCALAR >*,
229  /// Code alias.
230  typedef
231  typename HashTable< const PRMInstance< GUM_SCALAR >*,
233 
234  /// Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
236 
237  /// Private getter over evidences__, if necessary creates an EMap for i.
238  EMap& EMap__(const PRMInstance< GUM_SCALAR >* i);
239 
240  /// @}
241  };
242 
243 
244 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
245  extern template class PRMInference< double >;
246 #endif
247 
248 
249  } /* namespace prm */
250 } /* namespace gum */
251 
252 #include <agrum/PRM/inference/PRMInference_tpl.h>
253 
254 #endif /* GUM_PRM_INFERENCE_H */
PRMInference(const PRMInference &source)
Copy constructor.
void posterior(const Chain &chain, Potential< GUM_SCALAR > &m)
Compute the posterior of the formal attribute pointed by chain and stores it in m.
bool hasEvidence(const Chain &chain) const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> evidences__
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235
virtual void evidenceAdded_(const Chain &chain)=0
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference...
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
const EMap & evidence(const PRMInstance< GUM_SCALAR > &i) const
Returns EMap of evidences over i.
EMap & evidence(const PRMInstance< GUM_SCALAR > *i)
Returns EMap of evidences over i.
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
bool hasEvidence(const PRMInstance< GUM_SCALAR > *i) const
Returns EMap of evidences over i.
void clearEvidence()
Remove all evidences.
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.
const EMap & evidence(const PRMInstance< GUM_SCALAR > *i) const
Returns EMap of evidences over i.
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
EMap & EMap__(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences__, if necessary creates an EMap for i.
void joint(const std::vector< Chain > &chains, Potential< GUM_SCALAR > &j)
Compute the joint probability of the formals attributes pointed by chains and stores it in m...
void addEvidence(const Chain &chain, const Potential< GUM_SCALAR > &p)
Add an evidence to the given instance&#39;s elt.
virtual std::string name() const =0
Returns the name of the current inference algorithm.
NodeProperty< const Potential< GUM_SCALAR > *> EMap
Code alias.
Definition: PRMInference.h:59
void removeEvidence(const Chain &chain)
Remove evidence on the given instance&#39;s elt.
virtual ~PRMInference()
Destructor.
PRMInference & operator=(const PRMInference &source)
Copy operator.