C + + realizes simple genetic algorithm _c language

Source: Internet
Author: User
Tags bitset rand

This article illustrates a simple genetic algorithm for C + + implementation. Share to everyone for your reference. The implementation method is as follows:

Genetic algorithm GA #include <iostream> #include <cstdlib> #include <bitset> using namespace std; const int l=5;  Defines the length of the encoded int f (int x)//define the 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);//define the domain scope of x defined by const int pop_size=8;//define population size//int L;//Specify encoded length const int ng=20; number int t=0; Current propagation of algebraic int p[pop_size]; Define population int Q[pop_size]; The Next Generation Srand (6553) defining the breeding population, i.e. the population; To define the seed of random number generation double sum; Fitness sum double avl_sum; Moderate average value double p_probability[pop_size];
Fitness probability double pp[pop_size]; Double Pro; Define the probability of randomly generated float pc=0.90; Define cross probability float pm=0.05; 
The probability of defining variation cout<< "initial population";
  for (int i=0;i<pop_size;i++)//Generate initial No. 0 generation population {p[i]=rand ()%31; cout<<p[i]<< "";
   } cout<<endl;
   cout<<endl; void Xover (int &,int &); Declaring the Cross function//when the stop criterion is not satisfied that the propagation algebra is not to the maximum algebra, continues to reproduce while (t<=ng) {cout<< "reproducing algebra: 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++)//Compute sum sum +=f (p[i]);
 Avl_sum=sum/pop_size;
 cout<< "sum=" <<sum<<endl; 
    cout<< "moderate average value =" <<avl_sum<<endl; for (int i=0;i<pop_size;i++)//Calculate the fitness probability {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; //select parent 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];
///cross 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 number in "0,1" interval if (PRO&L      
T;PM) {bitset<l>v (p[i]);
V.flip (j);
P[i]=v.to_ulong ();
}} t++; cout<<endl;  
 Population breeding Generation} cout<< "final result:";
  for (int i (0); i<pop_size;i++)//algorithm end, output {cout<<p[i]<< "";
} cout<<endl;
return 0; }//Definition crossover operation void Xover (int &a,int &b) {int pos;////randomly generated hybrid point that is the first component to Exchange Pos=rand ()%5+1;
  OS component int j,k;
  J=pos;
K=pos;
Bitset<l>e (a); Bitset<l>f (b);            
The former POS components are exchanged for 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 ();

 }

I hope this article will help you with your C + + programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.