aGrUM  0.14.2
gum::prm::StructuredInference< GUM_SCALAR > Class Template Referenceabstract

<agrum/PRM/structuredInference.h> More...

#include <structuredInference.h>

+ Inheritance diagram for gum::prm::StructuredInference< GUM_SCALAR >:
+ Collaboration diagram for gum::prm::StructuredInference< GUM_SCALAR >:

Public Attributes

Timer timer
 
Timer plopTimer
 
double triang_time
 
double mining_time
 
double pattern_time
 
double inner_time
 
double obs_time
 
double full_time
 

Public Member Functions

std::string info () const
 
Constructor & destructor.
 StructuredInference (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
 Default constructor. More...
 
 StructuredInference (const StructuredInference &source)
 Copy constructor. More...
 
virtual ~StructuredInference ()
 Destructor. More...
 
StructuredInferenceoperator= (const StructuredInference &source)
 Copy operator. More...
 
Getters and setters.
void setPatternMining (bool b)
 Tells this algorithm to use pattern mining or not. More...
 
virtual std::string name () const
 Tells this algorithm to use pattern mining or not. More...
 
GSpan< GUM_SCALAR > & gspan ()
 Returns the instance of gspan used to search patterns. More...
 
const GSpan< GUM_SCALAR > & gspan () const
 Returns the instance of gspan used to search patterns. More...
 
void searchPatterns ()
 Search for patterns without doing any computations. More...
 
Query methods.
void marginal (const Chain &chain, Potential< GUM_SCALAR > &m)
 Compute the marginal of the formal attribute pointed by chain and stores it in m. More...
 
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. More...
 
Evidence handling.
EMapevidence (const PRMInstance< GUM_SCALAR > &i)
 Returns EMap of evidences over i. More...
 
EMapevidence (const PRMInstance< GUM_SCALAR > *i)
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns true if i has evidence. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const Chain &chain) const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
bool hasEvidence () const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
void addEvidence (const Chain &chain, const Potential< GUM_SCALAR > &p)
 Add an evidence to the given instance's elt. More...
 
void removeEvidence (const Chain &chain)
 Remove evidence on the given instance's elt. More...
 
void clearEvidence ()
 Remove all evidences. More...
 

Public Types

typedef std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *> EMap
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::iterator_safe EMapIterator
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::const_iterator_safe EMapConstIterator
 Code alias. More...
 

Protected Member Functions

Protected members.
virtual void _evidenceAdded (const typename PRMInference< GUM_SCALAR >::Chain &chain)
 See PRMInference::_evidenceAdded(). More...
 
virtual void _evidenceRemoved (const typename PRMInference< GUM_SCALAR >::Chain &chain)
 See PRMInference::_evidenceRemoved(). More...
 
virtual void _marginal (const typename PRMInference< GUM_SCALAR >::Chain &chain, Potential< GUM_SCALAR > &m)
 See PRMInference::_marginal(). More...
 
virtual void _joint (const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Potential< GUM_SCALAR > &j)
 See PRMInference::_joint(). More...
 

Classes

struct  CData
 Private structure to represent data about a Class<GUM_SCALAR>. More...
 
struct  PData
 Private structure to represent data about a pattern. More...
 
struct  RGData
 Private structure to represent data about a reduced graph. More...
 

Protected members.

virtual void _evidenceAdded (const Chain &chain)=0
 This method is called whenever an evidence is added, but AFTER any processing made by PRMInference. More...
 
virtual void _evidenceRemoved (const Chain &chain)=0
 This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference. More...
 
virtual void _marginal (const Chain &chain, Potential< GUM_SCALAR > &m)=0
 Generic method to compute the marginal of given element. More...
 
virtual void _joint (const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
 Generic method to compute the marginal of given element. More...
 
PRM< GUM_SCALAR > const * _prm
 The PRM<GUM_SCALAR> on which inference is done. More...
 
PRMSystem< GUM_SCALAR > const * _sys
 The Model on which inference is done. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::prm::StructuredInference< GUM_SCALAR >

<agrum/PRM/structuredInference.h>

This PRM<GUM_SCALAR> inference algorithm exploits the GSpan<GUM_SCALAR> algorithm to discover new patters and exploit them in a structured way.

Definition at line 61 of file structuredInference.h.

Member Typedef Documentation

◆ Chain

template<typename GUM_SCALAR>
typedef std::pair< const PRMInstance< GUM_SCALAR >*, const PRMAttribute< GUM_SCALAR >* > gum::prm::PRMInference< GUM_SCALAR >::Chain
inherited

Code alias.

Definition at line 54 of file PRMInference.h.

◆ EMap

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* > gum::prm::PRMInference< GUM_SCALAR >::EMap
inherited

Code alias.

Definition at line 57 of file PRMInference.h.

◆ EMapConstIterator

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* >::const_iterator_safe gum::prm::PRMInference< GUM_SCALAR >::EMapConstIterator
inherited

Code alias.

Definition at line 66 of file PRMInference.h.

◆ EMapIterator

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* >::iterator_safe gum::prm::PRMInference< GUM_SCALAR >::EMapIterator
inherited

Code alias.

Definition at line 62 of file PRMInference.h.

Constructor & Destructor Documentation

◆ StructuredInference() [1/2]

template<typename GUM_SCALAR >
gum::prm::StructuredInference< GUM_SCALAR >::StructuredInference ( const PRM< GUM_SCALAR > &  prm,
const PRMSystem< GUM_SCALAR > &  system,
gspan::SearchStrategy< GUM_SCALAR > *  strategy = 0 
)

Default constructor.

Definition at line 33 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::StructuredInference< GUM_SCALAR >::full_time, gum::prm::StructuredInference< GUM_SCALAR >::inner_time, gum::prm::StructuredInference< GUM_SCALAR >::mining_time, gum::prm::StructuredInference< GUM_SCALAR >::obs_time, gum::prm::StructuredInference< GUM_SCALAR >::pattern_time, and gum::prm::StructuredInference< GUM_SCALAR >::triang_time.

36  :
37  PRMInference< GUM_SCALAR >(prm, system),
38  __gspan(0), __pdata(0), __mining(false), __dot(".") {
39  GUM_CONSTRUCTOR(StructuredInference);
40  __gspan = new GSpan< GUM_SCALAR >(prm, system, strategy);
41  triang_time = 0.0;
42  mining_time = 0.0;
43  pattern_time = 0.0;
44  inner_time = 0.0;
45  obs_time = 0.0;
46  full_time = 0.0;
47  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
PData * __pdata
The pattern data of the pattern which one of its matches contains the query.
bool __mining
Flag which tells to use pattern mining or not.
std::string __dot
Unreduce the match containing the query.
StructuredInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.

◆ StructuredInference() [2/2]

template<typename GUM_SCALAR >
gum::prm::StructuredInference< GUM_SCALAR >::StructuredInference ( const StructuredInference< GUM_SCALAR > &  source)

Copy constructor.

Definition at line 50 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::PRMInference< GUM_SCALAR >::_prm, and gum::prm::PRMInference< GUM_SCALAR >::_sys.

51  :
52  PRMInference< GUM_SCALAR >(source),
53  __gspan(0), __pdata(0), __mining(source.__mining), __found_query(false),
54  __dot(".") {
55  GUM_CONS_CPY(StructuredInference);
56  __gspan = new GSpan< GUM_SCALAR >(*(this->_prm), *(this->_sys));
57  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
PData * __pdata
The pattern data of the pattern which one of its matches contains the query.
bool __mining
Flag which tells to use pattern mining or not.
std::string __dot
Unreduce the match containing the query.
bool __found_query
Flag with an explicit name.
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:210
StructuredInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.
PRMSystem< GUM_SCALAR > const * _sys
The Model on which inference is done.
Definition: PRMInference.h:213

◆ ~StructuredInference()

template<typename GUM_SCALAR >
gum::prm::StructuredInference< GUM_SCALAR >::~StructuredInference ( )
virtual

Destructor.

Definition at line 60 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__cdata_map, gum::prm::StructuredInference< GUM_SCALAR >::__elim_map, gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::StructuredInference< GUM_SCALAR >::__outputs, gum::prm::StructuredInference< GUM_SCALAR >::__pdata, gum::prm::StructuredInference< GUM_SCALAR >::__trash, and gum::prm::StructuredInference< GUM_SCALAR >::operator=().

60  {
61  GUM_DESTRUCTOR(StructuredInference);
62  delete this->__gspan;
63 
64  for (const auto& elt : __elim_map)
65  delete elt.second;
66 
67  for (const auto& elt : __cdata_map)
68  delete elt.second;
69 
70  for (const auto elt : __trash)
71  delete (elt);
72 
73  for (const auto& elt : __outputs)
74  delete elt.second;
75 
76  if (__pdata) delete __pdata;
77  }
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...
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
PData * __pdata
The pattern data of the pattern which one of its matches contains the query.
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...
StructuredInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system, gspan::SearchStrategy< GUM_SCALAR > *strategy=0)
Default constructor.
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __outputs
+ Here is the call graph for this function:

Member Function Documentation

◆ __addEdgesInReducedGraph()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__addEdgesInReducedGraph ( RGData data)
private

Add the nodes in the reduced graph.

Add edges in the reduced graph.

Definition at line 732 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__elim_map, gum::UndiGraph::addEdge(), gum::NodeGraphPart::addNode(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::atPos(), gum::Set< Key, Alloc >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::mods, gum::prm::StructuredInference< GUM_SCALAR >::RGData::outputs(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::pool, gum::prm::StructuredInference< GUM_SCALAR >::RGData::reducedGraph, gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::size(), and gum::prm::StructuredInference< GUM_SCALAR >::RGData::var2node.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph().

733  {
734  // We first add edges between variables already in pool (i.e. those of the
735  // reduced instances)
736  NodeId id_1, id_2;
737 
738  for (const auto pot : data.pool) {
739  const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
740 
741  for (Size var_1 = 0; var_1 < vars.size(); ++var_1) {
742  if (data.var2node.existsFirst(vars.atPos(var_1))) {
743  id_1 = data.var2node.second(vars.atPos(var_1));
744  } else {
745  id_1 = data.reducedGraph.addNode();
746  data.var2node.insert(vars.atPos(var_1), id_1);
747  data.mods.insert(id_1, vars.atPos(var_1)->domainSize());
748  data.outputs().insert(id_1);
749  }
750 
751  for (Size var_2 = var_1 + 1; var_2 < vars.size(); ++var_2) {
752  if (data.var2node.existsFirst(vars.atPos(var_2))) {
753  id_2 = data.var2node.second(vars.atPos(var_2));
754  } else {
755  id_2 = data.reducedGraph.addNode();
756  data.var2node.insert(vars.atPos(var_2), id_2);
757  data.mods.insert(id_2, vars.atPos(var_2)->domainSize());
758  data.outputs().insert(id_2);
759  }
760 
761  try {
762  data.reducedGraph.addEdge(id_1, id_2);
763  } catch (DuplicateElement&) {}
764  }
765  }
766  }
767 
768  // Adding potentials obtained from reduced patterns
769  for (const auto& elt : __elim_map) {
770  // We add edges between variables in the same reduced patterns
771  for (const auto pot : *elt.second) {
772  data.pool.insert(pot);
773  const Sequence< const DiscreteVariable* >& vars =
774  pot->variablesSequence();
775 
776  for (Size var_1 = 0; var_1 < vars.size(); ++var_1) {
777  if (data.var2node.existsFirst(vars.atPos(var_1))) {
778  id_1 = data.var2node.second(vars.atPos(var_1));
779  } else {
780  id_1 = data.reducedGraph.addNode();
781  data.var2node.insert(vars.atPos(var_1), id_1);
782  data.mods.insert(id_1, vars.atPos(var_1)->domainSize());
783  data.outputs().insert(id_1);
784  }
785 
786  for (Size var_2 = var_1 + 1; var_2 < vars.size(); ++var_2) {
787  if (data.var2node.existsFirst(vars.atPos(var_2))) {
788  id_2 = data.var2node.second(vars.atPos(var_2));
789  } else {
790  id_2 = data.reducedGraph.addNode();
791  data.var2node.insert(vars.atPos(var_2), id_2);
792  data.mods.insert(id_2, vars.atPos(var_2)->domainSize());
793  data.outputs().insert(id_2);
794  }
795 
796  try {
797  data.reducedGraph.addEdge(id_1, id_2);
798  } catch (DuplicateElement&) {}
799  }
800  }
801  }
802  }
803  }
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...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __allInstanceNoRefAttr()

template<typename GUM_SCALAR>
bool gum::prm::StructuredInference< GUM_SCALAR >::__allInstanceNoRefAttr ( typename StructuredInference< GUM_SCALAR >::PData data,
std::pair< Idx, std::string >  attr 
)
private

Definition at line 407 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::PData::matches.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildPatternGraph().

409  {
410  for (const auto mat : data.matches)
411  if (mat->atPos(attr.first)
412  ->hasRefAttr(mat->atPos(attr.first)->get(attr.second).id()))
413  return false;
414 
415  return true;
416  }
+ Here is the caller graph for this function:

◆ __buildPatternGraph()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__buildPatternGraph ( PData data,
Set< Potential< GUM_SCALAR > * > &  pool,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match 
)
private

Build the DAG corresponding to Pattern data.pattern, initialize pool with all the Potentials of all variables in data.pattern. The first match of data.pattern (aka data.match) is used.

Definition at line 346 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__allInstanceNoRefAttr(), gum::prm::StructuredInference< GUM_SCALAR >::__found_query, gum::prm::StructuredInference< GUM_SCALAR >::__insertNodeInElimLists(), gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::prm::StructuredInference< GUM_SCALAR >::__query_data, gum::prm::StructuredInference< GUM_SCALAR >::__str(), gum::UndiGraph::addEdge(), gum::NodeGraphPart::addNode(), gum::prm::StructuredInference< GUM_SCALAR >::PData::barren, gum::Set< Key, Alloc >::erase(), gum::Set< Key, Alloc >::exists(), gum::prm::StructuredInference< GUM_SCALAR >::PData::graph, gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::Set< Key, Alloc >::insert(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::PData::map, gum::prm::StructuredInference< GUM_SCALAR >::PData::matches, gum::prm::StructuredInference< GUM_SCALAR >::PData::mod, gum::prm::StructuredInference< GUM_SCALAR >::PData::node2attr, gum::prm::StructuredInference< GUM_SCALAR >::PData::obs(), gum::prm::StructuredInference< GUM_SCALAR >::PData::outputs(), gum::prm::StructuredInference< GUM_SCALAR >::PData::queries(), gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence(), and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern().

349  {
350  std::pair< Idx, std::string > v;
351  Potential< GUM_SCALAR >* pot = 0;
352 
353  for (const auto inst : match) {
354  for (const auto& elt : *inst) {
355  NodeId id = data.graph.addNode();
356  v = std::make_pair(match.pos(inst), elt.second->safeName());
357  data.map.insert(id, v);
358  data.node2attr.insert(id, __str(inst, elt.second));
359  data.mod.insert(id, elt.second->type()->domainSize());
360  data.vars.insert(id, &(elt.second->type().variable()));
361  pool.insert(
362  const_cast< Potential< GUM_SCALAR >* >(&(elt.second->cpf())));
363  pot =
364  &(const_cast< Potential< GUM_SCALAR >& >(inst->get(v.second).cpf()));
365 
366  for (const auto var : pot->variablesSequence()) {
367  try {
368  if (id != data.vars.first(var))
369  data.graph.addEdge(id, data.vars.first(var));
370  } catch (DuplicateElement&) {
371  } catch (NotFound&) {}
372  }
373 
374  __insertNodeInElimLists(data, match, inst, elt.second, id, v);
375 
376  if (data.inners().exists(id)
377  && (inst->type().containerDag().children(elt.second->id()).size()
378  == 0)
379  && __allInstanceNoRefAttr(data, v))
380  data.barren.insert(id);
381  }
382  }
383 
384  if (!__found_query) {
385  for (const auto mat : data.matches) {
386  if (mat->exists(
387  const_cast< PRMInstance< GUM_SCALAR >* >(__query.first))) {
388  Idx pos =
389  mat->pos(const_cast< PRMInstance< GUM_SCALAR >* >(__query.first));
390  DiscreteVariable& var =
391  match.atPos(pos)->get(__query.second->safeName()).type().variable();
392  NodeId id = data.vars.first(&var);
393  data.barren.erase(id);
394  data.inners().erase(id);
395  data.obs().erase(id);
396  data.outputs().erase(id);
397  data.queries().insert(id);
398  __found_query = true;
399  __query_data = std::make_pair(pos, __query.second->safeName());
400  break;
401  }
402  }
403  }
404  }
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)
bool __found_query
Flag with an explicit name.
PRMInference< GUM_SCALAR >::Chain __query
The query.
std::string __str(const PRMInstance< GUM_SCALAR > *i, const PRMAttribute< GUM_SCALAR > *a) const
bool __allInstanceNoRefAttr(typename StructuredInference::PData &data, std::pair< Idx, std::string > attr)
std::pair< Idx, std::string > __query_data
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __buildReduceGraph()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph ( RGData data)
private

This calls __reducePattern() over each pattern and then build the reduced graph which is used for inference. The reduce graph is a triangulated instance graph.

Definition at line 206 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__addEdgesInReducedGraph(), gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::StructuredInference< GUM_SCALAR >::__mining, gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern(), gum::prm::StructuredInference< GUM_SCALAR >::__trash, gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key, Alloc >::erase(), gum::Set< Key, Alloc >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::mining_time, gum::prm::StructuredInference< GUM_SCALAR >::RGData::mods, gum::prm::StructuredInference< GUM_SCALAR >::RGData::outputs(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::partial_order, gum::prm::StructuredInference< GUM_SCALAR >::pattern_time, gum::prm::StructuredInference< GUM_SCALAR >::plopTimer, gum::prm::StructuredInference< GUM_SCALAR >::RGData::pool, gum::prm::StructuredInference< GUM_SCALAR >::RGData::queries(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::reducedGraph, gum::Timer::reset(), gum::Set< Key, Alloc >::size(), gum::Timer::step(), and gum::prm::StructuredInference< GUM_SCALAR >::RGData::var2node.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::_marginal(), and gum::prm::StructuredInference< GUM_SCALAR >::searchPatterns().

207  {
208  // Launch the pattern mining
209  plopTimer.reset();
210 
211  if (__mining) __gspan->discoverPatterns();
212 
214  // Reducing each used pattern
215  plopTimer.reset();
216  typedef std::vector< gspan::Pattern* >::const_iterator Iter;
217 
218  for (Iter p = __gspan->patterns().begin(); p != __gspan->patterns().end();
219  ++p)
220  if (__gspan->matches(**p).size()) __reducePattern(*p);
221 
223  // reducing instance not already reduced in a pattern
225  // Adding edges using the pools
227  // Placing the query where it belongs
228  NodeId id = data.var2node.second(&(__query.second->type().variable()));
229  data.outputs().erase(id);
230  data.queries().insert(id);
231  // Triangulating, then eliminating
232  PartialOrderedTriangulation t(
233  &(data.reducedGraph), &(data.mods), &(data.partial_order));
234  const std::vector< NodeId >& elim_order = t.eliminationOrder();
235 
236  for (size_t i = 0; i < data.outputs().size(); ++i)
237  eliminateNode(data.var2node.first(elim_order[i]), data.pool, __trash);
238  }
void __reducePattern(const gspan::Pattern *p)
Proceed with the elimination of all inner variables (observed or not) of all usable matches of Patter...
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
bool __mining
Flag which tells to use pattern mining or not.
void __reduceAloneInstances(RGData &data)
Add the reduced potentials of instances not in any used patterns.
void __addEdgesInReducedGraph(RGData &data)
Add the nodes in the reduced graph.
void reset()
Reset the timer.
Definition: timer_inl.h:29
PRMInference< GUM_SCALAR >::Chain __query
The query.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __eliminateObservedNodes()

template<typename GUM_SCALAR>
Set< Potential< GUM_SCALAR > *> * gum::prm::StructuredInference< GUM_SCALAR >::__eliminateObservedNodes ( typename StructuredInference< GUM_SCALAR >::PData data,
const Set< Potential< GUM_SCALAR > * > &  pool,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match,
const std::vector< NodeId > &  elim_order 
)
private

Add in data.queries() any queried variable in one of data.pattern matches.

Proceeds with the elimination of observed variables in math and then call __translatePotSet().

Definition at line 494 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__translatePotSet(), gum::prm::StructuredInference< GUM_SCALAR >::__trash, gum::prm::eliminateNode(), gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::prm::StructuredInference< GUM_SCALAR >::PData::map, gum::prm::StructuredInference< GUM_SCALAR >::PData::obs(), gum::Set< Key, Alloc >::size(), and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern().

498  {
499  Set< Potential< GUM_SCALAR >* >* my_pool =
500  __translatePotSet(data, pool, match);
501  std::pair< Idx, std::string > target;
502  size_t end = data.inners().size() + data.obs().size();
503 
504  for (size_t idx = data.inners().size(); idx < end; ++idx) {
505  target = data.map[data.vars.first(data.vars.second(elim_order[idx]))];
506  eliminateNode(&(match[target.first]->get(target.second).type().variable()),
507  *my_pool,
508  __trash);
509  }
510 
511  return my_pool;
512  }
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
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.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __eliminateObservedNodesInSource()

template<typename GUM_SCALAR>
Set< Potential< GUM_SCALAR > *> * gum::prm::StructuredInference< GUM_SCALAR >::__eliminateObservedNodesInSource ( typename StructuredInference< GUM_SCALAR >::PData data,
const Set< Potential< GUM_SCALAR > * > &  pool,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match,
const std::vector< NodeId > &  elim_order 
)
private

Definition at line 472 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__trash, gum::prm::eliminateNode(), gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::prm::StructuredInference< GUM_SCALAR >::PData::map, gum::prm::StructuredInference< GUM_SCALAR >::PData::obs(), gum::Set< Key, Alloc >::size(), and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern().

476  {
477  Set< Potential< GUM_SCALAR >* >* my_pool =
478  new Set< Potential< GUM_SCALAR >* >(pool);
479  std::pair< Idx, std::string > target;
480  size_t end = data.inners().size() + data.obs().size();
481 
482  for (size_t idx = data.inners().size(); idx < end; ++idx) {
483  target = data.map[data.vars.first(data.vars.second(elim_order[idx]))];
484  eliminateNode(&(match[target.first]->get(target.second).type().variable()),
485  *my_pool,
486  __trash);
487  }
488 
489  return my_pool;
490  }
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __insertNodeInElimLists()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__insertNodeInElimLists ( typename StructuredInference< GUM_SCALAR >::PData data,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match,
PRMInstance< GUM_SCALAR > *  inst,
PRMAttribute< GUM_SCALAR > *  attr,
NodeId  id,
std::pair< Idx, std::string > &  v 
)
private

Definition at line 309 of file structuredInference_tpl.h.

References gum::Set< Key, Alloc >::exists(), gum::prm::PRMInstance< GUM_SCALAR >::get(), gum::prm::PRMInstance< GUM_SCALAR >::getRefAttr(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::Set< Key, Alloc >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::PData::matches, gum::prm::StructuredInference< GUM_SCALAR >::PData::obs(), gum::prm::StructuredInference< GUM_SCALAR >::PData::outputs(), gum::Set< Key, Alloc >::size(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildPatternGraph().

315  {
316  if ((*inst).hasRefAttr((*inst).get(v.second).id())) {
317  std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >& refs =
318  inst->getRefAttr(inst->get(v.second).id());
319 
320  for (auto r = refs.begin(); r != refs.end(); ++r) {
321  if (!match.exists(r->first)) {
322  data.outputs().insert(id);
323  break;
324  }
325  }
326  }
327 
328  if (!(data.outputs().size() && (data.outputs().exists(id)))) {
329  for (const auto m : data.matches) {
330  if (this->hasEvidence(
331  std::make_pair((*m)[v.first], &((*m)[v.first]->get(v.second))))) {
332  GUM_ASSERT(inst->type().name() == (*m)[v.first]->type().name());
333  GUM_ASSERT(inst->get(v.second).safeName()
334  == (*m)[v.first]->get(v.second).safeName());
335  data.obs().insert(id);
336  break;
337  }
338  }
339 
340  if (!(data.obs().size() && (data.obs().exists(id))))
341  data.inners().insert(id);
342  }
343  }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __reduceAloneInstances()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances ( RGData data)
private

Add the reduced potentials of instances not in any used patterns.

Definition at line 610 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__cdata_map, gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::prm::StructuredInference< GUM_SCALAR >::__reducedInstances, gum::prm::StructuredInference< GUM_SCALAR >::__trash, gum::prm::PRMInference< GUM_SCALAR >::_sys, gum::prm::StructuredInference< GUM_SCALAR >::CData::aggregators(), gum::prm::PRMInstance< GUM_SCALAR >::begin(), gum::prm::PRMInstance< GUM_SCALAR >::bijection(), gum::prm::StructuredInference< GUM_SCALAR >::CData::c, gum::prm::copyPotential(), gum::prm::StructuredInference< GUM_SCALAR >::CData::elim_order(), gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::prm::PRMInstance< GUM_SCALAR >::end(), gum::List< Val, Alloc >::erase(), gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::prm::PRMInstance< GUM_SCALAR >::get(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::prm::StructuredInference< GUM_SCALAR >::CData::inners(), gum::Set< Key, Alloc >::insert(), gum::List< Val, Alloc >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::CData::instances, gum::prm::StructuredInference< GUM_SCALAR >::CData::mods, gum::prm::StructuredInference< GUM_SCALAR >::CData::moral_graph, gum::prm::StructuredInference< GUM_SCALAR >::CData::outputs(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::pool, gum::prm::StructuredInference< GUM_SCALAR >::CData::pool, gum::List< Val, Alloc >::push_back(), gum::prm::PRMInstance< GUM_SCALAR >::size(), gum::Set< Key, Alloc >::size(), gum::List< Val, Alloc >::size(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph().

611  {
612  StructuredInference< GUM_SCALAR >::CData* data = 0;
613  Potential< GUM_SCALAR >* pot = nullptr;
614  PRMInstance< GUM_SCALAR >* inst = nullptr;
615 
616  for (const auto& elt : *this->_sys) {
617  inst = elt.second;
618 
619  if (!__reducedInstances.exists(inst)) {
620  // Checking if its not an empty class
621  if (inst->size()) {
622  Set< Potential< GUM_SCALAR >* > pool;
623 
624  try {
625  data = __cdata_map[&(inst->type())];
626  } catch (NotFound&) {
627  data = new StructuredInference< GUM_SCALAR >::CData(inst->type());
628  __cdata_map.insert(&(inst->type()), data);
629  }
630 
631  data->instances.insert(inst);
632  // Filling up the partial ordering
633  List< NodeSet > partial_order;
634 
635  if (data->inners().size()) partial_order.push_back(data->inners());
636 
637  if (data->aggregators().size())
638  for (const auto agg : data->aggregators())
639  partial_order[0].insert(agg);
640 
641  if (data->outputs().size()) partial_order.push_back(data->outputs());
642 
643  if (__query.first == inst) {
644  // First case, the instance contains the query
645  partial_order[0].erase(__query.second->id());
646 
647  if (partial_order[0].empty()) partial_order.erase(0);
648 
649  if (partial_order.size() > 1) {
650  partial_order[1].erase(__query.second->id());
651 
652  if (partial_order[1].empty()) partial_order.erase(1);
653  }
654 
655  NodeSet query_set;
656  query_set.insert(__query.second->id());
657  partial_order.insert(query_set);
658 
659  // Adding the potentials
660  for (auto attr = inst->begin(); attr != inst->end(); ++attr)
661  pool.insert(&(
662  const_cast< Potential< GUM_SCALAR >& >((*(attr.val())).cpf())));
663 
664  // Adding evidences if any
665  if (this->hasEvidence(inst))
666  for (const auto& elt : this->evidence(inst))
667  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
668 
669  PartialOrderedTriangulation t(
670  &(data->moral_graph), &(data->mods), &(partial_order));
671  const std::vector< NodeId >& v = t.eliminationOrder();
672 
673  if (partial_order.size() > 1)
674  for (size_t idx = 0; idx < partial_order[0].size(); ++idx)
676  &(inst->get(v[idx]).type().variable()), pool, __trash);
677  } else if (this->hasEvidence(inst)) {
678  // Second case, the instance has evidences
679  // Adding the potentials
680  for (const auto elt : *inst)
681  pool.insert(
682  &const_cast< Potential< GUM_SCALAR >& >(elt.second->cpf()));
683 
684  // Adding evidences
685  for (const auto& elt : this->evidence(inst))
686  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
687 
688  PartialOrderedTriangulation t(
689  &(data->moral_graph), &(data->mods), &(partial_order));
690 
691  for (size_t idx = 0; idx < partial_order[0].size(); ++idx)
693  &(inst->get(t.eliminationOrder()[idx]).type().variable()),
694  pool,
695  __trash);
696  } else {
697  // Last cast, the instance neither contains evidences nor
698  // instances
699  // We translate the class level potentials into the instance ones
700  // and
701  // proceed with elimination
702  for (const auto srcPot : data->pool) {
703  pot = copyPotential(inst->bijection(), *srcPot);
704  pool.insert(pot);
705  __trash.insert(pot);
706  }
707 
708  for (const auto agg : data->c.aggregates())
709  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(
710  inst->get(agg->id()).cpf())));
711 
712  // We eliminate inner aggregators with their parents if necessary
713  // (see
714  // CData constructor)
715  Size size = data->inners().size() + data->aggregators().size();
716 
717  for (size_t idx = data->inners().size(); idx < size; ++idx)
719  &(inst->get(data->elim_order()[idx]).type().variable()),
720  pool,
721  __trash);
722  }
723 
724  for (const auto pot : pool)
725  rg_data.pool.insert(pot);
726  }
727  }
728  }
729  }
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...
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PRMInference< GUM_SCALAR >::Chain __query
The query.
Set< const PRMInstance< GUM_SCALAR > *> __reducedInstances
This keeps track of reduced instances.
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:27
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
PRMSystem< GUM_SCALAR > const * _sys
The Model on which inference is done.
Definition: PRMInference.h:213
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __reducePattern()

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern ( const gspan::Pattern p)
private

Proceed with the elimination of all inner variables (observed or not) of all usable matches of Pattern p. Inner variables which are part of the query are not eliminated.

Definition at line 241 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__buildPatternGraph(), gum::prm::StructuredInference< GUM_SCALAR >::__elim_map, gum::prm::StructuredInference< GUM_SCALAR >::__eliminateObservedNodes(), gum::prm::StructuredInference< GUM_SCALAR >::__eliminateObservedNodesInSource(), gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::prm::StructuredInference< GUM_SCALAR >::__query_data, gum::prm::StructuredInference< GUM_SCALAR >::__reducedInstances, gum::prm::StructuredInference< GUM_SCALAR >::__removeBarrenNodes(), gum::prm::StructuredInference< GUM_SCALAR >::__translatePotSet(), gum::prm::StructuredInference< GUM_SCALAR >::__trash, gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key, Alloc >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::obs_time, gum::prm::StructuredInference< GUM_SCALAR >::plopTimer, and gum::Timer::step().

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph().

242  {
243  Set< Potential< GUM_SCALAR >* > pool;
244  typename StructuredInference< GUM_SCALAR >::PData data(*p,
245  __gspan->matches(*p));
246  __buildPatternGraph(data, pool, **(data.matches.begin()));
247  __removeBarrenNodes(data, pool);
248  PartialOrderedTriangulation t(
249  &(data.graph), &(data.mod), data.partial_order());
250  const std::vector< NodeId >& elim_order = t.eliminationOrder();
251 
252  for (size_t i = 0; i < data.inners().size(); ++i)
253  if (!data.barren.exists(elim_order[i]))
254  eliminateNode(data.vars.second(elim_order[i]), pool, __trash);
255 
256  typename GSpan< GUM_SCALAR >::MatchedInstances fake_patterns;
258  data.matches.begin();
259 
260  for (const auto elt : **iter)
261  __reducedInstances.insert(elt);
262 
263  if (data.obs().size())
264  __elim_map.insert(
265  *iter,
266  __eliminateObservedNodesInSource(data, pool, **iter, elim_order));
267  else
268  __elim_map.insert(*iter, new Set< Potential< GUM_SCALAR >* >(pool));
269 
270  ++iter;
271 
272  if (data.obs().size()) {
273  for (; iter != data.matches.end(); ++iter) {
274  try {
275  __elim_map.insert(
276  *iter, __eliminateObservedNodes(data, pool, **iter, elim_order));
277  } catch (OperationNotAllowed&) { fake_patterns.insert(*iter); }
278  }
279  } else {
280  for (; iter != data.matches.end(); ++iter) {
281  try {
282  __elim_map.insert(*iter, __translatePotSet(data, pool, **iter));
283  } catch (OperationNotAllowed&) { fake_patterns.insert(*iter); }
284  }
285  }
286 
287  for (const auto pat : fake_patterns) {
288  for (const auto elt : *pat)
289  __reducedInstances.erase(elt);
290 
291  data.matches.erase(pat);
292  }
293 
294  obs_time += plopTimer.step();
295 
296  if (data.queries().size())
297  for (const auto m : data.matches)
298  if (!(m->exists(
299  const_cast< PRMInstance< GUM_SCALAR >* >(__query.first))))
300  eliminateNode(&(m->atPos(__query_data.first)
301  ->get(__query_data.second)
302  .type()
303  .variable()),
304  *(__elim_map[m]),
305  __trash);
306  }
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...
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
Set< Potential< GUM_SCALAR > *> __trash
Keeping track of create potentials to delete them after inference.
Set< Sequence< PRMInstance< GUM_SCALAR > *> *> MatchedInstances
Code alias.
Definition: gspan.h:167
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
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)
void __removeBarrenNodes(typename StructuredInference::PData &data, Set< Potential< GUM_SCALAR > * > &pool)
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...
PRMInference< GUM_SCALAR >::Chain __query
The query.
Set< const PRMInstance< GUM_SCALAR > *> __reducedInstances
This keeps track of reduced instances.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
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.
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.
std::pair< Idx, std::string > __query_data
SetIterator< Key > const_iterator
Types for STL compliance.
Definition: set.h:175
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __removeBarrenNodes()

template<typename GUM_SCALAR>
void gum::prm::StructuredInference< GUM_SCALAR >::__removeBarrenNodes ( typename StructuredInference< GUM_SCALAR >::PData data,
Set< Potential< GUM_SCALAR > * > &  pool 
)
private

Definition at line 419 of file structuredInference_tpl.h.

References gum::SequenceImplementation< Key, Alloc, Gen >::back(), gum::prm::StructuredInference< GUM_SCALAR >::PData::barren, gum::MultiDimDecorator< GUM_SCALAR >::erase(), gum::SequenceImplementation< Key, Alloc, Gen >::erase(), gum::Set< Key, Alloc >::exists(), gum::prm::StructuredInference< GUM_SCALAR >::PData::graph, gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::Set< Key, Alloc >::insert(), gum::SequenceImplementation< Key, Alloc, Gen >::insert(), gum::EdgeGraphPart::neighbours(), gum::SequenceImplementation< Key, Alloc, Gen >::size(), and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern().

421  {
422  Sequence< NodeId > candidates;
423 
424  for (const auto node : data.barren) {
425  for (const auto pot : pool)
426  if (pot->contains(*data.vars.second(node))) {
427  pool.erase(pot);
428  break;
429  }
430 
431  for (const auto nei : data.graph.neighbours(node))
432  if (data.inners().exists(nei)) {
433  try {
434  candidates.insert(nei);
435  } catch (DuplicateElement&) {}
436  }
437  }
438 
439  NodeId node;
440  Potential< GUM_SCALAR >* my_pot = nullptr;
441  short count = 0;
442 
443  while (candidates.size()) {
444  node = candidates.back();
445  candidates.erase(node);
446  count = 0;
447 
448  for (const auto pot : pool) {
449  if (pot->contains(*data.vars.second(node))) {
450  ++count;
451  my_pot = pot;
452  }
453  }
454 
455  if (count == 1) {
456  pool.erase(my_pot);
457  data.barren.insert(node);
458 
459  for (const auto nei : data.graph.neighbours(node)) {
460  if (data.inners().exists(nei)) {
461  try {
462  candidates.insert(nei);
463  } catch (DuplicateElement&) {}
464  }
465  }
466  }
467  }
468  }
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:35
void erase(const Key &k)
Remove an element from the sequence.
Definition: sequence_tpl.h:450
Size NodeId
Type for node ids.
Definition: graphElements.h:97
const Key & back() const
Returns the last element of the sequence.
Definition: sequence_tpl.h:565
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __removeNode()

template<typename GUM_SCALAR>
INLINE void gum::prm::StructuredInference< GUM_SCALAR >::__removeNode ( typename StructuredInference< GUM_SCALAR >::PData data,
NodeId  id,
Set< Potential< GUM_SCALAR > * > &  pool 
)
private

Definition at line 1023 of file structuredInference_tpl.h.

References gum::Set< Key, Alloc >::erase(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::eraseFirst(), gum::UndiGraph::eraseNode(), gum::NodeGraphPart::exists(), gum::Set< Key, Alloc >::exists(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::existsFirst(), gum::prm::StructuredInference< GUM_SCALAR >::PData::graph, gum::prm::StructuredInference< GUM_SCALAR >::PData::inners(), gum::prm::StructuredInference< GUM_SCALAR >::PData::map, gum::prm::StructuredInference< GUM_SCALAR >::PData::mod, gum::prm::StructuredInference< GUM_SCALAR >::PData::node2attr, gum::prm::StructuredInference< GUM_SCALAR >::CData::pool, gum::prm::StructuredInference< GUM_SCALAR >::PData::pots, and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

1026  {
1027  data.graph.eraseNode(id);
1028  GUM_ASSERT(!data.graph.exists(id));
1029  data.mod.erase(id);
1030  GUM_ASSERT(!data.mod.exists(id));
1031  data.node2attr.eraseFirst(id);
1032  GUM_ASSERT(!data.node2attr.existsFirst(id));
1033  data.map.erase(id);
1034  GUM_ASSERT(!data.map.exists(id));
1035  data.vars.eraseFirst(id);
1036  GUM_ASSERT(!data.vars.existsFirst(id));
1037  data.inners().erase(id);
1038  GUM_ASSERT(!data.inners().exists(id));
1039  pool.erase(data.pots[id]);
1040  GUM_ASSERT(!pool.exists(data.pots[id]));
1041  data.pots.erase(id);
1042  GUM_ASSERT(!data.pots.exists(id));
1043  }
+ Here is the call graph for this function:

◆ __str() [1/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::StructuredInference< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMAttribute< GUM_SCALAR > *  a 
) const
private

Definition at line 976 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__dot, gum::prm::PRMObject::name(), and gum::prm::PRMClassElement< GUM_SCALAR >::safeName().

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__buildPatternGraph().

978  {
979  return i->name() + __dot + a->safeName();
980  }
std::string __dot
Unreduce the match containing the query.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __str() [2/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::StructuredInference< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMAttribute< GUM_SCALAR > &  a 
) const
private

Definition at line 983 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__dot, gum::prm::PRMObject::name(), and gum::prm::PRMClassElement< GUM_SCALAR >::safeName().

985  {
986  return i->name() + __dot + a.safeName();
987  }
std::string __dot
Unreduce the match containing the query.
+ Here is the call graph for this function:

◆ __str() [3/3]

template<typename GUM_SCALAR >
INLINE std::string gum::prm::StructuredInference< GUM_SCALAR >::__str ( const PRMInstance< GUM_SCALAR > *  i,
const PRMSlotChain< GUM_SCALAR > &  a 
) const
private

Definition at line 990 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__dot, gum::prm::PRMSlotChain< GUM_SCALAR >::lastElt(), and gum::prm::PRMObject::name().

992  {
993  return i->name() + __dot + a.lastElt().safeName();
994  }
std::string __dot
Unreduce the match containing the query.
+ Here is the call graph for this function:

◆ __translatePotSet()

template<typename GUM_SCALAR>
Set< Potential< GUM_SCALAR > *> * gum::prm::StructuredInference< GUM_SCALAR >::__translatePotSet ( typename StructuredInference< GUM_SCALAR >::PData data,
const Set< Potential< GUM_SCALAR > * > &  pool,
const Sequence< PRMInstance< GUM_SCALAR > * > &  match 
)
private

Translate a given Potential Set into one w.r.t. variables in match.

Definition at line 516 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__reducedInstances, gum::prm::copyPotential(), gum::BijectionImplementation< T1, T2, Alloc, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::existsFirst(), gum::BijectionImplementation< T1, T2, Alloc, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::first(), GUM_ERROR, gum::BijectionImplementation< T1, T2, Alloc, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::insert(), gum::prm::StructuredInference< GUM_SCALAR >::PData::map, gum::prm::StructuredInference< GUM_SCALAR >::PData::matches, and gum::prm::StructuredInference< GUM_SCALAR >::PData::vars.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__eliminateObservedNodes(), and gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern().

519  {
520 #ifdef DEBUG
521 
523  data.matches.begin();
524  iter != data.matches.end();
525  ++iter) {
526  GUM_ASSERT((**iter).size() == match.size());
527 
528  for (Size idx = 0; idx < match.size(); ++idx) {
529  GUM_ASSERT((**iter).atPos(idx)->type() == match.atPos(idx)->type());
530  }
531  }
532 
533 #endif
534  Set< Potential< GUM_SCALAR >* >* my_pool =
535  new Set< Potential< GUM_SCALAR >* >();
536  std::pair< Idx, std::string > target;
537  Bijection< const DiscreteVariable*, const DiscreteVariable* > bij;
538  const Sequence< PRMInstance< GUM_SCALAR >* >& source =
539  **(data.matches.begin());
540 
541  for (Size idx = 0; idx < match.size(); ++idx) {
542  __reducedInstances.insert(match[idx]);
543  const auto& chains = source[idx]->type().slotChains();
544 
545  for (const auto sc : chains) {
546 #ifdef DEBUG
547  GUM_ASSERT(!(sc->isMultiple()));
548 #endif
549 
550  try {
551  bij.insert(&(source[idx]
552  ->getInstance(sc->id())
553  .get(sc->lastElt().safeName())
554  .type()
555  .variable()),
556  &(match[idx]
557  ->getInstance(sc->id())
558  .get(sc->lastElt().safeName())
559  .type()
560  .variable()));
561  } catch (DuplicateElement&) {
562  try {
563  if (bij.first(&(match[idx]
564  ->getInstance(sc->id())
565  .get(sc->lastElt().safeName())
566  .type()
567  .variable()))
568  != &(source[idx]
569  ->getInstance(sc->id())
570  .get(sc->lastElt().safeName())
571  .type()
572  .variable())) {
573  delete my_pool;
574  GUM_ERROR(OperationNotAllowed, "fake pattern");
575  }
576  } catch (NotFound&) {
577  delete my_pool;
578  GUM_ERROR(OperationNotAllowed, "fake pattern");
579  }
580  }
581  }
582  }
583 
584  for (const auto p : pool) {
585  for (const auto v : p->variablesSequence()) {
586  try {
587  target = data.map[data.vars.first(v)];
588  bij.insert(
589  v, &(match[target.first]->get(target.second).type().variable()));
590  } catch (NotFound&) {
591  GUM_ASSERT(bij.existsFirst(v));
592  } catch (DuplicateElement&) {}
593  }
594 
595  try {
596  my_pool->insert(copyPotential(bij, *p));
597  } catch (Exception&) {
598  for (const auto pot : *my_pool)
599  delete pot;
600 
601  delete my_pool;
602  GUM_ERROR(OperationNotAllowed, "fake pattern");
603  }
604  }
605 
606  return my_pool;
607  }
Set< const PRMInstance< GUM_SCALAR > *> __reducedInstances
This keeps track of reduced instances.
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:27
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
SetIterator< Key > const_iterator
Types for STL compliance.
Definition: set.h:175
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _evidenceAdded() [1/2]

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::_evidenceAdded ( const typename PRMInference< GUM_SCALAR >::Chain chain)
protectedvirtual

See PRMInference::_evidenceAdded().

Definition at line 92 of file structuredInference_tpl.h.

93  {}

◆ _evidenceAdded() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::_evidenceAdded ( const Chain chain)
protectedpure virtualinherited

This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ _evidenceRemoved() [1/2]

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::_evidenceRemoved ( const typename PRMInference< GUM_SCALAR >::Chain chain)
protectedvirtual

See PRMInference::_evidenceRemoved().

Definition at line 96 of file structuredInference_tpl.h.

97  {}

◆ _evidenceRemoved() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::_evidenceRemoved ( const Chain chain)
protectedpure virtualinherited

This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ _joint() [1/2]

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::_joint ( const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &  queries,
Potential< GUM_SCALAR > &  j 
)
protectedvirtual

See PRMInference::_joint().

Definition at line 173 of file structuredInference_tpl.h.

References GUM_ERROR.

175  {
176  GUM_ERROR(FatalError, "not implemented");
177  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ _joint() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::_joint ( const std::vector< Chain > &  queries,
Potential< GUM_SCALAR > &  j 
)
protectedpure virtualinherited

Generic method to compute the marginal of given element.

Parameters
queriesSet of pairs of PRMInstance<GUM_SCALAR> and PRMAttribute<GUM_SCALAR>.
jCPF filled with the joint probability of queries. It is initialized properly.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ _marginal() [1/2]

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::_marginal ( const typename PRMInference< GUM_SCALAR >::Chain chain,
Potential< GUM_SCALAR > &  m 
)
protectedvirtual

See PRMInference::_marginal().

Definition at line 100 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph(), gum::prm::StructuredInference< GUM_SCALAR >::__found_query, gum::prm::StructuredInference< GUM_SCALAR >::__pdata, gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::Set< Key, Alloc >::begin(), gum::MultiDimCombinationDefault< GUM_SCALAR, TABLE >::combine(), gum::MultiDimDecorator< GUM_SCALAR >::contains(), gum::Instantiation::end(), gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::prm::StructuredInference< GUM_SCALAR >::full_time, gum::MultiDimDecorator< GUM_SCALAR >::get(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::Instantiation::inc(), gum::Set< Key, Alloc >::insert(), gum::prm::multPotential(), gum::Potential< GUM_SCALAR >::normalize(), gum::prm::StructuredInference< GUM_SCALAR >::RGData::pool, gum::Timer::reset(), gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), gum::Set< Key, Alloc >::size(), gum::Timer::step(), gum::prm::StructuredInference< GUM_SCALAR >::timer, and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

102  {
103  timer.reset();
104  __found_query = false;
105  __query = chain;
106  typename StructuredInference< GUM_SCALAR >::RGData data;
107 
108  if (!this->hasEvidence() && (chain.second->cpf().nbrDim() == 1)) {
109  Instantiation i(m);
110 
111  for (i.setFirst(); !i.end(); i.inc())
112  m.set(i, chain.second->cpf().get(i));
113 
114  return;
115  } else if (this->hasEvidence(chain)) {
116  Instantiation i(m);
117  const Potential< GUM_SCALAR >* e =
118  this->evidence(__query.first)[__query.second->id()];
119 
120  for (i.setFirst(); !i.end(); i.inc())
121  m.set(i, e->get(i));
122 
123  return;
124  }
125 
126  __buildReduceGraph(data);
127  Set< const Potential< GUM_SCALAR >* > pots;
128 
129  if (data.pool.size() > 1) {
130  for (const auto pot : data.pool)
131  if (pot->contains(__query.second->type().variable())) pots.insert(pot);
132 
133  if (pots.size() == 1) {
134  Potential< GUM_SCALAR >* pot =
135  const_cast< Potential< GUM_SCALAR >* >(*(pots.begin()));
136  GUM_ASSERT(pot->contains(__query.second->type().variable()));
137  GUM_ASSERT(pot->variablesSequence().size() == 1);
138  Instantiation i(*pot), j(m);
139 
140  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
141  m.set(j, pot->get(i));
142  } else {
143  MultiDimCombinationDefault< GUM_SCALAR, Potential > Comb(multPotential);
144  Potential< GUM_SCALAR >* tmp = Comb.combine(pots);
145  Instantiation i(m), j(*tmp);
146 
147  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
148  m.set(i, tmp->get(j));
149 
150  delete tmp;
151  }
152  } else {
153  Potential< GUM_SCALAR >* pot = *(data.pool.begin());
154  GUM_ASSERT(pot->contains(__query.second->type().variable()));
155  GUM_ASSERT(pot->variablesSequence().size() == 1);
156  Instantiation i(*pot), j(m);
157 
158  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
159  m.set(j, pot->get(i));
160  }
161 
162  m.normalize();
163 
164  if (__pdata) {
165  delete __pdata;
166  __pdata = 0;
167  }
168 
169  full_time = timer.step();
170  }
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PData * __pdata
The pattern data of the pattern which one of its matches contains the query.
bool __found_query
Flag with an explicit name.
void __buildReduceGraph(RGData &data)
This calls __reducePattern() over each pattern and then build the reduced graph which is used for inf...
void reset()
Reset the timer.
Definition: timer_inl.h:29
PRMInference< GUM_SCALAR >::Chain __query
The query.
void set(const Key &key, const Val &default_value)
Add a new property or modify it if it already existed.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
Potential< GUM_SCALAR > * multPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)
+ Here is the call graph for this function:

◆ _marginal() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::_marginal ( const Chain chain,
Potential< GUM_SCALAR > &  m 
)
protectedpure virtualinherited

Generic method to compute the marginal of given element.

Parameters
chain
mCPF filled with the marginal of elt. It is initialized properly.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ addEvidence()

template<typename GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::addEvidence ( const Chain chain,
const Potential< GUM_SCALAR > &  p 
)
inherited

Add an evidence to the given instance's elt.

Parameters
chainThe variable being observed.
pThe Potential added (by copy) as evidence.
Exceptions
NotFoundRaised if elt does not belong to i.
OperationNotAllowedRaised if p is inconsistent with elt.

Definition at line 106 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::observe().

107  {
108  if (chain.first->exists(chain.second->id())) {
109  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
110  GUM_ERROR(OperationNotAllowed,
111  "illegal evidence for the given PRMAttribute.");
112 
113  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
114  e->add(chain.second->type().variable());
115  Instantiation i(*e);
116 
117  for (i.setFirst(); !i.end(); i.inc())
118  e->set(i, p.get(i));
119 
120  PRMInference< GUM_SCALAR >::EMap& emap = __EMap(chain.first);
121 
122  if (emap.exists(chain.second->id())) {
123  delete emap[chain.second->id()];
124  emap[chain.second->id()] = e;
125  } else {
126  emap.insert(chain.second->id(), e);
127  }
128 
129  _evidenceAdded(chain);
130  } else {
131  GUM_ERROR(NotFound,
132  "the given PRMAttribute does not belong to this "
133  "Instance<GUM_SCALAR>.");
134  }
135  }
virtual void _evidenceAdded(const Chain &chain)=0
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference...
EMap & __EMap(const PRMInstance< GUM_SCALAR > *i)
Private getter over __evidences, if necessary creates an EMap for i.
NodeProperty< const Potential< GUM_SCALAR > *> EMap
Code alias.
Definition: PRMInference.h:57
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ clearEvidence()

template<typename GUM_SCALAR >
void gum::prm::PRMInference< GUM_SCALAR >::clearEvidence ( )
inherited

Remove all evidences.

Definition at line 33 of file PRMInference_tpl.h.

33  {
34  for (const auto& elt : __evidences) {
35  for (const auto& elt2 : *elt.second)
36  delete elt2.second;
37 
38  delete elt.second;
39  }
40 
41  __evidences.clear();
42  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232

◆ evidence() [1/4]

template<typename GUM_SCALAR>
INLINE PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > &  i)
inherited

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 153 of file PRMInference_tpl.h.

Referenced by gum::prm::SVED< GUM_SCALAR >::__insertEvidence(), gum::prm::SVE< GUM_SCALAR >::__insertEvidence(), gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), and gum::prm::StructuredInference< GUM_SCALAR >::_marginal().

153  {
154  try {
155  return *(__evidences[&i]);
156  } catch (NotFound&) {
157  GUM_ERROR(NotFound, "this instance has no evidence.");
158  }
159  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ evidence() [2/4]

template<typename GUM_SCALAR>
INLINE PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > *  i)
inherited

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 174 of file PRMInference_tpl.h.

174  {
175  try {
176  return *(__evidences[i]);
177  } catch (NotFound&) {
178  GUM_ERROR(NotFound, "this instance has no evidence.");
179  }
180  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ evidence() [3/4]

template<typename GUM_SCALAR>
INLINE const PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > &  i) const
inherited

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 163 of file PRMInference_tpl.h.

164  {
165  try {
166  return *(__evidences[&i]);
167  } catch (NotFound&) {
168  GUM_ERROR(NotFound, "this instance has no evidence.");
169  }
170  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ evidence() [4/4]

template<typename GUM_SCALAR>
INLINE const PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > *  i) const
inherited

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 184 of file PRMInference_tpl.h.

185  {
186  try {
187  return *(__evidences[i]);
188  } catch (NotFound&) {
189  GUM_ERROR(NotFound, "this instance has no evidence.");
190  }
191  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ gspan() [1/2]

template<typename GUM_SCALAR >
INLINE GSpan< GUM_SCALAR > & gum::prm::StructuredInference< GUM_SCALAR >::gspan ( )

Returns the instance of gspan used to search patterns.

Definition at line 1012 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan.

1012  {
1013  return *__gspan;
1014  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.

◆ gspan() [2/2]

template<typename GUM_SCALAR >
INLINE const GSpan< GUM_SCALAR > & gum::prm::StructuredInference< GUM_SCALAR >::gspan ( ) const

Returns the instance of gspan used to search patterns.

Definition at line 1018 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan.

1018  {
1019  return *__gspan;
1020  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.

◆ hasEvidence() [1/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const PRMInstance< GUM_SCALAR > &  i) const
inherited

Returns true if i has evidence.

Definition at line 194 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::observe(), and gum::prm::o3prmr::O3prmrInterpreter::unobserve().

195  {
196  return __evidences.exists(&i);
197  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
+ Here is the caller graph for this function:

◆ hasEvidence() [2/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const PRMInstance< GUM_SCALAR > *  i) const
inherited

Returns EMap of evidences over i.

Definition at line 200 of file PRMInference_tpl.h.

201  {
202  return __evidences.exists(i);
203  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232

◆ hasEvidence() [3/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const Chain chain) const
inherited

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 206 of file PRMInference_tpl.h.

206  {
207  return (hasEvidence(chain.first))
208  ? evidence(chain.first).exists(chain.second->id())
209  : false;
210  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

◆ hasEvidence() [4/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( ) const
inherited

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 213 of file PRMInference_tpl.h.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__insertNodeInElimLists(), gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), and gum::prm::StructuredInference< GUM_SCALAR >::_marginal().

213  {
214  return (__evidences.size() != (Size)0);
215  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ Here is the caller graph for this function:

◆ info()

template<typename GUM_SCALAR >
std::string gum::prm::StructuredInference< GUM_SCALAR >::info ( ) const

Definition at line 180 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::StructuredInference< GUM_SCALAR >::full_time, gum::prm::StructuredInference< GUM_SCALAR >::inner_time, gum::prm::StructuredInference< GUM_SCALAR >::mining_time, gum::prm::StructuredInference< GUM_SCALAR >::obs_time, gum::prm::StructuredInference< GUM_SCALAR >::pattern_time, and gum::prm::StructuredInference< GUM_SCALAR >::triang_time.

180  {
181  std::stringstream s;
182  s << "Triangulation time: " << triang_time << std::endl;
183  s << "Pattern mining time: " << mining_time << std::endl;
184  s << "Pattern elimination time: " << pattern_time << std::endl;
185  s << "Inner node elimination time: " << inner_time << std::endl;
186  s << "Observed node elimination time: " << obs_time << std::endl;
187  s << "Full inference time: " << full_time << std::endl;
188  s << "#patterns: " << __gspan->patterns().size() << std::endl;
189  Size count = 0;
190  typedef std::vector< gspan::Pattern* >::const_iterator Iter;
191 
192  for (Iter p = __gspan->patterns().begin(); p != __gspan->patterns().end();
193  ++p) {
194  if (__gspan->matches(**p).size()) {
195  s << "Pattern n°" << count++
196  << " match count: " << __gspan->matches(**p).size() << std::endl;
197  s << "Pattern n°" << count++ << " instance count: " << (**p).size()
198  << std::endl;
199  }
200  }
201 
202  return s.str();
203  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45

◆ joint()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::joint ( const std::vector< Chain > &  chains,
Potential< GUM_SCALAR > &  j 
)
inherited

Compute the joint probability of the formals attributes pointed by chains and stores it in m.

Parameters
chainsA Set of strings of the form instance.attribute.
jAn empty CPF which will be filed by the joint probability over chains.
Exceptions
NotFoundRaised if some chain in chains does not point to a formal attribute.
OperationNotAllowedRaise if m is not empty.

Definition at line 260 of file PRMInference_tpl.h.

262  {
263  if (j.nbrDim() > 0) {
264  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
265  }
266 
267  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
268  j.add(chain->second->type().variable());
269  }
270 
271  _joint(chains, j);
272  }
virtual void _joint(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
Generic method to compute the marginal of given element.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ marginal()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::marginal ( const Chain chain,
Potential< GUM_SCALAR > &  m 
)
inherited

Compute the marginal of the formal attribute pointed by chain and stores it in m.

Parameters
chainA string of the form instance.attribute.
mAn empty CPF which will be filed by the marginal of chain.
Exceptions
NotFoundRaised if chain is invalid.
WrongTypeRaised if chain does not point to an PRMAttribute<GUM_SCALAR>.
OperationNotAllowedRaise if m is not empty.

Definition at line 231 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::query().

233  {
234  if (m.nbrDim() > 0) {
235  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
236  }
237 
238  if (hasEvidence(chain)) {
239  m.add(chain.second->type().variable());
240  const Potential< GUM_SCALAR >& e =
241  *(evidence(chain.first)[chain.second->id()]);
242  Instantiation i(m), j(e);
243 
244  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
245  m.set(i, e.get(j));
246  } else {
247  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
248  typename PRMInference< GUM_SCALAR >::Chain good_chain = std::make_pair(
249  chain.first, &(chain.first->get(chain.second->safeName())));
250  m.add(good_chain.second->type().variable());
251  _marginal(good_chain, m);
252  } else {
253  m.add(chain.second->type().variable());
254  _marginal(chain, m);
255  }
256  }
257  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void _marginal(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the marginal of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:54
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ name()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::StructuredInference< GUM_SCALAR >::name ( ) const
virtual

Tells this algorithm to use pattern mining or not.

Implements gum::prm::PRMInference< GUM_SCALAR >.

Definition at line 1007 of file structuredInference_tpl.h.

1007  {
1008  return "StructuredInference";
1009  }

◆ operator=()

template<typename GUM_SCALAR >
StructuredInference< GUM_SCALAR > & gum::prm::StructuredInference< GUM_SCALAR >::operator= ( const StructuredInference< GUM_SCALAR > &  source)

Copy operator.

Definition at line 81 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__gspan, gum::prm::PRMInference< GUM_SCALAR >::_prm, and gum::prm::PRMInference< GUM_SCALAR >::_sys.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::~StructuredInference().

81  {
82  this->_prm = source._prm;
83  this->_sys = source._sys;
84 
85  if (this->__gspan) delete this->__gspan;
86 
87  this->__gspan = new GSpan< GUM_SCALAR >(*(this->_prm), *(this->_sys));
88  return *this;
89  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:210
PRMSystem< GUM_SCALAR > const * _sys
The Model on which inference is done.
Definition: PRMInference.h:213
+ Here is the caller graph for this function:

◆ removeEvidence()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMInference< GUM_SCALAR >::removeEvidence ( const Chain chain)
inherited

Remove evidence on the given instance's elt.

Parameters
chainThe variable being observed.
Exceptions
NotFoundRaised if the given names are not found.
WrongTypeRaised if the elt is not an PRMAttribute<GUM_SCALAR>.

Definition at line 218 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::unobserve().

218  {
219  try {
220  if (__EMap(chain.first).exists(chain.second->id())) {
221  _evidenceRemoved(chain);
222  delete __EMap(chain.first)[chain.second->id()];
223  __EMap(chain.first).erase(chain.second->id());
224  }
225  } catch (NotFound&) {
226  // Ok, we are only removing
227  }
228  }
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
virtual void _evidenceRemoved(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
EMap & __EMap(const PRMInstance< GUM_SCALAR > *i)
Private getter over __evidences, if necessary creates an EMap for i.
+ Here is the caller graph for this function:

◆ searchPatterns()

template<typename GUM_SCALAR >
void gum::prm::StructuredInference< GUM_SCALAR >::searchPatterns ( )

Search for patterns without doing any computations.

Definition at line 962 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__buildReduceGraph(), gum::prm::StructuredInference< GUM_SCALAR >::__found_query, gum::prm::StructuredInference< GUM_SCALAR >::__query, gum::prm::PRMInference< GUM_SCALAR >::_sys, and gum::prm::PRMInstance< GUM_SCALAR >::begin().

962  {
963  const PRMInstance< GUM_SCALAR >* i = (this->_sys->begin()).val();
964  __query = std::make_pair(i, i->begin().val());
965  __found_query = false;
966  typename StructuredInference< GUM_SCALAR >::RGData data;
967  __buildReduceGraph(data);
968  }
bool __found_query
Flag with an explicit name.
void __buildReduceGraph(RGData &data)
This calls __reducePattern() over each pattern and then build the reduced graph which is used for inf...
PRMInference< GUM_SCALAR >::Chain __query
The query.
PRMSystem< GUM_SCALAR > const * _sys
The Model on which inference is done.
Definition: PRMInference.h:213
+ Here is the call graph for this function:

◆ setPatternMining()

template<typename GUM_SCALAR >
INLINE void gum::prm::StructuredInference< GUM_SCALAR >::setPatternMining ( bool  b)

Tells this algorithm to use pattern mining or not.

Definition at line 971 of file structuredInference_tpl.h.

References gum::prm::StructuredInference< GUM_SCALAR >::__mining.

971  {
972  __mining = b;
973  }
bool __mining
Flag which tells to use pattern mining or not.

Member Data Documentation

◆ __cdata_map

template<typename GUM_SCALAR>
HashTable< const PRMClass< GUM_SCALAR >*, CData* > gum::prm::StructuredInference< GUM_SCALAR >::__cdata_map
private

Mapping between a Class<GUM_SCALAR> and data about instances reduced using only Class<GUM_SCALAR> level information.

Definition at line 260 of file structuredInference.h.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), and gum::prm::StructuredInference< GUM_SCALAR >::~StructuredInference().

◆ __dot

template<typename GUM_SCALAR>
std::string gum::prm::StructuredInference< GUM_SCALAR >::__dot
private

Unreduce the match containing the query.

Used to create strings

Definition at line 365 of file structuredInference.h.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__str().

◆ __elim_map

template<typename GUM_SCALAR>
HashTable< const Sequence< PRMInstance< GUM_SCALAR >* >*, Set< Potential< GUM_SCALAR >* >* > gum::prm::StructuredInference< GUM_SCALAR >::__elim_map
private

Mapping between a Pattern's match and its potential pool after inner variables were eliminated.

Definition at line 254 of file structuredInference.h.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__addEdgesInReducedGraph(), gum::prm::StructuredInference< GUM_SCALAR >::__reducePattern(), and gum::prm::StructuredInference< GUM_SCALAR >::~StructuredInference().

◆ __found_query

◆ __gspan

◆ __mining

template<typename GUM_SCALAR>
bool gum::prm::StructuredInference< GUM_SCALAR >::__mining
private

◆ __outputs

template<typename GUM_SCALAR>
HashTable< const PRMClass< GUM_SCALAR >*, std::vector< NodeId >* > gum::prm::StructuredInference< GUM_SCALAR >::__outputs
private

◆ __pdata

template<typename GUM_SCALAR>
PData* gum::prm::StructuredInference< GUM_SCALAR >::__pdata
private

The pattern data of the pattern which one of its matches contains the query.

Definition at line 275 of file structuredInference.h.

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::_marginal(), and gum::prm::StructuredInference< GUM_SCALAR >::~StructuredInference().

◆ __query

◆ __query_data

template<typename GUM_SCALAR>
std::pair< Idx, std::string > gum::prm::StructuredInference< GUM_SCALAR >::__query_data
private

◆ __reducedInstances

template<typename GUM_SCALAR>
Set< const PRMInstance< GUM_SCALAR >* > gum::prm::StructuredInference< GUM_SCALAR >::__reducedInstances
private

◆ __trash

◆ _prm

template<typename GUM_SCALAR>
PRM< GUM_SCALAR > const* gum::prm::PRMInference< GUM_SCALAR >::_prm
protectedinherited

◆ _sys

◆ full_time

◆ inner_time

◆ mining_time

◆ obs_time

◆ pattern_time

◆ plopTimer

◆ timer

template<typename GUM_SCALAR>
Timer gum::prm::StructuredInference< GUM_SCALAR >::timer

◆ triang_time


The documentation for this class was generated from the following files: