aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuredBayesBall.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 Headers of StructuredBayesBall.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #include <sstream>
30 #include <string>
31 #include <utility>
32 
33 #include <agrum/tools/core/hashTable.h>
34 #include <agrum/tools/core/set.h>
35 
36 #include <agrum/PRM/PRM.h>
37 #include <agrum/PRM/inference/PRMInference.h>
38 
39 #ifndef GUM_STRUCTURED_BB_H
40 # define GUM_STRUCTURED_BB_H
41 namespace gum {
42  namespace prm {
43 
44  /**
45  * @class StructuredBayesBall structuredBayesBall.h
46  * <agrum/PRM/structuredBayesBall.h>
47  * @brief This class represent the BayesBall algorithm applied on PRMs.
48  */
49  template < typename GUM_SCALAR >
50  class StructuredBayesBall {
51  public:
52  // ========================================================================
53  /// @name Constructors & destructor.
54  // ========================================================================
55  /// @{
56 
57  /// Default Constructor.
58  StructuredBayesBall(const PRMInference< GUM_SCALAR >& inference);
59 
60  /// Destructor.
61  ~StructuredBayesBall();
62 
63  /// @}
64  // ========================================================================
65  /// @name Getters and Setters.
66  // ========================================================================
67  /// @{
68 
69  /// Returns a unique key w.r.t. d-separation for i.
70  const std::string& key(const PRMInstance< GUM_SCALAR >* i) const;
71 
72  /// Returns a unique key w.r.t. d-separation for i.
73  const std::string& key(const PRMInstance< GUM_SCALAR >& i) const;
74 
75  /// Returns the set of requisite nodes w.r.t. d-separation for i.
76  const Set< NodeId >&
77  requisiteNodes(const PRMInstance< GUM_SCALAR >* i) const;
78 
79  /// Returns the set of requisite nodes w.r.t. d-separation for i.
80  const Set< NodeId >&
81  requisiteNodes(const PRMInstance< GUM_SCALAR >& i) const;
82 
83  /// Returns the number of occurrence of the given key, which is the number
84  /// of PRMInstance<GUM_SCALAR> sharing the same set of requisite nodes.
85  Size occurrence(const std::string& key) const;
86 
87  /// Returns the ratio between the total number of instances and the number
88  /// of instances with the same configuration.
89  float liftRatio() const;
90 
91  /// Returns true if i has requisite nodes.
92  bool exists(const PRMInstance< GUM_SCALAR >* i) const;
93 
94  /// Returns true if i has requisite nodes.
95  bool exists(const PRMInstance< GUM_SCALAR >& i) const;
96 
97  /// @}
98 
99  /// Compute the set or requisite nodes for each required instance given
100  /// the current set of observations.
101  /// Discard previous computations.
102  void compute(const PRMInstance< GUM_SCALAR >* i, NodeId n);
103 
104  /// Compute the set or requisite nodes for each required instance given
105  /// the current set of observations.
106  /// Discard previous computations.
107  void compute(const PRMInstance< GUM_SCALAR >& i, NodeId n);
108 
109  /// Returns true if there is a hard evidence on i->get(n).
110  bool isHardEvidence__(const PRMInstance< GUM_SCALAR >* i, NodeId n);
111 
112  private:
113  /// Copy constructor.
114  StructuredBayesBall(const StructuredBayesBall& source);
115 
116  /// Copy operator.
117  StructuredBayesBall& operator=(const StructuredBayesBall& source);
118 
119  /// Code alias
120  typedef HashTable< NodeId, std::pair< bool, bool > > MarkMap;
121  /// Code alias
122  typedef HashTable< const PRMInstance< GUM_SCALAR >*, MarkMap* > InstanceMap;
123  /// Code alias
124  std::pair< bool, bool >& getMark__(InstanceMap& marks,
125  const PRMInstance< GUM_SCALAR >* i,
126  NodeId n);
127  /// Code alias
128  const PRMSlotChain< GUM_SCALAR >& getSC__(const PRMInstance< GUM_SCALAR >* i,
129  NodeId n);
130 
131  /// Cleans this before a new computation.
132  void clean__();
133 
134  /// The real compute method.
135  void compute__(const PRMInstance< GUM_SCALAR >* i, NodeId n);
136 
137  /// When the ball is received on i->get(n) from a child.
138  void fromChild__(const PRMInstance< GUM_SCALAR >* i,
139  NodeId n,
140  InstanceMap& marks);
141 
142  /// When the ball is receive on i->get(n) from a parent.
143  void fromParent__(const PRMInstance< GUM_SCALAR >* i,
144  NodeId n,
145  InstanceMap& marks);
146 
147  /// Fill keyMap__ and reqMap__.
148  void fillMaps__(InstanceMap& marks);
149 
150  /// Builds the HashKey for the given instance and requisite nodes set.
151  std::string buildHashKey__(const PRMInstance< GUM_SCALAR >* i,
152  Set< NodeId >& req_nodes);
153 
154  /// The PRM at which model__ belongs.
155  const PRMInference< GUM_SCALAR >* inf__;
156 
157  /// Associate an PRMInstance<GUM_SCALAR> with a unique key w.r.t.
158  /// d-separation and
159  /// the
160  /// set of requisite nodes deduced from d-separation analysis.
161  HashTable< const PRMInstance< GUM_SCALAR >*,
162  std::pair< std::string, Set< NodeId >* > >
163  keyMap__;
164 
165  /// Associate a Key with the set of requisite nodes associated with it.
166  /// The Size value is the number of instance with the same key.
167  HashTable< std::string, std::pair< Set< NodeId >*, Size > > reqMap__;
168  };
169 
170 
171 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
172  extern template class StructuredBayesBall< double >;
173 # endif
174 
175 
176  } /* namespace prm */
177 } /* namespace gum */
178 
179 # include <agrum/PRM/inference/structuredBayesBall_tpl.h>
180 
181 #endif /* GUM_STRUCTURED_BB_H */