aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
ShaferShenoyLIMIDInference.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Implementation of an influence diagram inference algorithm based upon
25  * Shaffer-Shenoy's one for bayes net inferences
26  */
27 
28 #ifndef GUM_SHAFERSHENOY_LIMIDS_H
29 #define GUM_SHAFERSHENOY_LIMIDS_H
30 
31 #include <iostream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 #include <agrum/agrum.h>
37 
38 #include <agrum/tools/core/exceptions.h>
39 #include <agrum/tools/core/list.h>
40 
41 #include <agrum/tools/graphs/DAG.h>
42 #include <agrum/tools/graphs/algorithms/triangulations/partialOrderedTriangulation.h>
43 
44 #include <agrum/tools/multidim/implementations/multiDimBucket.h>
45 #include <agrum/tools/multidim/implementations/multiDimSparse.h>
46 #include <agrum/ID/inference/tools/decisionPotential.h>
47 
48 #include <agrum/ID/inference/tools/influenceDiagramInference.h>
49 #include <agrum/tools/graphs/algorithms/triangulations/defaultTriangulation.h>
50 
51 
52 namespace gum {
53 
54  /**
55  * @class InfluenceDiagramInference influenceDiagramInference.h
56  *<agrum/ID/inference/influenceDiagramInference.h>
57  * @brief This class implements an algorithm for inference
58  * in influence diagrams based upon Shaffer-Shenoy's one for bayes net
59  *inferences
60  * @ingroup id_group
61  *
62  * The class used for the triangulation is partialOrderedTriangulation.
63  */
64  template < typename GUM_SCALAR >
65  class ShaferShenoyLIMIDInference: public InfluenceDiagramInference< GUM_SCALAR > {
66  using PhiNodeProperty = NodeProperty< DecisionPotential< GUM_SCALAR > >;
67  using PsiArcProperty = ArcProperty< DecisionPotential< GUM_SCALAR > >;
68  using SetOfVars = Set< const DiscreteVariable* >;
69 
70 
71  public:
72  // ====================================================================
73  /// @name Constructor & destructor
74  // ====================================================================
75  /// @{
76 
77  /**
78  * Default constructor.
79  * @param infDiag the influence diagram we want to perform inference upon
80  */
81  explicit ShaferShenoyLIMIDInference(const InfluenceDiagram< GUM_SCALAR >* infDiag);
82 
83  /**
84  * Destructor.
85  */
86  virtual ~ShaferShenoyLIMIDInference();
87 
88  const JunctionTree* junctionTree() const;
89 
90  void clear() override;
91 
92  ///@{
93  /// No forgetting rule assumption
94  void addNoForgettingAssumption(const std::vector< NodeId >& ids);
95  void addNoForgettingAssumption(const std::vector< std::string >& names);
96  bool hasNoForgettingAssumption() const;
97  ///@}
98 
99  DAG reducedGraph() const { return reduced_; };
100 
101  std::vector< NodeSet > reversePartialOrder() const;
102 
103  InfluenceDiagram< GUM_SCALAR > reducedLIMID() const;
104 
105  bool isSolvable() const;
106 
107 
108  gum::Potential< GUM_SCALAR > optimalDecision(NodeId decisionId) final;
109  gum::Potential< GUM_SCALAR > optimalDecision(const std::string& decisionName) final {
110  return optimalDecision(this->influenceDiagram().idFromName(decisionName));
111  };
112 
113  /**
114  * Return the posterior probability of a node
115  *
116  * @param node
117  * @return the posterior probability
118  */
119  virtual const Potential< GUM_SCALAR >& posterior(NodeId node) final;
120  const Potential< GUM_SCALAR >& posterior(const std::string& name) final {
121  return posterior(this->influenceDiagram().idFromName(name));
122  };
123 
124  /**
125  * Return the posterior utility of a node
126  *
127  * @param node
128  * @return the posterior utility of a node
129  */
130  virtual const Potential< GUM_SCALAR >& posteriorUtility(NodeId node) final;
131  virtual const Potential< GUM_SCALAR >& posteriorUtility(const std::string& name) final {
132  return posteriorUtility(this->influenceDiagram().idFromName(name));
133  };
134 
135  /**
136  * Return the pair (mean,variance) for a node
137  *
138  * @param node
139  * @return the pair (mean,variance) for a node
140  */
141  virtual std::pair< GUM_SCALAR, GUM_SCALAR > meanVar(NodeId node) final;
142  std::pair< GUM_SCALAR, GUM_SCALAR > meanVar(const std::string& name) final {
143  return meanVar(this->influenceDiagram().idFromName(name));
144  };
145 
146  /**
147  * Return the pair (mean,variance) for the total utility (MEU)
148  *
149  * @return the pair (mean,variance) for MEU
150  */
151  std::pair< GUM_SCALAR, GUM_SCALAR > MEU() final;
152 
153  protected:
154  void onStateChanged_() override;
155  void onEvidenceAdded_(NodeId id, bool isHardEvidence) override;
156  void onEvidenceErased_(NodeId id, bool isHardEvidence) override;
157  void onAllEvidenceErased_(bool contains_hard_evidence) override;
158  void onEvidenceChanged_(NodeId id, bool hasChangedSoftHard) override;
159  void onModelChanged_(const GraphicalModel* model) override;
160  void updateOutdatedStructure_() override;
161  void updateOutdatedPotentials_() override;
162  void makeInference_() override;
163 
164  /// Returns the set of non-requisite for node d
165  NodeSet nonRequisiteNodes_(NodeId d) const;
166 
167  DAG reduced_;
168  CliqueGraph reducedJunctionTree_;
169  NodeProperty< NodeId > node_to_clique_;
170  EdgeProperty< SetOfVars > varsSeparator_;
171  NodeProperty< Potential< GUM_SCALAR > > strategies_;
172  NodeProperty< DecisionPotential< GUM_SCALAR > > posteriors_;
173  NodeProperty< DecisionPotential< GUM_SCALAR > > unconditionalDecisions_;
174 
175  void createReduced_();
176  std::vector< NodeSet > reversePartialOrder_;
177  std::vector< NodeId > solvabilityOrder_;
178  std::vector< NodeId > noForgettingOrder_;
179 
180  private:
181  void _completingNoForgettingAssumption_();
182  void _reducingLIMID_();
183  void _creatingPartialOrder_(const NodeSet& utilities);
184  void _checkingSolvability_(const NodeSet& utilities);
185  void _creatingJunctionTree_();
186  void _findingCliqueForEachNode_(DefaultTriangulation& triangulation);
187 
188  void initializingInference_(PhiNodeProperty& phi, PsiArcProperty& psi);
189  void collectingMessage_(PhiNodeProperty& phi, PsiArcProperty& psi, NodeId rootClique);
190  void collectingToFollowingRoot_(PhiNodeProperty& phi,
191  PsiArcProperty& psi,
192  NodeId fromClique,
193  NodeId toClique);
194  void deciding_(PhiNodeProperty& phi, PsiArcProperty& psi, NodeId decisionNode);
195  void transmittingMessage_(PhiNodeProperty& phi,
196  PsiArcProperty& psi,
197  NodeId fromClique,
198  NodeId toClique);
199  void transmittingFinalMessage_(PhiNodeProperty& phi,
200  PsiArcProperty& psi,
201  NodeId fromClique,
202  NodeId toClique);
203  void distributingMessage_(PhiNodeProperty& phi, PsiArcProperty& psi, NodeId rootClique);
204  void computingPosteriors_(const PhiNodeProperty& phi, const PsiArcProperty& psi);
205  DecisionPotential< double > integrating_(const PhiNodeProperty& phi,
206  const PsiArcProperty& psi,
207  NodeId clique,
208  NodeId except) const;
209  DecisionPotential< double >
210  integrating_(const PhiNodeProperty& phi, const PsiArcProperty& psi, NodeId clique) const;
211  void binarizingMax_(const Potential< GUM_SCALAR >& decision,
212  const Potential< GUM_SCALAR >& proba) const;
213  };
214 } /* namespace gum */
215 
216 #include <agrum/ID/inference/ShaferShenoyLIMIDInference_tpl.h>
217 
218 #endif /* GUM_SHAFERSHENOY_LIMIDS_H */