Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

cn94.hpp

Go to the documentation of this file.
00001 #ifndef _CN94_H
00002 #define _CN94_H
00003 
00004 #include <iostream>
00005 #include <iomanip>
00006 #include "properties.hpp"
00007 #include "geometry.hpp"
00008 #include "coloring.hpp"
00009 #include "arak.hpp"
00010 #include "coloring_mcmc.hpp"
00011 
00012 namespace Arak {
00013 
00014   using namespace Geometry;
00015 
00020   class CN94Proposal : public ColoringProposal {
00021 
00022   protected:
00023 
00027     enum MoveType {
00028       INT_TRIANGLE_BIRTH = 0,     
00029       INT_TRIANGLE_DEATH,         
00030       RECOLOR,                    
00031       MOVE_INT_VERTEX,            
00032       MOVE_BD_VERTEX_ALONG_BD,    
00033       MOVE_BD_VERTEX_PAST_CORNER, 
00034       INT_VERTEX_BIRTH,           
00035       INT_VERTEX_DEATH,           
00036       BD_TRIANGLE_BIRTH,          
00037       BD_TRIANGLE_DEATH,          
00038       CORNER_CUT_BIRTH,           
00039       CORNER_CUT_DEATH,           
00040       INVALID_MOVE,               
00041       UNSPECIFIED_MOVE            
00042     };
00043 
00047     static const int numMoveTypes = 13;
00048 
00052     static const char* moveNames[numMoveTypes];
00053       
00057     unsigned long int numProposed[numMoveTypes];
00058 
00062     unsigned long int numAccepted[numMoveTypes];
00063 
00068     class InteriorTriangleBirth : public ColoringMove {
00069 
00070       friend class CN94Proposal;
00071     
00072     protected:
00073 
00077       Point u, v, w;
00078 
00084       Coloring::VertexHandle vh;
00085 
00086     public:
00087 
00088       InteriorTriangleBirth() {}
00089 
00097       void reset(const Point& u, const Point& v, const Point& w);
00098 
00099       virtual void execute(Coloring& c);
00100 
00101       virtual void undo(Coloring& c);
00102 
00103     };
00104 
00108     InteriorTriangleBirth itb;
00109 
00114     class InteriorTriangleDeath : public ColoringMove {
00115 
00116       friend class CN94Proposal;
00117     
00118     protected:
00119 
00123       const CN94Proposal& proposal;
00124 
00130       Coloring::VertexHandle vh;
00131 
00136       Point u, v, w;
00137 
00143       bool isShort;
00144 
00145     public:
00146 
00152       InteriorTriangleDeath(const CN94Proposal& proposal) 
00153   : proposal(proposal) {}
00154 
00162       void reset(Coloring::VertexHandle vertex, bool isShort);
00163 
00164       virtual void execute(Coloring& c);
00165 
00166       virtual void undo(Coloring& c);
00167 
00168     };
00169 
00173     InteriorTriangleDeath itd;
00174 
00180     class Recolor : public ColoringMove {
00181 
00182       friend class CN94Proposal;
00183       friend class ModifiedCN94Proposal;
00184     
00185     protected:
00186 
00190       Coloring::IntEdgeHandle edge1;
00191 
00195       Coloring::IntEdgeHandle edge2;
00196 
00202       bool joinSourceToSource;
00203 
00207       bool convex;
00208 
00209     public:
00210 
00211       Recolor() {}
00212 
00226       void reset(Coloring::IntEdgeHandle edge1, 
00227      Coloring::IntEdgeHandle edge2,
00228      bool joinSourceToSource,
00229      bool convex);
00230 
00236       bool isConvex() { return convex; }
00237 
00238       virtual void execute(Coloring& c);
00239 
00240       virtual void undo(Coloring& c);
00241 
00242     };
00243 
00247     Recolor rec;
00248 
00252     class MoveInteriorVertex : public ColoringMove {
00253 
00254       friend class CN94Proposal;
00255     
00256     protected:
00257 
00261       Coloring::VertexHandle vertex;
00262 
00266       Point oldLoc;
00267 
00271       Point newLoc;
00272 
00273     public:
00274 
00275       MoveInteriorVertex() {}
00276 
00283       void reset(Coloring::VertexHandle vertex, const Point& point);
00284 
00285       virtual void execute(Coloring& c);
00286 
00287       virtual void undo(Coloring& c);
00288 
00289     };
00290 
00294     MoveInteriorVertex miv;
00295 
00299     class MoveVertexAlongBdEdge : public ColoringMove {
00300     
00301       friend class CN94Proposal;
00302     
00303     protected:
00304     
00308       Coloring::VertexHandle vertex;
00309     
00313       Point oldLoc;
00314 
00318       Point newLoc;
00319 
00320     public:
00321     
00322       MoveVertexAlongBdEdge() { }
00323       
00330       void reset(Coloring::VertexHandle vertex, const Point& point);
00331 
00332       virtual void execute(Coloring& c);
00333 
00334       virtual void undo(Coloring& c);
00335 
00336     };
00337 
00342     MoveVertexAlongBdEdge mbb;
00343 
00348     class MoveBdVertexPastCorner : public ColoringMove {
00349 
00350       friend class CN94Proposal;
00351     
00352     protected:
00353 
00357       Coloring::BdEdgeHandle edge;
00358 
00363       Point oldLoc;
00364 
00370       Point newLoc;
00371 
00372     public:
00373 
00374       MoveBdVertexPastCorner() {}
00375 
00386       void reset(Coloring::BdEdgeHandle edge, const Point& point);
00387 
00388       virtual void execute(Coloring& c);
00389 
00390       virtual void undo(Coloring& c);
00391 
00392     };
00393 
00398     MoveBdVertexPastCorner mbc;
00399 
00404     class InteriorVertexBirth : public ColoringMove {
00405 
00406       friend class CN94Proposal;
00407     
00408     protected:
00409     
00413       Coloring::VertexHandle prevVertex;
00414     
00419       Coloring::VertexHandle nextVertex;
00420 
00424       Point point;
00425 
00426     public:
00427 
00434       void reset(Coloring::IntEdgeHandle edge, const Point& point);
00435 
00436       virtual void execute(Coloring& c);
00437 
00438       virtual void undo(Coloring& c);
00439 
00440     };
00441 
00445     InteriorVertexBirth ivb;
00446 
00451     class InteriorVertexDeath : public ColoringMove {
00452 
00453       friend class CN94Proposal;
00454 
00455     protected:
00456 
00460       const CN94Proposal& proposal;
00461 
00465       Coloring::VertexHandle prevVertex;
00466 
00471       Point point;
00472 
00477       double newEdgeLength;
00478 
00479     public:
00480 
00486       InteriorVertexDeath(const CN94Proposal& proposal) 
00487   : proposal(proposal) {}
00488 
00494       void reset(Coloring::VertexHandle vertex);
00495 
00496       virtual void execute(Coloring& c);
00497 
00498       virtual void undo(Coloring& c);
00499 
00500     };
00501 
00505     InteriorVertexDeath ivd;
00506 
00511     class BoundaryTriangleBirth : public ColoringMove {
00512 
00513       friend class CN94Proposal;
00514       friend class ModifiedCN94Proposal;
00515     
00516     protected:
00517 
00518 
00524       Coloring::BdEdgeHandle edge;
00525 
00530       Point u, v;
00531 
00535       Point w;
00536 
00542       Coloring::VertexHandle vh;
00543 
00544 
00545     public:
00546 
00547       BoundaryTriangleBirth() {}
00548 
00557       void reset(Coloring::BdEdgeHandle e,
00558      const Point& u, 
00559      const Point& v, 
00560      const Point& w);
00561  
00562       virtual void execute(Coloring& c);
00563 
00564       virtual void undo(Coloring& c);
00565 
00566     };
00567 
00571     BoundaryTriangleBirth btb;
00572 
00577     class BoundaryTriangleDeath : public ColoringMove {
00578 
00579       friend class CN94Proposal;
00580       friend class ModifiedCN94Proposal;
00581     
00582     protected:
00583 
00589       Coloring::VertexHandle vh;
00590 
00596       Point up, vp;
00597 
00603       Point wp;
00604 
00610       Coloring::VertexHandle prevBoundaryVertex;
00611 
00617       Coloring::VertexHandle nextBoundaryVertex;
00618 
00619     public:
00620 
00621       BoundaryTriangleDeath() {}
00622 
00628       void reset(Coloring::VertexHandle vertex);
00629 
00630       virtual void execute(Coloring& c);
00631 
00632       virtual void undo(Coloring& c);
00633 
00634     };
00635 
00639     BoundaryTriangleDeath btd;
00640 
00644     class CornerCutBirth : public ColoringMove {
00645 
00646       friend class CN94Proposal;
00647     
00648     protected:
00649 
00653       Coloring::VertexHandle corner;
00654 
00659       Point u;
00660 
00665       Point v;
00666 
00672       Coloring::VertexHandle vh;
00673 
00674     public:
00675 
00676       CornerCutBirth() {}
00677 
00689       void reset(Coloring::VertexHandle corner, 
00690      const Point& u, const Point& v);
00691  
00692       virtual void execute(Coloring& c);
00693 
00694       virtual void undo(Coloring& c);
00695 
00696     };
00697 
00701     CornerCutBirth ccb;
00702 
00707     class CornerCutDeath : public ColoringMove {
00708 
00709       friend class CN94Proposal;
00710     
00711     protected:
00712 
00716       Coloring::VertexHandle corner;
00717 
00723       Point u;
00724 
00730       Point v;
00731 
00736       Coloring::VertexHandle vh;
00737 
00738     public:
00739 
00740       CornerCutDeath() {}
00741 
00748       void reset(Coloring::VertexHandle vertex);
00749 
00750       virtual void execute(Coloring& c);
00751 
00752       virtual void undo(Coloring& c);
00753 
00754     };
00755 
00759     CornerCutDeath ccd;
00760 
00773     virtual void sampleInteriorTriangleBirth(const Coloring& state, 
00774                Arak::Util::Random& random);
00775 
00810     virtual void sampleInteriorDeath(const Coloring& state, 
00811              Arak::Util::Random& random,
00812              bool reversible = true);
00813 
00814     virtual void proposeBdTriangleDeath(const Coloring& state,
00815           Coloring::VertexHandle prevVertex,
00816           Coloring::VertexHandle vertex,
00817           Coloring::VertexHandle nextVertex,
00818           bool reversible = true);
00819 
00820     virtual void proposeIntVertexDeath(const Coloring& state,
00821                Coloring::VertexHandle prevVertex,
00822                Coloring::VertexHandle vertex,
00823                Coloring::VertexHandle nextVertex,
00824                bool reversible = true);
00825 
00826     virtual void proposeIntTriangleDeath(const Coloring& state,
00827            Coloring::VertexHandle prevVertex,
00828            Coloring::VertexHandle vertex,
00829            Coloring::VertexHandle nextVertex,
00830            bool reversible = true);
00831 
00842     virtual void sampleRecolor(const Coloring& state, 
00843              Arak::Util::Random& random);
00844 
00857     virtual void sampleMoveBdVertexPastCorner(const Coloring& state, 
00858                 Arak::Util::Random& random);
00859 
00872     virtual void sampleInteriorVertexBirth(const Coloring& state, 
00873              Arak::Util::Random& random);
00874 
00885     virtual void sampleMoveInteriorVertex(const Coloring& state, 
00886             Arak::Util::Random& random);
00887 
00899     virtual void sampleMoveVertexAlongBd(const Coloring& state, 
00900            Arak::Util::Random& random);
00901 
00915     virtual void sampleBoundaryTriangleBirth(const Coloring& state, 
00916                Arak::Util::Random& random);
00917 
00935     virtual void sampleBoundaryTriangleDeath(const Coloring& state, 
00936                Arak::Util::Random& random,
00937                bool reversible = true);
00938 
00950     virtual void sampleCornerCutBirth(const Coloring& state, 
00951               Arak::Util::Random& random);
00952 
00963     virtual void sampleCornerCutDeath(const Coloring& state, 
00964               Arak::Util::Random& random);
00965 
00972     double ZETA_BD_NOT_MR;
00973 
00979     double ZETA_MOVE_NOT_RECOLOR;
00980 
00986     double ZETA_BIRTH_NOT_DEATH;
00987 
00993     double ZETA_INT_NOT_BOUNDARY;
00994 
01000     double ZETA_BOUND_NOT_CORNER;
01001 
01007     double ZETA_IB_VERTEX_NOT_TRIANGLE;
01008 
01012     MoveType curMoveType;
01013 
01020     double castingBoxRadius;
01021 
01030     Point sampleInCastingBox(const Point& p,
01031            Arak::Util::Random& random) const;
01032 
01040     bool inCastingBox(const Point& p, const Point& q) const;
01041 
01047     double castingArea() const { 
01048       return 4.0 * castingBoxRadius * castingBoxRadius; 
01049     }
01050 
01051   public:
01052   
01056     CN94Proposal(const Arak::Util::PropertyMap& props);
01057 
01061     virtual ~CN94Proposal();
01062 
01076     virtual void sample(const Coloring& state, 
01077       Arak::Util::Random& random,
01078       bool reversible = true);
01079 
01087     virtual ColoringMove& move();
01088 
01095     virtual double ll(const Coloring& c);
01096   
01104     virtual double rll(const Coloring& c);
01105 
01110     virtual void result(bool accepted) {
01111       if (accepted) numAccepted[int(curMoveType)]++;
01112     }
01113 
01120     template<typename charT, typename traits>
01121     void writeStatistics(std::basic_ostream<charT,traits>& out) const {
01122       double numProposals = 0.0;
01123       for (int i = 0; i < numMoveTypes; i++)
01124   numProposals += double(numProposed[i]);
01125       out << "Clifford & Nicholls (1994) proposal statistics:" << std::endl;
01126       out << std::setw(28) << "move type" 
01127     << std::setw(12) << "% proposed" 
01128     << std::setw(12) << "% accepted" 
01129     << std::endl;
01130       out << std::setw(28) << "----------------------------" 
01131     << std::setw(12) << "-----------" 
01132     << std::setw(12) << "-----------" 
01133     << std::endl;
01134       out.precision(3);
01135       for (int i = 0; i < numMoveTypes; i++) {
01136   out << std::setw(28) << moveNames[i]
01137       << std::setw(12) 
01138       << 100.0 * (double(numProposed[i]) / numProposals) 
01139       << std::setw(12) 
01140       << 100.0 * (double(numAccepted[i]) / double(numProposed[i])) 
01141       << std::endl;
01142       }
01143     }
01144 
01145   }; // End of class: Arak::CN94Proposal
01146 
01152   class ModifiedCN94Proposal : public CN94Proposal {
01153 
01154   protected:
01155 
01160     double ZETA_LOCAL_NOT_GLOBAL_RECOLOR;
01161 
01179     virtual void sampleRecolor(const Coloring& state, 
01180              Arak::Util::Random& random);
01181 
01186     double ZETA_LOCAL_NOT_GLOBAL_BD_TRI_BIRTH;
01187 
01207     virtual void sampleBoundaryTriangleBirth(const Coloring& state, 
01208                Arak::Util::Random& random);
01209 
01214     class SlideInteriorVertex : public CN94Proposal::MoveInteriorVertex {
01215 
01216       friend class ModifiedCN94Proposal;
01217     
01218     protected:
01219 
01224       bool next;
01225 
01226     public:
01227 
01228       SlideInteriorVertex() {}
01229 
01239       void reset(Coloring::VertexHandle vertex, bool next, 
01240      const Point& point) {
01241   CN94Proposal::MoveInteriorVertex::reset(vertex, point);
01242   this->next = next;
01243       }
01244 
01245     };
01246 
01250     SlideInteriorVertex siv;
01251 
01256     double ZETA_SLIDE_NOT_MOVE_INT_VERTEX;
01257 
01267     bool slide_not_move;
01268 
01283     virtual void sampleSlideInteriorVertex(const Coloring& state, 
01284              Arak::Util::Random& random);
01285 
01296     virtual void sampleMoveInteriorVertex(const Coloring& state, 
01297             Arak::Util::Random& random);
01298 
01299   public:
01300 
01304     ModifiedCN94Proposal(const Arak::Util::PropertyMap& props);
01305 
01309     virtual ~ModifiedCN94Proposal() { }
01310 
01318     virtual ColoringMove& move() {
01319       if ((curMoveType == MOVE_INT_VERTEX) && slide_not_move)
01320   return siv;
01321       else return CN94Proposal::move();
01322     }
01323 
01330     virtual double ll(const Coloring& c);
01331   
01339     virtual double rll(const Coloring& c);
01340   };
01341 
01342 } // End of namespace: Arak
01343 
01344 #endif

Generated on Wed May 25 14:39:15 2005 for Arak by doxygen 1.3.6