Skip to content

Commit

Permalink
variable mutation rate
Browse files Browse the repository at this point in the history
  • Loading branch information
macrosoft committed Nov 18, 2012
1 parent d4c2700 commit a9deecb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
28 changes: 17 additions & 11 deletions geneticalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
}
Expand Down Expand Up @@ -102,14 +102,15 @@ 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;
}
createCache();
currentCar = -1;
generationNum = 0;
mutationRate = 0;
}

void GeneticAlgorithm::nextCar() {
Expand Down Expand Up @@ -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);
Expand All @@ -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++)
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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++) {
Expand Down
10 changes: 6 additions & 4 deletions geneticalgorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -73,10 +74,11 @@ class GeneticAlgorithm: public QObject {
int callListIndex;
unsigned short int colors[POP_SIZE][16][3];
QVector<float> 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;
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void MainWindow::setSpeed(int newSpeed) {

void MainWindow::showAbout() {
QMessageBox::about(this, tr("About carbox2d"),
tr("<h2>Carbox2d 0.2.1</h2>"
tr("<h2>Carbox2d 0.2.2</h2>"
"<p>Evolution simulator like "
"<a href='http://boxcar2d.com/'>http://boxcar2d.com/</a></p>"
"<p>Copyright &copy; 2012 Zagidulin Artyom</p>"
Expand Down
2 changes: 1 addition & 1 deletion render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a9deecb

Please sign in to comment.