aGrUM  0.13.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

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.

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.

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.

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

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.
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
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

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 731 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 406 of file structuredInference_tpl.h.

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

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

408  {
409  for (const auto mat : data.matches)
410  if (mat->atPos(attr.first)
411  ->hasRefAttr(mat->atPos(attr.first)->get(attr.second).id()))
412  return false;
413 
414  return true;
415  }

+ Here is the caller graph for this function:

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 345 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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...
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
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.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
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.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 493 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().

497  {
498  Set< Potential< GUM_SCALAR >* >* my_pool =
499  __translatePotSet(data, pool, match);
500  std::pair< Idx, std::string > target;
501  size_t end = data.inners().size() + data.obs().size();
502 
503  for (size_t idx = data.inners().size(); idx < end; ++idx) {
504  target = data.map[data.vars.first(data.vars.second(elim_order[idx]))];
505  eliminateNode(&(match[target.first]->get(target.second).type().variable()),
506  *my_pool,
507  __trash);
508  }
509 
510  return my_pool;
511  }
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:

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 471 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().

475  {
476  Set< Potential< GUM_SCALAR >* >* my_pool =
477  new Set< Potential< GUM_SCALAR >* >(pool);
478  std::pair< Idx, std::string > target;
479  size_t end = data.inners().size() + data.obs().size();
480 
481  for (size_t idx = data.inners().size(); idx < end; ++idx) {
482  target = data.map[data.vars.first(data.vars.second(elim_order[idx]))];
483  eliminateNode(&(match[target.first]->get(target.second).type().variable()),
484  *my_pool,
485  __trash);
486  }
487 
488  return my_pool;
489  }
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:

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 308 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().

314  {
315  if ((*inst).hasRefAttr((*inst).get(v.second).id())) {
316  std::vector< std::pair< PRMInstance< GUM_SCALAR >*, std::string > >& refs =
317  inst->getRefAttr(inst->get(v.second).id());
318 
319  for (auto r = refs.begin(); r != refs.end(); ++r) {
320  if (!match.exists(r->first)) {
321  data.outputs().insert(id);
322  break;
323  }
324  }
325  }
326 
327  if (!(data.outputs().size() && (data.outputs().exists(id)))) {
328  for (const auto m : data.matches) {
329  if (this->hasEvidence(
330  std::make_pair((*m)[v.first], &((*m)[v.first]->get(v.second))))) {
331  GUM_ASSERT(inst->type().name() == (*m)[v.first]->type().name());
332  GUM_ASSERT(inst->get(v.second).safeName()
333  == (*m)[v.first]->get(v.second).safeName());
334  data.obs().insert(id);
335  break;
336  }
337  }
338 
339  if (!(data.obs().size() && (data.obs().exists(id))))
340  data.inners().insert(id);
341  }
342  }
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:

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 609 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 242 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, __eliminateObservedNodesInSource(data, pool, **iter, elim_order));
266  else
267  __elim_map.insert(*iter, new Set< Potential< GUM_SCALAR >* >(pool));
268 
269  ++iter;
270 
271  if (data.obs().size()) {
272  for (; iter != data.matches.end(); ++iter) {
273  try {
274  __elim_map.insert(
275  *iter, __eliminateObservedNodes(data, pool, **iter, elim_order));
276  } catch (OperationNotAllowed&) { fake_patterns.insert(*iter); }
277  }
278  } else {
279  for (; iter != data.matches.end(); ++iter) {
280  try {
281  __elim_map.insert(*iter, __translatePotSet(data, pool, **iter));
282  } catch (OperationNotAllowed&) { fake_patterns.insert(*iter); }
283  }
284  }
285 
286  for (const auto pat : fake_patterns) {
287  for (const auto elt : *pat)
288  __reducedInstances.erase(elt);
289 
290  data.matches.erase(pat);
291  }
292 
293  obs_time += plopTimer.step();
294 
295  if (data.queries().size())
296  for (const auto m : data.matches)
297  if (!(m->exists(
298  const_cast< PRMInstance< GUM_SCALAR >* >(__query.first))))
299  eliminateNode(&(m->atPos(__query_data.first)
300  ->get(__query_data.second)
301  .type()
302  .variable()),
303  *(__elim_map[m]),
304  __trash);
305  }
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...
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 > * > * __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.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
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 > * > * __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
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 418 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().

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

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 1021 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.

1024  {
1025  data.graph.eraseNode(id);
1026  GUM_ASSERT(!data.graph.exists(id));
1027  data.mod.erase(id);
1028  GUM_ASSERT(!data.mod.exists(id));
1029  data.node2attr.eraseFirst(id);
1030  GUM_ASSERT(!data.node2attr.existsFirst(id));
1031  data.map.erase(id);
1032  GUM_ASSERT(!data.map.exists(id));
1033  data.vars.eraseFirst(id);
1034  GUM_ASSERT(!data.vars.existsFirst(id));
1035  data.inners().erase(id);
1036  GUM_ASSERT(!data.inners().exists(id));
1037  pool.erase(data.pots[id]);
1038  GUM_ASSERT(!pool.exists(data.pots[id]));
1039  data.pots.erase(id);
1040  GUM_ASSERT(!data.pots.exists(id));
1041  }

+ Here is the call graph for this function:

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 974 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().

976  {
977  return i->name() + __dot + a->safeName();
978  }
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:

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 981 of file structuredInference_tpl.h.

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

983  {
984  return i->name() + __dot + a.safeName();
985  }
std::string __dot
Unreduce the match containing the query.

+ Here is the call graph for this function:

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 988 of file structuredInference_tpl.h.

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

990  {
991  return i->name() + __dot + a.lastElt().safeName();
992  }
std::string __dot
Unreduce the match containing the query.

+ Here is the call graph for this function:

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 515 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().

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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  {}
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 >.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::addEvidence().

+ Here is the caller graph for this function:

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  {}
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 >.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::removeEvidence().

+ Here is the caller graph for this function:

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:66
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 >.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::joint().

+ Here is the caller graph for this function:

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  }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
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...
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Definition: timer_inl.h:39
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.
Potential< GUM_SCALAR > * multPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)

+ Here is the call graph for this function:

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 >.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::marginal().

+ Here is the caller graph for this function:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__EMap(), gum::prm::PRMInference< GUM_SCALAR >::_evidenceAdded(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::MultiDimDecorator< GUM_SCALAR >::contains(), gum::Instantiation::end(), gum::HashTable< Key, Val, Alloc >::exists(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::Instantiation::inc(), gum::HashTable< Key, Val, Alloc >::insert(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Remove all evidences.

Definition at line 33 of file PRMInference_tpl.h.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::operator=(), and gum::prm::PRMInference< GUM_SCALAR >::~PRMInference().

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

+ Here is the caller graph for this function:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

Referenced by gum::prm::SVED< GUM_SCALAR >::__insertEvidence(), gum::prm::SVE< GUM_SCALAR >::__insertEvidence(), gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), gum::prm::StructuredInference< GUM_SCALAR >::_marginal(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), and gum::prm::PRMInference< 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:66

+ Here is the caller graph for this function:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

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:66
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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

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:66
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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

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:66
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 1010 of file structuredInference_tpl.h.

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

1010  {
1011  return *__gspan;
1012  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
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 1016 of file structuredInference_tpl.h.

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

1016  {
1017  return *__gspan;
1018  }
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

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:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

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
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.

References gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::HashTable< Key, Val, Alloc >::exists(), and gum::prm::PRMInference< GUM_SCALAR >::hasEvidence().

206  {
207  return (hasEvidence(chain.first))
208  ? evidence(chain.first).exists(chain.second->id())
209  : false;
210  }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
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.

+ Here is the call graph for this function:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

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

213  {
214  return (__evidences.size() != (Size)0);
215  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
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:

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  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
GSpan< GUM_SCALAR > * __gspan
Pointer over th GSpan<GUM_SCALAR> instance used by this class.
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.

References gum::prm::PRMInference< GUM_SCALAR >::_joint(), gum::MultiDimDecorator< GUM_SCALAR >::add(), GUM_ERROR, and gum::MultiDimDecorator< GUM_SCALAR >::nbrDim().

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:66

+ Here is the call graph for this function:

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.

References gum::prm::PRMInference< GUM_SCALAR >::_marginal(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::Instantiation::inc(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

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  }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
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.
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:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 1005 of file structuredInference_tpl.h.

1005  {
1006  return "StructuredInference";
1007  }
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:

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.

References gum::prm::PRMInference< GUM_SCALAR >::__EMap(), gum::prm::PRMInference< GUM_SCALAR >::_evidenceRemoved(), gum::HashTable< Key, Val, Alloc >::erase(), and gum::HashTable< Key, Val, Alloc >::exists().

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 call graph for this function:

+ Here is the caller graph for this function:

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

Search for patterns without doing any computations.

Definition at line 960 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().

960  {
961  const PRMInstance< GUM_SCALAR >* i = (this->_sys->begin()).val();
962  __query = std::make_pair(i, i->begin().val());
963  __found_query = false;
964  typename StructuredInference< GUM_SCALAR >::RGData data;
965  __buildReduceGraph(data);
966  }
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:

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 969 of file structuredInference_tpl.h.

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

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

Member Data Documentation

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().

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().

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().

template<typename GUM_SCALAR>
bool gum::prm::StructuredInference< GUM_SCALAR >::__mining
private
template<typename GUM_SCALAR>
HashTable< const PRMClass< GUM_SCALAR >*, std::vector< NodeId >* > gum::prm::StructuredInference< GUM_SCALAR >::__outputs
private
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().

template<typename GUM_SCALAR>
std::pair< Idx, std::string > gum::prm::StructuredInference< GUM_SCALAR >::__query_data
private
template<typename GUM_SCALAR>
Set< const PRMInstance< GUM_SCALAR >* > gum::prm::StructuredInference< GUM_SCALAR >::__reducedInstances
private
template<typename GUM_SCALAR>
PRM< GUM_SCALAR > const* gum::prm::PRMInference< GUM_SCALAR >::_prm
protectedinherited
template<typename GUM_SCALAR>
Timer gum::prm::StructuredInference< GUM_SCALAR >::timer

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