Jump to content
  • Advertisement
Sign in to follow this  
Betrayer_of_Code

Help with my GA

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on getting this program to work for quite a while. It is supposed to solve the problem "52 = a + 2b + 3c". Please help me to get it to work. It solves the problem sometimes, but other times it just loops the same number until the program ends or it crashes. I am almost positive it is something wrong with the "Choose_Mate()" function
#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()
{

	srand(GetTickCount());
	
    fout.open("stats.txt");
    start_pop();
	
	cout << "Simulating Population\n";
	
    while(1)
	{
	population.FitSum = 0;
	population.ProbSum = 0;
    population.generation++;
	fitness();
	probability();
	choose_mate();
	mutate();
	}
return 0;
}
	

void fitness()
{
    int sum = 0;
    
    if(population.generation > 1000)
    {
        system("pause");
        exit(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;
	

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

    /*for(int i = 0; i < MAXPOP; i++)
	   { 
        if(individual.fitness > (population.FitSum / MAXPOP))
	       {
	           individual.fitness += 20;
	       }
    
        if(individual.fitness < (population.FitSum / MAXPOP))
	       {
	           individual.fitness -= 20;
	       }
        } */   
	return;
}

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

void choose_mate()
{
    INDIVIDUAL newpop[MAXPOP];
	int pop = 0;
	bool fmalleles[3];
    
    while(pop < MAXPOP)
        {
            fmalleles[rand() % 3] = true;
            fmalleles[rand() % 3] = true;
            bool father = false;
            bool mother = false;
            int RandFather = (rand() % population.ProbSum);
            int RandMother = (rand() % population.ProbSum);
        
            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)
                        {
                            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()
{
    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 == 15 || mutate == 21)
                {
                    if(pm == 0)
                        {
                            individual[pop].allele[allele] += 1;
                        }
                    else
                        {
                            individual[pop].allele[allele] -= 1;
                        }    
                }            
            pop++;
        }
    return;
}          


Share this post


Link to post
Share on other sites
Advertisement
At first glance:
- You have an off-by-one error regarding MAXPOP (you access newpop[MAXPOP] in the loop when you shouldn't)
- No idea what 'fmalleles' does - is this your crossover operator, effectively? - but it appears that you fail to initialise the array to begin with and then very quickly turn each of the 3 members to true
- "RandFather > individual.probability && RandFather < individual[i + 1].probability || RandFather == individual.probability" should probably be "RandFather >= individual.probability && RandFather < individual[i + 1].probability"

Personally I would clean up parts of that function by moving key aspects into other distinct functions: eg. one to perform the selection, another one to perform the cross over, etc.

Share this post


Link to post
Share on other sites
Thanks ill try that out now.
I do not understand what you mean here
"You have an off-by-one error regarding MAXPOP (you access newpop[MAXPOP] in the loop when you shouldn't"

Even without that part I didn't understand it works, so far as I can tell 100% of the time now. Thank you a lot.

Share this post


Link to post
Share on other sites
individual[MAXPOP] - you have MAXPOP individuals, from 0 to MAXPOP-1.

for(int i = 0; i < (MAXPOP); i++) - That means i can equal MAXPOP-1.

if(RandFather > individual.probability && RandFather < individual[i + 1] - that means you access individual[MAXPOP-1 + 1], which doesn't exist.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!