aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
potential.h
Go to the documentation of this file.
1 
28 #ifndef GUM_POTENTIAL_H
29 #define GUM_POTENTIAL_H
30 
31 #include <vector>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/set.h>
37 
38 namespace gum {
39 
40  // ==========================================================================
41  // === POTENTIAL ===
42  // ==========================================================================
43 
59  template < typename GUM_SCALAR >
60  class Potential: public MultiDimDecorator< GUM_SCALAR > {
61  public:
62  // =========================================================================
64  // =========================================================================
66 
73  Potential();
74 
80 
98 
104 
105 
109  ~Potential();
110 
112  // ========================================================================
114  // ========================================================================
116 
117  virtual Potential< GUM_SCALAR >* newFactory() const;
118 
120  // ========================================================================
122  // ========================================================================
124 
125  const Potential< GUM_SCALAR >& random() const;
126 
129 
131  const Potential< GUM_SCALAR >& randomCPT() const;
132 
136  const Potential< GUM_SCALAR >& noising(GUM_SCALAR alpha) const;
137 
144  margSumOut(const Set< const DiscreteVariable* >& del_vars) const;
145 
152  margSumIn(const Set< const DiscreteVariable* >& kept_vars) const;
153 
160  margProdOut(const Set< const DiscreteVariable* >& del_vars) const;
161 
168  margProdIn(const Set< const DiscreteVariable* >& kept_vars) const;
169 
176  margMinOut(const Set< const DiscreteVariable* >& del_vars) const;
177 
184  margMinIn(const Set< const DiscreteVariable* >& kept_vars) const;
185 
192  margMaxOut(const Set< const DiscreteVariable* >& del_vars) const;
193 
200  margMaxIn(const Set< const DiscreteVariable* >& kept_vars) const;
201 
206 
208  GUM_SCALAR sum() const;
210  GUM_SCALAR product() const;
212  GUM_SCALAR max() const;
214  GUM_SCALAR min() const;
217  GUM_SCALAR maxNonOne() const;
220  GUM_SCALAR minNonZero() const;
221 
223  Set< Instantiation > findAll(GUM_SCALAR v) const;
228 
230  GUM_SCALAR entropy() const;
231 
237  reorganize(const std::vector< const DiscreteVariable* >& vars) const;
238 
244  reorganize(const std::vector< std::string >& vars) const;
245 
249  Potential< GUM_SCALAR > extract(const Instantiation& inst) const;
250 
255 
259  Potential< GUM_SCALAR > putFirst(const std::string& varname) const;
260 
270  fillWith(const Potential< GUM_SCALAR >& src) const;
271 
290  const std::vector< std::string >& mapSrc) const;
291 
301  fillWith(const std::vector< GUM_SCALAR >& v) const;
302 
308  const Potential< GUM_SCALAR >& fillWith(const GUM_SCALAR& v) const;
309 
310 
314  const Potential< GUM_SCALAR >& abs() const;
315 
319  const Potential< GUM_SCALAR >& normalize() const;
320 
324  const Potential< GUM_SCALAR >& sq() const;
325 
329  const Potential< GUM_SCALAR >& log2() const;
330 
339  GUM_SCALAR KL(const Potential< GUM_SCALAR >& p) const;
340 
348  const Potential< GUM_SCALAR >& normalizeAsCPT(const Idx& varId = 0) const;
349 
353  const Potential< GUM_SCALAR >& scale(GUM_SCALAR v) const;
354 
358  const Potential< GUM_SCALAR >& translate(GUM_SCALAR v) const;
359 
363  const Potential< GUM_SCALAR >& inverse(void) const;
364 
368  Idx draw() const;
369 
371 
372  // ========================================================================
374  // ========================================================================
376 
377 
380  if (p2.empty())
382  if (this->empty())
384 
385  return Potential< GUM_SCALAR >(*this->content() + *p2.content());
386  }
387 
389  Potential< GUM_SCALAR > operator+(const GUM_SCALAR& v) const {
390  return Potential< GUM_SCALAR >(*this).translate(v);
391  }
392 
395  if (p2.empty())
397  if (this->empty()) {
398  auto p = Potential< GUM_SCALAR >(p2);
399  p.apply([this](GUM_SCALAR x) { return this->empty_value_ - x; });
400  return p;
401  }
402  return Potential< GUM_SCALAR >(*this->content() - *p2.content());
403  }
404 
406  Potential< GUM_SCALAR > operator-(const GUM_SCALAR& v) const {
407  return Potential< GUM_SCALAR >(*this).translate(-v);
408  }
409 
412  if (p2.empty()) return Potential< GUM_SCALAR >(*this).scale(p2.empty_value_);
413  if (this->empty())
414  return Potential< GUM_SCALAR >(p2).scale(this->empty_value_);
415 
416  return Potential< GUM_SCALAR >(*this->content() * *p2.content());
417  }
418 
420  Potential< GUM_SCALAR > operator*(const GUM_SCALAR& v) const {
421  return Potential< GUM_SCALAR >(*this).scale(v);
422  }
423 
426  if (p2.empty())
427  return Potential< GUM_SCALAR >(*this).scale(1 / p2.empty_value_);
428  if (this->empty()) {
429  auto p = Potential< GUM_SCALAR >(p2);
430  p.apply([this](GUM_SCALAR x) { return this->empty_value_ / x; });
431  return p;
432  }
433  return Potential< GUM_SCALAR >(*this->content() / *p2.content());
434  }
436  Potential< GUM_SCALAR > operator/(const GUM_SCALAR& v) const {
437  return Potential< GUM_SCALAR >(*this).scale(1 / v);
438  }
439 
441  *this = *this + r;
442  return *this;
443  }
444  Potential< GUM_SCALAR >& operator+=(const GUM_SCALAR& v) {
445  this->translate(v);
446  return *this;
447  }
448 
450  *this = *this * r;
451  return *this;
452  }
453  Potential< GUM_SCALAR >& operator*=(const GUM_SCALAR& v) {
454  this->scale(v);
455  return *this;
456  }
457 
459  *this = *this - r;
460  return *this;
461  }
462  Potential< GUM_SCALAR >& operator-=(const GUM_SCALAR& v) {
463  this->translate(-v);
464  return *this;
465  }
466 
468  *this = *this / r;
469  return *this;
470  }
471  Potential< GUM_SCALAR >& operator/=(const GUM_SCALAR& v) {
472  this->scale(1 / v);
473  return *this;
474  }
475 
476  bool operator==(const Potential< GUM_SCALAR >& r) const {
477  if (this->empty()) {
478  if (r.empty())
479  return this->empty_value_ == r.empty_value_;
480  else
481  return false;
482  } else {
483  if (r.empty())
484  return false;
485  else
486  return (*this->content_) == (*r.content_);
487  }
488  }
489 
490  bool operator!=(const Potential< GUM_SCALAR >& r) const {
491  return !operator==(r);
492  }
493 
494  virtual const std::string toString() const {
496  }
497 
499 
500  protected:
502  complementVars_(const Set< const DiscreteVariable* >& del_vars) const;
503  };
504 
505 
506 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
507  extern template class Potential< double >;
508 #endif
509 
510 
511 } /* namespace gum */
512 
514 #endif /* GUM_POTENTIAL_H */
Potential< GUM_SCALAR > operator+(const Potential< GUM_SCALAR > &p2) const
the function to be used to add two Potentials
Definition: potential.h:379
~Potential()
Destructor.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
Potential< GUM_SCALAR > extract(const Instantiation &inst) const
create a new Potential extracted from *this given a partial instantiation
Potential< GUM_SCALAR > operator/(const Potential< GUM_SCALAR > &p2) const
the function to be used to divide two Potentials
Definition: potential.h:425
virtual const std::string toString() const
Default implementation of MultiDimContainer::set().
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
GUM_SCALAR min() const
min of all elements in the Potential
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Potential< GUM_SCALAR > & operator-=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:458
Potential< GUM_SCALAR > & operator-=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:462
MultiDimImplementation< GUM_SCALAR > * content_
The true container.
GUM_SCALAR minNonZero() const
min of all non zero elements in the Potential
const Potential< GUM_SCALAR > & random() const
generate a random Potential with each parameter in [0,1]
Set< const DiscreteVariable *> complementVars_(const Set< const DiscreteVariable * > &del_vars) const
const Potential< GUM_SCALAR > & scale(GUM_SCALAR v) const
multiply (each value of) *this by v
const Potential< GUM_SCALAR > & inverse(void) const
the function to inverse (each value of) *this
Potential< GUM_SCALAR > & operator/=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:467
Base class for discrete random variable.
Potential< GUM_SCALAR > & operator*=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:449
GUM_SCALAR KL(const Potential< GUM_SCALAR > &p) const
compute KL divergence between this and p Checks the compatibility and then compute KL divergence ...
Potential< GUM_SCALAR > margProdOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
const Potential< GUM_SCALAR > & normalizeAsCPT(const Idx &varId=0) const
normalisation of this as a CPT for the variable varId
Set< Instantiation > argmin() const
set of instantiation corresponding to the min in the Potential
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Potential< GUM_SCALAR > operator/(const GUM_SCALAR &v) const
the function to be used to divide a Potential by a scalar
Definition: potential.h:436
virtual bool empty() const final
Returns true if no var is in *this.
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
GUM_SCALAR product() const
product of all elements in the Potential
Potential< GUM_SCALAR > margProdIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using multiplication as operation (and implementation-optimized operations) ...
const Potential< GUM_SCALAR > & sq() const
apply $x^2$ on every element of the container
virtual void apply(std::function< GUM_SCALAR(GUM_SCALAR) > f) const final
Apply a function on every element of the container.
bool operator==(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:476
Idx draw() const
get a value at random from a 1-D distribution
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src) const
copy a Potential data using name of variables and labels (not necessarily the same variables in the s...
Potential< GUM_SCALAR > margSumIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using sum as operation (and implementation-optimized operations)
Set< Instantiation > argmax() const
set of instantiation corresponding to the max in the Potential
Potential< GUM_SCALAR > & operator=(const Potential< GUM_SCALAR > &src)
Default constructor.
Definition: potential_tpl.h:95
Potential< GUM_SCALAR > & operator+=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:444
GUM_SCALAR max() const
max of all elements in the Potential
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Potential< GUM_SCALAR > reorganize(const std::vector< const DiscreteVariable * > &vars) const
create a new Potential with another order
Copyright 2005-2020 Pierre-Henri WUILLEMIN() et Christophe GONZALES() () info_at_agrum_dot_org.
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Definition: potential.h:394
Potential< GUM_SCALAR > & operator*=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:453
const Potential< GUM_SCALAR > & abs() const
Apply abs on every element of the container.
Potential< GUM_SCALAR > margMaxIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using max as operation (and implementation-optimized operations)
GUM_SCALAR entropy() const
entropy of the Potential
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
Potential< GUM_SCALAR > & operator+=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Definition: potential.h:440
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
Decorator design pattern in order to separate implementations from multidimensional matrix concepts...
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Potential< GUM_SCALAR > operator+(const GUM_SCALAR &v) const
the function to be used to add a GUM_SCALAR to a Potential
Definition: potential.h:389
GUM_SCALAR sum() const
sum of all elements in the Potential
GUM_SCALAR empty_value_
value of the MultiDimDecorator if no dimension.
Potential< GUM_SCALAR > & operator/=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Definition: potential.h:471
virtual const std::string toString() const
the function to be used to add two Potentials
Definition: potential.h:494
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
<agrum/tools/multidim/multiDimImplementation.h>
Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using max as operation (and implementation-optimized operations)
Size Idx
Type for indexes.
Definition: types.h:53
GUM_SCALAR maxNonOne() const
max of all non one elements in the Potential
Potential< GUM_SCALAR > margMinOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using min as operation (and implementation-optimized operations)
Potential< GUM_SCALAR > operator*(const Potential< GUM_SCALAR > &p2) const
the function to be used to multiply two Potentials
Definition: potential.h:411
const Potential< GUM_SCALAR > & noising(GUM_SCALAR alpha) const
add a noise in a CPT by mixing (1-alpha)this+alpha.randomCPT()
const Potential< GUM_SCALAR > & translate(GUM_SCALAR v) const
add v to (each value of) *this
bool operator!=(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
Definition: potential.h:490
Potential< GUM_SCALAR > operator*(const GUM_SCALAR &v) const
the function to be used to multiply a Potential and a scalar
Definition: potential.h:420
virtual Potential< GUM_SCALAR > * newFactory() const
Default implementation of MultiDimContainer::set().
const Potential< GUM_SCALAR > & randomDistribution() const
generate a random Distribution in the Potential
Potential()
Default constructor.
Definition: potential_tpl.h:38
Potential< GUM_SCALAR > putFirst(const DiscreteVariable *var) const
create a new Potential with a certain variable in first
Potential< GUM_SCALAR > operator-(const GUM_SCALAR &v) const
the function to be used to substract a GUM_SCALAR from a Potential
Definition: potential.h:406
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)