libDAI
|
00001 /* This file is part of libDAI - http://www.libdai.org/ 00002 * 00003 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved. 00004 * 00005 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 00006 */ 00007 00008 00011 00012 00013 #ifndef __defined_libdai_trwbp_h 00014 #define __defined_libdai_trwbp_h 00015 00016 00017 #include <string> 00018 #include <dai/daialg.h> 00019 #include <dai/factorgraph.h> 00020 #include <dai/properties.h> 00021 #include <dai/enum.h> 00022 #include <dai/bp.h> 00023 00024 00025 namespace dai { 00026 00027 00029 00048 class TRWBP : public BP { 00049 protected: 00051 00056 std::vector<Real> _weight; 00057 00058 public: 00060 00064 size_t nrtrees; 00065 00066 public: 00068 00069 00070 TRWBP() : BP(), _weight() {} 00071 00073 00078 TRWBP( const FactorGraph &fg, const PropertySet &opts ) : BP(fg, opts), _weight() { 00079 setProperties( opts ); 00080 construct(); 00081 } 00083 00085 00086 virtual TRWBP* clone() const { return new TRWBP(*this); } 00087 virtual TRWBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new TRWBP( fg, opts ); } 00088 virtual std::string name() const { return "TRWBP"; } 00089 virtual Real logZ() const; 00090 virtual void setProperties( const PropertySet &opts ); 00091 virtual PropertySet getProperties() const; 00092 virtual std::string printProperties() const; 00094 00096 00097 00098 Real Weight( size_t I ) const { return _weight[I]; } 00099 00101 const std::vector<Real>& Weights() const { return _weight; } 00102 00104 void setWeight( size_t I, Real c ) { _weight[I] = c; } 00105 00107 00109 void setWeights( const std::vector<Real> &c ) { _weight = c; } 00110 00112 void addTreeToWeights( const RootedTree &tree ); 00113 00115 void sampleWeights( size_t nrTrees ); 00116 00117 protected: 00119 00122 virtual Prob calcIncomingMessageProduct( size_t I, bool without_i, size_t i ) const; 00123 00125 virtual void calcBeliefV( size_t i, Prob &p ) const; 00126 00127 // Calculates unnormalized belief of factor \a I 00128 virtual void calcBeliefF( size_t I, Prob &p ) const { 00129 p = calcIncomingMessageProduct( I, false, 0 ); 00130 } 00131 00132 // Helper function for constructors 00133 virtual void construct(); 00134 }; 00135 00136 00137 } // end of namespace dai 00138 00139 00140 #endif