本文整理汇总了C++中DNA::mutate方法的典型用法代码示例。如果您正苦于以下问题:C++ DNA::mutate方法的具体用法?C++ DNA::mutate怎么用?C++ DNA::mutate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DNA
的用法示例。
在下文中一共展示了DNA::mutate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
}
示例5: doPool
//void GenePool::updatePool(double crossover, double mutationchance, double severity, int tourneysize, int gen)
void GenePool::doPool(int maxsize, int deltype, int tourneysize, double crossover, double mutationchance, double severity, int numgens, double mutdev, double jostlechance, int simtype, int runs, int smult, int lmult)
{
int top = 1;
DNA thedna; //set by mutate/breed
for (int fill = 1;fill < maxsize;fill++)
{
dnavect.push_back(thedna);
fitness.push_back(-1);
}
//printf("fitness[%d] = %d ; cycle = %d\n", num, fitness[num], gs.cycle);
//if (fitness[num] < 0) {dnavect[num].save("bad.dna");}
//delete if needed
int tourney[tourneysize];
Random ran;
for (int vvi = 0;vvi < numgens;vvi++)
{
//int selected[5]; //the index of tourney selected dudes
//printf("ms1\n");
int sa = 0;int sb = 0; //the two selected to mate
//pick best 'tourneysize' from the tourney
//printf("ms2\n");
//select sample for tourney
for (int ii = 0;ii < tourneysize; ii++)
{
tourney[ii] = ran.nextInt(top);
}
int highestfit = -1;int highestfitindex = 0;
//printf("ms3\n");
for (int b = 0;b < tourneysize;b++)
{
if (fitness[tourney[b]] > highestfit) {highestfit = fitness[tourney[b]];highestfitindex = tourney[b];}
}
sa = highestfitindex;
//printf("ms4\n");
for (int ii = 0;ii < tourneysize; ii++)
{
tourney[ii] = ran.nextInt(top);
}
highestfit = -1;highestfitindex = 0;
//printf("ms5\n");
for (int b = 0;b < tourneysize;b++)
{
if (fitness[tourney[b]] > highestfit) {highestfit = fitness[tourney[b]];highestfitindex = tourney[b];}
}
sb = highestfitindex;
//printf("ms6\n");
//breed and mutate x1
int inf1 = vvi;int inf2 = sa;int inf3 = fitness[sa];int inf4 = sb;int inf5 = fitness[sb];
thedna = this->dnavect[sa].mate(dnavect[sb], crossover, reactionlock);
//printf("msvv\n");
double fmut = ran.nextGaussian()*mutdev+mutationchance;
thedna.mutate(fmut, severity, reactionlock, jostlechance);
//printf("ms7\n");
int putwhere = 0;
if (top == maxsize)
{
if (deltype == 0)
{
int lowest = -1;
int lowestindex = 0;
for(int i = 0;i < top;i++)
{
if ((fitness[i] < lowest) || lowest == -1) {lowest = fitness[i]; lowestindex = i;}
}
//printf("ms8\n");
putwhere = lowestindex;
dnavect[putwhere] = thedna;
}
if (deltype == 1) //random del
{
putwhere = ran.nextInt(top);
dnavect[putwhere] = thedna;
}
if (deltype == 2)
{
for (int ii = 0;ii < tourneysize; ii++)
{
tourney[ii] = ran.nextInt(top);
}
int lowfit = -1;int lowfitindex = 0;
for (int b = 0;b < tourneysize;b++)
{
if (fitness[tourney[b]] < lowfit || lowfit == -1) {lowfit = fitness[tourney[b]];lowfitindex = tourney[b];}
}
//.........这里部分代码省略.........