00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00014
00015
00016 #ifndef __defined_libdai_regiongraph_h
00017 #define __defined_libdai_regiongraph_h
00018
00019
00020 #include <iostream>
00021 #include <dai/bipgraph.h>
00022 #include <dai/factorgraph.h>
00023 #include <dai/weightedgraph.h>
00024
00025
00026 namespace dai {
00027
00028
00030 class Region : public VarSet {
00031 private:
00033 Real _c;
00034
00035 public:
00037 Region() : VarSet(), _c(1.0) {}
00038
00040 Region( const VarSet& x, Real c ) : VarSet(x), _c(c) {}
00041
00043 const Real& c() const { return _c; }
00044
00046 Real& c() { return _c; }
00047 };
00048
00049
00051 class FRegion : public Factor {
00052 private:
00054 Real _c;
00055
00056 public:
00058 FRegion() : Factor(), _c(1.0) {}
00059
00061 FRegion( const Factor& x, Real c ) : Factor(x), _c(c) {}
00062
00064 const Real& c() const { return _c; }
00065
00067 Real& c() { return _c; }
00068 };
00069
00070
00072
00094 class RegionGraph : public FactorGraph {
00095 protected:
00097 BipartiteGraph _G;
00098
00100 std::vector<FRegion> _ORs;
00101
00103 std::vector<Region> _IRs;
00104
00106 std::vector<size_t> _fac2OR;
00107
00108
00109 public:
00111
00112
00113 RegionGraph() : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {}
00114
00116
00118 RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
00119 construct( fg, ors, irs, edges );
00120
00121
00122 #ifdef DAI_DEBUG
00123 checkCountingNumbers();
00124 #endif
00125 }
00126
00128
00139 RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& cl ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
00140 constructCVM( fg, cl );
00141
00142
00143 #ifdef DAI_DEBUG
00144 checkCountingNumbers();
00145 #endif
00146 }
00147
00149 virtual RegionGraph* clone() const { return new RegionGraph(*this); }
00151
00153
00154
00155 size_t nrORs() const { return _ORs.size(); }
00157 size_t nrIRs() const { return _IRs.size(); }
00158
00160 const FRegion& OR( size_t alpha ) const {
00161 DAI_DEBASSERT( alpha < nrORs() );
00162 return _ORs[alpha];
00163 }
00165 FRegion& OR( size_t alpha ) {
00166 DAI_DEBASSERT( alpha < nrORs() );
00167 return _ORs[alpha];
00168 }
00169
00171 const Region& IR( size_t beta ) const {
00172 DAI_DEBASSERT( beta < nrIRs() );
00173 return _IRs[beta];
00174 }
00176 Region& IR( size_t beta ) {
00177 DAI_DEBASSERT( beta < nrIRs() );
00178 return _IRs[beta];
00179 }
00180
00182 size_t fac2OR( size_t I ) const {
00183 DAI_DEBASSERT( I < nrFactors() );
00184 DAI_DEBASSERT( I < _fac2OR.size() );
00185 return _fac2OR[I];
00186 }
00187
00189 const Neighbors& nbOR( size_t alpha ) const { return _G.nb1(alpha); }
00190
00192 const Neighbors& nbIR( size_t beta ) const { return _G.nb2(beta); }
00193
00195
00199 const BipartiteGraph& DAG() const { return _G; }
00201
00203
00204
00205
00210 bool checkCountingNumbers() const;
00212
00214
00215
00216 virtual void setFactor( size_t I, const Factor& newFactor, bool backup = false ) {
00217 FactorGraph::setFactor( I, newFactor, backup );
00218 recomputeOR( I );
00219 }
00220
00222 virtual void setFactors( const std::map<size_t, Factor>& facs, bool backup = false ) {
00223 FactorGraph::setFactors( facs, backup );
00224 VarSet ns;
00225 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
00226 ns |= fac->second.vars();
00227 recomputeORs( ns );
00228 }
00230
00232
00233
00234
00236 virtual void ReadFromFile( const char* ) {
00237 DAI_THROW(NOT_IMPLEMENTED);
00238 }
00239
00241
00243 virtual void WriteToFile( const char* , size_t =15 ) const {
00244 DAI_THROW(NOT_IMPLEMENTED);
00245 }
00246
00248 friend std::ostream& operator<< ( std::ostream& os, const RegionGraph& rg );
00249
00251
00253 virtual void printDot( std::ostream& ) const {
00254 DAI_THROW(NOT_IMPLEMENTED);
00255 }
00257
00258 protected:
00260 void construct( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges );
00261
00263 void constructCVM( const FactorGraph& fg, const std::vector<VarSet>& cl, size_t verbose=0 );
00264
00266
00268 void recomputeORs();
00269
00271
00273 void recomputeORs( const VarSet& vs );
00274
00276
00278 void recomputeOR( size_t I );
00279
00281
00287 void calcCVMCountingNumbers();
00288
00289 };
00290
00291
00292 }
00293
00294
00295 #endif