aGrUM  0.16.0
structuredInference.h
Go to the documentation of this file.
1 
30 #ifndef GUM_STRUCTURED_INFERENCE_H
31 #define GUM_STRUCTURED_INFERENCE_H
32 
33 #include <string>
34 
35 #include <agrum/core/timer.h>
36 
38 
45 
47 
48 #include <agrum/PRM/PRM.h>
51 
52 namespace gum {
53  namespace prm {
54 
63  template < typename GUM_SCALAR >
64  class StructuredInference : public PRMInference< GUM_SCALAR > {
65  public:
66  // ========================================================================
68  // ========================================================================
70 
73  const PRMSystem< GUM_SCALAR >& system,
75 
78 
80  virtual ~StructuredInference();
81 
84 
86  // ========================================================================
88  // ========================================================================
90 
92  void setPatternMining(bool b);
93 
94  virtual std::string name() const;
95 
98 
100  const GSpan< GUM_SCALAR >& gspan() const;
101 
103  void searchPatterns();
104 
106  protected:
107  // ========================================================================
109  // ========================================================================
111 
113  virtual void
114  _evidenceAdded(const typename PRMInference< GUM_SCALAR >::Chain& chain);
115 
117  virtual void
119 
121  virtual void
122  _marginal(const typename PRMInference< GUM_SCALAR >::Chain& chain,
124 
126  virtual void _joint(
127  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
129 
131  private:
133  struct RGData {
146  RGData();
148  ~RGData();
150  inline NodeSet& outputs() { return partial_order[0]; }
152  inline NodeSet& queries() { return partial_order[1]; }
153  };
154 
156  struct PData {
182  PData(const gspan::Pattern& p,
185  PData(const PData& source);
187  ~PData();
189  inline NodeSet& inners() { return __partial_order[0]; }
192  inline NodeSet& obs() { return __partial_order[1]; }
194  inline NodeSet& outputs() { return __partial_order[2]; }
196  inline NodeSet& queries() { return __partial_order[3]; }
197  // We use the first match for computations
198  // inline const Sequence<PRMInstance<GUM_SCALAR>*>& match() const {
199  // return
200  // **(matches.begin());}
201  // Remove any empty set in partial_order
203 
204  private:
212  };
213 
215  struct CData {
229  CData(const PRMClass< GUM_SCALAR >& c);
231  ~CData();
233  inline NodeSet& inners() { return __inners; }
235  inline NodeSet& aggregators() { return __aggregators; }
237  inline NodeSet& outputs() { return __outputs; }
239  inline std::vector< NodeId >& elim_order() { return __elim_order; }
240 
241  private:
242  std::vector< NodeId > __elim_order;
247  };
248 
251 
258 
264 
267 
269 
272 
275 
279 
281  bool __mining;
282 
285  std::pair< Idx, std::string > __query_data;
286 
292  void __buildReduceGraph(RGData& data);
293 
295  // MSVC void __addNodesInReducedGraph( RGData& data );
296 
298  void __addEdgesInReducedGraph(RGData& data);
299 
300  void __removeNode(typename StructuredInference::PData& data,
301  NodeId id,
303 
305  void __reduceAloneInstances(RGData& data);
306 
312  void __reducePattern(const gspan::Pattern* p);
313 
319  void
322  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
323 
325  typename StructuredInference::PData& data,
326  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
329  NodeId id,
330  std::pair< Idx, std::string >& v);
331 
333  std::pair< Idx, std::string > attr);
334 
337 
340  // MVSC void __buildQuerySet( PData& data );
341 
345  typename StructuredInference::PData& data,
346  const Set< Potential< GUM_SCALAR >* >& pool,
347  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
348  const std::vector< NodeId >& elim_order);
349 
351  typename StructuredInference::PData& data,
352  const Set< Potential< GUM_SCALAR >* >& pool,
353  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
354  const std::vector< NodeId >& elim_order);
355 
359  const Set< Potential< GUM_SCALAR >* >& pool,
360  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
361 
363  // MVSC void __unreduceMatchWithQuery();
364 
365  // MVSC std::vector<NodeId>* __getClassOutputs( const
366  // PRMClass<GUM_SCALAR>* c );
368  std::string __dot;
369  std::string __str(const PRMInstance< GUM_SCALAR >* i,
370  const PRMAttribute< GUM_SCALAR >* a) const;
371  std::string __str(const PRMInstance< GUM_SCALAR >* i,
372  const PRMAttribute< GUM_SCALAR >& a) const;
373  std::string __str(const PRMInstance< GUM_SCALAR >* i,
374  const PRMSlotChain< GUM_SCALAR >& a) const;
375 
376  public:
377  // For bench/debug purpose.
380  double triang_time;
381  double mining_time;
382  double pattern_time;
383  double inner_time;
384  double obs_time;
385  double full_time;
386  std::string info() const;
387  };
388 
389 
390 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
391  extern template class StructuredInference< double >;
392 #endif
393 
394 
395  } /* namespace prm */
396 } /* namespace gum */
397 
399 
400 #endif /* GUM_STRUCTURED_INFERENCE_H */
List< NodeSet > partial_order
The partial order used of variable elimination.
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...
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...
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
void __reducePattern(const gspan::Pattern *p)
Proceed with the elimination of all inner variables (observed or not) of all usable matches of Patter...
NodeProperty< Size > mods
The class variables modalities.
UndiGraph reducedGraph
The reduced graph.
virtual void _marginal(const typename PRMInference< GUM_SCALAR >::Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference::_marginal().
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. ...
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
GSpan< GUM_SCALAR >::MatchedInstances & matches
A reference over the usable matches of pattern.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
UndiGraph moral_graph
The class moral graph. NodeId matches those in c.
PData * __pdata
The pattern data of the pattern which one of its matches contains the query.
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
bool __mining
Flag which tells to use pattern mining or not.
Set< Potential< GUM_SCALAR > *> * __eliminateObservedNodesInSource(typename StructuredInference::PData &data, const Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match, const std::vector< NodeId > &elim_order)
List< NodeSet > * __real_order
A copy of __partial_order without empty sets.
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)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Private structure to represent data about a Class<GUM_SCALAR>.
void __reduceAloneInstances(RGData &data)
Add the reduced potentials of instances not in any used patterns.
UndiGraph graph
A yet to be triangulated undigraph.
NodeSet & obs()
Returns the set of inner and observed nodes given all the matches of pattern.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::string __dot
Unreduce the match containing the query.
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
NodeSet & outputs()
Returns the set of outputs nodes.
bool __found_query
Flag with an explicit name.
void __addEdgesInReducedGraph(RGData &data)
Add the nodes in the reduced graph.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for generic Hash Tables.
Definition: hashTable.h:679
const gspan::Pattern & pattern
The pattern for which this represents data about it.
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 __buildReduceGraph(RGData &data)
This calls __reducePattern() over each pattern and then build the reduced graph which is used for inf...
NodeSet & outputs()
Returns the set of outputs nodes (which will be eliminated).
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
void __removeBarrenNodes(typename StructuredInference::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< const Sequence< PRMInstance< GUM_SCALAR > *> *, Set< Potential< GUM_SCALAR > *> *> __elim_map
Mapping between a Pattern&#39;s match and its potential pool after inner variables were eliminated...
<agrum/PRM/structuredInference.h>
Bijection< NodeId, const DiscreteVariable *> vars
Bijection between graph&#39;s nodes and their corresponding DiscreteVariable, for inference purpose...
Private structure to represent data about a pattern.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
PRMInference< GUM_SCALAR >::Chain __query
The query.
StructuredInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.
Set< Potential< GUM_SCALAR > *> pool
The potential pool obtained by C elimination of inner nodes.
Set< const PRMInstance< GUM_SCALAR > *> __reducedInstances
This keeps track of reduced instances.
Set< NodeId > barren
Set of barren nodes.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:229
virtual std::string name() const
Tells this algorithm to use pattern mining or not.
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
List< NodeSet > __partial_order
We&#39;ll use a PartialOrderedTriangulation with three sets: output, nodes and obs with children outside ...
NodeSet & inners()
Returns the set of inner nodes.
Set< Potential< GUM_SCALAR > *> * __eliminateObservedNodes(typename StructuredInference::PData &data, const Set< Potential< GUM_SCALAR > * > &pool, const Sequence< PRMInstance< GUM_SCALAR > * > &match, const std::vector< NodeId > &elim_order)
Add in data.queries() any queried variable in one of data.pattern matches.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
Definition: PRMObject.h:221
NodeProperty< Size > mod
The pattern&#39;s variables modalities.
NodeSet & aggregators()
Returns the set of aggregators and their parents.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:57
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.
virtual void _joint(const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference::_joint().
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __outputs
std::string __str(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
Definition: PRMInference.h:52
bool __allInstanceNoRefAttr(typename StructuredInference::PData &data, std::pair< Idx, std::string > attr)
Private structure to represent data about a reduced graph.
std::vector< NodeId > & elim_order()
The elimination order for nodes of this class.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
This class discovers pattern in a PRM<GUM_SCALAR>&#39;s PRMSystem<GUM_SCALAR> to speed up structured infe...
Definition: DFSTree.h:57
std::pair< Idx, std::string > __query_data
Base class for undirected graphs.
Definition: undiGraph.h:109
virtual ~StructuredInference()
Destructor.
Class used to compute response times for benchmark purposesThis class represents a classic timer...
Definition: timer.h:51
NodeSet & inners()
Returns the set of inner nodes.
GSpan< GUM_SCALAR > & gspan()
Returns the instance of gspan used to search patterns.
virtual void _evidenceRemoved(const typename PRMInference< GUM_SCALAR >::Chain &chain)
See PRMInference::_evidenceRemoved().
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:61
Bijection< NodeId, std::string > node2attr
A bijection to easily keep track between graph and attributes, its of the form instance_name DOT attr...
virtual void _evidenceAdded(const typename PRMInference< GUM_SCALAR >::Chain &chain)
See PRMInference::_evidenceAdded().
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
This contains all the information we want for a node in a DFSTree.
Definition: pattern.h:73
This is an abstract class used to tune search strategies in the gspan algorithm.
Definition: DFSTree.h:61
Size NodeId
Type for node ids.
Definition: graphElements.h:98
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 > *> __trash
void __removeNode(typename StructuredInference::PData &data, NodeId id, Set< Potential< GUM_SCALAR > * > &pool)
void searchPatterns()
Search for patterns without doing any computations.