12 areGenotypesAdded_ =
false;
19 "number of antibodies cloned every generation (default: 100)");
21 "number of clones (percentage) for every antibody (default: 1.0)");
23 "mutation rate (default: 0.2)");
25 "fraction of population regenerated every generation (default: 0)");
26 registerParameter(state,
"cloningVersion", (voidP)
new std::string(
"proportional"), ECF::STRING,
27 "cloning version, static or proportional (default: proportional)");
28 registerParameter(state,
"selectionScheme", (voidP)
new std::string(
"CLONALG1"), ECF::STRING,
29 "which selection scheme to use, CLONALG1 or CLONALG2 (default: CLONALG1)");
35 voidP lBound = state->getGenotypes()[0]->getParameterValue(state,
"lbound");
36 lbound = *((
double*) lBound.get());
38 voidP uBound = state->getGenotypes()[0]->getParameterValue(state,
"ubound");
39 ubound = *((
double*) uBound.get());
41 voidP dimension_ = state->getGenotypes()[0]->getParameterValue(state,
"dimension");
42 dimension = *((uint*) dimension_.get());
45 voidP populationSize_ = state->getRegistry()->getEntry(
"population.size");
46 uint populationSize = *((uint*) populationSize_.get());
49 n = *((uint*) n_.get());
50 if( n<1 || n>populationSize) {
51 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'n' to be an integer in range <0, population.size] ");
56 beta = *((
double*) beta_.get());
58 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'beta' to be a double greater than 0");
63 c = *((
double*) c_.get());
65 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'c' to be a double greater than 0");
70 d = *((
double*) d_.get());
72 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'd' to be a double in range [0, 1] ");
78 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'cloningVersion' to be either 'static' or a 'proportional'");
84 ECF_LOG(state, 1,
"Error: CLONALG requires parameter 'selectionScheme' to be either 'CLONALG1' or 'CLONALG2'");
90 GenotypeP activeGenotype = state->getGenotypes()[0];
91 RealValueGenotypeP rv = boost::dynamic_pointer_cast<RealValueGenotype> (activeGenotype);
93 ECF_LOG_ERROR(state,
"Error: CLONALG algorithm accepts only a RealValueGenotype derived genotype! (FloatingPoint or Binary)");
98 if(areGenotypesAdded_)
103 FloatingPointP flpoint[2];
104 for(uint iGen = 1; iGen < 2; iGen++) {
106 state->setGenotype(flpoint[iGen]);
111 flpoint[iGen]->setParameterValue(state,
"lbound", (voidP)
new double(0));
112 flpoint[iGen]->setParameterValue(state,
"ubound", (voidP)
new double(0.01));
115 ECF_LOG(state, 1,
"CLONALG algorithm: added 1 FloatingPoint genotype (parentAntibody)");
119 areGenotypesAdded_ =
true;
127 std::vector<IndividualP> clones;
130 cloningPhase(state, deme, clones);
131 hypermutationPhase(state, deme, clones);
132 selectionPhase(state, deme, clones);
133 birthPhase(state, deme, clones);
143 for( uint i = 0; i < deme->getSize(); i++ ) {
144 RealValueGenotypeP flp = boost::static_pointer_cast<RealValueGenotype> (deme->at(i)->getGenotype(1));
145 double &parentAb = flp->realValue[0];
146 parentAb = (double)i;
152bool Clonalg::cloningPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
155 uint clonesPerAntibody = (uint) (
beta * deme->getSize());
158 for( uint i = 0; i < deme->getSize(); i++ )
159 clones.push_back(deme->at(i));
165 clones.erase (clones.begin()+
n,clones.end());
167 for( uint i = 0; i <
n; i++ ) {
168 IndividualP antibody = clones.at(i);
172 for (uint j = 0; j < clonesPerAntibody; j++)
173 clones.push_back(
copy(antibody));
178 uint proportionalCloneNo = clonesPerAntibody/(i+1);
179 for (uint j = 0; j < proportionalCloneNo ; j++)
180 clones.push_back(
copy(antibody));
188bool Clonalg::hypermutationPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
197 uint clonesPerAntibody = (uint) (
beta * deme->getSize());
201 uint parentIndex = 0;
203 for( uint i = 0; i < clones.size(); i++ ) {
204 IndividualP antibody = clones.at(i);
206 RealValueGenotypeP flp = boost::static_pointer_cast<RealValueGenotype> (antibody->getGenotype(0));
207 std::vector< double > &antibodyVars = flp->realValue;
211 k = 1+ i/(clonesPerAntibody +1);
215 counter += 1 + clonesPerAntibody/parentIndex;
220 M = (int) ((1- 1/(
double)(k)) * (
c*dimension) + (
c*dimension));
223 for (uint j = 0; j < M; j++) {
224 uint param = state->getRandomizer()->getRandomInteger((
int)antibodyVars.size());
226 double randDouble1 = state->getRandomizer()->getRandomDouble();
227 double randDouble2 = state->getRandomizer()->getRandomDouble();
228 double value = antibodyVars[param] + (1-2*randDouble1)* 0.2 * (ubound - lbound) * pow(2, -16*randDouble2 );
232 else if (value <lbound)
236 antibodyVars[param] = value;
244bool Clonalg::selectionPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
250 int formerParent = -1;
251 std::vector<IndividualP> temp_clones;
253 for (uint i = 0; i < clones.size(); i++) {
254 RealValueGenotypeP flp = boost::static_pointer_cast<RealValueGenotype> (clones.at(i)->getGenotype(1));
255 double &parentAb = flp->realValue[0];
256 if (formerParent != parentAb) {
257 temp_clones.push_back(clones.at(i));
258 formerParent = (int) parentAb;
261 clones = temp_clones;
265 uint selNumber = (uint)((1-
d)*deme->getSize());
268 if(selNumber < clones.size())
269 clones.erase (clones.begin()+ selNumber, clones.end());
275bool Clonalg::birthPhase(StateP state, DemeP deme, std::vector<IndividualP> &clones)
278 uint birthNumber = (uint) (deme->getSize() - clones.size());
280 IndividualP newAntibody =
copy(deme->at(0));
281 RealValueGenotypeP flp = boost::static_pointer_cast<RealValueGenotype> (newAntibody->getGenotype(0));
283 for (uint i = 0; i<birthNumber; i++) {
285 flp->initialize(state);
289 clones.push_back(
copy(newAntibody));
298 for( uint i = 0; i < clones.size(); i++ )
299 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.
double d
fraction of population regenerated every generation
static bool sortPopulationByFitness(IndividualP ab1, IndividualP ab2)
sort vector of antibodies in regards to their fitness
static bool sortPopulationByParentAndFitness(IndividualP ab1, IndividualP ab2)
sort vector of antibodies first by their antibody parents and then by their fitness
std::string cloningVersion
specifies whether to use static or proportional cloning
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
bool markAntibodies(DemeP deme)
mark antibodies so the alg can know which clone belongs to which parent Antibody
uint n
number of antibodies cloned every generation
double beta
parameter which determines the number of clones for every antibody
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 clones vector
double c
mutation parameter
std::string selectionScheme
specifies which selection scheme to use CLONALG1 or CLONALG2
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.