00001 #include "ECF_base.h"
00002 #include "ECF_macro.h"
00003 #include "AlgOptIA.h"
00004 #include <cmath>
00005
00006
00007
00008 OptIA::OptIA()
00009 {
00010 name_ = "OptIA";
00011
00012 areGenotypesAdded_ = false;
00013 }
00014
00015
00016 void OptIA::registerParameters(StateP state)
00017 {
00018 registerParameter(state, "dup", (voidP) new uint(5), ECF::INT,
00019 "number of clones for each individual in clone population (dafault: 5)");
00020 registerParameter(state, "c", (voidP) new double(0.2), ECF::DOUBLE,
00021 "mutation rate (default: 0.2)");
00022 registerParameter(state, "tauB", (voidP) new double(100), ECF::DOUBLE,
00023 "maximum number of generations to keep an individual without improvement (default: 100)");
00024 registerParameter(state, "elitism", (voidP) new uint(0), ECF::UINT,
00025 "use elitism (default: 0)");
00026 }
00027
00028
00029 bool OptIA::initialize(StateP state)
00030 {
00031 voidP lBound = state->getGenotypes()[0]->getParameterValue(state, "lbound");
00032 lbound = *((double*) lBound.get());
00033
00034 voidP uBound = state->getGenotypes()[0]->getParameterValue(state, "ubound");
00035 ubound = *((double*) uBound.get());
00036
00037 voidP dimension_ = state->getGenotypes()[0]->getParameterValue(state, "dimension");
00038 dimension = *((uint*) dimension_.get());
00039
00040 voidP dup_ = getParameterValue(state, "dup");
00041 dup = *((uint*) dup_.get());
00042 if( *((int*) dup_.get()) <= 0 ) {
00043 ECF_LOG(state, 1, "Error: opt-IA requires parameter 'dup' to be an integer greater than 0");
00044 throw "";}
00045
00046 voidP c_ = getParameterValue(state, "c");
00047 c = *((double*) c_.get());
00048 if( c <= 0 ) {
00049 ECF_LOG(state, 1, "Error: opt-IA requires parameter 'c' to be a double greater than 0");
00050 throw "";}
00051
00052 voidP tauB_ = getParameterValue(state, "tauB");
00053 tauB = *((double*) tauB_.get());
00054 if( tauB < 0 ) {
00055 ECF_LOG(state, 1, "Error: opt-IA requires parameter 'tauB' to be a nonnegative double value");
00056 throw "";}
00057
00058 voidP elitism_ = getParameterValue(state, "elitism");
00059 elitism = (*((uint*) elitism_.get())) != 0;
00060
00061
00062 FloatingPointP flp (new FloatingPoint::FloatingPoint);
00063 if(state->getGenotypes()[0]->getName() != flp->getName()) {
00064 ECF_LOG_ERROR(state, "Error: opt-IA algorithm accepts only a FloatingPoint genotype!");
00065 throw ("");}
00066
00067
00068 if(areGenotypesAdded_)
00069 return true;
00070
00071
00072 FloatingPointP flpoint[2];
00073 for(uint iGen = 1; iGen < 2; iGen++) {
00074 flpoint[iGen] = (FloatingPointP) new FloatingPoint::FloatingPoint;
00075 state->setGenotype(flpoint[iGen]);
00076
00077 flpoint[iGen]->setParameterValue(state, "dimension", (voidP) new uint(1));
00078
00079
00080 flpoint[iGen]->setParameterValue(state, "lbound", (voidP) new double(0));
00081 flpoint[iGen]->setParameterValue(state, "ubound", (voidP) new double(0.01));
00082
00083 }
00084 ECF_LOG(state, 1, "opt-IA algorithm: added 1 FloatingPoint genotype (antibody age)");
00085
00086
00087 areGenotypesAdded_ = true;
00088
00089 return true;
00090 }
00091
00092
00093 bool OptIA::advanceGeneration(StateP state, DemeP deme)
00094 {
00095 std::vector<IndividualP> clones;
00096
00097 cloningPhase(state, deme, clones);
00098 hypermutationPhase(state, deme, clones);
00099 agingPhase(state, deme, clones);
00100 selectionPhase(state, deme, clones);
00101 birthPhase(state, deme, clones);
00102 replacePopulation(state, deme, clones);
00103
00104 return true;
00105 }
00106
00107
00108 bool OptIA::cloningPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00109 {
00110
00111 for( uint i = 0; i < deme->getSize(); i++ )
00112 clones.push_back(deme->at(i));
00113
00114 for( uint i = 0; i < deme->getSize(); i++ ){
00115 IndividualP antibody = clones.at(i);
00116
00117
00118 for (uint j = 0; j < dup; j++)
00119 clones.push_back(copy(antibody));
00120 }
00121
00122 return true;
00123 }
00124
00125
00126 bool OptIA::hypermutationPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00127 {
00128 uint M;
00129 uint k;
00130
00131
00132 std::sort (clones.begin(), clones.end(), sortPopulationByFitness);
00133
00134 for( uint i = 0; i < clones.size(); i++ ){
00135 IndividualP antibody = clones.at(i);
00136
00137 FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(0));
00138 std::vector< double > &antibodyVars = flp->realValue;
00139
00140 k = 1 + i/(dup+1);
00141 M =(int) ((1- 1/(double)(k)) * (c*dimension) + (c*dimension));
00142
00143
00144 for (uint j = 0; j < M; j++){
00145 uint param = state->getRandomizer()->getRandomInteger((int)antibodyVars.size());
00146
00147 double randDouble1 = state->getRandomizer()->getRandomDouble();
00148 double randDouble2 = state->getRandomizer()->getRandomDouble();
00149 double value = antibodyVars[param] + (1-2*randDouble1)* 0.2 * (ubound - lbound) * pow(2, -16*randDouble2 );
00150
00151 if (value > ubound)
00152 value = ubound;
00153 else if (value <lbound)
00154 value = lbound;
00155
00156
00157 antibodyVars[param] = value;
00158 }
00159 FitnessP parentFitness = antibody->fitness;
00160 evaluate(antibody);
00161
00162
00163 if(antibody-> fitness->isBetterThan(parentFitness)){
00164 flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1));
00165 double &age = flp->realValue[0];
00166 age = 0;
00167 }
00168 }
00169 return true;
00170 }
00171
00172
00173 bool OptIA::agingPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00174 {
00175
00176 std::sort (clones.begin(), clones.end(), sortPopulationByFitness);
00177
00178 std::vector<IndividualP> temp_clones;
00179
00180 for (uint i = 0; i < clones.size(); i++){
00181 IndividualP antibody = clones.at(i);
00182
00183
00184 FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (antibody->getGenotype(1));
00185 double &age = flp->realValue[0];
00186 age += 1;
00187
00188
00189 if (age <=tauB)
00190 temp_clones.push_back(antibody);
00191
00192 else if (elitism == 1 && i == 0)
00193 temp_clones.push_back(antibody);
00194 }
00195 clones = temp_clones;
00196 return true;
00197 }
00198
00199
00200 bool OptIA::selectionPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00201 {
00202
00203 std::sort (clones.begin(), clones.end(), sortPopulationByFitness);
00204
00205
00206 if(clones.size() > deme->getSize())
00207 clones.erase (clones.begin()+ deme->getSize(), clones.end());
00208
00209 return true;
00210 }
00211
00212
00213 bool OptIA::birthPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00214 {
00215
00216 uint birthNumber = deme->getSize() - clones.size();
00217
00218
00219 if (birthNumber == 0) return true;
00220
00221 IndividualP newAntibody = copy(deme->at(0));
00222 FloatingPointP flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(0));
00223
00224 for (uint i = 0; i<birthNumber; i++){
00225
00226 flp->initialize(state);
00227 evaluate(newAntibody);
00228
00229
00230 flp = boost::dynamic_pointer_cast<FloatingPoint::FloatingPoint> (newAntibody->getGenotype(1));
00231 double &age = flp->realValue[0];
00232 age = 0;
00233
00234
00235 clones.push_back(copy(newAntibody));
00236 }
00237 return true;
00238 }
00239
00240
00242 bool OptIA::replacePopulation(StateP state, DemeP deme, std::vector<IndividualP> &clones)
00243 {
00244 for( uint i = 0; i < clones.size(); i++ )
00245 deme->replace(i, clones.at(i));
00246
00247 clones.clear();
00248
00249 return true;
00250 }