1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
9 /// \file
10 /// \brief Defines class DecMAP, which constructs a MAP state by decimation
13 #ifndef __defined_libdai_decmap_h
14 #define __defined_libdai_decmap_h
17 #include <dai/daialg.h>
20 namespace dai {
23 /// Approximate inference algorithm DecMAP, which constructs a MAP state by decimation
24 /** Decimation involves repeating the following two steps until no free variables remain:
25 * - run an approximate inference algorithm,
26 * - clamp the factor with the lowest entropy to its most probable state
27 */
28 class DecMAP : public DAIAlgFG {
29 private:
30 /// Stores the final MAP state
31 std::vector<size_t> _state;
32 /// Stores the log probability of the MAP state
33 Real _logp;
34 /// Maximum difference encountered so far
35 Real _maxdiff;
36 /// Number of iterations needed
37 size_t _iters;
39 public:
40 /// Parameters for DecMAP
41 struct Properties {
42 /// Verbosity (amount of output sent to stderr)
43 size_t verbose;
45 /// Complete or partial reinitialization of clamped subgraphs?
46 bool reinit;
48 /// Name of the algorithm used to calculate the beliefs on clamped subgraphs
49 std::string ianame;
51 /// Parameters for the algorithm used to calculate the beliefs on clamped subgraphs
52 PropertySet iaopts;
53 } props;
55 public:
56 /// Default constructor
57 DecMAP() : DAIAlgFG(), _state(), _logp(), _maxdiff(), _iters(), props() {}
59 /// Construct from FactorGraph \a fg and PropertySet \a opts
60 /** \param fg Factor graph.
61 * \param opts Parameters @see Properties
62 */
63 DecMAP( const FactorGraph &fg, const PropertySet &opts );
66 /// \name General InfAlg interface
67 //@{
68 virtual DecMAP* clone() const { return new DecMAP(*this); }
69 virtual std::string name() const { return "DECMAP"; }
70 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
71 virtual Factor belief( const VarSet &/*vs*/ ) const;
72 virtual Factor beliefV( size_t i ) const;
73 virtual Factor beliefF( size_t I ) const { return belief( factor(I).vars() ); }
74 virtual std::vector<Factor> beliefs() const;
75 virtual Real logZ() const { return _logp; }
76 virtual std::vector<size_t> findMaximum() const { return _state; }
77 virtual void init() { _maxdiff = 0.0; _iters = 0; }
78 virtual void init( const VarSet &/*ns*/ ) { init(); }
79 virtual Real run();
80 virtual Real maxDiff() const { return _maxdiff; }
81 virtual size_t Iterations() const { return _iters; }
82 virtual void setProperties( const PropertySet &opts );
83 virtual PropertySet getProperties() const;
84 virtual std::string printProperties() const;
85 //@}
86 };
89 } // end of namespace dai
92 #endif