Replaced Name members by name() virtual functions (fixing a bug in matlab/dai.cpp)
[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 public:
58 /// Default constructor
59 DecMAP() : DAIAlgFG(), _state(), _logp(), _maxdiff(), _iters(), props() {}
60
61 /// Construct from FactorGraph \a fg and PropertySet \a opts
62 /** \param fg Factor graph.
63 * \param opts Parameters @see Properties
64 */
65 DecMAP( const FactorGraph &fg, const PropertySet &opts );
66
67
68 /// \name General InfAlg interface
69 //@{
70 virtual DecMAP* clone() const { return new DecMAP(*this); }
71 virtual std::string name() const { return "DECMAP"; }
72 virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
73 virtual Factor belief( const VarSet &/*vs*/ ) const;
74 virtual Factor beliefV( size_t i ) const;
75 virtual Factor beliefF( size_t I ) const { return belief( factor(I).vars() ); }
76 virtual std::vector<Factor> beliefs() const;
77 virtual Real logZ() const { return _logp; }
78 virtual std::vector<size_t> findMaximum() const { return _state; }
79 virtual void init() { _maxdiff = 0.0; _iters = 0; }
80 virtual void init( const VarSet &/*ns*/ ) { init(); }
81 virtual Real run();
82 virtual Real maxDiff() const { return _maxdiff; }
83 virtual size_t Iterations() const { return _iters; }
84 virtual void setProperties( const PropertySet &opts );
85 virtual PropertySet getProperties() const;
86 virtual std::string printProperties() const;
87 //@}
88 };
89
90
91 } // end of namespace dai
92
93
94 #endif