View Single Post
08-10-2005, 06:46 AM
 Guest Posts: n/a
Re: Where can i find GeneticFPGA toolkit

Hi Eric
Thanks for the code. It is quite complicated as compared to the psuedo
code. I tried making my own application in C#.It works sometimes but
sometimes it just enters an idefinite loop.This is when my population
values go beyond the right hand side constant value(ax + b = c
'value of constant c').I have a few questions for you:
1. What if you get the answer in the first population, the
corresponding normalised value would be infinite?
2.Are there any special conditions you need to check before populating
the list?

Here is my code which is based on your psuedo code.see if it makes
sense to you.
private void ImplementGA()
{
Random rnd = new Random();
int popsize=6;
int[] population = new int[popsize];
int i;
bool Found=false;
DateTime dt= new DateTime();
double a,b,c;
a=double.Parse(CoefficientTextBox.Text);
b=double.Parse(Constant1TextBox.Text);
c=double.Parse(Constant2TextBox.Text);

//Step 1
//Generate a random population
for(i=0;i<popsize;i++)
{
population.SetValue((int)(rnd.NextDouble()*10),i);
}

//Step 2
//Fitness Function
double[] fitness = new double[popsize];
//(ax + b = c)
int x;
for(i=0;i<popsize;i++)
{
//fitness = ABS(1 - ((ax + b)/c))
x=population[i];
fitness[i] = Math.Abs(1 - ((a*x + b)/c));
}

//My idea to check before repopulating
//Check for the end condition
for(i=0;i<popsize;i++)
{
if(fitness[i] == 0)
{
Result=population[i];
Found=true;
}
//else
//Found=false;
}

while(Found == false)
{
//Step 3
//Repopulate the population based on the fitness function
double ErrorTotal=0, NormalisedError=0;
double[] normalised = new double[popsize];
int[] RepopulationPercentage = new int[popsize];
for(i=0;i<popsize;i++)
{
ErrorTotal = ErrorTotal + fitness[i];

}
//normlization

for(i=0;i<popsize;i++)
{
normalised[i] = ErrorTotal / fitness[i];
}
//total of normalised error
for(i=0;i<popsize;i++)
{
NormalisedError = NormalisedError + normalised[i];
}
if(NormalisedError > 99999999)
Flag = true;
//Repopulation percentage
for(i=0;i<popsize;i++)
{
RepopulationPercentage[i] = (int)((normalised[i] /
NormalisedError) * 100);
}

//Elitism - copy the best solution to the next generation
int Best=100;
for(i=0;i<popsize;i++)
{
if(Flag==false)
{
if(Best>=RepopulationPercentage[i])
{
Best=population[i];
}
}
else if(fitness[i] == 0)
{
Best=population[i];
}

}

//Step 4
//Repopulate - Only mutation is being used
population[0]=Best;

//Crossover
for(i=1;i<popsize-1;i++) //elements
{
population.SetValue((int)((population[i]+population[i+1])/2),i);
}
population[popsize-1]=Best + 1;

//Mutation
for(i=1;i<popsize;i=i+2) //elements
{
if(dt.Second%2 == 0)
population.SetValue((int)(population[i]+1),i);
else
population.SetValue((int)(population[i]-1),i);

}

listBox1.Items.Add("Above list is the final population");

//Calculate the fitness of the new population
for(i=0;i<popsize;i++)
{
//fitness = ABS(1 - ((ax + b)/c))
x=population[i];
fitness[i] = Math.Abs(1 - ((a*x + b)/c));
}

//Check for the end condition
for(i=0;i<popsize;i++)
{
if(fitness[i] == 0)
{
Result=population[i];
Found=true;
}
//else
// Found=false;
}
}