27 #ifndef GUM_POTENTIAL_H 28 #define GUM_POTENTIAL_H 32 #include <agrum/agrum.h> 33 #include <agrum/tools/core/set.h> 34 #include <agrum/tools/multidim/implementations/multiDimDecorator.h> 35 #include <agrum/tools/multidim/implementations/multiDimImplementation.h> 58 template <
typename GUM_SCALAR >
78 explicit Potential(MultiDimImplementation< GUM_SCALAR >* aContent);
90 Potential(MultiDimImplementation< GUM_SCALAR >* aContent,
91 const MultiDimContainer< GUM_SCALAR >& src);
149 Potential< GUM_SCALAR >
margSumIn(
const Set<
const DiscreteVariable* >& kept_vars)
const;
177 Potential< GUM_SCALAR >
margMinIn(
const Set<
const DiscreteVariable* >& kept_vars)
const;
191 Potential< GUM_SCALAR >
margMaxIn(
const Set<
const DiscreteVariable* >& kept_vars)
const;
199 GUM_SCALAR
sum()
const;
203 GUM_SCALAR
max()
const;
205 GUM_SCALAR
min()
const;
227 Potential< GUM_SCALAR >
reorganize(
const std::vector<
const DiscreteVariable* >& vars)
const;
277 const std::vector< std::string >& mapSrc)
const;
287 const Potential< GUM_SCALAR >&
fillWith(
const std::vector< GUM_SCALAR >& data)
const;
344 GUM_SCALAR
KL(
const Potential< GUM_SCALAR >& p)
const;
385 if (p2.empty())
return Potential< GUM_SCALAR >(*
this).translate(p2.empty_value_);
386 if (
this->empty())
return Potential< GUM_SCALAR >(p2).translate(
this->empty_value_);
388 return Potential< GUM_SCALAR >(*
this->content() + *p2.content());
393 return Potential< GUM_SCALAR >(*
this).translate(v);
398 if (p2.empty())
return Potential< GUM_SCALAR >(*
this).translate(-p2.empty_value_);
401 p.apply([
this](GUM_SCALAR x) {
return this->empty_value_ - x; });
404 return Potential< GUM_SCALAR >(*
this->content() - *p2.content());
409 return Potential< GUM_SCALAR >(*
this).translate(-v);
414 if (p2.empty())
return Potential< GUM_SCALAR >(*
this).scale(p2.empty_value_);
415 if (
this->empty())
return Potential< GUM_SCALAR >(p2).scale(
this->empty_value_);
417 return Potential< GUM_SCALAR >(*
this->content() * *p2.content());
422 return Potential< GUM_SCALAR >(*
this).scale(v);
427 if (p2.empty())
return Potential< GUM_SCALAR >(*
this).scale(1 / p2.empty_value_);
430 p.apply([
this](GUM_SCALAR x) {
return this->empty_value_ / x; });
433 return Potential< GUM_SCALAR >(*
this->content() / *p2.content());
437 return Potential< GUM_SCALAR >(*
this).scale(1 / v);
479 return this->empty_value_ == r.empty_value_;
486 return (*
this->content_) == (*r.content_);
492 Potential< GUM_SCALAR >& operator<<(
const DiscreteVariable& v) {
498 auto table =
this->content();
499 if (table->nbrDim() == 0) {
return "[]"; }
500 const Size colwidth = 6;
501 const Size numberwidth = 9;
502 const Size nbrLigMax = 6;
504 std::stringstream ss;
505 ss << std::left << std::fixed << std::endl;
506 ss.precision(numberwidth - 5);
508 const auto& var = table->variable(0);
510 const Size nbparents = table->nbrDim() - 1;
511 const Size nbcol = var.domainSize();
512 const std::string maskparent(colwidth,
'-');
513 const std::string masknumber(numberwidth,
'-');
516 ss << std::setw(nbparents * (colwidth + 1) - 1) <<
" " 518 ss <<
" " << std::setw(nbcol * (numberwidth + 1) - 3)
519 << var.name().substr(0, nbcol * (numberwidth + 1) - 3) <<
"|";
523 for (Idx i = 1; i <= nbparents; i++)
524 ss << std::setw(colwidth) << table->variable(i).name().substr(0, colwidth) <<
"|";
527 for (Idx i = 0; i < nbcol; i++)
528 ss << std::setw(numberwidth) << var.label(i).substr(0, numberwidth) <<
"|";
533 for (Idx i = 1; i <= nbparents; i++)
534 ss << maskparent <<
"|";
537 for (Idx i = 0; i < nbcol; i++)
538 ss << masknumber <<
"|";
540 Instantiation I(*table);
542 auto drawligne = [&]() {
544 for (Idx i = 1; i <= nbparents; i++)
545 ss << std::setw(colwidth) << table->variable(i).label(I.val(i)).substr(0, colwidth)
549 for (I.setFirstVar(var); !I.end(); I.incVar(var))
550 ss <<
" " << std::setw(numberwidth - 1) << table->get(I) <<
"|";
555 Size nbrLig = table->domainSize() / var.domainSize();
556 if (nbrLig < nbrLigMax * 2 + 1) {
557 for (I.setFirst(); !I.end(); I.incNotVar(var))
561 for (I.setFirst(); !I.end(); I.incNotVar(var)) {
563 if (cpt > nbrLigMax)
break;
566 ss <<
"[..." << nbrLig - nbrLigMax * 2 <<
" more line(s) ...]" << std::endl;
568 for (Idx revi = 1; revi < nbrLigMax; revi++)
570 for (I.setFirstVar(var); !I.end(); I.incNotVar(var)) {
585 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 586 extern template class Potential<
double >;
589 template <
typename GUM_SCALAR >
591 return arg.new_log2();
594 template <
typename GUM_SCALAR >
596 return arg.new_abs();
599 template <
typename GUM_SCALAR >
606 #include <agrum/tools/multidim/potential_tpl.h> Potential< GUM_SCALAR > operator+(const Potential< GUM_SCALAR > &p2) const
the function to be used to add two Potentials
aGrUM's Potential is a multi-dimensional array with tensor operators.
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
const Potential< GUM_SCALAR > & fillWith(const GUM_SCALAR &val) const
Automatically fills this MultiDimContainer with the value v.
Potential< GUM_SCALAR > isNonZeroMap() const
create a boolean-like potential using the predicate isNonZero
Potential< GUM_SCALAR > abs(const Potential< GUM_SCALAR > &arg)
GUM_SCALAR min() const
min of all elements in the Potential
Potential< GUM_SCALAR > reorganize(const std::vector< std::string > &vars) const
create a new Potential with another order
Potential(MultiDimImplementation< GUM_SCALAR > *aContent)
Creates an potential around aContent.
INLINE void emplace(Args &&... args)
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
Potential< GUM_SCALAR > putFirst(const std::string &varname) const
create a new Potential with a certain variable in first
Potential< GUM_SCALAR > & operator-=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
Potential< GUM_SCALAR > & operator-=(const GUM_SCALAR &v)
the function to be used to add two Potentials
const Potential< GUM_SCALAR > & fillWith(const std::vector< GUM_SCALAR > &data) const
Automatically fills the potential with the values in v.
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 > new_abs() const
Create a new potential and apply abs on every element of the container.
const Potential< GUM_SCALAR > new_log2() const
Create a new potential and apply $log_2(x)$ on every element of the container.
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
Potential< GUM_SCALAR > & operator*=(const Potential< GUM_SCALAR > &r)
the function to be used to add two Potentials
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) ...
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
Potential< GUM_SCALAR > operator/(const GUM_SCALAR &v) const
the function to be used to divide a Potential by a scalar
Potential< GUM_SCALAR > sq(const Potential< GUM_SCALAR > &arg)
Set< Instantiation > findAll(GUM_SCALAR v) const
set of instantiation corresponding to the parameter v in the Potential
const Potential< GUM_SCALAR > new_sq() const
Create a new potential and apply $x^2$ on every element of the container.
GUM_SCALAR product() const
product of all elements in the Potential
Potential< GUM_SCALAR > & operator=(Potential< GUM_SCALAR > &&src)
Default constructor.
const Potential< GUM_SCALAR > & fillWith(const Potential< GUM_SCALAR > &src, const std::vector< std::string > &mapSrc) const
copy a Potential data using the sequence of names in mapSrc to find the corresponding variables...
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
bool operator==(const Potential< GUM_SCALAR > &r) const
the function to be used to add two Potentials
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.
Potential< GUM_SCALAR > & operator+=(const GUM_SCALAR &v)
the function to be used to add two Potentials
GUM_SCALAR max() const
max of all elements in the Potential
Potential< GUM_SCALAR > operator-(const Potential< GUM_SCALAR > &p2) const
the function to be used to subtract two Potentials
Potential< GUM_SCALAR > & operator*=(const GUM_SCALAR &v)
the function to be used to add two Potentials
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)
virtual std::string toString() const
the function to be used to add two Potentials
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
const Potential< GUM_SCALAR > & log2() const
apply $log_2(x)$ on every element of the container
Potential(MultiDimImplementation< GUM_SCALAR > *aContent, const MultiDimContainer< GUM_SCALAR > &src)
Copy constructor.
Potential< GUM_SCALAR > operator+(const GUM_SCALAR &v) const
the function to be used to add a GUM_SCALAR to a Potential
GUM_SCALAR sum() const
sum of all elements in the Potential
Potential< GUM_SCALAR > & operator/=(const GUM_SCALAR &v)
the function to be used to add two Potentials
Potential(Potential< GUM_SCALAR > &&from)
move constructor & assignement
const Potential< GUM_SCALAR > & randomCPT() const
generate a random CPT in the Potential
Potential< GUM_SCALAR > margMaxOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using max as operation (and implementation-optimized operations)
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
Potential(const Potential< GUM_SCALAR > &src)
Copy constructor & assignment.
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
Potential< GUM_SCALAR > operator*(const GUM_SCALAR &v) const
the function to be used to multiply a Potential and a scalar
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.
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
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Potential< GUM_SCALAR > margMinIn(const Set< const DiscreteVariable * > &kept_vars) const
Projection using min as operation (and implementation-optimized operations)