29 #ifndef AGRUM_DECISIONPOTENTIAL_H 30 #define AGRUM_DECISIONPOTENTIAL_H 32 #include <agrum/tools/core/set.h> 33 #include <agrum/tools/variables/discreteVariable.h> 34 #include <agrum/tools/multidim/instantiation.h> 35 #include <agrum/tools/multidim/potential.h> 43 template <
typename GUM_SCALAR >
50 GUM_CONSTRUCTOR(DecisionPotential);
51 probPot.fillWith(GUM_SCALAR(1));
52 utilPot.fillWith(GUM_SCALAR(0));
56 GUM_DESTRUCTOR(DecisionPotential);
60 DecisionPotential(
const Potential< GUM_SCALAR >& prob,
const Potential< GUM_SCALAR >& util) :
62 GUM_CONSTRUCTOR(DecisionPotential);
67 GUM_CONS_CPY(DecisionPotential);
71 gum::Potential< GUM_SCALAR > p;
72 p.fillWith(GUM_SCALAR(1));
74 p.fillWith(GUM_SCALAR(0));
79 GUM_OP_CPY(DecisionPotential);
80 if (&src ==
this)
return *
this;
81 probPot = src.probPot;
82 utilPot = src.utilPot;
89 GUM_CONS_MOV(DecisionPotential);
93 GUM_OP_MOV(DecisionPotential);
94 if (&src ==
this)
return *
this;
95 probPot = std::forward< Potential< GUM_SCALAR > >(src.probPot);
96 utilPot = std::forward< Potential< GUM_SCALAR > >(src.utilPot);
102 return ((p.probPot ==
this->probPot)
103 && (p.probPot * p.utilPot ==
this->probPot *
this->utilPot));
109 for (
const auto& v: probPot.variablesSequence()) {
110 if (v->name() == name)
return v;
112 for (
const auto& v: utilPot.variablesSequence()) {
113 if (v->name() == name)
return v;
116 GUM_ERROR(NotFound,
"'" << name <<
"' can not be found in DecisionPotential.")
119 void insertProba(
const gum::Potential< GUM_SCALAR >& proba) { probPot *= proba; }
121 void insertUtility(
const gum::Potential< GUM_SCALAR >& util) { utilPot += util; }
133 return DecisionPotential< GUM_SCALAR >::marginalization(*
this, onto);
137 return DecisionPotential< GUM_SCALAR >::marginalization(*
this, ontonames);
143 Potential< GUM_SCALAR > res(p1);
144 Instantiation I(res);
145 for (I.setFirst(); !I.end(); I.inc()) {
146 if (res[I] != 0) res.set(I, res[I] / p2[I]);
153 return DecisionPotential< GUM_SCALAR >(dp1.probPot * dp2.probPot, dp1.utilPot + dp2.utilPot);
158 const Set<
const DiscreteVariable* >& onto) {
159 const auto pr = dp.probPot.margSumIn(onto);
160 return DecisionPotential(pr, divideEvenZero((dp.probPot * dp.utilPot).margSumIn(onto), pr));
165 const std::vector< std::string >& ontonames) {
166 Set<
const DiscreteVariable* > onto;
167 for (
const auto& varname: ontonames) {
168 onto.insert(dp.variable(varname));
170 return marginalization(dp, onto);
174 auto tmp = probPot * utilPot;
175 GUM_SCALAR s = probPot.sum();
176 double m = tmp.sum() / s;
177 double m2 = (tmp * utilPot).sum() / s;
178 return std::pair< GUM_SCALAR, GUM_SCALAR >(m, m2 - m * m);
182 return "prob : " + probPot.toString() +
" util:" + utilPot.toString();
186 template <
typename GUM_SCALAR >
188 out << array.toString();
DecisionPotential< GUM_SCALAR > operator*(const DecisionPotential< GUM_SCALAR > &dp1) const
bool operator!=(const DecisionPotential< GUM_SCALAR > &p) const
static DecisionPotential< GUM_SCALAR > combination(const DecisionPotential< GUM_SCALAR > &dp1, const DecisionPotential< GUM_SCALAR > &dp2)
Potential< GUM_SCALAR > probPot
const DiscreteVariable * variable(const std::string &name) const
INLINE void emplace(Args &&... args)
DecisionPotential< GUM_SCALAR > operator*=(const DecisionPotential< GUM_SCALAR > &dp1)
DecisionPotential(const Potential< GUM_SCALAR > &prob, const Potential< GUM_SCALAR > &util)
Potential< GUM_SCALAR > utilPot
DecisionPotential(DecisionPotential< GUM_SCALAR > &&dp)
DecisionPotential< GUM_SCALAR > & operator=(DecisionPotential< GUM_SCALAR > &&src)
<agrum/ID/inference/decisionPotential.h>
void insertUtility(const gum::Potential< GUM_SCALAR > &util)
virtual std::string toString() const
bool operator==(const DecisionPotential< GUM_SCALAR > &p) const
void insertProba(const gum::Potential< GUM_SCALAR > &proba)
DecisionPotential< GUM_SCALAR > & operator=(const DecisionPotential< GUM_SCALAR > &src)
std::pair< GUM_SCALAR, GUM_SCALAR > meanVar()
std::ostream & operator<<(std::ostream &out, const DecisionPotential< GUM_SCALAR > &array)
static DecisionPotential< GUM_SCALAR > marginalization(const DecisionPotential< GUM_SCALAR > &dp, const std::vector< std::string > &ontonames)
DecisionPotential< GUM_SCALAR > operator^(const std::vector< std::string > &ontonames) const
static Potential< GUM_SCALAR > divideEvenZero(const Potential< GUM_SCALAR > &p1, const Potential< GUM_SCALAR > &p2)
DecisionPotential(const DecisionPotential< GUM_SCALAR > &dp)