Hi all,

I'm having trouble with a genetic algorithm. Basically I want to be able to keep 10 of the original population if a certain variable (Elitism) is set to one. This also has to change the size of the population. This is the code thats comes first in the function wether or not elitism is set to one.

the timesby is simply to scale the number of offspring depending on the size of the initial population. Directly following this is the code to deal with elitism.Code:int count=0;//counter for changing population int timesby = 1;//scale factor for population size int numkids1 = 13*timesby;//number of offspring based on fitness int numkids2 = 12*timesby; int numkids3 = 11*timesby; int numkids4 = 10*timesby; int numkids5 = 9*timesby; int numkids6 = 8*timesby; int numkids7 = 7*timesby; int numkids8 = 6*timesby; int numkids9 = 5*timesby; int numkids10 = 4*timesby; int leftovers = 15*timesby;//number to be made up by the rest of the pop if(PopSize == 200)//scale the number of kids available timesby = 2; if(PopSize == 500) timesby = 5; if(PopSize == 1000) timesby = 10; if(PopSize == 5000) timesby = 50; if(PopSize == 10000) timesby = 100;

Whenever I run this with elitism turned on I get junk values. However if I declare the numkids variables within the elitism condition i.e.Code:if(Elitist == 1)//if elitism is on { count = 10;//then don't alter the first ten lines numkids1 = 12*timesby;//then lower offspring count to take into account numkids2 = 11*timesby;//that we will keep the original 10. numkids3 = 10*timesby; numkids4 = 9*timesby; numkids5 = 8*timesby; numkids6 = 7*timesby; numkids7 = 6*timesby; numkids8 = 5*timesby; numkids9 = 4*timesby; numkids10 = 3*timesby; leftovers = 15*timesby; for(int i=0; i<10; i++) NextGen[i] = LineArray[i]; //copy the ten best to the next gen array }

It works fine. I'm not sure which value its using for the subsequent calculations though. My question is why is this not bringing up a redefinition error when its compiled. It is all defined within one function. I really don't understand this so any help would be greatly appreciated.Code:if(Elitist == 1)//if elitism is on { int count = 10;//then don't alter the first ten lines int numkids1 = 12*timesby;//then lower offspring count to take into account int numkids2 = 11*timesby;//that we will keep the original 10. int numkids3 = 10*timesby; int numkids4 = 9*timesby; int numkids5 = 8*timesby; int numkids6 = 7*timesby; int numkids7 = 6*timesby; int numkids8 = 5*timesby; int numkids9 = 4*timesby; int numkids10 = 3*timesby; int leftovers = 15*timesby; for(int i=0; i<10; i++) NextGen[i] = LineArray[i]; //copy the ten best to the next gen array }

Cheers

DYlan

p.s. Though I had better enclose the whole function just in case;

Code:void SetUpNextGeneration(CLine LineArray[], CLine NextGen[], int Elitist, int MutationRate, int PopSize) { int count=0;//counter for changing population int timesby = 1;//scale factor for population size int numkids1 = 13*timesby;//number of offspring based on fitness int numkids2 = 12*timesby; int numkids3 = 11*timesby; int numkids4 = 10*timesby; int numkids5 = 9*timesby; int numkids6 = 8*timesby; int numkids7 = 7*timesby; int numkids8 = 6*timesby; int numkids9 = 5*timesby; int numkids10 = 4*timesby; int leftovers = 15*timesby;//number to be made up by the rest of the pop if(PopSize == 200)//scale the number of kids available timesby = 2; if(PopSize == 500) timesby = 5; if(PopSize == 1000) timesby = 10; if(PopSize == 5000) timesby = 50; if(PopSize == 10000) timesby = 100; if(Elitist == 1)//if elitism is on { count = 10;//then don't alter the first ten lines numkids1 = 12*timesby;//then lower offspring count to take into account numkids2 = 11*timesby;//that we will keep the original 10. numkids3 = 10*timesby; numkids4 = 9*timesby; numkids5 = 8*timesby; numkids6 = 7*timesby; numkids7 = 6*timesby; numkids8 = 5*timesby; numkids9 = 4*timesby; numkids10 = 3*timesby; leftovers = 15*timesby; for(int i=0; i<10; i++) NextGen[i] = LineArray[i]; //copy the ten best to the next gen array } count = MakeLitter(LineArray[0], LineArray, numkids1, count, 0, NextGen); count = MakeLitter(LineArray[1], LineArray, numkids2, count, 1, NextGen); count = MakeLitter(LineArray[2], LineArray, numkids3, count, 2, NextGen); count = MakeLitter(LineArray[3], LineArray, numkids4, count, 3, NextGen); count = MakeLitter(LineArray[4], LineArray, numkids5, count, 4, NextGen); count = MakeLitter(LineArray[5], LineArray, numkids6, count, 5, NextGen); count = MakeLitter(LineArray[6], LineArray, numkids7, count, 6, NextGen); count = MakeLitter(LineArray[7], LineArray, numkids8, count, 7, NextGen); count = MakeLitter(LineArray[8], LineArray, numkids9, count, 8, NextGen); count = MakeLitter(LineArray[9], LineArray, numkids10, count, 9, NextGen); int startat = 0;//see below do { int Parent1 = rand()%1001;//get a random first parent count = MakeLitter(LineArray[Parent1], LineArray, 1, count, 0, NextGen); startat++; }while(startat<leftovers); }