aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuredInference.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 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
113  evidenceAdded_(const typename PRMInference< GUM_SCALAR >::Chain& chain);
114 
115  /// See PRMInference::evidenceRemoved_().
116  virtual void
117  evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain& chain);
118 
119  /// See PRMInference::posterior_().
120  virtual void
121  posterior_(const typename PRMInference< GUM_SCALAR >::Chain& chain,
122  Potential< GUM_SCALAR >& m);
123 
124  /// See PRMInference::joint_().
125  virtual void joint_(
126  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
127  Potential< GUM_SCALAR >& j);
128 
129  /// @}
130  private:
131  /// Private structure to represent data about a reduced graph.
132  struct RGData {
133  /// The reduced graph.
135  /// Mapping between NodeId and modalities.
137  /// Mapping between DiscreteVariable and NodeId.
139  /// The pool of potentials matching the reduced graph
141  /// Partial order used for triangulation, first is outputs nodes, second
142  /// query nodes.
144  /// Default constructor
145  RGData();
146  /// Destructor.
147  ~RGData();
148  /// Returns the set of outputs nodes (which will be eliminated).
149  inline NodeSet& outputs() { return partial_order[0]; }
150  /// Returns the set of query nodes (which will not be eliminated).
151  inline NodeSet& queries() { return partial_order[1]; }
152  };
153 
154  /// Private structure to represent data about a pattern.
155  struct PData {
156  /// The pattern for which this represents data about it
158  /// A reference over the usable matches of pattern
159  typename GSpan< GUM_SCALAR >::MatchedInstances& matches;
160  /// A yet to be triangulated undigraph
162  /// The pattern's variables modalities
164  /// A bijection to easily keep track between graph and attributes, its
165  /// of
166  /// the
167  /// form instance_name DOT attr_name
169  /// To ease translating potentials from one match to another
171  /// Bijection between graph's nodes and their corresponding
172  /// DiscreteVariable,
173  /// for
174  /// inference purpose
176  /// To handle barren nodes
178  /// Set of barren nodes
180  /// Default constructor.
181  PData(const gspan::Pattern& p,
182  typename GSpan< GUM_SCALAR >::MatchedInstances& m);
183  /// Copy constructor.
184  PData(const PData& source);
185  /// Destructor
186  ~PData();
187  /// Returns the set of inner nodes
188  inline NodeSet& inners() { return partial_order__[0]; }
189  /// Returns the set of inner and observed nodes given all the matches of
190  /// pattern
191  inline NodeSet& obs() { return partial_order__[1]; }
192  /// Returns the set of outputs nodes given all the matches of pattern
193  inline NodeSet& outputs() { return partial_order__[2]; }
194  /// Returns the set of queried nodes given all the matches of pattern
195  inline NodeSet& queries() { return partial_order__[3]; }
196  // We use the first match for computations
197  // inline const Sequence<PRMInstance<GUM_SCALAR>*>& match() const {
198  // return
199  // **(matches.begin());}
200  // Remove any empty set in partial_order
201  const List< NodeSet >* partial_order();
202 
203  private:
204  /// We'll use a PartialOrderedTriangulation with three sets: output,
205  /// nodes
206  /// and obs
207  /// with children outside the pattern and the other nodes
209  /// A copy of partial_order__ without empty sets
211  };
212 
213  /// Private structure to represent data about a Class<GUM_SCALAR>.
214  struct CData {
215  /// The class about what this data is about.
216  const PRMClass< GUM_SCALAR >& c;
217  /// The class moral graph. NodeId matches those in c.
219  /// The class variables modalities.
221  /// The partial order used of variable elimination.
223  /// The Set of Instances reduces at class level.
225  /// The potential pool obtained by C elimination of inner nodes.
227  /// Default constructor.
228  CData(const PRMClass< GUM_SCALAR >& c);
229  /// Destructor.
230  ~CData();
231  /// Returns the set of inner nodes.
232  inline NodeSet& inners() { return inners__; }
233  /// Returns the set of aggregators and their parents.
234  inline NodeSet& aggregators() { return aggregators__; }
235  /// Returns the set of outputs nodes.
236  inline NodeSet& outputs() { return outputs__; }
237  /// The elimination order for nodes of this class
238  inline std::vector< NodeId >& elim_order() { return elim_order__; }
239 
240  private:
246  };
247 
248  /// Pointer over th GSpan<GUM_SCALAR> instance used by this class.
250 
251  /// Mapping between a Pattern's match and its potential pool after inner
252  /// variables
253  /// were eliminated.
254  HashTable< const Sequence< PRMInstance< GUM_SCALAR >* >*,
255  Set< Potential< GUM_SCALAR >* >* >
257 
258  /// Mapping between a Class<GUM_SCALAR> and data about instances reduced
259  /// using
260  /// only Class<GUM_SCALAR> level
261  /// information.
263 
264  /// Keeping track of create potentials to delete them after inference.
266 
268 
269  /// This keeps track of reduced instances.
271 
272  /// The query
273  typename PRMInference< GUM_SCALAR >::Chain query__;
274 
275  /// The pattern data of the pattern which one of its matches contains
276  /// the query
278 
279  /// Flag which tells to use pattern mining or not
280  bool mining__;
281 
282  /// Flag with an explicit name
285 
286  /// This calls reducePattern__() over each pattern and then build the
287  /// reduced
288  /// graph
289  /// which is used for inference.
290  /// The reduce graph is a triangulated instance graph.
291  void buildReduceGraph__(RGData& data);
292 
293  /// Add the nodes in the reduced graph.
294  // MSVC void addNodesInReducedGraph__( RGData& data );
295 
296  /// Add edges in the reduced graph.
297  void addEdgesInReducedGraph__(RGData& data);
298 
299  void removeNode__(typename StructuredInference::PData& data,
300  NodeId id,
301  Set< Potential< GUM_SCALAR >* >& pool);
302 
303  /// Add the reduced potentials of instances not in any used patterns.
304  void reduceAloneInstances__(RGData& data);
305 
306  /// Proceed with the elimination of all inner variables (observed or not)
307  /// of
308  /// all
309  /// usable matches of Pattern p.
310  /// Inner variables which are part of the query are not eliminated.
311  void reducePattern__(const gspan::Pattern* p);
312 
313  /// Build the DAG corresponding to Pattern data.pattern, initialize pool
314  /// with
315  /// all the Potentials of all variables in data.pattern. The first match
316  /// of
317  /// data.pattern (aka data.match) is used.
318  void
320  Set< Potential< GUM_SCALAR >* >& pool,
321  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
322 
324  typename StructuredInference::PData& data,
325  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
328  NodeId id,
329  std::pair< Idx, std::string >& v);
330 
331  bool allInstanceNoRefAttr__(typename StructuredInference::PData& data,
332  std::pair< Idx, std::string > attr);
333 
334  void removeBarrenNodes__(typename StructuredInference::PData& data,
335  Set< Potential< GUM_SCALAR >* >& pool);
336 
337  /// Add in data.queries() any queried variable in one of data.pattern
338  /// matches.
339  // MVSC void buildQuerySet__( PData& data );
340 
341  /// Proceeds with the elimination of observed variables in math and then
342  /// call translatePotSet__().
344  typename StructuredInference::PData& data,
345  const Set< Potential< GUM_SCALAR >* >& pool,
346  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
347  const std::vector< NodeId >& elim_order);
348 
350  typename StructuredInference::PData& data,
351  const Set< Potential< GUM_SCALAR >* >& pool,
352  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
353  const std::vector< NodeId >& elim_order);
354 
355  /// Translate a given Potential Set into one w.r.t. variables in match.
356  Set< Potential< GUM_SCALAR >* >*
358  const Set< Potential< GUM_SCALAR >* >& pool,
359  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
360 
361  /// Unreduce the match containing the query.
362  // MVSC void unreduceMatchWithQuery__();
363 
364  // MVSC std::vector<NodeId>* getClassOutputs__( const
365  // PRMClass<GUM_SCALAR>* c );
366  /// Used to create strings
367  std::string dot__;
368  std::string str__(const PRMInstance< GUM_SCALAR >* i,
369  const PRMAttribute< GUM_SCALAR >* a) const;
370  std::string str__(const PRMInstance< GUM_SCALAR >* i,
371  const PRMAttribute< GUM_SCALAR >& a) const;
372  std::string str__(const PRMInstance< GUM_SCALAR >* i,
373  const PRMSlotChain< GUM_SCALAR >& a) const;
374 
375  public:
376  // For bench/debug purpose.
379  double triang_time;
380  double mining_time;
381  double pattern_time;
382  double inner_time;
383  double obs_time;
384  double full_time;
385  std::string info() const;
386  };
387 
388 
389 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
390  extern template class StructuredInference< double >;
391 #endif
392 
393 
394  } /* namespace prm */
395 } /* namespace gum */
396 
397 #include <agrum/PRM/inference/structuredInference_tpl.h>
398 
399 #endif /* GUM_STRUCTURED_INFERENCE_H */
List< NodeSet > partial_order
The partial order used of variable elimination.
NodeProperty< Size > mods
The class variables modalities.
List< NodeSet > partial_order__
We&#39;ll use a PartialOrderedTriangulation with three sets: output, nodes and obs with children outside ...
UndiGraph reducedGraph
The reduced graph.
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.
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. ...
std::string str__(const PRMInstance< GUM_SCALAR > *i, const PRMSlotChain< GUM_SCALAR > &a) const
virtual void evidenceRemoved_(const typename PRMInference< GUM_SCALAR >::Chain &chain)
See PRMInference::evidenceRemoved_().
std::pair< Idx, std::string > query_data__
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
UndiGraph moral_graph
The class moral graph. NodeId matches those in c.
void removeNode__(typename StructuredInference::PData &data, NodeId id, Set< Potential< GUM_SCALAR > * > &pool)
void reducePattern__(const gspan::Pattern *p)
Proceed with the elimination of all inner variables (observed or not) of all usable matches of Patter...
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.
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)
GSpan< GUM_SCALAR > * gspan__
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
UndiGraph graph
A yet to be triangulated undigraph.
bool allInstanceNoRefAttr__(typename StructuredInference::PData &data, std::pair< Idx, std::string > attr)
std::string dot__
Unreduce the match containing the query.
NodeSet & obs()
Returns the set of inner and observed nodes given all the matches of pattern.
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.
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_().
Bijection< NodeId, const DiscreteVariable *> vars
Bijection between graph&#39;s nodes and their corresponding DiscreteVariable, for inference purpose...
bool found_query__
Flag with an explicit name.
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_().
void reduceAloneInstances__(RGData &data)
Add the reduced potentials of instances not in any used patterns.
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.
StructuredInference(const StructuredInference &source)
Copy constructor.
NodeSet & inners()
Returns the set of inner nodes.
void removeBarrenNodes__(typename StructuredInference::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
void addEdgesInReducedGraph__(RGData &data)
Add the nodes in the reduced graph.
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.
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.
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...
virtual ~StructuredInference()
Destructor.
bool mining__
Flag which tells to use pattern mining or not.
Set< const PRMInstance< GUM_SCALAR > *> reducedInstances__
This keeps track of reduced instances.
CData(const PRMClass< GUM_SCALAR > &c)
Default constructor.
NodeSet & inners()
Returns the set of inner nodes.
void buildReduceGraph__(RGData &data)
This calls reducePattern__() over each pattern and then build the reduced graph which is used for inf...
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.
Set< Potential< GUM_SCALAR > *> trash__
NodeProperty< Size > mods
Mapping between NodeId and modalities.
const PRMClass< GUM_SCALAR > & c
The class about what this data is about.
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.
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 * pdata__
The pattern data of the pattern which one of its matches contains the query.
void searchPatterns()
Search for patterns without doing any computations.