Fixed example_imagesegmentation by adding InfAlg::setMaxIter(size_t)
[libdai.git] / include / dai / decmap.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2010 Joris Mooij [joris dot mooij at libdai dot org]
8 */
9
10
11 /// \file
12 /// \brief Defines class DecMAP, which constructs a MAP state by decimation
13
14
15 #ifndef __defined_libdai_decmap_h
16 #define __defined_libdai_decmap_h
17
18
19 #include <dai/daialg.h>
20
21
22 namespace dai {
23
24
25 /// Approximate inference algorithm DecMAP, which constructs a MAP state by decimation
26 /** Decimation involves repeating the following two steps until no free variables remain:
27 * - run an approximate inference algorithm,
28 * - clamp the factor with the lowest entropy to its most probable state
29 */
30 class DecMAP : public DAIAlgFG {
31 private:
32 /// Stores the final MAP state
33 std::vector<size_t> _state;
34 /// Stores the log probability of the MAP state
35 Real _logp;
36 /// Maximum difference encountered so far
37 Real _maxdiff;
38 /// Number of iterations needed
39 size_t _iters;
40
41 public:
42 /// Parameters for DecMAP
43 struct Properties {
44 /// Verbosity (amount of output sent to stderr)
45 size_t verbose;
46
47 /// Complete or partial reinitialization of clamped subgraphs?
48 bool reinit;
49
50 /// Name of the algorithm used to calculate the beliefs on clamped subgraphs
51 std::string ianame;
52
53 /// Parameters for the algorithm used to calculate the beliefs on clamped subgraphs
54 PropertySet iaopts;
55 } props;
56
57 /// Name of this inference algorithm
58 static const char *Name;
59
60 public:
61 /// Default constructor
62 DecMAP() : DAIAlgFG(), _state(), _logp(), _maxdiff(), _iters(), props() {}
63
64 /// Construct from FactorGraph \a fg and PropertySet \a opts
65 /** \param fg Factor graph.
66 * \param opts Parameters @see Properties
67 */
68 DecMAP( const FactorGraph &fg, const PropertySet &opts );
69
70
71 /// \name General InfAlg interface
72 //@{
73 virtual DecMAP* clone() const { return new DecMAP(*this); }
74 virtual std::string identify() const;
75 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
76 virtual Factor belief( const VarSet &/*vs*/ ) const;
77 virtual Factor beliefV( size_t i ) const;
78 virtual Factor beliefF( size_t I ) const { return belief( factor(I).vars() ); }
79 virtual std::vector<Factor> beliefs() const;
80 virtual Real logZ() const { return _logp; }
81 virtual std::vector<size_t> findMaximum() const { return _state; }
82 virtual void init() { _maxdiff = 0.0; _iters = 0; }
83 virtual void init( const VarSet &/*ns*/ ) { init(); }
84 virtual Real run();
85 virtual Real maxDiff() const { return _maxdiff; }
86 virtual size_t Iterations() const { return _iters; }
87 virtual void setProperties( const PropertySet &opts );
88 virtual PropertySet getProperties() const;
89 virtual std::string printProperties() const;
90 //@}
91 };
92
93
94 } // end of namespace dai
95
96
97 #endif