00001 #include "ell/Walk.hh"
00002 #include <biu/assertbiu.hh>
00003
00004
00005 namespace ell
00006 {
00010
00011
00012 StateCollector*
00013 Walk::walk(
00014 const State* const start,
00015 StateCollector* const scWalk,
00016 const NeighborGenerator* const ng,
00017 const StateAcceptor* const sa,
00018 const WalkAbortionCriterion* const wac,
00019 StateCollector* const scRej
00020 )
00021 {
00022 scWalk->add(*start);
00023 State* lastAccepted = NULL;
00024
00025 while (!wac->abort(scWalk))
00026 {
00027
00028
00029 lastAccepted = scWalk->getLastAdded()->clone();
00030
00031 State::NeighborListPtr neighbors = ng->getNeighborList(lastAccepted);
00032 State::NeighborList::Iterator it = neighbors->begin();
00033
00034 bool go = false;
00035
00036 while (neighbors->end() != it && !go)
00037 {
00038 if (sa->accept(scWalk, *it))
00039 {
00040 scWalk->add(*it);
00041 go = true;
00042 }
00043 else if (scRej!=NULL) {
00044 scRej->add(*it);
00045 }
00046 ++it;
00047 }
00048
00049
00050 if (lastAccepted != NULL) {
00051 delete lastAccepted;
00052 lastAccepted = NULL;
00053 }
00054
00055 if (!go) {
00056
00057 break;
00058 }
00059 }
00060
00061 return scWalk;
00062 }
00063
00064
00065
00069
00070
00071 WalkAdaptive::WalkAdaptive(const NeighborGenType ngt, const bool isDeg_)
00072 : t(ngt), isDeg(isDeg_) { }
00073
00074
00075 StateCollector*
00076 WalkAdaptive::walkAdaptive(
00077 const State* const s,
00078 StateCollector* const scWalk,
00079 const NeighborGenType t,
00080 StateCollector* const scRej,
00081 const bool isDegenerate)
00082 {
00083
00084 NeighborGenerator* ng;
00085 switch (t) {
00086 case RANDOM:
00087 ng = new NG_SuccessiveRandom();
00088 break;
00089 case SUCCESSIVE:
00090 ng = new NG_Successive();
00091 break;
00092 default:
00093 assertbiu(false, "NeighborGenType not supported");
00094 break;
00095 }
00096
00097 StateAcceptor * sa = NULL;
00098
00099
00100 if (isDegenerate) {
00101
00102
00103 sa = new SA_EC_Le();
00104 } else {
00105
00106 sa = new SA_E_Le();
00107 }
00108
00109
00110
00111 WalkAbortionCriterion* wac = new WAC_OpenEnd();
00112
00113
00114 Walk::walk(s, scWalk, ng, sa, wac, scRej);
00115
00116 delete ng;
00117 delete sa;
00118 delete wac;
00119
00120 return scWalk;
00121 }
00122
00123
00124 StateCollector*
00125 WalkAdaptive::walk (const State* const s,
00126 StateCollector* const sc,
00127 StateCollector* const scRej)
00128 const
00129 {
00130
00131 return WalkAdaptive::walkAdaptive(s, sc, this->t, scRej, this->isDeg);
00132 };
00133
00134
00135
00139
00140
00141 WalkGradient::WalkGradient(const bool isDeg_)
00142 : isDeg(isDeg_) { }
00143
00144
00145 StateCollector*
00146 WalkGradient::walkGradient(
00147 const State* const start,
00148 StateCollector* const scWalk,
00149 const bool isDegenerate)
00150 {
00151
00152 scWalk->add(*start);
00153
00154 State * lastAdded = NULL;
00155 State * bestNeigh = NULL;
00156
00157 do {
00158
00159
00160
00161 lastAdded = scWalk->getLastAdded()->clone();
00162
00163
00164 double minEfound = lastAdded->getEnergy(), lastE = 0.0;
00165
00166 State::NeighborListPtr neighbors = lastAdded->getNeighborList();
00167 State::NeighborList::Iterator it = neighbors->begin();
00168
00169
00170 if (bestNeigh != NULL) {
00171 delete bestNeigh;
00172 bestNeigh = NULL;
00173 }
00174 while (neighbors->end() != it) {
00175 if ( (lastE = it->getEnergy()) < minEfound
00176 || (isDegenerate
00177 && lastE == minEfound
00178 && it->compress() < lastAdded->compress()) )
00179 {
00180 if (bestNeigh != NULL)
00181 delete bestNeigh;
00182 bestNeigh = it->clone();
00183 minEfound = lastE;
00184 }
00185 ++it;
00186 }
00187
00188 if (bestNeigh != NULL) {
00189
00190 scWalk->add(*bestNeigh);
00191 }
00192
00193
00194 delete lastAdded;
00195
00196 } while (bestNeigh != NULL);
00197
00198 return scWalk;
00199 }
00200
00201
00202 StateCollector*
00203 WalkGradient::walk(
00204 const State* const start,
00205 StateCollector* const scWalk,
00206 StateCollector* const scRej)
00207 const
00208 {
00209
00210 return WalkGradient::walkGradient(start, scWalk, this->isDeg);
00211 }
00212
00213
00217
00218 WalkMC::WalkMC(
00219 const WalkAbortionCriterion* const wac,
00220 const double beta)
00221 : wac(wac) , beta(beta) {
00222 }
00223
00224
00225 StateCollector*
00226 WalkMC::walkMC(
00227 const State* const start,
00228 StateCollector* const sc,
00229 const WalkAbortionCriterion* const wac,
00230 const double beta,
00231 StateCollector* const scRej)
00232 {
00233 NeighborGenerator* ng = new NG_Random();
00234
00235
00236 StateAcceptor* sa = new SA_Metropolis(beta);
00237
00238 Walk::walk(start, sc, ng, sa, wac, scRej);
00239
00240 delete ng;
00241 delete sa;
00242
00243 return sc;
00244 }
00245
00246
00247 StateCollector*
00248 WalkMC::walk(
00249 const State* const start,
00250 StateCollector* const sc,
00251 StateCollector* const scRej)
00252 const
00253 {
00254
00255 return WalkMC::walkMC(start, sc, this->wac, this->beta, scRej);
00256 }
00257
00261
00262 WalkRandom::WalkRandom(
00263 const WalkAbortionCriterion* const wac_
00264 )
00265 : wac(wac_)
00266 {
00267 }
00268
00269
00270 StateCollector*
00271 WalkRandom::walkRandom(
00272 const State* const start,
00273 StateCollector* const sc,
00274 const WalkAbortionCriterion* const wac,
00275 StateCollector* const scRej)
00276 {
00277 NeighborGenerator* ng = new NG_SuccessiveRandom();
00278
00279 StateAcceptor* sa = new SA_All();
00280
00281 Walk::walk(start, sc, ng, sa, wac, scRej);
00282
00283 delete sa;
00284 delete ng;
00285
00286 return sc;
00287 }
00288
00289
00290 StateCollector*
00291 WalkRandom::walk(
00292 const State* const start,
00293 StateCollector* const sc,
00294 StateCollector* const scRej)
00295 const
00296 {
00297
00298 return WalkRandom::walkRandom(start, sc, this->wac, scRej);
00299 }
00300
00301
00302 }