I have prepared my Genetic Algorithm Example application for upload. I used the QT library to develop the application, but the executable should run on almost any Windows system, whether QT installed or not.
The executable is located HERE.
The source files are located HERE.
A code listing with comments follows, refer to my earlier post on this topic for more information. To build and execute this code, simple create a new QT gui application, and add the relevant files from the source directory.
gaentity.h:
#ifndef GAENTITY_H
#define GAENTITY_H
class GAEntity
{
public:
GAEntity(int maxnumber);
GAEntity();
int solution; //This is the first, and so far only, “chromasome” of the GA entity
int fitness;
};
#endif // GAENTITY_H
gaentity.cpp:
#include “gaentity.h”
GAEntity::GAEntity(int maxnumber)
{
solution = rand() % maxnumber + 1; //All GA entities are initialised with a random value from 1 to the max number.
fitness = 999; //lower fitness values are better, so initialise entity with an impossibly high value
}
GAEntity::GAEntity()
{
solution = rand() % 1000 + 1; // default constructor, assumes max value is 1000
fitness = 999;
}
Main Logic Function:
void MainWindow::runbtnpushed(){
srand ( time(NULL) ); //init time for random number function
QString s = “”;
//init variables from gui:
int targetnumber = ui->targetIN->value(); //The number that the AI is trying to guess
int maxgenerations = ui->maxgenerationsIN->value(); //The max number of generations that the algorithm will run for.
int populationsize = ui->popsizeIN->value(); //The number of entities to create in each generation, the more there are, the more chance they will solve the problem
int bestfitness = 999;
for(int i = 0; i < populationsize;i++){ GAEntity ent = GAEntity(ui->maxnumberIN->value()); //Create I entities and initialise to random value
ent.fitness = abs(targetnumber – ent.solution); //determine fitness (Simply subtract the solution from the targetnumber, and ignore the sign)
population.push_back(ent); //add to population vector
}
int count = 0;
//pick the best two candidates, mate them, produce new population
GAEntity parent1;
GAEntity parent2;
while(bestfitness > 0 && count < maxgenerations){
count++;
parent1.fitness = 999;
parent1.solution = 0;
//choose parents:
for(size_t i = 0; i < population.size();i++){ //lesser fitness is better
GAEntity ent = population[i];
//fitness:
int fitness = abs(targetnumber – abs(ent.solution));
//this code finds the two entities with the highest fitness. This would be an excellent place for improvement!
if(fitness < parent1.fitness){
parent2.fitness = parent1.fitness;
parent2.solution = parent1.solution;
parent1.fitness = ent.fitness;
parent1.solution = ent.solution;
}else if(fitness < parent2.fitness){ parent2.fitness = ent.fitness; parent2.solution = ent.solution; } } //mate parents: GAEntity child; child.solution = (parent1.solution + parent2.solution)/2; //This is the “seed” for the next generation //print data to gui s = “Generation: “; s.append(QString::number(count)); s.append(” Parent 1 Fitness: “); s.append(QString::number(abs(targetnumber – abs(parent1.solution)))); s.append(” Parent 2 Fitness: “); s.append(QString::number(abs(targetnumber – abs(parent2.solution)))); ui->textBrowser->append(s);
if(abs(targetnumber – abs(child.solution) == 0)){ //Solution has been found!
//qDebug() << “Solution found!” << child.solution << ” Generation: ” << count; ui->textBrowser->append(“”);
s = “Solution found! Solution: “;
s.append(QString::number(child.solution));
s.append(” Generation: “);
s.append(QString::number(count));
ui->textBrowser->append(s);
bestfitness = 0;
}
population.clear(); //delete old population
//mutate child and produce new population:
for(int i = 0; i < populationsize;i++){
GAEntity ent;
//Random Mutation:
float rnd1 = static_cast (rand()) / static_cast (RAND_MAX);
int rnd = rand() % 2 + 0;
if(rnd1 > 0.5 && child.solution-rnd >0)
ent.solution = child.solution-rnd;
else
ent.solution = child.solution+rnd;
//eval fitness here:
//ent.fitness = abs(targetnumber – ent.solution);
population.push_back(ent);
}
}
//print out all entities:
for(size_t i = 0; i < population.size();i++){
// qDebug() << “ID: ” << i << ” Solution: ” << population[i].solution << ” Fitness: ” << abs(targetnumber – population[i].solution);//population[i].fitness;
}
}