Skip to content
Snippets Groups Projects
Commit b8966d6a authored by Sae-Dan Weerapan's avatar Sae-Dan Weerapan
Browse files

Upload New File

parent 13c5f531
No related branches found
No related tags found
No related merge requests found
//
// Created by weerapan on 26/05/2020.
//
#ifndef MH_BUILDER_RANDOMEDITERATIVEIMPROVEMENT_H
#define MH_BUILDER_RANDOMEDITERATIVEIMPROVEMENT_H
namespace opt {
/**
* Class representing an BII
* @tparam IN
*/
template <typename IN>
class RandomedIterativeImprovement : public LocalSearch<IN> {
public:
using LocalSearch<IN>::operator();
RandomedIterativeImprovement(
LocalSearch<IN> &_localSearch,
core::Algorithm<IN> &_perturbation,
core::Criterion<IN> &_stopCriterion,
core::Eval<IN> &_eval,
double walkprob = 0.7)
:
LocalSearch<IN>(_eval),
localSearch(&_localSearch),
perturbation(&_perturbation),
stopCriterion(&_stopCriterion),
constT(walkprob)
{ }
void init(const IN &_in) {
LocalSearch<IN>::init(_in);
stopCriterion->init(_in);
}
virtual void operator()(IN &_in, core::Criterion<IN> &_criterion) {
this->init(_in);
core::CriterionAnd<IN> criterionComb(*stopCriterion, _criterion);
replaceSolutionIfImpr(this->currentSol);
while (criterionComb(_in)) {
IN newsol(this->currentSol);
(*perturbation)(newsol);
//consTT = core::RNGHelper::get()->uniform();
//std::cout<<"con:"<< consTT <<std::endl;
if(core::RNGHelper::get()->uniform() < constT) {
replaceSolutionIfImpr(newsol);
//std::cout <<"newsol1:"<< newsol << std::endl;
} else {
(*localSearch)(newsol, criterionComb);
replaceSolutionIfImpr(newsol);
//std::cout <<"newsol2:"<< newsol << std::endl;
}
}
_in = this->bestSol;
}
void setPerturbation(core::Algorithm<IN> &_perturbation) {
perturbation = &_perturbation;
}
void setAlgorithm(core::Algorithm<IN> &_algorithm) {
localSearch = &_algorithm;
}
void setCriterion(core::Algorithm<IN> &_criterion) {
stopCriterion = &_criterion;
}
void replaceSolutionIfImpr(IN &_newsol) {
if ( _newsol > this->bestSol ) {
this->bestSol = _newsol;
} else if (accept(_newsol, this->currentSol))
this->bestSol = _newsol;
}
bool accept(IN &newsol, IN &current) {
double proba = exp((newsol.fitness().scalar()) - current.fitness().scalar());
//std::cout<<"proba:" << proba << std::endl;
return core::RNGHelper::get()->uniform() < proba;
}
protected:
core::Algorithm<IN> *perturbation;
core::Algorithm<IN> *localSearch;
core::Criterion<IN> *stopCriterion;
double constT;
//double consTT;
};
}
#endif //MH_BUILDER_RANDOMEDITERATIVEIMPROVEMENT_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment