2#include "ECF_derived.h"
4#include "AlgArtificialBeeColony.h"
7ArtificialBeeColony::ArtificialBeeColony()
10 name_ =
"ArtificialBeeColony";
19 isTrialAdded_ =
false;
29 "Maximum number of cycles for each individual (default: 300)");
32 "Elitism: the current best food source is preserved (default: 1)");
39 selFitOp->initialize(state);
40 selFitOp->setSelPressure(2);
41 selBestOp->initialize(state);
42 selWorstOp->initialize(state);
43 selRandomOp->initialize(state);
45 voidP sptr = state->getRegistry()->getEntry(
"population.size");
46 uint size = *((uint*) sptr.get());
47 probability_.resize(size);
51 GenotypeP activeGenotype = state->getGenotypes()[0];
52 RealValueGenotypeP rv = boost::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
54 ECF_LOG_ERROR(state,
"Error: ABC algorithm accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
59 limit_ = *((uint*) limitp.get());
61 if( (
int)limit_ < 0 ) {
62 ECF_LOG(state, 1,
"Error: ABC algorithm requires parameter 'limit' to be a nonnegative value!");
67 elitism_ = *((
bool*) elitismp.get());
69 voidP lBound = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
70 lbound_ = *((
double*) lBound.get());
71 voidP uBound = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
72 ubound_ = *((
double*) uBound.get());
78 FloatingPointP flpoint[2];
79 for(uint iGen = 1; iGen < 2; iGen++) {
82 state->setGenotype(flpoint[iGen]);
87 flpoint[iGen]->setParameterValue(state,
"lbound", (voidP)
new double(0));
88 flpoint[iGen]->setParameterValue(state,
"ubound", (voidP)
new double(0.01));
90 ECF_LOG(state, 1,
"ABC algorithm: added 1 FloatingPoint genotype (trial)");
133 employedBeesPhase(state, deme);
134 onlookerBeesPhase(state, deme);
135 scoutBeesPhase(state, deme);
141bool ArtificialBeeColony::employedBeesPhase(StateP state, DemeP deme)
143 for( uint i = 0; i < deme->getSize(); i++ ) {
144 IndividualP food = deme->at(i);
145 createNewFoodSource(food, state, deme);
151bool ArtificialBeeColony::onlookerBeesPhase(StateP state, DemeP deme)
155 calculateProbabilities(state, deme);
156 int demeSize = (int) deme->getSize();
157 int i = state->getRandomizer()->getRandomInteger(demeSize);
159 while( n < demeSize) {
160 int fact = i++ % demeSize;
161 IndividualP food = deme->at(fact);
163 if (state->getRandomizer()->getRandomDouble() < probability_[fact]){
165 createNewFoodSource(food, state, deme);
173bool ArtificialBeeColony::calculateProbabilities(StateP state, DemeP deme)
175 IndividualP bestFood = selBestOp->select(*deme);
176 double bestFitness = bestFood->fitness->getValue();
177 double worstFitness = selWorstOp->select(*deme)->fitness->getValue();
181 if(bestFitness < worstFitness && bestFitness < 0)
182 offset = 0.1 - bestFitness;
183 else if(worstFitness < 0)
184 offset = 0.1 - worstFitness;
187 for( uint i = 0; i < deme->getSize(); i++ ) {
188 IndividualP food = deme->at(i);
189 double thisFitness = food->fitness->getValue();
191 if (bestFitness == thisFitness)
192 probability_[i] = 1.0;
193 else if (thisFitness < bestFitness)
194 probability_[i] = 0.1 + 0.9 * (thisFitness + offset) / (bestFitness + offset);
196 probability_[i] = 0.1 + 0.9 * (bestFitness + offset) / (thisFitness + offset);
206bool ArtificialBeeColony::scoutBeesPhase(StateP state, DemeP deme)
208 IndividualP unimproved;
209 IndividualP bestFood = selBestOp->select(*deme);
212 for( uint i = 0; i < deme->getSize(); i++ ) {
213 IndividualP food = deme->at(i);
219 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(1));
220 double &trial = flp->realValue[0];
223 if (trial > limit_ && trial > maxTrial){
230 if (unimproved != NULL){
231 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (unimproved->getGenotype(1));
232 double &trial = flp->realValue[0];
234 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (unimproved->getGenotype(0));
235 flp->initialize(state);
243bool ArtificialBeeColony::createNewFoodSource(IndividualP food, StateP state, DemeP deme)
246 IndividualP neighbour;
248 neighbour = selRandomOp->select(*deme);
249 }
while(food->index == neighbour->index);
252 IndividualP newFood =
copy(food);
254 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(0));
255 std::vector< double > &foodVars = flp->realValue;
256 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (neighbour->getGenotype(0));
257 std::vector< double > &neighbourVars = flp->realValue;
258 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (newFood->getGenotype(0));
259 std::vector< double > &newFoodVars = flp->realValue;
262 uint param = state->getRandomizer()->getRandomInteger((
int)foodVars.size());
263 double factor = state->getRandomizer()->getRandomDouble();
264 double value = foodVars[param] * (1 - 2 * factor) * (foodVars[param] - neighbourVars[param]);
267 else if (value < lbound_)
271 newFoodVars[param] = value;
274 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (food->getGenotype(1));
275 double &foodTrial = flp->realValue[0];
280 if(newFood->fitness->isBetterThan(food->fitness) )
282 foodVars[param] = value;
IndividualP copy(IndividualP source)
Helper function: make a copy of an individual.
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.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
FloatingPoint class - implements genotype as a vector of floating point values.
bool setParameterValue(StateP state, std::string name, voidP value)
Write single parameter value to Registry.
Best individual selection operator.
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.
Worst individual selection operator.