View Single Post
  #12 (permalink)  
Old 08-08-2005, 02:58 PM
Eric
Guest
 
Posts: n/a
Default Re: Where can i find GeneticFPGA toolkit

//-------Creates Initial Population
-------------------------------------------------
for (int k=0; k < Popsize; k++)
{
for(int h=0; h < max; h++)
{
Population[k].SingleVector[h]=rand() % 2;
}
StoreCover[k]=evaluate(Faults, Population[k].SingleVector);

}
//--- Genetic Algorithm Iterations
-------------------------------------------------------
for (int y=0; y < 10; y++) //---- 10 itertions
{
total=0;
//------Repopulation Based on Coverage Criterian
-------------------------------------
posistion=1;
value1=0;
value2=0;
value3=0;
for (k=0; k < max; k++)
Population[0].repopulation[k]=Population[0].SingleVector[k];
// ---- Makes sure non of the values repeat in repopulation
for (k=1; k < Popsize; k++)
{
for (int h=0; h<k ;h++)
{
value1=0; value2=0;
for (int L=0; L < max; L++) if (Population[h].repopulation[L]==1)
value1=value1+ pow(2,L);
for (L=0; L < max; L++) if (Population[k].SingleVector[L]==1)
value2=value2+ pow(2,L);
if (value1 != value2)
{
value3=0;
}else{ value3=1; h=k;}
}
if (value3==0)
{
for (int j=0; j < max; j++)
Population[posistion].repopulation[j]=Population[k].SingleVector[j];
posistion++;
}
}
// cout << "Fixed population \n";
for (k=0; k < posistion; k++)
{
// for (int n=0; n< max; n++)
// {
// cout <<Population[k].repopulation[n];
// }
StoreCover[k]=evaluate(Faults, Population[k].repopulation);
// cout << " " << StoreCover[k] << "\n";
}
// cout << "\n\n\n";
for (k=0; k < posistion; k++)
{
total=total+ StoreCover[k];
}
// cout << "\n\n total=" <<total;

for (int p=0; p<Popsize; p++)
{

random=rand() % 101; //Generates random populations
// cout << "\n";

int start=0, end=0;
for (k=0; k < Popsize; k++)
{
start=end;
end=end+(StoreCover[k]*100/total);
if ((random>=start) && (random<end))
{
for (int n=0; n< max; n++)
{
Population[p].NewVector[n]=Population[k].repopulation[n];
// cout <<Population[k].SingleVector[n];
}
}
}
}
// cout <<"\n";
for (k=0; k < Popsize; k++) //--Swaps NewVector with SingleVector
{
for (int n=0; n< max; n++)
{
Population[k].SingleVector[n]=Population[k].NewVector[n];

// cout <<Population[k].SingleVector[n];
}
StoreCover[k]=evaluate(Faults, Population[k].SingleVector);
// cout << " " << StoreCover[k] << "\n";
}
//------------------------------------------------------------------------------------
// ------ Cross Overs
----------------------------------------------------------------
int random1=rand() % Popsize; //Find 1st vector to swap
int random2=rand() % Popsize; //Find second Vector to swap
int random3=rand() % max; //Finds place to cut
int temp=0;
// cout << "Cross Over " << random1 << " " << random2 << " " <<
random3 <<"\n";
for (int n=0; n< random3; n++)
{
temp=Population[random1].SingleVector[n];
Population[random1].SingleVector[n]=Population[random2].SingleVector[n];
Population[random2].SingleVector[n]=temp;

}
for (k=0; k < Popsize; k++)
{
// for (n=0; n< max; n++)
// {
// cout <<Population[k].SingleVector[n];
// }
StoreCover[k]=evaluate(Faults, Population[k].SingleVector);
// cout << " " << StoreCover[k] << "\n";
}
//------------------------------------------------------------------------------------
// ------ Mutation
----------------------------------------------------------------
random1=rand() % Popsize; //Find 1st vector to swap
random3=rand() % max; //Finds place to swap
// cout << "Mutation " << random1 << " " << random3 <<"\n";

if (Population[random1].SingleVector[random3]==0)
Population[random1].SingleVector[random3]=1;
else Population[random1].SingleVector[random3]=0;

for (k=0; k < Popsize; k++)
{
// for (n=0; n< max; n++)
// {
// cout <<Population[k].SingleVector[n];
// }
StoreCover[k]=evaluate(Faults, Population[k].SingleVector);
// cout << " " << StoreCover[k] << "\n";
}
//------------------------------------------------------------------------------------
}
//-------------Displays final population
---------------------------------------------------
// cout << "Final Set of Vectors\n";
for (k=0; k < posistion; k++) //posistion = number of elements
{
for (int n=0; n< max; n++)
{
// cout <<Population[k].repopulation[n]; //final nonrepeating
elements
}
StoreCover[k]=evaluate(Faults, Population[k].SingleVector);
// cout << " " << StoreCover[k] << "\n";
}
}

Reply With Quote