33 template <
typename GUM_SCALAR >
34 MarginalTargetedInference< GUM_SCALAR >::MarginalTargetedInference(
35 const IBayesNet< GUM_SCALAR >* bn) :
36 BayesNetInference< GUM_SCALAR >(bn) {
38 if (
this->hasNoModel_()) {
39 BayesNetInference< GUM_SCALAR >::_setBayesNetDuringConstruction_(bn);
44 _targeted_mode_ =
false;
45 _targets_ = bn->dag().asNodeSet();
48 GUM_CONSTRUCTOR(MarginalTargetedInference);
53 template <
typename GUM_SCALAR >
54 MarginalTargetedInference< GUM_SCALAR >::~MarginalTargetedInference() {
55 GUM_DESTRUCTOR(MarginalTargetedInference);
60 template <
typename GUM_SCALAR >
61 void MarginalTargetedInference< GUM_SCALAR >::onModelChanged_(
const GraphicalModel* bn) {
62 _targeted_mode_ =
true;
63 _setAllMarginalTargets_();
72 template <
typename GUM_SCALAR >
73 INLINE
bool MarginalTargetedInference< GUM_SCALAR >::isTarget(NodeId node)
const {
75 if (
this->hasNoModel_())
76 GUM_ERROR(NullElement,
77 "No Bayes net has been assigned to the " 78 "inference algorithm");
79 if (!
this->BN().dag().exists(node)) {
80 GUM_ERROR(UndefinedElement, node <<
" is not a NodeId in the bn")
83 return _targets_.contains(node);
87 template <
typename GUM_SCALAR >
88 INLINE
bool MarginalTargetedInference< GUM_SCALAR >::isTarget(
const std::string& nodeName)
const {
89 return isTarget(
this->BN().idFromName(nodeName));
94 template <
typename GUM_SCALAR >
95 INLINE
void MarginalTargetedInference< GUM_SCALAR >::eraseAllTargets() {
96 onAllMarginalTargetsErased_();
101 this->setState_(GraphicalModelInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
106 template <
typename GUM_SCALAR >
107 void MarginalTargetedInference< GUM_SCALAR >::addTarget(NodeId target) {
109 if (
this->hasNoModel_())
110 GUM_ERROR(NullElement,
111 "No Bayes net has been assigned to the " 112 "inference algorithm");
114 if (!
this->BN().dag().exists(target)) {
115 GUM_ERROR(UndefinedElement, target <<
" is not a NodeId in the bn")
120 if (!_targets_.contains(target)) {
121 _targets_.insert(target);
122 onMarginalTargetAdded_(target);
123 this->setState_(GraphicalModelInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
129 template <
typename GUM_SCALAR >
130 void MarginalTargetedInference< GUM_SCALAR >::addAllTargets() {
132 if (
this->hasNoModel_())
133 GUM_ERROR(NullElement,
134 "No Bayes net has been assigned to the " 135 "inference algorithm");
139 for (
const auto target:
this->BN().dag()) {
140 if (!_targets_.contains(target)) {
141 _targets_.insert(target);
142 onMarginalTargetAdded_(target);
143 this->setState_(GraphicalModelInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
150 template <
typename GUM_SCALAR >
151 void MarginalTargetedInference< GUM_SCALAR >::addTarget(
const std::string& nodeName) {
153 if (
this->hasNoModel_())
154 GUM_ERROR(NullElement,
155 "No Bayes net has been assigned to the " 156 "inference algorithm");
158 addTarget(
this->BN().idFromName(nodeName));
163 template <
typename GUM_SCALAR >
164 void MarginalTargetedInference< GUM_SCALAR >::eraseTarget(NodeId target) {
166 if (
this->hasNoModel_())
167 GUM_ERROR(NullElement,
168 "No Bayes net has been assigned to the " 169 "inference algorithm");
171 if (!
this->BN().dag().exists(target)) {
172 GUM_ERROR(UndefinedElement, target <<
" is not a NodeId in the bn")
176 if (_targets_.contains(target)) {
177 _targeted_mode_ =
true;
179 onMarginalTargetErased_(target);
180 _targets_.erase(target);
181 this->setState_(GraphicalModelInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
187 template <
typename GUM_SCALAR >
188 void MarginalTargetedInference< GUM_SCALAR >::eraseTarget(
const std::string& nodeName) {
190 if (
this->hasNoModel_())
191 GUM_ERROR(NullElement,
192 "No Bayes net has been assigned to the " 193 "inference algorithm");
195 eraseTarget(
this->BN().idFromName(nodeName));
200 template <
typename GUM_SCALAR >
201 INLINE
const NodeSet& MarginalTargetedInference< GUM_SCALAR >::targets()
const noexcept {
206 template <
typename GUM_SCALAR >
207 INLINE
const Size MarginalTargetedInference< GUM_SCALAR >::nbrTargets()
const noexcept {
208 return _targets_.size();
213 template <
typename GUM_SCALAR >
214 void MarginalTargetedInference< GUM_SCALAR >::_setAllMarginalTargets_() {
216 if (!
this->hasNoModel_()) {
217 _targets_ =
this->BN().dag().asNodeSet();
218 onAllMarginalTargetsAdded_();
228 template <
typename GUM_SCALAR >
229 const Potential< GUM_SCALAR >& MarginalTargetedInference< GUM_SCALAR >::posterior(NodeId node) {
230 if (
this->hardEvidenceNodes().contains(node)) {
return *(
this->evidence()[node]); }
232 if (!isTarget(node)) {
234 GUM_ERROR(UndefinedElement, node <<
" is not a target node")
237 if (!
this->isInferenceDone()) {
this->makeInference(); }
239 return posterior_(node);
243 template <
typename GUM_SCALAR >
244 const Potential< GUM_SCALAR >&
245 MarginalTargetedInference< GUM_SCALAR >::posterior(
const std::string& nodeName) {
246 return posterior(
this->BN().idFromName(nodeName));
252 template <
typename GUM_SCALAR >
253 INLINE GUM_SCALAR MarginalTargetedInference< GUM_SCALAR >::H(NodeId X) {
254 return posterior(X).entropy();
260 template <
typename GUM_SCALAR >
261 INLINE GUM_SCALAR MarginalTargetedInference< GUM_SCALAR >::H(
const std::string& nodeName) {
262 return H(
this->BN().idFromName(nodeName));
266 template <
typename GUM_SCALAR >
267 Potential< GUM_SCALAR >
268 MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(NodeId target,
const NodeSet& evs) {
269 const auto& vtarget =
this->BN().variable(target);
271 if (evs.contains(target)) {
272 GUM_ERROR(InvalidArgument,
273 "Target <" << vtarget.name() <<
"> (" << target <<
") can not be in evs (" << evs
276 auto condset =
this->BN().minimalCondSet(target, evs);
278 Potential< GUM_SCALAR > res;
279 this->eraseAllTargets();
280 this->eraseAllEvidence();
281 res.add(
this->BN().variable(target));
282 this->addTarget(target);
283 for (
const auto& n: condset) {
284 res.add(
this->BN().variable(n));
285 this->addEvidence(n, 0);
288 Instantiation inst(res);
289 for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
291 for (
const auto& n: condset)
292 this->chgEvidence(n, inst.val(
this->BN().variable(n)));
293 this->makeInference();
295 for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
296 res.set(inst,
this->posterior(target)[inst]);
298 inst.setFirstVar(vtarget);
305 template <
typename GUM_SCALAR >
306 Potential< GUM_SCALAR > MarginalTargetedInference< GUM_SCALAR >::evidenceImpact(
307 const std::string& target,
308 const std::vector< std::string >& evs) {
309 const auto& bn =
this->BN();
310 return evidenceImpact(bn.idFromName(target), bn.nodeset(evs));
314 template <
typename GUM_SCALAR >
315 INLINE
bool MarginalTargetedInference< GUM_SCALAR >::isTargetedMode_()
const {
316 return _targeted_mode_;
318 template <
typename GUM_SCALAR >
319 INLINE
void MarginalTargetedInference< GUM_SCALAR >::setTargetedMode_() {
320 if (!_targeted_mode_) {
322 _targeted_mode_ =
true;