int NextCaptureOrCheck(MOVES * m) // used in QuiesceCheck() { int move; switch (m->phase) { case 0: while (m->next < m->last) { move = SelectBest(m); if (BadCapture(m->p, move)) continue; return move; } m->phase = 1; case 1: m->last = GenerateQuietChecks(m->p, m->move); ScoreQuiet(m); m->phase = 2; case 2: while (m->next < m->last) { move = SelectBest(m); if (Swap(m->p, Fsq(move), Tsq(move)) < 0) continue; return move; } } return 0; }
void RunGA(){ int generations = 0; while(max_generation >= 0 && generations <= max_generation){ GAStep(); if(generations % 100 == 0){ std::cout << "Generacion: " << generations << "\n"; } if(generations % 1000 == 0){ Chromosome *best = SelectBest(); QImage image = DrawImage(best); image.save(QString("out/out_") + QString::number(generations) + ".png"); qDebug() << "Fitness: " << best->Fitness(); } generations++; } DrawSVG(SelectBest(), "Final.svg"); }
int NextCapture(MOVES *m) { int move; while (m->next < m->last) { move = SelectBest(m); return move; } return 0; }
void LsysGa::DoCreep() { // do stuff for(int i = 0; i < m_nNumMutants; ++i) { int nToMutate = rand()%m_nPopSize; m_arrMutationResults[i].Creep(m_arrCurrentPop[nToMutate], 0.1); // make it decay later } EvaluatePopulation(m_arrMutationResults, m_nNumMutants); SelectBest(m_arrCurrentPop, (m_nPopSize)/2, m_arrMutationResults, m_nNumMutants); }
void LsysGa::DoCrossOver() { // do stuff for(int i = 0; i < m_nNumOffspring; ++i) { int nParent1 = rand()%m_nPopSize; int nParent2 = (rand()%(m_nPopSize-1)); nParent2= (nParent1+nParent2+1)%m_nPopSize; m_arrCrossoverResults[i].CrossOver( m_arrCurrentPop[nParent1], 1.0-m_arrCurrentPop[nParent1].m_lfFitness, m_arrCurrentPop[nParent2], 1.0-m_arrCurrentPop[nParent2].m_lfFitness); } EvaluatePopulation(m_arrCrossoverResults, m_nNumOffspring); SelectBest(m_arrCurrentPop, m_nPopSize/5, m_arrCrossoverResults, m_nNumOffspring); }
void GAStep(){ Chromosome *elite = SelectBest(); Chromosome clonA; Chromosome clonB; Chromosome *ap = NULL; // Elitism elite->Clone(&clonA); InsertPobB(&clonA); // harem int fraction = int(HAREM * sizePopulationA); for(int i = 0; i < fraction; i += 2){ elite->Clone(&clonA); SelectTournament()->Clone(&clonB); Crossover(&clonA, &clonB); clonA.Mutate(); clonB.Mutate(); clonA.Fitness() = Distance(&clonA); clonB.Fitness() = Distance(&clonB); InsertPobB(&clonA); InsertPobB(&clonB); } // Fill while((sizePopulationA - sizePopulationB) > 0){ ap = SelectTournament(); ap->Clone(&clonA); ap = SelectTournament(); ap->Clone(&clonB); Crossover(&clonA, &clonB); clonA.Mutate(); clonB.Mutate(); clonA.Fitness() = Distance(&clonA); clonB.Fitness() = Distance(&clonB); if(clonA.Fitness() < clonB.Fitness()) InsertPobB(&clonA); else InsertPobB(&clonB); } UpdatePopulation(); }
/** AI Wants to purchase Astronauts */ void AIAstroPur(char plr) { int cost; int astrosInPool = 0; if (Data->P[plr].AstroLevel == 0) { cost = 20; } else { cost = 15; } // Player has no cash, no astronauts if (cost > Data->P[plr].Cash) { return; } switch (Data->P[plr].AstroLevel) { case 0: astrosInPool = ASTRO_POOL_LVL1; break; case 1: astrosInPool = ASTRO_POOL_LVL2; break; case 2: astrosInPool = ASTRO_POOL_LVL3; break; case 3: astrosInPool = ASTRO_POOL_LVL4; break; case 4: astrosInPool = ASTRO_POOL_LVL5; break; default: break; }; // Select best astronauts out of number of positions to fill SelectBest(plr, astrosInPool); return; }
int NextMove(MOVES *m, int *flag) { int move; switch (m->phase) { case 0: // return transposition table move, if legal move = m->trans_move; if (move && Legal(m->p, move)) { m->phase = 1; *flag = MV_HASH; return move; } case 1: // helper phase: generate captures m->last = GenerateCaptures(m->p, m->move); ScoreCaptures(m); m->next = m->move; m->badp = m->bad; m->phase = 2; case 2: // return good captures, save bad ones on the separate list while (m->next < m->last) { move = SelectBest(m); if (move == m->trans_move) continue; if (BadCapture(m->p, move)) { *m->badp++ = move; continue; } *flag = MV_CAPTURE; return move; } case 3: // first killer move move = m->killer1; if (move && move != m->trans_move && m->p->pc[Tsq(move)] == NO_PC && Legal(m->p, move)) { m->phase = 4; *flag = MV_KILLER; return move; } case 4: // second killer move move = m->killer2; if (move && move != m->trans_move && m->p->pc[Tsq(move)] == NO_PC && Legal(m->p, move)) { m->phase = 5; *flag = MV_KILLER; return move; } case 5: // refutation move move = m->ref_move; if (move && move != m->trans_move && m->p->pc[Tsq(move)] == NO_PC && move != m->killer1 && move != m->killer2 && Legal(m->p, move)) { m->phase = 6; *flag = MV_NORMAL; return move; } case 6: // helper phase: generate quiet moves m->last = GenerateQuiet(m->p, m->move); ScoreQuiet(m); m->next = m->move; m->phase = 7; case 7: // return quiet moves while (m->next < m->last) { move = SelectBest(m); if (move == m->trans_move || move == m->killer1 || move == m->killer2 || move == m->ref_move) continue; *flag = MV_NORMAL; return move; } m->next = m->bad; m->phase = 8; case 8: // return bad captures if (m->next < m->badp) { *flag = MV_BADCAPT; return *m->next++; } } return 0; }
bool DisplayModeManager::MakeWindowFullscreen(WindowEvents * window_events, int width, int height) { SetMode(SelectBest(width, height)); window_events->EnterFullscreen(); return true; }