25 selBestOp->initialize(state);
28 pa = *((
double*)pDiscovery.get());
31 ECF_LOG_ERROR(state,
"Error - pa must be in interval [0,1]");
36 voidP lBound = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
37 lbound = *((
double*)lBound.get());
38 voidP uBound = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
39 ubound = *((
double*)uBound.get());
40 voidP sptr = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
41 numDimension = *((uint*)sptr.get());
45 GenotypeP activeGenotype = state->getGenotypes()[0];
46 RealValueGenotypeP rv = std::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
48 ECF_LOG_ERROR(state,
"Error: Cuckoo Search algorithm accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
58 double sigma = 0.696574502;
59 std::random_device rd;
60 std::mt19937 gen(rd());
61 std::normal_distribution<double> nd(0.0, 1.0);
63 IndividualP best = selBestOp->select(*deme);
64 FloatingPointP bestFp = std::static_pointer_cast<FloatingPoint::FloatingPoint> (best->getGenotype(0));
68 for (uint i = 0; i < deme->size(); i++) {
69 IndividualP trial = (IndividualP)deme->at(i)->copy();
70 FloatingPointP trialFp = std::static_pointer_cast<FloatingPoint::FloatingPoint> (trial->getGenotype(0));
71 for (uint j = 0; j < numDimension; j++) {
72 double u = nd(gen)* sigma;
74 double step = u / pow(fabs(v), 2 / (
double)3);
75 double randn = nd(gen);
76 double diff = trialFp->realValue[j] - bestFp->realValue[j];
77 double stepsize = 0.01 * step * diff;
78 trialFp->realValue[j] = trialFp->realValue[j] + stepsize*randn;
79 if (trialFp->realValue[j] > ubound)
80 trialFp->realValue[j] = ubound;
81 if (trialFp->realValue[j] < lbound)
82 trialFp->realValue[j] = lbound;
85 if (trial->fitness->isBetterThan(deme->at(i)->fitness))
90 std::vector<IndividualP> nest1;
91 std::vector<IndividualP> nest2;
92 for (uint i = 0; i < deme->size(); i++) {
93 IndividualP indCp = (IndividualP)deme->at(i)->copy();
94 nest1.push_back(indCp);
95 nest2.push_back(indCp);
100 std::shuffle(nest1.begin(), nest1.end(), gen);
101 std::shuffle(nest2.begin(), nest2.end(), gen);
102 double randNum = (double)rand() / RAND_MAX;
103 for (uint i = 0; i < deme->size(); i++) {
104 IndividualP trial = (IndividualP)deme->at(i)->copy();
105 FloatingPointP trialFp1 = std::static_pointer_cast<FloatingPoint::FloatingPoint> (nest1.at(i)->getGenotype(0));
106 FloatingPointP trialFp2 = std::static_pointer_cast<FloatingPoint::FloatingPoint> (nest2.at(i)->getGenotype(0));
107 FloatingPointP trialFp = std::static_pointer_cast<FloatingPoint::FloatingPoint> (trial->getGenotype(0));
109 for (uint j = 0; j < numDimension; j++) {
110 if ((
double)rand() / RAND_MAX <
pa) {
111 double stepsize = (trialFp1->realValue[j] - trialFp2->realValue[j])*randNum;
112 trialFp->realValue[j] += stepsize;
113 if (trialFp->realValue[j] > ubound)
114 trialFp->realValue[j] = ubound;
115 if (trialFp->realValue[j] < lbound)
116 trialFp->realValue[j] = lbound;
121 if (trial->fitness->isBetterThan(deme->at(i)->fitness))
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.