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_(
const GraphicalModel* mn) {
62 _targeted_mode_ =
true;
63 _setAllMarginalTargets_();
72 template <
typename GUM_SCALAR >
73 INLINE
bool MarginalTargetedMNInference< GUM_SCALAR >::isTarget(NodeId node)
const {
75 if (
this->hasNoModel_())
76 GUM_ERROR(NullElement,
77 "No Markov net has been assigned to the " 78 "inference algorithm");
79 if (!
this->MN().graph().exists(node)) {
80 GUM_ERROR(UndefinedElement, node <<
" is not a NodeId in the Markov network")
83 return _targets_.contains(node);
87 template <
typename GUM_SCALAR >
89 MarginalTargetedMNInference< GUM_SCALAR >::isTarget(
const std::string& nodeName)
const {
90 return isTarget(
this->MN().idFromName(nodeName));
95 template <
typename GUM_SCALAR >
96 INLINE
void MarginalTargetedMNInference< GUM_SCALAR >::eraseAllTargets() {
97 onAllMarginalTargetsErased_();
102 this->setState_(MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
107 template <
typename GUM_SCALAR >
108 void MarginalTargetedMNInference< GUM_SCALAR >::addTarget(NodeId target) {
110 if (
this->hasNoModel_())
111 GUM_ERROR(NullElement,
112 "No Markov net has been assigned to the " 113 "inference algorithm");
115 if (!
this->MN().graph().exists(target)) {
116 GUM_ERROR(UndefinedElement, target <<
" is not a NodeId in the Markov network")
121 if (!_targets_.contains(target)) {
122 _targets_.insert(target);
123 onMarginalTargetAdded_(target);
124 this->setState_(MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
130 template <
typename GUM_SCALAR >
131 void MarginalTargetedMNInference< GUM_SCALAR >::addAllTargets() {
133 if (
this->hasNoModel_())
134 GUM_ERROR(NullElement,
135 "No Markov net has been assigned to the " 136 "inference algorithm");
140 for (
const auto target:
this->MN().graph()) {
141 if (!_targets_.contains(target)) {
142 _targets_.insert(target);
143 onMarginalTargetAdded_(target);
144 this->setState_(MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
151 template <
typename GUM_SCALAR >
152 void MarginalTargetedMNInference< GUM_SCALAR >::addTarget(
const std::string& nodeName) {
154 if (
this->hasNoModel_())
155 GUM_ERROR(NullElement,
156 "No Markov net has been assigned to the " 157 "inference algorithm");
159 addTarget(
this->MN().idFromName(nodeName));
164 template <
typename GUM_SCALAR >
165 void MarginalTargetedMNInference< GUM_SCALAR >::eraseTarget(NodeId target) {
167 if (
this->hasNoModel_())
168 GUM_ERROR(NullElement,
169 "No Markov net has been assigned to the " 170 "inference algorithm");
172 if (!
this->MN().graph().exists(target)) {
173 GUM_ERROR(UndefinedElement, target <<
" is not a NodeId in the Markov network")
177 if (_targets_.contains(target)) {
178 _targeted_mode_ =
true;
180 onMarginalTargetErased_(target);
181 _targets_.erase(target);
182 this->setState_(MarkovNetInference< GUM_SCALAR >::StateOfInference::OutdatedStructure);
188 template <
typename GUM_SCALAR >
189 void MarginalTargetedMNInference< GUM_SCALAR >::eraseTarget(
const std::string& nodeName) {
191 if (
this->hasNoModel_())
192 GUM_ERROR(NullElement,
193 "No Markov net has been assigned to the " 194 "inference algorithm");
196 eraseTarget(
this->MN().idFromName(nodeName));
201 template <
typename GUM_SCALAR >
202 INLINE
const NodeSet& MarginalTargetedMNInference< GUM_SCALAR >::targets()
const noexcept {
207 template <
typename GUM_SCALAR >
208 INLINE
const Size MarginalTargetedMNInference< GUM_SCALAR >::nbrTargets()
const noexcept {
209 return _targets_.size();
214 template <
typename GUM_SCALAR >
215 void MarginalTargetedMNInference< GUM_SCALAR >::_setAllMarginalTargets_() {
217 if (!
this->hasNoModel_()) {
218 _targets_ =
this->MN().graph().asNodeSet();
219 onAllMarginalTargetsAdded_();
229 template <
typename GUM_SCALAR >
230 const Potential< GUM_SCALAR >& MarginalTargetedMNInference< GUM_SCALAR >::posterior(NodeId node) {
231 if (
this->hardEvidenceNodes().contains(node)) {
return *(
this->evidence()[node]); }
233 if (!isTarget(node)) {
235 GUM_ERROR(UndefinedElement, node <<
" is not a target node")
238 if (!
this->isInferenceDone()) {
this->makeInference(); }
240 return posterior_(node);
244 template <
typename GUM_SCALAR >
245 const Potential< GUM_SCALAR >&
246 MarginalTargetedMNInference< GUM_SCALAR >::posterior(
const std::string& nodeName) {
247 return posterior(
this->MN().idFromName(nodeName));
253 template <
typename GUM_SCALAR >
254 INLINE GUM_SCALAR MarginalTargetedMNInference< GUM_SCALAR >::H(NodeId X) {
255 return posterior(X).entropy();
261 template <
typename GUM_SCALAR >
262 INLINE GUM_SCALAR MarginalTargetedMNInference< GUM_SCALAR >::H(
const std::string& nodeName) {
263 return H(
this->MN().idFromName(nodeName));
267 template <
typename GUM_SCALAR >
268 Potential< GUM_SCALAR >
269 MarginalTargetedMNInference< GUM_SCALAR >::evidenceImpact(NodeId target,
const NodeSet& evs) {
270 const auto& vtarget =
this->MN().variable(target);
272 if (evs.contains(target)) {
273 GUM_ERROR(InvalidArgument,
274 "Target <" << vtarget.name() <<
"> (" << target <<
") can not be in evs (" << evs
277 auto condset =
this->MN().minimalCondSet(target, evs);
279 Potential< GUM_SCALAR > res;
280 this->eraseAllTargets();
281 this->eraseAllEvidence();
282 res.add(
this->MN().variable(target));
283 this->addTarget(target);
284 for (
const auto& n: condset) {
285 res.add(
this->MN().variable(n));
286 this->addEvidence(n, 0);
289 Instantiation inst(res);
290 for (inst.setFirst(); !inst.end(); inst.incNotVar(vtarget)) {
292 for (
const auto& n: condset)
293 this->chgEvidence(n, inst.val(
this->MN().variable(n)));
294 this->makeInference();
296 for (inst.setFirstVar(vtarget); !inst.end(); inst.incVar(vtarget)) {
297 res.set(inst,
this->posterior(target)[inst]);
299 inst.setFirstVar(vtarget);
306 template <
typename GUM_SCALAR >
307 Potential< GUM_SCALAR > MarginalTargetedMNInference< GUM_SCALAR >::evidenceImpact(
308 const std::string& target,
309 const std::vector< std::string >& evs) {
310 const auto& mn =
this->MN();
311 return evidenceImpact(mn.idFromName(target), mn.nodeset(evs));
315 template <
typename GUM_SCALAR >
316 INLINE
bool MarginalTargetedMNInference< GUM_SCALAR >::isTargetedMode_()
const {
317 return _targeted_mode_;
319 template <
typename GUM_SCALAR >
320 INLINE
void MarginalTargetedMNInference< GUM_SCALAR >::setTargetedMode_() {
321 if (!_targeted_mode_) {
323 _targeted_mode_ =
true;