aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuredBayesBall.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 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 >& requisiteNodes(const PRMInstance< GUM_SCALAR >* i) const;
77 
78  /// Returns the set of requisite nodes w.r.t. d-separation for i.
79  const Set< NodeId >& requisiteNodes(const PRMInstance< GUM_SCALAR >& i) const;
80 
81  /// Returns the number of occurrence of the given key, which is the number
82  /// of PRMInstance<GUM_SCALAR> sharing the same set of requisite nodes.
83  Size occurrence(const std::string& key) const;
84 
85  /// Returns the ratio between the total number of instances and the number
86  /// of instances with the same configuration.
87  float liftRatio() const;
88 
89  /// Returns true if i has requisite nodes.
90  bool exists(const PRMInstance< GUM_SCALAR >* i) const;
91 
92  /// Returns true if i has requisite nodes.
93  bool exists(const PRMInstance< GUM_SCALAR >& i) const;
94 
95  /// @}
96 
97  /// Compute the set or requisite nodes for each required instance given
98  /// the current set of observations.
99  /// Discard previous computations.
100  void compute(const PRMInstance< GUM_SCALAR >* i, NodeId n);
101 
102  /// Compute the set or requisite nodes for each required instance given
103  /// the current set of observations.
104  /// Discard previous computations.
105  void compute(const PRMInstance< GUM_SCALAR >& i, NodeId n);
106 
107  /// Returns true if there is a hard evidence on i->get(n).
108  bool _isHardEvidence_(const PRMInstance< GUM_SCALAR >* i, NodeId n);
109 
110  private:
111  /// Copy constructor.
112  StructuredBayesBall(const StructuredBayesBall& source);
113 
114  /// Copy operator.
115  StructuredBayesBall& operator=(const StructuredBayesBall& source);
116 
117  /// Code alias
118  typedef HashTable< NodeId, std::pair< bool, bool > > MarkMap;
119  /// Code alias
120  typedef HashTable< const PRMInstance< GUM_SCALAR >*, MarkMap* > InstanceMap;
121  /// Code alias
122  std::pair< bool, bool >&
123  _getMark_(InstanceMap& marks, const PRMInstance< GUM_SCALAR >* i, NodeId n);
124  /// Code alias
125  const PRMSlotChain< GUM_SCALAR >& _getSC_(const PRMInstance< GUM_SCALAR >* i, NodeId n);
126 
127  /// Cleans this before a new computation.
128  void _clean_();
129 
130  /// The real compute method.
131  void _compute_(const PRMInstance< GUM_SCALAR >* i, NodeId n);
132 
133  /// When the ball is received on i->get(n) from a child.
134  void _fromChild_(const PRMInstance< GUM_SCALAR >* i, NodeId n, InstanceMap& marks);
135 
136  /// When the ball is receive on i->get(n) from a parent.
137  void _fromParent_(const PRMInstance< GUM_SCALAR >* i, NodeId n, InstanceMap& marks);
138 
139  /// Fill _keyMap_ and _reqMap_.
140  void _fillMaps_(InstanceMap& marks);
141 
142  /// Builds the HashKey for the given instance and requisite nodes set.
143  std::string _buildHashKey_(const PRMInstance< GUM_SCALAR >* i, Set< NodeId >& req_nodes);
144 
145  /// The PRM at which _model_ belongs.
146  const PRMInference< GUM_SCALAR >* _inf_;
147 
148  /// Associate an PRMInstance<GUM_SCALAR> with a unique key w.r.t.
149  /// d-separation and
150  /// the
151  /// set of requisite nodes deduced from d-separation analysis.
152  HashTable< const PRMInstance< GUM_SCALAR >*, std::pair< std::string, Set< NodeId >* > >
153  _keyMap_;
154 
155  /// Associate a Key with the set of requisite nodes associated with it.
156  /// The Size value is the number of instance with the same key.
157  HashTable< std::string, std::pair< Set< NodeId >*, Size > > _reqMap_;
158  };
159 
160 
161 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
162  extern template class StructuredBayesBall< double >;
163 # endif
164 
165 
166  } /* namespace prm */
167 } /* namespace gum */
168 
169 # include <agrum/PRM/inference/structuredBayesBall_tpl.h>
170 
171 #endif /* GUM_STRUCTURED_BB_H */