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 bool foundNext = true;
00025
00026 while (foundNext && !wac->abort(scWalk))
00027 {
00028
00029
00030 lastAccepted = scWalk->getLastAdded()->clone(lastAccepted);
00031
00032 State::NeighborListPtr neighbors = ng->getNeighborList(lastAccepted);
00033 State::NeighborList::Iterator it = neighbors->begin();
00034
00035 foundNext = false;
00036
00037 while (neighbors->end() != it)
00038 {
00039 if (sa->accept(scWalk, *it))
00040 {
00041 scWalk->add(*it);
00042 foundNext = true;
00043 break;
00044 }
00045 else if (scRej!=NULL) {
00046 scRej->add(*it);
00047 }
00048 ++it;
00049 }
00050
00051 }
00052
00053
00054 if (lastAccepted != NULL) {
00055 delete lastAccepted;
00056 }
00057
00058 return scWalk;
00059 }
00060
00061
00062
00066
00067
00068 WalkAdaptive::WalkAdaptive(const NeighborGenType ngt)
00069 : t(ngt)
00070 { }
00071
00072
00073 StateCollector*
00074 WalkAdaptive::walkAdaptive(
00075 const State* const s,
00076 StateCollector* const scWalk,
00077 const NeighborGenType t,
00078 StateCollector* const scRej
00079 )
00080 {
00081
00082 NeighborGenerator* ng;
00083 switch (t) {
00084 case RANDOM:
00085 ng = new NG_SuccessiveRandom();
00086 break;
00087 case SUCCESSIVE:
00088 ng = new NG_Successive();
00089 break;
00090 default:
00091 assertbiu(false, "NeighborGenType not supported");
00092 break;
00093 }
00094
00095
00096 SA_Le sa;
00097
00098
00099
00100 WAC_OpenEnd wac;
00101
00102
00103 Walk::walk(s, scWalk, ng, &sa, &wac, scRej);
00104
00105 delete ng;
00106
00107 return scWalk;
00108 }
00109
00110
00111 StateCollector*
00112 WalkAdaptive::walk (const State* const s,
00113 StateCollector* const sc,
00114 StateCollector* const scRej)
00115 const
00116 {
00117
00118 return WalkAdaptive::walkAdaptive(s, sc, this->t, scRej);
00119 }
00120
00121
00122
00126
00127
00128 WalkGradient::WalkGradient()
00129 { }
00130
00131
00132 StateCollector*
00133 WalkGradient::walkGradient(
00134 const State* const start,
00135 StateCollector* const scWalk
00136 )
00137 {
00138
00139 scWalk->add(*start);
00140
00141 const State * lastAdded = NULL;
00142 State * bestNeigh = NULL;
00143
00144 do {
00145
00146
00147
00148 lastAdded = scWalk->getLastAdded();
00149 assertbiu(lastAdded != NULL, "no last element in StateCollector");
00150
00151 double minEfound = lastAdded->getEnergy();
00152
00153 State::NeighborListPtr neighbors = lastAdded->getNeighborList();
00154 State::NeighborList::Iterator it = neighbors->begin();
00155
00156
00157 bestNeigh = lastAdded->clone(bestNeigh);
00158 while (neighbors->end() != it) {
00159 if ( it->operator<(*bestNeigh) )
00160 {
00161 bestNeigh = it->clone(bestNeigh);
00162 minEfound = bestNeigh->getEnergy();
00163 }
00164 ++it;
00165 }
00166
00167 if (State::less(bestNeigh,lastAdded)) {
00168
00169 scWalk->add(*bestNeigh);
00170 } else {
00171 delete bestNeigh;
00172 bestNeigh = NULL;
00173 }
00174 } while (bestNeigh != NULL);
00175
00176 return scWalk;
00177 }
00178
00179
00180 StateCollector*
00181 WalkGradient::walk(
00182 const State* const start,
00183 StateCollector* const scWalk,
00184 StateCollector* const scRej)
00185 const
00186 {
00187
00188 return WalkGradient::walkGradient(start, scWalk);
00189 }
00190
00191
00195
00196 WalkMC::WalkMC(
00197 const WalkAbortionCriterion* const wac_,
00198 const double kT_)
00199 : wac(wac_) , kT(kT_) {
00200 }
00201
00202
00203 StateCollector*
00204 WalkMC::walkMC(
00205 const State* const start,
00206 StateCollector* const sc,
00207 const WalkAbortionCriterion* const wac,
00208 const double kT,
00209 StateCollector* const scRej)
00210 {
00211 NG_Random ng;
00212
00213
00214 SA_Metropolis sa(kT);
00215
00216 Walk::walk(start, sc, &ng, &sa, wac, scRej);
00217
00218 return sc;
00219 }
00220
00221
00222 StateCollector*
00223 WalkMC::walk(
00224 const State* const start,
00225 StateCollector* const sc,
00226 StateCollector* const scRej)
00227 const
00228 {
00229
00230 return WalkMC::walkMC(start, sc, this->wac, this->kT, scRej);
00231 }
00232
00236
00237 WalkRandom::WalkRandom(
00238 const WalkAbortionCriterion* const wac_
00239 )
00240 : wac(wac_)
00241 {
00242 }
00243
00244
00245 StateCollector*
00246 WalkRandom::walkRandom(
00247 const State* const start,
00248 StateCollector* const sc,
00249 const WalkAbortionCriterion* const wac,
00250 StateCollector* const scRej)
00251 {
00252 NG_SuccessiveRandom ng;
00253
00254 SA_All sa;
00255
00256 Walk::walk(start, sc, &ng, &sa, wac, scRej);
00257
00258 return sc;
00259 }
00260
00261
00262 StateCollector*
00263 WalkRandom::walk(
00264 const State* const start,
00265 StateCollector* const sc,
00266 StateCollector* const scRej)
00267 const
00268 {
00269
00270 return WalkRandom::walkRandom(start, sc, this->wac, scRej);
00271 }
00272
00273
00274 }