3const int RANDOM_GENOTYPE = 0;
4const int ALL_GENOTYPES = 1;
12 state->getRegistry()->registerEntry(
"crossover.genotypes", (voidP)
new std::string(
"random"), ECF::STRING,
13 "if there are multiple genotypes, which to cross? 'random': a random pair, 'all': all pairs (default: random)");
14 state->getRegistry()->registerEntry(
"crossover.protected", (voidP)
new std::string(
""), ECF::STRING,
15 "indexes of genotypes (separated by spaces) that are excluded (protected) from crossover (default: none)");
29 voidP sptr = state->getRegistry()->getEntry(
"crossover.genotypes");
30 std::string crxGen = *((std::string*)sptr.get());
35 else if(crxGen ==
"random")
38 ECF_LOG_ERROR(state,
"Warning: invalid parameter value (key: crossover.genotypes)");
42 sptr = state->getRegistry()->getEntry(
"crossover.protected");
43 ss << *((std::string*) sptr.get());
47 ECF_LOG_ERROR(state,
"Error: invalid genotype index (key: crossover.protected)!");
54 for(uint gen = 0; gen <
operators.size(); gen++) {
59 std::vector<double> empty;
63 for(uint i = 0; i < nOps; i++) {
68 std::vector<double> probs(nOps);
69 probs[0] =
operators[gen][0]->probability_;
70 for(uint i = 1; i < nOps; i++) {
71 probs[i] = probs[i - 1] +
operators[gen][i]->probability_;
73 if(probs[nOps - 1] == 0)
76 if(probs[nOps - 1] != 1) {
77 double normal = probs[nOps - 1];
78 ECF_LOG_ERROR(state,
"Warning: " +
operators[gen][0]->myGenotype_->getName() +
79 " crossover operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) +
")");
80 for(uint i = 0; i < probs.size(); i++)
97 child->fitness->setInvalid();
99 state_->getContext()->firstParent = ind1;
100 state_->getContext()->secondParent = ind2;
101 state_->getContext()->child = child;
102 ECF_LOG(state_, 5,
"Crossover, 1st parent: " + ind1->toString()
103 +
"\nCrossover, 2nd parent: " + ind2->toString());
107 ind1 = (IndividualP) ind1->copy();
110 ind2 = (IndividualP) ind2->copy();
117 uint iGenotype = state_->getRandomizer()->getRandomInteger((
int)ind1->size());
121 for(uint i = 0; i < iGenotype; i++)
122 child->at(i) = (GenotypeP) ind1->at(i)->copy();
123 for(uint i = iGenotype + 1; i < child->size(); i++)
124 child->at(i) = (GenotypeP) ind2->at(i)->copy();
127 if(
opProb[iGenotype][0] < 0)
128 iOperator = state_->getRandomizer()->getRandomInteger((
int)
operators[iGenotype].size());
130 double random = state_->getRandomizer()->getRandomDouble();
132 while(
opProb[iGenotype][iOperator] < random)
135 operators[iGenotype][iOperator]->mate(ind1->at(iGenotype), ind2->at(iGenotype), child->at(iGenotype));
139 for(uint iGenotype = 0; iGenotype < ind1->size(); iGenotype++) {
144 if(
opProb[iGenotype][0] < 0)
145 iOperator = state_->getRandomizer()->getRandomInteger((
int)
operators[iGenotype].size());
147 double random = state_->getRandomizer()->getRandomDouble();
149 while(
opProb[iGenotype][iOperator] < random)
152 operators[iGenotype][iOperator]->mate(ind1->at(iGenotype), ind2->at(iGenotype), child->at(iGenotype));
156 ECF_LOG(state_, 5,
"Crossover, new individual: " + child->toString());
void registerParameters(StateP)
Register crossover related but Genotype unrelated parameters.
uint crxGenotypes_
what genotypes (if more than one) to cross (random, all)
std::vector< bool > protectedGenotypes_
protected (non-crossable) genotypes flags
bool mate(IndividualP ind1, IndividualP ind2, IndividualP child)
Crosses 2 individuals.
std::vector< std::vector< double > > opProb
usage probabilities for each CrossoverOp operator
bool initialize(StateP)
Initialize all crossover operators of all active genotypes.
std::vector< std::vector< CrossoverOpP > > operators
vectors of crx operators for each genotype