View Single Post
  #14 (permalink)  
Old 08-10-2005, 06:46 AM
Guest
 
Posts: n/a
Default 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.
I really appreciate your help.
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
listBox1.Items.Add("Initial Population");
for(i=0;i<popsize;i++)
{
population.SetValue((int)(rnd.NextDouble()*10),i);
listBox1.Items.Add(population[i]);
}
listBox1.Items.Add("");
listBox1.Items.Add("");





//Step 2
//Fitness Function
listBox1.Items.Add("Corresponding Fitness Values");
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));
listBox1.Items.Add(fitness[i]);
}
listBox1.Items.Add("");
listBox1.Items.Add("");





//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];

}
listBox1.Items.Add("Total Error = "+ErrorTotal);
//normlization
listBox1.Items.Add("");
listBox1.Items.Add("");
listBox1.Items.Add("");

listBox1.Items.Add("Normalisation values");
for(i=0;i<popsize;i++)
{
normalised[i] = ErrorTotal / fitness[i];
listBox1.Items.Add(normalised[i]);
}
//total of normalised error
for(i=0;i<popsize;i++)
{
NormalisedError = NormalisedError + normalised[i];
}
if(NormalisedError > 99999999)
Flag = true;
listBox1.Items.Add("");
listBox1.Items.Add("");
listBox1.Items.Add("Normalised Error = "+NormalisedError);
//Repopulation percentage
listBox1.Items.Add("");
listBox1.Items.Add("");
listBox1.Items.Add("Repopulation percentage");
for(i=0;i<popsize;i++)
{
RepopulationPercentage[i] = (int)((normalised[i] /
NormalisedError) * 100);
listBox1.Items.Add(RepopulationPercentage[i]);
}


//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];
}

}
listBox1.Items.Add("Best = "+Best);



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

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




//Mutation
listBox1.Items.Add("After Mutation");
listBox1.Items.Add(population[0]);
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(population[i]);
}

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




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




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

//Display the answer
AnswerTextBox.Text = Result.ToString();
}

Reply With Quote