本文整理汇总了C++中DNA::crossover方法的典型用法代码示例。如果您正苦于以下问题:C++ DNA::crossover方法的具体用法?C++ DNA::crossover怎么用?C++ DNA::crossover使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DNA
的用法示例。
在下文中一共展示了DNA::crossover方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: draw
//--------------------------------------------------------------
void ofApp::draw()
{
if (!foundSolution)
{
for (int i = 0; i < ArrayCount(population); i++)
{
population[i].Fitness();
}
vector<DNA> matingPool = vector<DNA>();
for (int i = 0; i < ArrayCount(population); i++)
{
int n = int(population[i].fitness * ArrayCount(population));
for (int k = 0; k < n; k++)
{
matingPool.push_back(population[i]);
}
}
for (int i = 0; i < ArrayCount(population); i++)
{
int a = int(ofRandom(0, matingPool.size()));
int b = int(ofRandom(0, matingPool.size()));
DNA parentA = matingPool[a];
DNA parentB = matingPool[b];
DNA child = parentA.crossover(parentB);
child.mutate();
population[i] = child;
}
}
ofClear(ofColor::white);
ofSetColor(ofColor::black);
for (int i = 0; i < ArrayCount(population); i++)
{
//string str = population[i].genes;
population[i].genes[ArrayCount(population[i].genes)] = '\0';
if (population[i].genes == target)
{
foundSolution = true;
ofNoFill();
ofSetLineWidth(6);
ofSetCurveResolution(200);
ofCircle(ofPoint((int(i / POPULATION_PER_COLUMN) * Y_SPACING) + 100,
((i % POPULATION_PER_COLUMN) * X_SPACING) + BUFFER_SPACING), 120);
}
myFont.drawString(population[i].genes,
(int(i / POPULATION_PER_COLUMN) * Y_SPACING) + BUFFER_SPACING,
((i % POPULATION_PER_COLUMN) * X_SPACING) + BUFFER_SPACING);
}
}
示例2: reproduction
// Making the next generation
void Population::reproduction()
{
// Refill the population with children from the mating pool
for( int i = 0; i < mPopulation.size(); i++ )
{
// Sping the wheel of fortune to pick two parents
int m = randInt( mMatingPool.size() );
int d = randInt( mMatingPool.size() );
// Pick two parents
RocketRef mom = mMatingPool[m];
RocketRef dad = mMatingPool[d];
// Get their genes
DNA *momgenes = mom->getDNA();
DNA *dadgenes = dad->getDNA();
// Mate their genes
DNA *child = momgenes->crossover( dadgenes );
// Mutate their genes
child->mutate( mMutationRate );
// Fill the new population with the new child
Vec2f location = Vec2f( getWindowWidth() / 2.0, getWindowHeight() + 20.0 );
mPopulation[i].reset(); // get rid of the old rocket
mPopulation[i] = std::make_shared<Rocket>( location, child, mTarget );
}
mGenerations++;
}
示例3: reproduce
void Population::reproduce() {
for (int i = 0; i < popSize; i++) {
int a = ofRandom(matingPool.size());
int b = ofRandom(matingPool.size());
DNA partnerA = matingPool[a];
DNA partnerB = matingPool[b];
//Step 3a: Crossover
DNA child = partnerA.crossover(partnerB);
//Step 3b: Mutation
child.mutate(mutationRate);
//Note that we are overwriting the population with the new children. When draw() loops, we will perform all the same steps with the new population of children.
population[i] = child;
}
}
示例4: reproduction
void Population::reproduction() {
for (int i = 0; i < (int)elements.size(); i++) {
elements.at(i)->computeFitness(target);
}
vector<DNA *> pool = matingPool();
for (int i = 0; i < (int)elements.size(); i++) {
DNA * partnerA = pool.at(randInt(pool.size()));
DNA * partnerB = pool.at(randInt(pool.size()));
DNA * child = partnerA->crossover(partnerB);
child->mutate(mutationRate);
elements.at(i) = child;
}
}