# Wierd Output on My G.A. Code

This topic is 4998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote a some code to solve the problem x = a + 2b + 3c, where I can change the value of x fairly easily. When I run the code (with x = to 52 currently) I get a fairly normal log, except for one thing...Right in the middle, EVERY time I get a string of "Average Fitness" that are the same. No mutations occur in this set of about 100 generations. After that and before that mutations occur normally? Is there a reason for this? here is my code. Yes i realize i shouldnt use namespaces. I think my computer gets board :D
#include <iostream>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <windows.h>

using namespace std;

#define MAXPOP 10
#define MAXALL 3
// 52 = a + 2b + 3c

ofstream fout;
void start_pop(), fitness(), probability(), choose_mate(), mutate();

class POPULATION
{
public:
int FitSum;
int ProbSum;
int generation;
}population;

class INDIVIDUAL
{
public:
int fitness;
int probability;
int allele[3];

int mother;
int father;

}individual[MAXPOP];

int main()
{

fout.open("stats.txt");
start_pop();

while(1)
{
population.FitSum = 0;
population.ProbSum = 0;
population.generation++;
cout << population.generation << endl;
fitness();
probability();
choose_mate();
mutate();
//system("pause");
}
return 0;
}

void fitness()
{
if(population.generation > 500)
{
system("pause");
exit(0);
}

int sum = 0;
for(int i = 0; i < MAXPOP; i++)
{
individual.fitness = 100 - (abs(52 - (individual.allele[0] + (2 * individual.allele[1]) + (3 * individual.allele[2]))));
population.FitSum += individual.fitness;

//fout << "Fitness: " << individual.fitness << endl;

if(individual.fitness == 100)
{
//solve(i);
fout << "\n\nSOLVED:\n";
fout << individual.allele[0] << endl;
fout << individual.allele[1] << endl;
fout << individual.allele[2] << endl;
fout << "Generation: " << population.generation;
fout << "\n+++++++++++++++++++++++++++\n";
cout << "\nSolved\n";
system("pause");
exit(0);
}

if(individual.fitness > (population.FitSum / MAXPOP))
{
individual.fitness += 5;
sum += 5;
}

if(individual.fitness < (population.FitSum / MAXPOP))
{
individual.fitness -= 5;
sum -= 5;
}
}
population.FitSum += sum;

fout << "Average Fitness: " << (population.FitSum / MAXPOP) << endl;

return;
}

void probability()
{
for(int i = 0; i < (MAXPOP); i++)
{
individual.probability = individual.fitness + population.ProbSum;
population.ProbSum += individual.fitness;
}

fout << "Probability:     " << (population.ProbSum / MAXPOP) << endl;

return;
}

void choose_mate()
{
srand(GetTickCount());

INDIVIDUAL newpop[MAXPOP];
int pop = 0;

while(pop < MAXPOP)
{

bool fmalleles[3];

fmalleles[rand() % 3] = true;
fmalleles[rand() % 3] = true;

bool father = false;
bool mother = false;
int RandFather = (rand() % population.ProbSum);
//cout << endl << RandFather;
int RandMother = (rand() % population.ProbSum);
//cout << endl << RandMother << endl;
for(int i = 0; i < (MAXPOP); i++)
{
if(RandFather > individual.probability && RandFather < individual[i + 1].probability || RandFather == individual.probability)
{
father = true;
int trans = 0;
while(trans < 3)
{
if(fmalleles[trans] == true)
{
newpop[pop].allele[trans] = individual.allele[trans];
}
trans++;
}
}

if(RandMother > individual.probability && RandMother < individual[i + 1].probability || RandMother == individual.probability)
{
//cout << "\nMom\n";
mother = true;
int trans = 0;
while(trans < 3)
{
if(fmalleles[trans] == false)
{
newpop[pop].allele[trans] = individual.allele[trans];
}
trans++;
}
}

}

if(mother == false || father == false)
{
pop -= 1;
}
pop++;
}
for(int i = 0; i < (MAXPOP); i++)
{
individual = newpop;
}
return;
}

void start_pop()
{
srand(GetTickCount());

for(int i = 0; i < (MAXPOP); i++)
{
for(int j = 0; j < 3; j++)
{
individual.allele[j] = rand() % 10 + 1;
}
}
return;
}

void mutate()
{
int pop = 0;
while(pop < MAXPOP)
{
int allele = rand() % 3;
int pm = rand() % 1;
int mutate = rand() % 50;

if(mutate == 2  || mutate == 21)
{
individual[pop].allele[allele] = rand() % 10;
fout << "\nindividual[" << pop << "] mutated " << allele << " allele" << endl;
}

if(mutate == 5 || mutate == 15)
{
fout << "\nindividual[" << pop << "] mutated";
if(pm == 0)
{
individual[pop].allele[allele] += 1;
}

else
{
individual[pop].allele[allele] -= 1;
}
fout << endl;
}
pop++;
}
return;
}


[Edited by - Betrayer_of_Code on November 13, 2004 9:31:51 PM]

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002290
×