void DSMGA2::backMixingE(Chromosome& source, list<int>& mask, Chromosome& des) { cout <<"_bme"; Chromosome trial(ell); trial = des; for (list<int>::iterator it = mask.begin(); it != mask.end(); ++it) trial.setVal(*it, source.getVal(*it)); if (trial.getFitness() > des.getFitness()) { pHash.erase(des.getKey()); pHash[trial.getKey()] = trial.getFitness(); EQ = false; //des.getIndex()++; des = trial; return; } if (trial.getFitness() >= des.getFitness()) { pHash.erase(des.getKey()); pHash[trial.getKey()] = trial.getFitness(); des = trial; return; } }
int Dilemma::compare(const void *x, const void *y){ const Chromosome xx = *(Chromosome*)x; const Chromosome yy = *(Chromosome*)y; if(xx.getFitness() < yy.getFitness()) return 1; if(xx.getFitness() > yy.getFitness()) return -1; return 0; }
void Sorting::sort(std::vector<Chromosome>& pop) { std::vector<Chromosome> sorted; while (pop.size() != 0) { double bestFitness = -100000; Chromosome bestChromosome; int bestChromomeIndex = -1; for (size_t i = 0; i < pop.size(); i++) { if (pop.at(i).getFitness() > bestFitness) { bestChromosome = pop.at(i); bestFitness = bestChromosome.getFitness(); bestChromomeIndex = i; } } sorted.push_back(bestChromosome); pop.erase(pop.begin() + bestChromomeIndex); } for (size_t i = 0; i < sorted.size(); i++) { pop.push_back(sorted.at(i)); } }
bool Chromosome::operator < (const Chromosome& another)const { if(this->TestCaseCount < another.getCount()) return true; else if(this->TestCaseCount > another.getCount()) return false; else return((this->fitness)< another.getFitness()); }
Chromosome::Chromosome(const Chromosome& other) { //copy ctor this->TSList = other.getSequence(); this->conditions = other.getConditionHash(); this->TestCaseCount = other.getCount(); this->TotalWeight = other.getWeight(); this->fitness = other.getFitness(); this->mcount = other.getMessageCount(); this->size = other.getSize(); }
bool Population::hasDuplication(Chromosome &chromosome) { /* * Check chromosome for duplication. */ for (std::vector<Chromosome>::iterator k=list.begin(); k!=list.end(); k++) { if ((*k).getFitness()==chromosome.getFitness() && (&(*k) != &chromosome)) { return( true ); } } return( false ); }
bool DSMGA2::restrictedMixing(Chromosome& ch, list<int>& mask) { cout << "_rmbool"<<endl; bool taken = false; size_t lastUB = 0; for (size_t ub = 1; ub <= mask.size(); ++ub) { size_t size = 1; Chromosome trial(ell); trial = ch; for (list<int>::iterator it = mask.begin(); it != mask.end(); ++it) { trial.flip(*it); ++size; if (size > ub) break; } if (isInP(trial)) continue; if (trial.getFitness() >= ch.getFitness()) { pHash.erase(ch.getKey()); pHash[trial.getKey()] = trial.getFitness(); taken = true; ch = trial; } if (taken) { lastUB = ub; break; } } if (lastUB != 0) { while (mask.size() > lastUB) mask.pop_back(); } return taken; }
int main(int argc, char** argv){ EA<double> c = EA<double>(100, 2, VALUE, 0.0, 500, UNIFORM_CROSSOVER, 1.0, GAUSSIAN, 1.0, TOURNAMENT, 70, ELITISM); c.setFitnessFunction(function); c.randomInitialization(); c.printPopulation(); std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); Chromosome<double> *res = c.evolveThreaded(10000, 8); std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count(); c.printPopulation(); std::cout << std::endl << res->getFitness() << std::endl; std::cout << "Duration: " << duration << std::endl; return 0; }