bool ArtificialBeeColony::onlookerBeesPhase(StateP state, DemeP deme) { // jednostavni odabir, uz selFitPropOp /* for( uint i = 0; i < deme->getSize(); i++ ) { // for each food source // choose a food source depending on its fitness value (better individuals are more likely to be chosen) IndividualP food = selFitOp->select(*deme); createNewFoodSource(food, state, deme); } */ // uz vjerojatnosti, jedinka po jedinka calculateProbabilities(state, deme); int demeSize = deme->getSize(); int i = state->getRandomizer()->getRandomInteger(demeSize); int n = 0; while( n < demeSize) { int fact = i++ % demeSize; IndividualP food = deme->at(fact); if (state->getRandomizer()->getRandomDouble() < probability_[fact]){ n++; createNewFoodSource(food, state, deme); } } return true; }
bool createNewFoodSource(IndividualP food, StateP state, DemeP deme) { //for each food source find a neighbour IndividualP neighbour; do{ neighbour = selRandomOp->select(*deme); }while(food->index == neighbour->index); //potential new food source IndividualP newFood = copy(food); FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(0)); std::vector< double > &foodVars = flp->realValue; flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (neighbour->getGenotype(0)); std::vector< double > &neighbourVars = flp->realValue; flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newFood->getGenotype(0)); std::vector< double > &newFoodVars = flp->realValue; uint param = state->getRandomizer()->getRandomInteger((int)foodVars.size()); double factor = state->getRandomizer()->getRandomDouble(); double value = foodVars[param] * (1-2*factor)*(foodVars[param]-neighbourVars[param]); if (value > ubound) value = ubound; else if (value <lbound) value = lbound; //produce a modification on the food source (discover a new food source) newFoodVars[param] = value; evaluate(newFood); flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(1)); double &foodTrial = flp->realValue[0]; // d) if the fitness value of the new food source is better than that of the original source, // memorize the new source, forget the old one and set trial to 0 // otherwise keep the old one and increment trial if(newFood->fitness->isBetterThan( food->fitness) ) { foodVars[param] = value; evaluate(food); foodTrial = 0; } else { foodTrial +=1; } return true; }
//! cross donor vectors with population members to create trial vectors void DifferentialEvolution::crossover(DemeP deme, uint index, StateP state) { // get population member and corresponding donor vector FloatingPoint::FloatingPoint* flp1 = (FloatingPoint::FloatingPoint*) (deme->at(index)->getGenotype().get()); int dim = (int) flp1->realValue.size(); FloatingPoint::FloatingPoint* flp2 = (FloatingPoint::FloatingPoint*) donor_vector[index]->getGenotype().get(); // crossover their elements (keep the result in donor_vector) for(uint i = 0; i < flp1->realValue.size(); i++) { if (state->getRandomizer()->getRandomDouble() <= CR_ || i == state->getRandomizer()->getRandomInteger(dim)) { } else { flp2->realValue[i] = flp1->realValue[i]; } } }
bool hypermutationPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones) { uint M; // M - number of mutations of a single antibody uint k; //sort std::sort (clones.begin(), clones.end(), sortPopulationByFitness); for( uint i = 0; i < clones.size(); i++ ){ // for each antibody in vector clones IndividualP antibody = clones.at(i); FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(0)); std::vector< double > &antibodyVars = flp->realValue; k = 1 + i/(dup+1); M =(int) ((1- 1/(double)(k)) * (c*dimension) + (c*dimension)); // mutate M times for (uint j = 0; j < M; j++){ uint param = state->getRandomizer()->getRandomInteger((int)antibodyVars.size()); double randDouble1 = state->getRandomizer()->getRandomDouble(); double randDouble2 = state->getRandomizer()->getRandomDouble(); double value = antibodyVars[param] + (1-2*randDouble1)* 0.2 * (ubound - lbound) * pow(2, -16*randDouble2 ); if (value > ubound) value = ubound; else if (value <lbound) value = lbound; //produce a mutation on the antibody antibodyVars[param] = value; } FitnessP parentFitness = antibody->fitness; evaluate(antibody); // if the clone is better than its parent, reset clone's age if(antibody-> fitness->isBetterThan(parentFitness)){ flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1)); double &age = flp->realValue[0]; age = 0; } } return true; }