Multiple changes: changes in build system, one workaround and one bug fix
[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/dai_config.h>
18 #ifdef DAI_WITH_DECMAP
19
20
21 #include <dai/daialg.h>
22
23
24 namespace dai {
25
26
27 /// Approximate inference algorithm DecMAP, which constructs a MAP state by decimation
28 /** Decimation involves repeating the following two steps until no free variables remain:
29 * - run an approximate inference algorithm,
30 * - clamp the factor with the lowest entropy to its most probable state
31 */
32 class DecMAP : public DAIAlgFG {
33 private:
34 /// Stores the final MAP state
35 std::vector<size_t> _state;
36 /// Stores the log probability of the MAP state
37 Real _logp;
38 /// Maximum difference encountered so far
39 Real _maxdiff;
40 /// Number of iterations needed
41 size_t _iters;
42
43 public:
44 /// Parameters for DecMAP
45 struct Properties {
46 /// Verbosity (amount of output sent to stderr)
47 size_t verbose;
48
49 /// Complete or partial reinitialization of clamped subgraphs?
50 bool reinit;
51
52 /// Name of the algorithm used to calculate the beliefs on clamped subgraphs
53 std::string ianame;
54
55 /// Parameters for the algorithm used to calculate the beliefs on clamped subgraphs
56 PropertySet iaopts;
57 } props;
58
59 public:
60 /// Default constructor
61 DecMAP() : DAIAlgFG(), _state(), _logp(), _maxdiff(), _iters(), props() {}
62
63 /// Construct from FactorGraph \a fg and PropertySet \a opts
64 /** \param fg Factor graph.
65 * \param opts Parameters @see Properties
66 */
67 DecMAP( const FactorGraph &fg, const PropertySet &opts );
68
69
70 /// \name General InfAlg interface
71 //@{
72 virtual DecMAP* clone() const { return new DecMAP(*this); }
73 virtual DecMAP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new DecMAP( fg, opts ); }
74 virtual std::string name() const { return "DECMAP"; }
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
98
99
100 #endif