Sign in to follow this  
Betrayer_of_Code

Wierd Output on My G.A. Code

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[i].fitness = 100 - (abs(52 - (individual[i].allele[0] + (2 * individual[i].allele[1]) + (3 * individual[i].allele[2]))));
	population.FitSum += individual[i].fitness;
	
	//fout << "Fitness: " << individual[i].fitness << endl;

	if(individual[i].fitness == 100)
	{
	//solve(i);
	fout << "\n\nSOLVED:\n";
	fout << individual[i].allele[0] << endl;
	fout << individual[i].allele[1] << endl;
	fout << individual[i].allele[2] << endl;
	fout << "Generation: " << population.generation;
	fout << "\n+++++++++++++++++++++++++++\n";
	cout << "\nSolved\n";
    system("pause");
	exit(0);
	}
	
	if(individual[i].fitness > (population.FitSum / MAXPOP))
	{
	    individual[i].fitness += 5;
	    sum += 5;
    }
    
    if(individual[i].fitness < (population.FitSum / MAXPOP))
	{
	    individual[i].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[i].probability = individual[i].fitness + population.ProbSum;
    population.ProbSum += individual[i].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[i].probability && RandFather < individual[i + 1].probability || RandFather == individual[i].probability)
    {
        //cout << "\nDad\n";
        father = true;
        int trans = 0;
        while(trans < 3)
        {
            if(fmalleles[trans] == true)
            {
            newpop[pop].allele[trans] = individual[i].allele[trans];
            }
            trans++;
        }        
    }
    
    if(RandMother > individual[i].probability && RandMother < individual[i + 1].probability || RandMother == individual[i].probability)
    {
        //cout << "\nMom\n";
        mother = true;
   	    int trans = 0;
        while(trans < 3)
        {
            if(fmalleles[trans] == false)
            {
            newpop[pop].allele[trans] = individual[i].allele[trans];
            }
            trans++;
        }   
    }
   
    }

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

void start_pop()
{
    srand(GetTickCount());
    
    for(int i = 0; i < (MAXPOP); i++)
    {
        for(int j = 0; j < 3; j++)
        {
        individual[i].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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this