aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuredInference.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 StructuredInference.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_STRUCTURED_INFERENCE_H
30 #define GUM_STRUCTURED_INFERENCE_H
31 
32 #include <string>
33 
34 #include <agrum/tools/core/timer.h>
35 
36 #include <agrum/BN/inference/variableElimination.h>
37 
38 #include <agrum/tools/multidim/ICIModels/multiDimNoisyORCompound.h>
39 #include <agrum/tools/multidim/ICIModels/multiDimNoisyORNet.h>
40 #include <agrum/tools/multidim/implementations/multiDimSparse.h>
41 #include <agrum/tools/multidim/potential.h>
42 #include <agrum/tools/multidim/utils/operators/multiDimCombinationDefault.h>
43 #include <agrum/tools/multidim/utils/operators/projections4MultiDim.h>
44 
45 #include <agrum/tools/graphs/algorithms/triangulations/partialOrderedTriangulation.h>
46 
47 #include <agrum/PRM/PRM.h>
48 #include <agrum/PRM/inference/PRMInference.h>
49 #include <agrum/PRM/inference/gspan.h>
50 
51 namespace gum {
52  namespace prm {
53 
54  /**
55  * @class StructuredInference structuredInference.h
56  *<agrum/PRM/structuredInference.h>
57  *
58  * This PRM<GUM_SCALAR> inference algorithm exploits the GSpan<GUM_SCALAR>
59  *algorithm to discover new
60  * patters and exploit them in a structured way.
61  */
62  template < typename GUM_SCALAR >
64  public:
65  // ========================================================================
66  /// @name Constructor & destructor.
67  // ========================================================================
68  /// @{
69 
70  /// Default constructor.
71  StructuredInference(const PRM< GUM_SCALAR >& prm,
72  const PRMSystem< GUM_SCALAR >& system,
73  gspan::SearchStrategy< GUM_SCALAR >* strategy = 0);
74 
75  /// Copy constructor.
77 
78  /// Destructor.
79  virtual ~StructuredInference();
80 
81  /// Copy operator.
83 
84  /// @}
85  // ========================================================================
86  /// @name Getters and setters.
87  // ========================================================================
88  /// @{
89 
90  /// Tells this algorithm to use pattern mining or not.
91  void setPatternMining(bool b);
92 
93  virtual std::string name() const;
94 
95  /// Returns the instance of gspan used to search patterns.
96  GSpan< GUM_SCALAR >& gspan();
97 
98  /// Returns the instance of gspan used to search patterns.
99  const GSpan< GUM_SCALAR >& gspan() const;
100 
101  /// Search for patterns without doing any computations.
102  void searchPatterns();
103 
104  /// @}
105  protected:
106  // ========================================================================
107  /// @name Protected members.
108  // ========================================================================
109  /// @{
110 
111  /// See PRMInference::evidenceAdded_().
112  virtual void evidenceAdded_(const typename PRMInference< GUM_SCALAR >::Chain& chain);
113 
114  /// See PRMInference::evidenceRemoved_().
115  virtual void evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain& chain);
116 
117  /// See PRMInference::posterior_().
118  virtual void posterior_(const typename PRMInference< GUM_SCALAR >::Chain& chain,
119  Potential< GUM_SCALAR >& m);
120 
121  /// See PRMInference::joint_().
122  virtual void joint_(const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
123  Potential< GUM_SCALAR >& j);
124 
125  /// @}
126  private:
127  /// Private structure to represent data about a reduced graph.
128  struct RGData {
129  /// The reduced graph.
131  /// Mapping between NodeId and modalities.
133  /// Mapping between DiscreteVariable and NodeId.
135  /// The pool of potentials matching the reduced graph
137  /// Partial order used for triangulation, first is outputs nodes, second
138  /// query nodes.
140  /// Default constructor
141  RGData();
142  /// Destructor.
143  ~RGData();
144  /// Returns the set of outputs nodes (which will be eliminated).
145  inline NodeSet& outputs() { return partial_order[0]; }
146  /// Returns the set of query nodes (which will not be eliminated).
147  inline NodeSet& queries() { return partial_order[1]; }
148  };
149 
150  /// Private structure to represent data about a pattern.
151  struct PData {
152  /// The pattern for which this represents data about it
154  /// A reference over the usable matches of pattern
155  typename GSpan< GUM_SCALAR >::MatchedInstances& matches;
156  /// A yet to be triangulated undigraph
158  /// The pattern's variables modalities
160  /// A bijection to easily keep track between graph and attributes, its
161  /// of
162  /// the
163  /// form instance_name DOT attr_name
165  /// To ease translating potentials from one match to another
167  /// Bijection between graph's nodes and their corresponding
168  /// DiscreteVariable,
169  /// for
170  /// inference purpose
172  /// To handle barren nodes
174  /// Set of barren nodes
176  /// Default constructor.
177  PData(const gspan::Pattern& p, typename GSpan< GUM_SCALAR >::MatchedInstances& m);
178  /// Copy constructor.
179  PData(const PData& source);
180  /// Destructor
181  ~PData();
182  /// Returns the set of inner nodes
183  inline NodeSet& inners() { return _partial_order_[0]; }
184  /// Returns the set of inner and observed nodes given all the matches of
185  /// pattern
186  inline NodeSet& obs() { return _partial_order_[1]; }
187  /// Returns the set of outputs nodes given all the matches of pattern
188  inline NodeSet& outputs() { return _partial_order_[2]; }
189  /// Returns the set of queried nodes given all the matches of pattern
190  inline NodeSet& queries() { return _partial_order_[3]; }
191  // We use the first match for computations
192  // inline const Sequence<PRMInstance<GUM_SCALAR>*>& match() const {
193  // return
194  // **(matches.begin());}
195  // Remove any empty set in partial_order
196  const List< NodeSet >* partial_order();
197 
198  private:
199  /// We'll use a PartialOrderedTriangulation with three sets: output,
200  /// nodes
201  /// and obs
202  /// with children outside the pattern and the other nodes
204  /// A copy of _partial_order_ without empty sets
206  };
207 
208  /// Private structure to represent data about a Class<GUM_SCALAR>.
209  struct CData {
210  /// The class about what this data is about.
211  const PRMClass< GUM_SCALAR >& c;
212  /// The class moral graph. NodeId matches those in c.
214  /// The class variables modalities.
216  /// The partial order used of variable elimination.
218  /// The Set of Instances reduces at class level.
220  /// The potential pool obtained by C elimination of inner nodes.
222  /// Default constructor.
223  CData(const PRMClass< GUM_SCALAR >& c);
224  /// Destructor.
225  ~CData();
226  /// Returns the set of inner nodes.
227  inline NodeSet& inners() { return _inners_; }
228  /// Returns the set of aggregators and their parents.
229  inline NodeSet& aggregators() { return _aggregators_; }
230  /// Returns the set of outputs nodes.
231  inline NodeSet& outputs() { return _outputs_; }
232  /// The elimination order for nodes of this class
233  inline std::vector< NodeId >& elim_order() { return _elim_order_; }
234 
235  private:
241  };
242 
243  /// Pointer over th GSpan<GUM_SCALAR> instance used by this class.
245 
246  /// Mapping between a Pattern's match and its potential pool after inner
247  /// variables
248  /// were eliminated.
251 
252  /// Mapping between a Class<GUM_SCALAR> and data about instances reduced
253  /// using
254  /// only Class<GUM_SCALAR> level
255  /// information.
257 
258  /// Keeping track of create potentials to delete them after inference.
260 
262 
263  /// This keeps track of reduced instances.
265 
266  /// The query
267  typename PRMInference< GUM_SCALAR >::Chain _query_;
268 
269  /// The pattern data of the pattern which one of its matches contains
270  /// the query
272 
273  /// Flag which tells to use pattern mining or not
274  bool _mining_;
275 
276  /// Flag with an explicit name
279 
280  /// This calls _reducePattern_() over each pattern and then build the
281  /// reduced
282  /// graph
283  /// which is used for inference.
284  /// The reduce graph is a triangulated instance graph.
285  void _buildReduceGraph_(RGData& data);
286 
287  /// Add the nodes in the reduced graph.
288  // MSVC void _addNodesInReducedGraph_( RGData& data );
289 
290  /// Add edges in the reduced graph.
291  void _addEdgesInReducedGraph_(RGData& data);
292 
293  void _removeNode_(typename StructuredInference::PData& data,
294  NodeId id,
295  Set< Potential< GUM_SCALAR >* >& pool);
296 
297  /// Add the reduced potentials of instances not in any used patterns.
298  void _reduceAloneInstances_(RGData& data);
299 
300  /// Proceed with the elimination of all inner variables (observed or not)
301  /// of
302  /// all
303  /// usable matches of Pattern p.
304  /// Inner variables which are part of the query are not eliminated.
305  void _reducePattern_(const gspan::Pattern* p);
306 
307  /// Build the DAG corresponding to Pattern data.pattern, initialize pool
308  /// with
309  /// all the Potentials of all variables in data.pattern. The first match
310  /// of
311  /// data.pattern (aka data.match) is used.
312  void _buildPatternGraph_(PData& data,
313  Set< Potential< GUM_SCALAR >* >& pool,
314  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
315 
317  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
320  NodeId id,
321  std::pair< Idx, std::string >& v);
322 
323  bool _allInstanceNoRefAttr_(typename StructuredInference::PData& data,
324  std::pair< Idx, std::string > attr);
325 
326  void _removeBarrenNodes_(typename StructuredInference::PData& data,
327  Set< Potential< GUM_SCALAR >* >& pool);
328 
329  /// Add in data.queries() any queried variable in one of data.pattern
330  /// matches.
331  // MVSC void _buildQuerySet_( PData& data );
332 
333  /// Proceeds with the elimination of observed variables in math and then
334  /// call _translatePotSet_().
335  Set< Potential< GUM_SCALAR >* >*
337  const Set< Potential< GUM_SCALAR >* >& pool,
338  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
339  const std::vector< NodeId >& elim_order);
340 
341  Set< Potential< GUM_SCALAR >* >*
343  const Set< Potential< GUM_SCALAR >* >& pool,
344  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
345  const std::vector< NodeId >& elim_order);
346 
347  /// Translate a given Potential Set into one w.r.t. variables in match.
348  Set< Potential< GUM_SCALAR >* >*
350  const Set< Potential< GUM_SCALAR >* >& pool,
351  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
352 
353  /// Unreduce the match containing the query.
354  // MVSC void _unreduceMatchWithQuery_();
355 
356  // MVSC std::vector<NodeId>* _getClassOutputs_( const
357  // PRMClass<GUM_SCALAR>* c );
358  /// Used to create strings
359  std::string _dot_;
360  std::string _str_(const PRMInstance< GUM_SCALAR >* i,
361  const PRMAttribute< GUM_SCALAR >* a) const;
362  std::string _str_(const PRMInstance< GUM_SCALAR >* i,
363  const PRMAttribute< GUM_SCALAR >& a) const;
364  std::string _str_(const PRMInstance< GUM_SCALAR >* i,
365  const PRMSlotChain< GUM_SCALAR >& a) const;
366 
367  public:
368  // For bench/debug purpose.
371  double triang_time;
372  double mining_time;
373  double pattern_time;
374  double inner_time;
375  double obs_time;
376  double full_time;
377  std::string info() const;
378  };
379 
380 
381 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
382  extern template class StructuredInference< double >;
383 #endif
384 
385 
386  } /* namespace prm */
387 } /* namespace gum */
388 
389 #include <agrum/PRM/inference/structuredInference_tpl.h>
390 
391 #endif /* GUM_STRUCTURED_INFERENCE_H */
List< NodeSet > partial_order
The partial order used of variable elimination.
NodeProperty< Size > mods
The class variables modalities.
UndiGraph reducedGraph
The reduced graph.
void _addEdgesInReducedGraph_(RGData &data)
Add the nodes in the reduced graph.
void _buildPatternGraph_(PData &data, Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
Build the DAG corresponding to Pattern data.pattern, initialize pool with all the Potentials of all v...
PData(const PData &source)
Copy constructor.
const GSpan< GUM_SCALAR > & gspan() const
Returns the instance of gspan used to search patterns.
PData(const gspan::Pattern &p, typename GSpan< GUM_SCALAR >::MatchedInstances &m)
Default constructor.
PData * _pdata_
The pattern data of the pattern which one of its matches contains the query.
NodeSet & queries()
Returns the set of queried nodes given all the matches of pattern.
List< NodeSet > partial_order
Partial order used for triangulation, first is outputs nodes, second query nodes. ...
GSpan< GUM_SCALAR > * _gspan_
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
std::string _str_(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
List< NodeSet > * _real_order_
A copy of partial_order without empty sets.
virtual void evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain &chain)
See PRMInference::evidenceRemoved_().
List< NodeSet > _partial_order_
We&#39;ll use a PartialOrderedTriangulation with three sets: output, nodes and obs with children outside ...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
UndiGraph moral_graph
The class moral graph. NodeId matches those in c.
NodeSet & outputs()
Returns the set of outputs nodes given all the matches of pattern.
NodeProperty< std::pair< Idx, std::string > > map
To ease translating potentials from one match to another.
Set< const PRMInstance< GUM_SCALAR > *> instances
The Set of Instances reduces at class level.
Bijection< const DiscreteVariable *, NodeId > var2node
Mapping between DiscreteVariable and NodeId.
std::string _str_(const PRMInstance< GUM_SCALAR > *i, const PRMSlotChain< GUM_SCALAR > &a) const
void _removeBarrenNodes_(typename StructuredInference::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
void _buildReduceGraph_(RGData &data)
This calls reducePattern() over each pattern and then build the reduced graph which is used for infer...
Set< const PRMInstance< GUM_SCALAR > *> _reducedInstances_
This keeps track of reduced instances.
UndiGraph graph
A yet to be triangulated undigraph.
NodeSet & obs()
Returns the set of inner and observed nodes given all the matches of pattern.
std::string _dot_
Unreduce the match containing the query.
NodeSet & outputs()
Returns the set of outputs nodes.
NodeSet & queries()
Returns the set of query nodes (which will not be eliminated).
StructuredInference & operator=(const StructuredInference &source)
Copy operator.
NodeProperty< Potential< GUM_SCALAR > *> pots
To handle barren nodes.
void _removeNode_(typename StructuredInference::PData &data, NodeId id, Set< Potential< GUM_SCALAR > * > &pool)
NodeSet & outputs()
Returns the set of outputs nodes (which will be eliminated).
virtual void posterior_(const typename PRMInference< GUM_SCALAR >::Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference::posterior_().
void _reducePattern_(const gspan::Pattern *p)
Proceed with the elimination of all inner variables (observed or not) of all usable matches of Patter...
bool _allInstanceNoRefAttr_(typename StructuredInference::PData &data, std::pair< Idx, std::string > attr)
Bijection< NodeId, const DiscreteVariable *> vars
Bijection between graph&#39;s nodes and their corresponding DiscreteVariable, for inference purpose...
void _insertNodeInElimLists_(typename StructuredInference::PData &data, const Sequence< PRMInstance< GUM_SCALAR > * > &match, PRMInstance< GUM_SCALAR > *inst, PRMAttribute< GUM_SCALAR > *attr, NodeId id, std::pair< Idx, std::string > &v)
Set< Potential< GUM_SCALAR > *> _trash_
StructuredInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.
virtual void joint_(const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference::joint_().
std::pair< Idx, std::string > _query_data_
Set< Potential< GUM_SCALAR > *> * _translatePotSet_(typename StructuredInference::PData &data, const Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match)
Translate a given Potential Set into one w.r.t. variables in match.
Set< NodeId > barren
Set of barren nodes.
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
virtual std::string name() const
Tells this algorithm to use pattern mining or not.
bool _mining_
Flag which tells to use pattern mining or not.
StructuredInference(const StructuredInference &source)
Copy constructor.
NodeSet & inners()
Returns the set of inner nodes.
virtual void evidenceAdded_(const typename PRMInference< GUM_SCALAR >::Chain &chain)
See PRMInference::evidenceAdded_().
NodeProperty< Size > mod
The pattern&#39;s variables modalities.
NodeSet & aggregators()
Returns the set of aggregators and their parents.
std::vector< NodeId > & elim_order()
The elimination order for nodes of this class.
virtual ~StructuredInference()
Destructor.
HashTable< const PRMClass< GUM_SCALAR > *, CData *> _cdata_map_
Mapping between a Class<GUM_SCALAR> and data about instances reduced using only Class<GUM_SCALAR> lev...
CData(const PRMClass< GUM_SCALAR > &c)
Default constructor.
NodeSet & inners()
Returns the set of inner nodes.
GSpan< GUM_SCALAR > & gspan()
Returns the instance of gspan used to search patterns.
Bijection< NodeId, std::string > node2attr
A bijection to easily keep track between graph and attributes, its of the form instance_name DOT attr...
Set< Potential< GUM_SCALAR > *> pool
The pool of potentials matching the reduced graph.
void setPatternMining(bool b)
Tells this algorithm to use pattern mining or not.
void _reduceAloneInstances_(RGData &data)
Add the reduced potentials of instances not in any used patterns.
bool _found_query_
Flag with an explicit name.
NodeProperty< Size > mods
Mapping between NodeId and modalities.
const PRMClass< GUM_SCALAR > & c
The class about what this data is about.
void searchPatterns()
Search for patterns without doing any computations.