3#include "AlgGenHookeJeeves.h"
4#include "SelFitnessProportionalOp.h"
5#include "SelRandomOp.h"
6#include "floatingpoint/FloatingPoint.h"
9GenHookeJeeves::GenHookeJeeves()
12 name_ =
"GenHookeJeeves";
15 areGenotypesAdded_ =
false;
19 selBestOp_ =
static_cast<SelectionOperatorP
> (
new SelBestOp);
20 selRandomOp_ =
static_cast<SelectionOperatorP
> (
new SelRandomOp);
39 GenotypeP activeGenotype = state->getGenotypes()[0];
40 RealValueGenotypeP rv = boost::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
42 ECF_LOG_ERROR(state,
"Error: This algorithm accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
47 selFitPropOp_->initialize(state);
48 selBestOp_->initialize(state);
49 selRandomOp_->initialize(state);
53 precision_ = *((
double*) sptr.get());
55 initialMove_ = *((
double*) sptr.get());
57 localOnly_ = *((uint*) sptr.get()) ?
true :
false;
60 sptr = state->getRegistry()->getEntry(
"population.size");
61 uint size = *((uint*) sptr.get());
62 for(uint i = 0; i < size; i++) {
63 delta_.push_back(initialMove_);
64 changed_.push_back(
true);
65 converged_.push_back(
false);
71 if(areGenotypesAdded_)
77 sptr = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
78 uint numDimension = *((uint*) sptr.get());
79 sptr = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
80 lbound_ = *((
double*) sptr.get());
81 sptr = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
82 ubound_ = *((
double*) sptr.get());
86 state->setGenotype(fp);
91 state->setGenotype(fp2);
92 fp2->setParameterValue(state,
"dimension", (voidP)
new uint(1));
93 fp2->setParameterValue(state,
"lbound", (voidP)
new double(0));
94 fp2->setParameterValue(state,
"ubound", (voidP)
new double(1));
98 fp->setParameterValue(state,
"dimension", (voidP)
new uint(numDimension));
99 fp->setParameterValue(state,
"lbound", (voidP)
new double(lbound_));
100 fp->setParameterValue(state,
"ubound", (voidP)
new double(ubound_));
103 areGenotypesAdded_ =
true;
112 FitnessP neighbor[2];
113 neighbor[0] = (FitnessP) (*deme)[0]->fitness->copy();
114 neighbor[1] = (FitnessP) (*deme)[0]->fitness->copy();
120 for(uint i = 0; i < deme->size(); i++) {
122 if(localOnly_ && converged_[i])
125 IndividualP ind = deme->at(i);
127 FloatingPointP x = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (ind->getGenotype(0));
129 FloatingPointP xn = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (ind->getGenotype(1));
134 xn = (FloatingPointP) x->copy();
136 finalFit = (FitnessP) ind->fitness->copy();
142 finalFit = temp->fitness;
146 for(uint dim = 0; dim < x->realValue.size(); dim++) {
147 xn->realValue[dim] += delta_[i];
150 if(xn->realValue[dim] <= ubound_) {
153 neighbor[0] = temp->fitness;
157 if(neighbor[0]->isBetterThan(finalFit)) {
158 finalFit = neighbor[0];
165 xn->realValue[dim] -= 2 * delta_[i];
168 if(xn->realValue[dim] >= lbound_) {
171 neighbor[1] = temp->fitness;
174 if(neighbor[1]->isBetterThan(finalFit)) {
175 finalFit = neighbor[1];
181 xn->realValue[dim] += delta_[i];
187 if(finalFit->isBetterThan(neighbor[0]) && finalFit->isBetterThan(neighbor[1]))
189 else if(neighbor[0]->isBetterThan(neighbor[1])) {
190 xn->realValue[dim] += delta_[i];
191 finalFit = neighbor[0];
194 xn->realValue[dim] -= delta_[i];
195 finalFit = neighbor[1];
202 if(finalFit->isBetterThan(ind->fitness)) {
203 FloatingPointP xnc (xn->copy());
205 for(uint dim = 0; dim < x->realValue.size(); dim++)
206 xn->realValue[dim] = 2 * xn->realValue[dim] - x->realValue[dim];
209 for(uint dim = 0; dim < xn->realValue.size(); dim++) {
210 if(xn->realValue[dim] < lbound_)
211 xn->realValue[dim] = lbound_;
212 if(xn->realValue[dim] > ubound_)
213 xn->realValue[dim] = ubound_;
217 ind->fitness = finalFit;
222 xn = (FloatingPointP) x->copy();
233 if(converged_[i] ==
false && changed_[i] ==
true && delta_[i] < precision_) {
234 converged_[i] =
true;
238 FloatingPointP fp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (ind->getGenotype(2));
239 fp->realValue[0] = (double) state->getGenerationNo();
243 if(convergedTotal_ == converged_.size()) {
244 state->setTerminateCond();
245 std::cout <<
"svi konvergirali!" << std::endl;
253 if(changed_[i] ==
true && delta_[i] < precision_ && (selBestOp_->select(*deme) != ind)) {
254 IndividualP first, second;
255 first = second = selFitPropOp_->select(*deme);
256 while(second == first)
257 second = selFitPropOp_->select(*deme);
265 mate(first, second, ind);
268 delta_[i] = initialMove_;
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
std::string name_
algorithm name
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Helper function: register a single parameter with the system.
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
FloatingPoint class - implements genotype as a vector of floating point values.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Individual class - inherits a vector of Genotype objects.
Best individual selection operator.
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.