aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMInference.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 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 >*, const PRMAttribute< GUM_SCALAR >* >
56 
57  /// Code alias.
58  typedef NodeProperty< const Potential< GUM_SCALAR >* > EMap;
59 
60  /// Code alias.
61  typedef typename NodeProperty< const Potential< GUM_SCALAR >* >::iterator_safe EMapIterator;
62 
63  /// Code alias.
64  typedef typename NodeProperty< const Potential< GUM_SCALAR >* >::const_iterator_safe
66 
67  // ========================================================================
68  /// @name Constructor & destructor.
69  // ========================================================================
70  /// @{
71 
72  /// Default constructor.
73  PRMInference(const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& system);
74 
75  /// Copy constructor.
76  PRMInference(const PRMInference& source);
77 
78  /// Destructor.
79  virtual ~PRMInference();
80 
81  /// Copy operator.
82  PRMInference& operator=(const PRMInference& source);
83 
84  /// @}
85  // ========================================================================
86  /// @name Getters & setters.
87  // ========================================================================
88  /// @{
89 
90  /// Returns the name of the current inference algorithm
91  virtual std::string name() const = 0;
92 
93  /// @}
94  // ========================================================================
95  // ========================================================================
96  /// @name Query methods.
97  // ========================================================================
98  /// @{
99 
100  /**
101  * Compute the posterior of the formal attribute pointed by chain and
102  * stores it in m.
103  *
104  * @param chain A string of the form instance.attribute.
105  * @param m An empty CPF which will be filed by the posterior of chain.
106  * @throw NotFound Raised if chain is invalid.
107  * @throw TypeError Raised if chain does not point to an
108  *PRMAttribute<GUM_SCALAR>.
109  * @throw OperationNotAllowed Raise if m is not empty.
110  */
111  void posterior(const Chain& chain, Potential< GUM_SCALAR >& m);
112 
113  /**
114  * Compute the joint probability of the formals attributes pointed by
115  * chains and stores it in m.
116  *
117  * @param chains A Set of strings of the form instance.attribute.
118  * @param j An empty CPF which will be filed by the joint probability
119  * over chains.
120  * @throw NotFound Raised if some chain in chains does not point to a
121  * formal attribute.
122  * @throw OperationNotAllowed Raise if m is not empty.
123  */
124  void joint(const std::vector< Chain >& chains, Potential< GUM_SCALAR >& j);
125 
126  /// @}
127  // ========================================================================
128  /// @name Evidence handling.
129  // ========================================================================
130  /// @{
131 
132  /// Returns EMap of evidences over i.
133  /// @throw NotFound if i has no evidence.
134  EMap& evidence(const PRMInstance< GUM_SCALAR >& i);
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  const EMap& evidence(const PRMInstance< GUM_SCALAR >& i) const;
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 true if i has evidence.
149  bool hasEvidence(const PRMInstance< GUM_SCALAR >& i) const;
150 
151  /// Returns EMap of evidences over i.
152  bool hasEvidence(const PRMInstance< GUM_SCALAR >* i) const;
153 
154  /// Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
155  bool hasEvidence(const Chain& chain) const;
156 
157  /// Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
158  bool hasEvidence() const;
159 
160  /// Add an evidence to the given instance's elt.
161  /// @param chain The variable being observed.
162  /// @param p The Potential added (by copy) as evidence.
163  ///
164  /// @throw NotFound Raised if elt does not belong to i.
165  /// @throw OperationNotAllowed Raised if p is inconsistent with elt.
166  void addEvidence(const Chain& chain, const Potential< GUM_SCALAR >& p);
167 
168  /// Remove evidence on the given instance's elt.
169  /// @param chain The variable being observed.
170  ///
171  /// @throw NotFound Raised if the given names are not found.
172  /// @throw TypeError Raised if the elt is not an PRMAttribute<GUM_SCALAR>.
173  void removeEvidence(const Chain& chain);
174 
175  /// Remove all evidences.
176  void clearEvidence();
177 
178  /// @}
179  protected:
180  // ========================================================================
181  /// @name Protected members.
182  // ========================================================================
183  /// @{
184 
185  /// This method is called whenever an evidence is added, but AFTER
186  /// any processing made by PRMInference.
187  virtual void evidenceAdded_(const Chain& chain) = 0;
188 
189  /// This method is called whenever an evidence is removed, but BEFORE
190  /// any processing made by PRMInference.
191  virtual void evidenceRemoved_(const Chain& chain) = 0;
192 
193  /// @brief Generic method to compute the posterior of given element.
194  /// @param chain
195  /// @param m CPF filled with the posterior of elt. It is initialized
196  /// properly.
197  virtual void posterior_(const Chain& chain, Potential< GUM_SCALAR >& m) = 0;
198 
199  /// @brief Generic method to compute the posterior of given element.
200  /// @param queries Set of pairs of PRMInstance<GUM_SCALAR> and
201  /// PRMAttribute<GUM_SCALAR>.
202  /// @param j CPF filled with the joint probability of queries. It is
203  /// initialized properly.
204  virtual void joint_(const std::vector< Chain >& queries, Potential< GUM_SCALAR >& j) = 0;
205 
206  /// The PRM<GUM_SCALAR> on which inference is done.
207  PRM< GUM_SCALAR > const* prm_;
208 
209  /// The Model on which inference is done.
211 
212  /// @}
213 
214  private:
215  // ========================================================================
216  /// @name Private evidence handling methods and members.
217  // ========================================================================
218  /// @{
219 
220  /// Code alias.
221  typedef typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::iterator_safe
223  /// Code alias.
224  typedef typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::const_iterator_safe
226 
227  /// Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
229 
230  /// Private getter over _evidences_, if necessary creates an EMap for i.
231  EMap& _EMap_(const PRMInstance< GUM_SCALAR >* i);
232 
233  /// @}
234  };
235 
236 
237 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
238  extern template class PRMInference< double >;
239 #endif
240 
241 
242  } /* namespace prm */
243 } /* namespace gum */
244 
245 #include <agrum/PRM/inference/PRMInference_tpl.h>
246 
247 #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 & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
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.
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:58
void removeEvidence(const Chain &chain)
Remove evidence on the given instance&#39;s elt.
virtual ~PRMInference()
Destructor.
PRMInference & operator=(const PRMInference &source)
Copy operator.