From a9deecb4f50c4264b2951658b122f1f31247f38e Mon Sep 17 00:00:00 2001 From: Zagidulin Artyom Date: Sun, 18 Nov 2012 20:43:43 +0400 Subject: [PATCH] variable mutation rate --- geneticalgorithm.cpp | 28 +++++++++++++++++----------- geneticalgorithm.h | 10 ++++++---- mainwindow.cpp | 2 +- render.cpp | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/geneticalgorithm.cpp b/geneticalgorithm.cpp index db7b462..863d362 100755 --- a/geneticalgorithm.cpp +++ b/geneticalgorithm.cpp @@ -61,6 +61,10 @@ float GeneticAlgorithm::getMaxScore(const int index) { return maxScore[index]; } +int GeneticAlgorithm::getOffspringsCount(const int index) { + return offspringsCount[index]; +} + float GeneticAlgorithm::getScore(const int index) { if (generationNum && !currentCar) return scores[index]; @@ -69,10 +73,6 @@ float GeneticAlgorithm::getScore(const int index) { return scores[index]; } -int GeneticAlgorithm::getSpringCount(const int index) { - return springsCount[index]; -} - int GeneticAlgorithm::getTime(const int index) { return times[index]; } @@ -102,7 +102,7 @@ void GeneticAlgorithm::init() { colors[i][j][GREEN] = green; colors[i][j][BLUE] = blue; } - springsCount[i] = 0; + offspringsCount[i] = 0; callLists[i] = 0; parentsCallLists[i][0] = 0; parentsCallLists[i][1] = 0; @@ -110,6 +110,7 @@ void GeneticAlgorithm::init() { createCache(); currentCar = -1; generationNum = 0; + mutationRate = 0; } void GeneticAlgorithm::nextCar() { @@ -139,6 +140,11 @@ void GeneticAlgorithm::nextGenetation() { } else if (compareCar(scores[i], times[i], scores[max2], times[max2])) max2 = i; } + bool newRecord = qAbs(scores[max1] - maxScore[maxScore.size() - 1]) >= 1; + if (mutationRate < MAX_MUTATION_RATE && !newRecord) + mutationRate += 0.5; + else if (newRecord) + mutationRate = 0; maxScore.push_back(scores[max1]); avgScore.push_back(total/POP_SIZE); copyChrome(max1, 0); @@ -149,13 +155,13 @@ void GeneticAlgorithm::nextGenetation() { bool queue[POP_SIZE]; for (int i = 0; i < POP_SIZE; i++) { queue[i] = true; - springsCount[i] = 0; + offspringsCount[i] = 0; } for (int i = 0; i < POP_SIZE/2; i++) { int a = getRandomChrome(queue); int b = getRandomChrome(queue); winners[i] = compareCar(scores[a], times[a], scores[b], times[b])? a: b; - springsCount[winners[i]]++; + offspringsCount[winners[i]]++; } crossover(winners[0], winners[1], 2, 3); for (int i = 0; i < POP_SIZE; i++) @@ -164,7 +170,7 @@ void GeneticAlgorithm::nextGenetation() { int parentA = winners[i]; int parentB = getRandomChrome(queue, parentA); crossover(parentA, parentB, i*2, i*2 + 1); - springsCount[parentB]++; + offspringsCount[parentB]++; } mutation(); createCache(); @@ -184,8 +190,8 @@ void GeneticAlgorithm::setScoreAndTime(float score, float time) { //private -bool GeneticAlgorithm::compareCar(const float scoreA, const float timeA, const int scoreB, - const float timeB) { +bool GeneticAlgorithm::compareCar(const float scoreA, const float timeA, + const int scoreB, const float timeB) { if (scoreA > scoreB) return true; if (scoreB > scoreA) @@ -274,7 +280,7 @@ int GeneticAlgorithm::getRandomChrome(bool queue[], const int excluding) { void GeneticAlgorithm::mutation() { for (int i = 2; i < POP_SIZE; i++) { for (int j = 0; j < CROMES_SIZE; j++) { - if (qrand()%100 < MUTATION_RATE) { + if (qrand()%1000 < mutationRate*10.0) { chromes[i][j] = float(qrand())/float(RAND_MAX); int colorIndex = j < 16? j/2: (j - 16)/3; for (int channel = 0; channel < 3; channel++) { diff --git a/geneticalgorithm.h b/geneticalgorithm.h index 1e4628d..2b73e4c 100755 --- a/geneticalgorithm.h +++ b/geneticalgorithm.h @@ -21,8 +21,8 @@ class GeneticAlgorithm: public QObject { int getGenerationNum(); float getMagnitude(const int index); float getMaxScore(const int index); + int getOffspringsCount(const int index); float getScore(const int index); - int getSpringCount(const int index); int getTime(const int index); int getWheelOn(const int index); float getWheelRadius(const int index); @@ -45,12 +45,13 @@ class GeneticAlgorithm: public QObject { static const int POP_SIZE = 32; static const float WHEEL_PROB0 = 0.5; //(0:1] static const int START_WHEELS_GEN = 16; - static const int MUTATION_RATE = 5; + static const int MAX_MUTATION_RATE = 5.0; static const int RED = 0; static const int GREEN = 1; static const int BLUE = 2; - bool compareCar(const float scoreA, const float timeA, const int scoreB, const float timeB); + bool compareCar(const float scoreA, const float timeA, const int scoreB, + const float timeB); void copyChrome(const int parent, const int offspring); void copyChromes(); void createCache(); @@ -73,10 +74,11 @@ class GeneticAlgorithm: public QObject { int callListIndex; unsigned short int colors[POP_SIZE][16][3]; QVector maxScore; + float mutationRate; unsigned short int oldColors[POP_SIZE][16][3]; unsigned int parentsCallLists[POP_SIZE][2]; float scores[POP_SIZE]; - int springsCount[POP_SIZE]; + int offspringsCount[POP_SIZE]; float times[POP_SIZE]; int currentCar; int generationNum; diff --git a/mainwindow.cpp b/mainwindow.cpp index 8c8657d..55093e0 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -96,7 +96,7 @@ void MainWindow::setSpeed(int newSpeed) { void MainWindow::showAbout() { QMessageBox::about(this, tr("About carbox2d"), - tr("

Carbox2d 0.2.1

" + tr("

Carbox2d 0.2.2

" "

Evolution simulator like " "http://boxcar2d.com/

" "

Copyright © 2012 Zagidulin Artyom

" diff --git a/render.cpp b/render.cpp index af6402a..a5ec551 100755 --- a/render.cpp +++ b/render.cpp @@ -270,7 +270,7 @@ void Render::drawTable() { for (int i = 0; i < 32; i++) { qglColor(Qt::black); if (!algorithm->getCarNum() && algorithm->getGenerationNum()) { - switch (algorithm->getSpringCount(i)) { + switch (algorithm->getOffspringsCount(i)) { case 0: qglColor(Qt::red); break; case 1: qglColor(Qt::black);