12 areGenotypesAdded_ =
false;
19 "number of clones for each individual in clone population (dafault: 5)");
21 "mutation rate (default: 0.2)");
23 "maximum number of generations to keep an individual without improvement (default: 100)");
25 "use elitism (default: 0)");
31 voidP lBound = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
32 lbound = *((
double*) lBound.get());
34 voidP uBound = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
35 ubound = *((
double*) uBound.get());
37 voidP dimension_ = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
38 dimension = *((uint*) dimension_.get());
41 dup = *((uint*) dup_.get());
42 if( *((
int*) dup_.get()) <= 0 ) {
43 ECF_LOG(state, 1,
"Error: opt-IA requires parameter 'dup' to be an integer greater than 0");
47 c = *((
double*) c_.get());
49 ECF_LOG(state, 1,
"Error: opt-IA requires parameter 'c' to be a double greater than 0");
53 tauB = *((
double*) tauB_.get());
55 ECF_LOG(state, 1,
"Error: opt-IA requires parameter 'tauB' to be a nonnegative double value");
59 elitism = (*((uint*) elitism_.get())) != 0;
63 GenotypeP activeGenotype = state->getGenotypes()[0];
64 RealValueGenotypeP rv = boost::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
66 ECF_LOG_ERROR(state,
"Error: opt-IA accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
71 if(areGenotypesAdded_)
75 FloatingPointP flpoint[2];
76 for(uint iGen = 1; iGen < 2; iGen++) {
78 state->setGenotype(flpoint[iGen]);
83 flpoint[iGen]->setParameterValue(state,
"lbound", (voidP)
new double(0));
84 flpoint[iGen]->setParameterValue(state,
"ubound", (voidP)
new double(0.01));
87 ECF_LOG(state, 1,
"opt-IA algorithm: added 1 FloatingPoint genotype (antibody age)");
90 areGenotypesAdded_ =
true;
98 std::vector<IndividualP> clones;
100 cloningPhase(state, deme, clones);
101 hypermutationPhase(state, deme, clones);
102 agingPhase(state, deme, clones);
103 selectionPhase(state, deme, clones);
104 birthPhase(state, deme, clones);
111bool OptIA::cloningPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
114 for( uint i = 0; i < deme->getSize(); i++ )
115 clones.push_back(deme->at(i));
117 for( uint i = 0; i < deme->getSize(); i++ ){
118 IndividualP antibody = clones.at(i);
121 for (uint j = 0; j <
dup; j++)
122 clones.push_back(
copy(antibody));
129bool OptIA::hypermutationPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
137 for( uint i = 0; i < clones.size(); i++ ){
138 IndividualP antibody = clones.at(i);
140 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(0));
141 std::vector< double > &antibodyVars = flp->realValue;
144 M =(int) ((1- 1/(
double)(k)) * (
c*dimension) + (
c*dimension));
147 for (uint j = 0; j < M; j++){
148 uint param = state->getRandomizer()->getRandomInteger((
int)antibodyVars.size());
150 double randDouble1 = state->getRandomizer()->getRandomDouble();
151 double randDouble2 = state->getRandomizer()->getRandomDouble();
152 double value = antibodyVars[param] + (1-2*randDouble1)* 0.2 * (ubound - lbound) * pow(2, -16*randDouble2 );
156 else if (value <lbound)
160 antibodyVars[param] = value;
162 FitnessP parentFitness = antibody->fitness;
166 if(antibody-> fitness->isBetterThan(parentFitness)){
167 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1));
168 double &age = flp->realValue[0];
176bool OptIA::agingPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
181 std::vector<IndividualP> temp_clones;
183 for (uint i = 0; i < clones.size(); i++){
184 IndividualP antibody = clones.at(i);
187 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1));
188 double &age = flp->realValue[0];
193 temp_clones.push_back(antibody);
195 else if (
elitism == 1 && i == 0)
196 temp_clones.push_back(antibody);
198 clones = temp_clones;
203bool OptIA::selectionPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
209 if(clones.size() > deme->getSize())
210 clones.erase (clones.begin()+ deme->getSize(), clones.end());
216bool OptIA::birthPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
219 uint birthNumber = deme->getSize() - clones.size();
222 if (birthNumber == 0)
return true;
224 IndividualP newAntibody =
copy(deme->at(0));
225 FloatingPointP flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(0));
227 for (uint i = 0; i<birthNumber; i++){
229 flp->initialize(state);
233 flp = boost::static_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(1));
234 double &age = flp->realValue[0];
238 clones.push_back(
copy(newAntibody));
247 for( uint i = 0; i < clones.size(); i++ )
248 deme->replace(i, clones.at(i));
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.
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.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
bool replacePopulation(StateP state, DemeP deme, std::vector< IndividualP > &clones)
replace population with the contents of the clones vector
double c
mutation parameter
double tauB
maximum number of generations without improvement
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
bool elitism
specifies whether to use elitism or not
static bool sortPopulationByFitness(IndividualP ab1, IndividualP ab2)
sort vector of antibodies in regards to their fitness
uint dup
number of clones per antibody