33 template <
typename GUM_SCALAR >
34 MarginalTargetedMNInference< GUM_SCALAR >::MarginalTargetedMNInference(
35 const IMarkovNet< GUM_SCALAR >* mn) :
36 MarkovNetInference< GUM_SCALAR >(mn) {
38 if (
this->hasNoModel_()) {
39 MarkovNetInference< GUM_SCALAR >::setMarkovNetDuringConstruction__(mn);
44 targeted_mode__ =
false;
45 targets__ = mn->graph().asNodeSet();
48 GUM_CONSTRUCTOR(MarginalTargetedMNInference);
53 template <
typename GUM_SCALAR >
54 MarginalTargetedMNInference< GUM_SCALAR >::~MarginalTargetedMNInference() {
55 GUM_DESTRUCTOR(MarginalTargetedMNInference);
60 template <
typename GUM_SCALAR >
61 void MarginalTargetedMNInference< GUM_SCALAR >::onModelChanged_(
62 const GraphicalModel* mn) {
63 targeted_mode__ =
true;
64 setAllMarginalTargets__();
73 template <
typename GUM_SCALAR >
75 MarginalTargetedMNInference< GUM_SCALAR >::isTarget(NodeId node)
const {
77 if (
this->hasNoModel_())
78 GUM_ERROR(NullElement,
79 "No Markov net has been assigned to the " 80 "inference algorithm");
81 if (!
this->MN().graph().exists(node)) {
82 GUM_ERROR(UndefinedElement,
83 node <<
" is not a NodeId in the Markov network");
86 return targets__.contains(node);
90 template <
typename GUM_SCALAR >
91 INLINE
bool MarginalTargetedMNInference< GUM_SCALAR >::isTarget(
92 const std::string& nodeName)
const {
93 return isTarget(
this->MN().idFromName(nodeName));
98 template <
typename GUM_SCALAR >
99 INLINE
void MarginalTargetedMNInference< GUM_SCALAR >::eraseAllTargets() {
100 onAllMarginalTargetsErased_();
106 MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
111 template <
typename GUM_SCALAR >
112 void MarginalTargetedMNInference< GUM_SCALAR >::addTarget(NodeId target) {
114 if (
this->hasNoModel_())
115 GUM_ERROR(NullElement,
116 "No Markov net has been assigned to the " 117 "inference algorithm");
119 if (!
this->MN().graph().exists(target)) {
120 GUM_ERROR(UndefinedElement,
121 target <<
" is not a NodeId in the Markov network");
126 if (!targets__.contains(target)) {
127 targets__.insert(target);
128 onMarginalTargetAdded_(target);
130 MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
136 template <
typename GUM_SCALAR >
137 void MarginalTargetedMNInference< GUM_SCALAR >::addAllTargets() {
139 if (
this->hasNoModel_())
140 GUM_ERROR(NullElement,
141 "No Markov net has been assigned to the " 142 "inference algorithm");
146 for (
const auto target:
this->MN().graph()) {
147 if (!targets__.contains(target)) {
148 targets__.insert(target);
149 onMarginalTargetAdded_(target);
151 MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
158 template <
typename GUM_SCALAR >
159 void MarginalTargetedMNInference< GUM_SCALAR >::addTarget(
160 const std::string& nodeName) {
162 if (
this->hasNoModel_())
163 GUM_ERROR(NullElement,
164 "No Markov net has been assigned to the " 165 "inference algorithm");
167 addTarget(
this->MN().idFromName(nodeName));
172 template <
typename GUM_SCALAR >
173 void MarginalTargetedMNInference< GUM_SCALAR >::eraseTarget(NodeId target) {
175 if (
this->hasNoModel_())
176 GUM_ERROR(NullElement,
177 "No Markov net has been assigned to the " 178 "inference algorithm");
180 if (!
this->MN().graph().exists(target)) {
181 GUM_ERROR(UndefinedElement,
182 target <<
" is not a NodeId in the Markov network");
186 if (targets__.contains(target)) {
187 targeted_mode__ =
true;
189 onMarginalTargetErased_(target);
190 targets__.erase(target);
192 MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
198 template <
typename GUM_SCALAR >
199 void MarginalTargetedMNInference< GUM_SCALAR >::eraseTarget(
200 const std::string& nodeName) {
202 if (
this->hasNoModel_())
203 GUM_ERROR(NullElement,
204 "No Markov net has been assigned to the " 205 "inference algorithm");
207 eraseTarget(
this->MN().idFromName(nodeName));
212 template <
typename GUM_SCALAR >
213 INLINE
const NodeSet&
214 MarginalTargetedMNInference< GUM_SCALAR >::targets()
const noexcept {
219 template <
typename GUM_SCALAR >
221 MarginalTargetedMNInference< GUM_SCALAR >::nbrTargets()
const noexcept {
222 return targets__.size();
227 template <
typename GUM_SCALAR >
228 void MarginalTargetedMNInference< GUM_SCALAR >::setAllMarginalTargets__() {
230 if (!
this->hasNoModel_()) {
231 targets__ =
this->MN().graph().asNodeSet();
232 onAllMarginalTargetsAdded_();
242 template <
typename GUM_SCALAR >
243 const Potential< GUM_SCALAR >&
244 MarginalTargetedMNInference< GUM_SCALAR >::posterior(NodeId node) {
245 if (
this->hardEvidenceNodes().contains(node)) {
246 return *(
this->evidence()[node]);
249 if (!isTarget(node)) {
251 GUM_ERROR(UndefinedElement, node <<
" is not a target node");
254 if (!
this->isInferenceDone()) {
this->makeInference(); }
256 return posterior_(node);
260 template <
typename GUM_SCALAR >
261 const Potential< GUM_SCALAR >&
262 MarginalTargetedMNInference< GUM_SCALAR >::posterior(
263 const std::string& nodeName) {
264 return posterior(
this->MN().idFromName(nodeName));
270 template <
typename GUM_SCALAR >
271 INLINE GUM_SCALAR MarginalTargetedMNInference< GUM_SCALAR >::H(NodeId X) {
272 return posterior(X).entropy();
278 template <
typename GUM_SCALAR >
280 MarginalTargetedMNInference< GUM_SCALAR >::H(
const std::string& nodeName) {
281 return H(
this->MN().idFromName(nodeName));
285 template <
typename GUM_SCALAR >
286 Potential< GUM_SCALAR >
287 MarginalTargetedMNInference< GUM_SCALAR >::evidenceImpact(
289 const NodeSet& evs) {
290 const auto& vtarget =
this->MN().variable(target);
292 if (evs.contains(target)) {
293 GUM_ERROR(InvalidArgument,
294 "Target <" << vtarget.name() <<
"> (" << target
295 <<
") can not be in evs (" << evs <<
").");
297 auto condset =
this->MN().minimalCondSet(target, evs);
299 Potential< GUM_SCALAR > res;
300 this->eraseAllTargets();
301 this->eraseAllEvidence();
302 res.add(
this->MN().variable(target));
303 this->addTarget(target);
304 for (
const auto& n: condset) {
305 res.add(
this->MN().variable(n));
306 this->addEvidence(n, 0);
309 Instantiation inst(res);
310 for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
312 for (
const auto& n: condset)
313 this->chgEvidence(n, inst.val(
this->MN().variable(n)));
314 this->makeInference();
316 for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
317 res.set(inst,
this->posterior(target)[inst]);
319 inst.setFirstVar(vtarget);
326 template <
typename GUM_SCALAR >
327 Potential< GUM_SCALAR >
328 MarginalTargetedMNInference< GUM_SCALAR >::evidenceImpact(
329 const std::string& target,
330 const std::vector< std::string >& evs) {
331 const auto& mn =
this->MN();
332 return evidenceImpact(mn.idFromName(target), mn.nodeset(evs));
336 template <
typename GUM_SCALAR >
337 INLINE
bool MarginalTargetedMNInference< GUM_SCALAR >::isTargetedMode_()
const {
338 return targeted_mode__;
340 template <
typename GUM_SCALAR >
341 INLINE
void MarginalTargetedMNInference< GUM_SCALAR >::setTargetedMode_() {
342 if (!targeted_mode__) {
344 targeted_mode__ =
true;