C++ implements simple genetic algorithm

  • 2020-04-02 03:07:25
  • OfStack

This paper illustrates the C++ implementation of simple genetic algorithm. Share with you for your reference. The specific implementation method is as follows:


//Genetic algorithm GA
#include<iostream>
#include <cstdlib>
#include<bitset>
using namespace std;
const int L=5; //Define the length of the code
int f(int x) //Define the test function f(x).
{
int result;
result=x*x*x-60*x*x+900*x+100;
return result;
}
int main(int argc,char *argv[])
{
int a(0),b(32); //I'm going to define the domain of x
const int pop_size=8; //Define population size
// int L; // Specifies the length of the encoding  
const int NG=20; //The algebra that specifies the maximum reproduction of a population
int t=0; //Algebra of current propagation
int p[pop_size]; //Define the population
int q[pop_size]; //Define a breeding population as the next generation of the population
srand(6553); //Defines the seed for random number generation
double sum; //That sum
double avl_sum; //Moderate average
double p_probability[pop_size]; //The probability of that
double pp[pop_size];
double pro; //Define the probability of random generation
float pc=0.90; //Define the probability of crossing
float pm=0.05; //Define the probability of variation
cout<<" Initial population  "; 
for(int i=0;i<pop_size;i++) //Generation 0 population
  {
p[i]=rand()%31;
cout<<p[i]<<" ";
  }
   cout<<endl;
   cout<<endl;
   void Xover(int &,int &); //Declared cross function
//When the stop criterion is not satisfied, that is, the reproduction algebra does not reach the maximum algebra, the reproduction continues
while(t<=NG)             
{ 
cout<<" Algebra of reproduction: t="<<t<<endl;
sum=0.0;
for(int i=0;i<pop_size;i++)      
  {
q[i]=p[i];
cout<<q[i]<<" ";
  }
  cout<<endl;
 for(int i=0;i<pop_size;i++) //Calculate the sum
   sum +=f(p[i]);
 avl_sum=sum/pop_size;
 cout<<"sum="<<sum<<endl;
 cout<<" Moderate average ="<<avl_sum<<endl; 
    for(int i=0;i<pop_size;i++) // To calculate The probability of that
    {
      p_probability[i]=f(p[i])/sum;
if(i==0)
{
pp[i]=p_probability[i];
cout<<"pp"<<i<<"="<<pp[i]<<endl;
}
      else
      {
       pp[i]=p_probability[i]+pp[i-1];
    cout<<"pp"<<i<<"="<<pp[i]<<endl;
      }
//cout<<"p_probability"<<i<<"="<<p_probability[i]<<endl;
    }
    //Choose parents
     for(int i=0;i<pop_size;i++) 
     {
     pro=rand()%1000/1000.0;
if(pro>=pp[0]&&pro<pp[1])
  p[i]=q[0]; 
else if(pro>=pp[1]&&pro<pp[2])
       p[i]=q[1];
     else if(pro>=pp[2]&&pro<pp[3])
       p[i]=q[2];
     else if(pro>=pp[3]&&pro<pp[4])
       p[i]=q[3];
     else if(pro>=pp[4]&&pro<pp[5])
       p[i]=q[4];
     else 
       p[i]=q[5]; 
     }
//Hybrid operator
int r=0;
int z=0; 
for(int j=0;j<pop_size;j++) 
{
  pro=rand()%1000/1000.0;
if(pro<pc)
{
 ++z;
 if(z%2==0)
  Xover(p[r],p[j]);
 else
  r=j; 
} 
} 
//Mutation operator
for(int i=1;i<=pop_size;i++)
 for(int j=0;j<L;j++)
{
 pro=rand()%1000/1000.0; //Generate random Numbers in the interval [0,1]
if(pro<pm)
{
bitset<L>v(p[i]);      
v.flip(j);
p[i]=v.to_ulong();
} 
 } 
t++;
cout<<endl; //Population generation
 }
 cout<<" Final results: ";  
 for(int i(0);i<pop_size;i++) //The algorithm ends, output the result
  {
 cout<<p[i]<<" ";
  }
  cout<<endl;
return 0;
}
//Define hybridization operation
 void Xover(int &a,int &b)         
{ 
int pos; //Randomly generated hybridization points are the number of components to exchange with each other
pos=rand()%5+1; //Of the n components, the pos component is randomly determined
int j,k;
  j=pos;
  k=pos;
bitset<L>e(a);
bitset<L>f(b); //The first pos components exchange with each other
bitset<L>g;            
bitset<L>h;
for(int i=0;i<pos;i++)
{
if(e[i]==1)
 g.set(i); 
   }
  for(int i=0;i<pos;i++)
   {
   if(f[i]==1)
    h.set(i);
   }
  for(j;j<L;j++)
   {
   if(f[j]==1)
    g.set(j);
   }
  for(k;k<L;k++)
   {
   if(e[k]==1)
    h.set(k);
   }
a=g.to_ulong();
b=h.to_ulong();  
}

Hope that the article described in the C++ programming to help you.


Related articles: