7f7a671efe4e08810fc051615c8cb828db466022
[libdai.git] / include / dai / decmap.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines class DecMAP, which constructs a MAP state by decimation
11
12
13 #ifndef __defined_libdai_decmap_h
14 #define __defined_libdai_decmap_h
15
16
17 #include <dai/daialg.h>
18
19
20 namespace dai {
21
22
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;
38
39 public:
40 /// Parameters for DecMAP
41 struct Properties {
42 /// Verbosity (amount of output sent to stderr)
43 size_t verbose;
44
45 /// Complete or partial reinitialization of clamped subgraphs?
46 bool reinit;
47
48 /// Name of the algorithm used to calculate the beliefs on clamped subgraphs
49 std::string ianame;
50
51 /// Parameters for the algorithm used to calculate the beliefs on clamped subgraphs
52 PropertySet iaopts;
53 } props;
54
55 public:
56 /// Default constructor
57 DecMAP() : DAIAlgFG(), _state(), _logp(), _maxdiff(), _iters(), props() {}
58
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 );
64
65
66 /// \name General InfAlg interface
67 //@{
68 virtual DecMAP* clone() const { return new DecMAP(*this); }
69 virtual DecMAP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new DecMAP( fg, opts ); }
70 virtual std::string name() const { return "DECMAP"; }
71 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
72 virtual Factor belief( const VarSet &/*vs*/ ) const;
73 virtual Factor beliefV( size_t i ) const;
74 virtual Factor beliefF( size_t I ) const { return belief( factor(I).vars() ); }
75 virtual std::vector<Factor> beliefs() const;
76 virtual Real logZ() const { return _logp; }
77 virtual std::vector<size_t> findMaximum() const { return _state; }
78 virtual void init() { _maxdiff = 0.0; _iters = 0; }
79 virtual void init( const VarSet &/*ns*/ ) { init(); }
80 virtual Real run();
81 virtual Real maxDiff() const { return _maxdiff; }
82 virtual size_t Iterations() const { return _iters; }
83 virtual void setProperties( const PropertySet &opts );
84 virtual PropertySet getProperties() const;
85 virtual std::string printProperties() const;
86 //@}
87 };
88
89
90 } // end of namespace dai
91
92
93 #endif