Wierd Output on My G.A. Code

Started by
-1 comments, last by Betrayer_of_Code 19 years, 5 months ago
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.probability || RandFather == individual<span style="font-weight:bold;">.probability)
    {
        <span class="cpp-comment">//cout &lt;&lt; "\nDad\n";</span>
        father = <span class="cpp-keyword">true</span>;
        <span class="cpp-keyword">int</span> trans = <span class="cpp-number">0</span>;
        <span class="cpp-keyword">while</span>(trans &lt; <span class="cpp-number">3</span>)
        {
            <span class="cpp-keyword">if</span>(fmalleles[trans] == <span class="cpp-keyword">true</span>)
            {
            newpop[pop].allele[trans] = individual<span style="font-weight:bold;">.allele[trans];
            }
            trans++;
        }        
    }
    
    <span class="cpp-keyword">if</span>(RandMother &gt; individual<span style="font-weight:bold;">.probability &amp;&amp; RandMother &lt; individual.probability || RandMother == individual<span style="font-weight:bold;">.probability)
    {
        <span class="cpp-comment">//cout &lt;&lt; "\nMom\n";</span>
        mother = <span class="cpp-keyword">true</span>;
   	    <span class="cpp-keyword">int</span> trans = <span class="cpp-number">0</span>;
        <span class="cpp-keyword">while</span>(trans &lt; <span class="cpp-number">3</span>)
        {
            <span class="cpp-keyword">if</span>(fmalleles[trans] == <span class="cpp-keyword">false</span>)
            {
            newpop[pop].allele[trans] = individual<span style="font-weight:bold;">.allele[trans];
            }
            trans++;
        }   
    }
   
    }

<span class="cpp-keyword">if</span>(mother == <span class="cpp-keyword">false</span> || father == <span class="cpp-keyword">false</span>)
{
    pop -= <span class="cpp-number">1</span>;
}
pop++;
} 
    <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; (MAXPOP); i++)
    {
    individual<span style="font-weight:bold;"> = newpop<span style="font-weight:bold;">;
    }                    
    <span class="cpp-keyword">return</span>;
}

<span class="cpp-keyword">void</span> start_pop()
{
    srand(GetTickCount());
    
    <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; (MAXPOP); i++)
    {
        <span class="cpp-keyword">for</span>(<span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; <span class="cpp-number">3</span>; j++)
        {
        individual<span style="font-weight:bold;">.allele[j] = rand() % <span class="cpp-number">10</span> + <span class="cpp-number">1</span>;
        }       
    }
<span class="cpp-keyword">return</span>;
} 

<span class="cpp-keyword">void</span> mutate()
{
    <span class="cpp-keyword">int</span> pop = <span class="cpp-number">0</span>;
    <span class="cpp-keyword">while</span>(pop &lt; MAXPOP)
    {
        <span class="cpp-keyword">int</span> allele = rand() % <span class="cpp-number">3</span>;
        <span class="cpp-keyword">int</span> pm = rand() % <span class="cpp-number">1</span>;
        <span class="cpp-keyword">int</span> mutate = rand() % <span class="cpp-number">50</span>;
        
        <span class="cpp-keyword">if</span>(mutate == <span class="cpp-number">2</span>  || mutate == <span class="cpp-number">21</span>)
        {
            individual[pop].allele[allele] = rand() % <span class="cpp-number">10</span>;
            fout &lt;&lt; <span class="cpp-literal">"\nindividual["</span> &lt;&lt; pop &lt;&lt; <span class="cpp-literal">"] mutated "</span> &lt;&lt; allele &lt;&lt; <span class="cpp-literal">" allele"</span> &lt;&lt; endl;
        }
        
        <span class="cpp-keyword">if</span>(mutate == <span class="cpp-number">5</span> || mutate == <span class="cpp-number">15</span>)
        {
            fout &lt;&lt; <span class="cpp-literal">"\nindividual["</span> &lt;&lt; pop &lt;&lt; <span class="cpp-literal">"] mutated"</span>;
            <span class="cpp-keyword">if</span>(pm == <span class="cpp-number">0</span>)
            {
                individual[pop].allele[allele] += <span class="cpp-number">1</span>;
            }
            
            <span class="cpp-keyword">else</span>
            {
                individual[pop].allele[allele] -= <span class="cpp-number">1</span>;
            }
            fout &lt;&lt; endl;
        }            
    pop++;
    }
<span class="cpp-keyword">return</span>;
}           


</pre></div><!–ENDSCRIPT–> 

<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - Betrayer_of_Code on November 13, 2004 9:31:51 PM]<!–EDIT–></span><!–/EDIT–>
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.

This topic is closed to new replies.

Advertisement