aGrUM  0.14.2
structuredInference.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #ifndef GUM_STRUCTURED_INFERENCE_H
28 #define GUM_STRUCTURED_INFERENCE_H
29 
30 #include <string>
31 
32 #include <agrum/core/timer.h>
33 
35 
42 
44 
45 #include <agrum/PRM/PRM.h>
48 
49 namespace gum {
50  namespace prm {
51 
60  template < typename GUM_SCALAR >
61  class StructuredInference : public PRMInference< GUM_SCALAR > {
62  public:
63  // ========================================================================
65  // ========================================================================
67 
70  const PRMSystem< GUM_SCALAR >& system,
72 
75 
77  virtual ~StructuredInference();
78 
81 
83  // ========================================================================
85  // ========================================================================
87 
89  void setPatternMining(bool b);
90 
91  virtual std::string name() const;
92 
95 
97  const GSpan< GUM_SCALAR >& gspan() const;
98 
100  void searchPatterns();
101 
103  protected:
104  // ========================================================================
106  // ========================================================================
108 
110  virtual void
111  _evidenceAdded(const typename PRMInference< GUM_SCALAR >::Chain& chain);
112 
114  virtual void
116 
118  virtual void
119  _marginal(const typename PRMInference< GUM_SCALAR >::Chain& chain,
121 
123  virtual void _joint(
124  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& queries,
126 
128  private:
130  struct RGData {
143  RGData();
145  ~RGData();
147  inline NodeSet& outputs() { return partial_order[0]; }
149  inline NodeSet& queries() { return partial_order[1]; }
150  };
151 
153  struct PData {
179  PData(const gspan::Pattern& p,
182  PData(const PData& source);
184  ~PData();
186  inline NodeSet& inners() { return __partial_order[0]; }
189  inline NodeSet& obs() { return __partial_order[1]; }
191  inline NodeSet& outputs() { return __partial_order[2]; }
193  inline NodeSet& queries() { return __partial_order[3]; }
194  // We use the first match for computations
195  // inline const Sequence<PRMInstance<GUM_SCALAR>*>& match() const {
196  // return
197  // **(matches.begin());}
198  // Remove any empty set in partial_order
200 
201  private:
209  };
210 
212  struct CData {
226  CData(const PRMClass< GUM_SCALAR >& c);
228  ~CData();
230  inline NodeSet& inners() { return __inners; }
232  inline NodeSet& aggregators() { return __aggregators; }
234  inline NodeSet& outputs() { return __outputs; }
236  inline std::vector< NodeId >& elim_order() { return __elim_order; }
237 
238  private:
239  std::vector< NodeId > __elim_order;
244  };
245 
248 
255 
261 
264 
266 
269 
272 
276 
278  bool __mining;
279 
282  std::pair< Idx, std::string > __query_data;
283 
289  void __buildReduceGraph(RGData& data);
290 
292  // MSVC void __addNodesInReducedGraph( RGData& data );
293 
295  void __addEdgesInReducedGraph(RGData& data);
296 
297  void __removeNode(typename StructuredInference::PData& data,
298  NodeId id,
300 
302  void __reduceAloneInstances(RGData& data);
303 
309  void __reducePattern(const gspan::Pattern* p);
310 
316  void
319  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
320 
322  typename StructuredInference::PData& data,
323  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
326  NodeId id,
327  std::pair< Idx, std::string >& v);
328 
330  std::pair< Idx, std::string > attr);
331 
334 
337  // MVSC void __buildQuerySet( PData& data );
338 
342  typename StructuredInference::PData& data,
343  const Set< Potential< GUM_SCALAR >* >& pool,
344  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
345  const std::vector< NodeId >& elim_order);
346 
348  typename StructuredInference::PData& data,
349  const Set< Potential< GUM_SCALAR >* >& pool,
350  const Sequence< PRMInstance< GUM_SCALAR >* >& match,
351  const std::vector< NodeId >& elim_order);
352 
356  const Set< Potential< GUM_SCALAR >* >& pool,
357  const Sequence< PRMInstance< GUM_SCALAR >* >& match);
358 
360  // MVSC void __unreduceMatchWithQuery();
361 
362  // MVSC std::vector<NodeId>* __getClassOutputs( const
363  // PRMClass<GUM_SCALAR>* c );
365  std::string __dot;
366  std::string __str(const PRMInstance< GUM_SCALAR >* i,
367  const PRMAttribute< GUM_SCALAR >* a) const;
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 PRMSlotChain< GUM_SCALAR >& a) const;
372 
373  public:
374  // For bench/debug purpose.
377  double triang_time;
378  double mining_time;
379  double pattern_time;
380  double inner_time;
381  double obs_time;
382  double full_time;
383  std::string info() const;
384  };
385 
386 
387 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
388  extern template class StructuredInference< double >;
389 #endif
390 
391 
392  } /* namespace prm */
393 } /* namespace gum */
394 
396 
397 #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:57
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.
class for NoisyOR-net implementation as multiDim
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
Headers of gspan.
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:60
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:1019
Headers of MultiDimSparse.
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)
Headers of PRM.
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.
class for multiDimNoisyORCompound
std::string __dot
Unreduce the match containing the query.
Generic doubly linked lists.
Definition: list.h:369
Class used to compute response times for benchmark purposes.
gum is the global namespace for all aGrUM entities
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.
Implementation of a variable elimination algorithm for inference in Bayesian Networks.
The class for generic Hash Tables.
Definition: hashTable.h:676
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:162
void __removeBarrenNodes(typename StructuredInference::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
Header of the Potential class.
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.
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.
Headers of PRMInference.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:226
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:1803
Efficient functionals for projecting multiDimensional tables.
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:218
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:54
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:49
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:63
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:63
This class discovers pattern in a PRM<GUM_SCALAR>&#39;s PRMSystem<GUM_SCALAR> to speed up structured infe...
Definition: DFSTree.h:54
std::pair< Idx, std::string > __query_data
Base class for undirected graphs.
Definition: undiGraph.h:106
virtual ~StructuredInference()
Destructor.
Class used to compute response times for benchmark purposesThis class represents a classic timer...
Definition: timer.h:48
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:58
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.
Inline implementation of StructuredInference.
This contains all the information we want for a node in a DFSTree.
Definition: pattern.h:70
This is an abstract class used to tune search strategies in the gspan algorithm.
Definition: DFSTree.h:58
Size NodeId
Type for node ids.
Definition: graphElements.h:97
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.