本文整理汇总了C++中Candidate::getFitness方法的典型用法代码示例。如果您正苦于以下问题:C++ Candidate::getFitness方法的具体用法?C++ Candidate::getFitness怎么用?C++ Candidate::getFitness使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Candidate
的用法示例。
在下文中一共展示了Candidate::getFitness方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: gaUpdate
void GA::gaUpdate(){
timer.stop();
myPop->nextGeneration(mutationChance, mutationAmout, breedingCutOffParents, genCount+1);
Candidate fittest = myPop->getFittest();
if((genCount+1) % 10 == 0){
qDebug()<<"Generation:"<<genCount+1;
qDebug()<<"Fittest:"<<fittest.getFitness();
}
genCount++;
/// DRAW FITTEST SET OF POLYGONS ON THE ACTUAL DRAWING!
fittest.drawAllPolys(drawing);
/// Update label my sending Pixmap via signal
emit new_drawing_created(drawing);
timer.start();
}
示例2: Candidate
Population::Population(int s, int dlen, int dclen, int verts, int numPolys, QPixmap *original, QPixmap *drawing){
size = s;
oPix = original;
dPix = drawing;
/// Initialising population with candidates
Candidate::setDNALength(dlen);
Candidate::setDNACrossedLength(dclen);
Candidate::setPolyPts(verts);
Candidate::setNumPolys(numPolys);
Candidate *newCand;
for(int i=0; i<size; i++){
//qDebug()<<"Population"<<i+1;
newCand = new Candidate(oPix, dPix, 0);
candidates.push_back(*newCand); // A new candidate is generated and pushed into the vector
std::vector <float> newCandDna = newCand->getDnaValues();
qDebug()<<newCand->getGen()<<" | "<<i<<" | "<<newCand->getFitness()<<" | "<<newCandDna[0]<<newCandDna[1200]<<newCandDna[1600];
}
}
示例3: nextGeneration
void Population::nextGeneration(int mutateChance, int mutateAmt, float breedingCutoff, int newgenNum){
//qDebug()<<"Generation:"<<newgenNum;
if(candidates.size()==1){
/// Asexual reproduction
Candidate parent = candidates[0];
Candidate child(oPix, dPix, parent.getDnaValues(), parent.getDnaValues(), mutateChance, mutateAmt, newgenNum);
if(child.getFitness()>parent.getFitness()){
candidates[0] = child;
}
}
else{
//qDebug()<<"Before Sorting DNA SIZE"<<candidates[0].getDnaValues().size();
/// Candidates are more than one in number
/// So, they can do Sexual reproduction
std::sort(candidates.begin(), candidates.end(), compareSort);
/// Check if sorted
//if(newgenNum==20 || newgenNum==30){
/*for(int i=0; i<candidates.size(); i++){
Candidate c = candidates[i];
std::vector<float> c_dna = candidates[i].getDnaValues();
qDebug()<<c.getGen()<<" | "<<i<<" | "<<c.getFitness()<<" | "<<c_dna[0]<<c_dna[1200]<<c_dna[1600];
}*/
//}
/// Sorting worked!
std::vector <Candidate> children;
int numOfBreedingParents = floor(candidates.size()*breedingCutoff);
int numOfChildren = ceil(1/breedingCutoff);
for (int i=0;i<numOfBreedingParents;i++) { /// Each breeding parent has numOfChildren number of children other parents chosen randomly
std::vector <float> dna = candidates.at(i).getDnaValues();
for (int j=0;j<numOfChildren;j++) {
int rndCandidate = getRandomPartner(0,numOfBreedingParents-1,i);
/// To avoid asexual reproduction, rndCandidate should be any value between 0 & numOfBreedingParents (excluding i)
//qDebug()<<"Just before breeding"<<dna.size()<<candidates[rndCandidate].getDnaValues().size();
Candidate newChild (oPix, dPix, dna, candidates[rndCandidate].getDnaValues(), mutateChance, mutateAmt, newgenNum);
children.push_back(newChild);
}
}
//qDebug()<<"reproduction complete";
//#if 0
/// lets say parents cut off was 0.25 and population size 40
/// 40*0.25 = 10 parents will breed and give birth to 1/0.25=4 children each
/// So, we still have 10*4 = 40 individuals in our population!
/// Each parent can breed with ANY of the other parents. We randomly choose which parent to breed with. They could've bred earlier too
/// Now we kill of parents
candidates = children;
/// Check population size (only for verification purposes)
while(candidates.size()>size){
candidates.pop_back();
}
//#endif
} // end of else
/// Print next generation to check if it worked
/*for(int i=0; i<candidates.size(); i++){
/// Will print i | GenNum | Fitness
qDebug()<<i<<" | "<<candidates.at(i).getGen()<<" | "<<candidates.at(i).getFitness();
}*/
/// It Worked!! :D :D :D
}