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
00087 class RegionGraph : public FactorGraph {
00088 public:
00090 BipartiteGraph G;
00091
00093 std::vector<FRegion> ORs;
00094
00096 std::vector<Region> IRs;
00097
00099 std::vector<size_t> fac2OR;
00100
00101
00102 public:
00104
00105
00106 RegionGraph() : FactorGraph(), G(), ORs(), IRs(), fac2OR() {}
00107
00109 RegionGraph( const FactorGraph &fg ) : FactorGraph(fg), G(), ORs(), IRs(), fac2OR() {}
00110
00112
00114 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() {
00115 construct( fg, ors, irs, edges );
00116
00117
00118 #ifdef DAI_DEBUG
00119 checkCountingNumbers();
00120 #endif
00121 }
00122
00124
00127 RegionGraph( const FactorGraph &fg, const std::vector<Region> &ors, const std::vector<Region> &irs, const std::vector<std::pair<size_t,size_t> > &edges );
00128
00130
00141 RegionGraph( const FactorGraph &fg, const std::vector<VarSet> &cl ) : FactorGraph(), G(), ORs(), IRs(), fac2OR() {
00142 constructCVM( fg, cl );
00143
00144
00145 #ifdef DAI_DEBUG
00146 checkCountingNumbers();
00147 #endif
00148 }
00149
00151 virtual RegionGraph* clone() const { return new RegionGraph(*this); }
00153
00155
00156
00157 size_t nrORs() const { return ORs.size(); }
00159 size_t nrIRs() const { return IRs.size(); }
00160
00162 const FRegion & OR(size_t alpha) const { return ORs[alpha]; }
00164 FRegion & OR(size_t alpha) { return ORs[alpha]; }
00165
00167 const Region & IR(size_t beta) const { return IRs[beta]; }
00169 Region & IR(size_t beta) { return IRs[beta]; }
00170
00172 const Neighbors & nbOR( size_t alpha ) const { return G.nb1(alpha); }
00174 const Neighbors & nbIR( size_t beta ) const { return G.nb2(beta); }
00175
00177
00182 bool checkCountingNumbers() const;
00184
00186
00187
00188 virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
00189 FactorGraph::setFactor( I, newFactor, backup );
00190 RecomputeOR( I );
00191 }
00192
00194 virtual void setFactors( const std::map<size_t, Factor> & facs, bool backup = false ) {
00195 FactorGraph::setFactors( facs, backup );
00196 VarSet ns;
00197 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
00198 ns |= fac->second.vars();
00199 RecomputeORs( ns );
00200 }
00201
00203
00205 void RecomputeORs();
00206
00208
00210 void RecomputeORs( const VarSet &vs );
00211
00213
00215 void RecomputeOR( size_t I );
00216
00218
00224 void calcCountingNumbers();
00226
00228
00229
00230 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
00232
00233 protected:
00235 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 );
00236
00238 void constructCVM( const FactorGraph &fg, const std::vector<VarSet> &cl );
00239 };
00240
00241
00242 }
00243
00244
00245 #endif