# C + + realizes simple genetic algorithm

Source: Internet
Author: User
Tags bitset rand

This paper illustrates a simple genetic algorithm for C + + implementation. Share to everyone for your reference. The implementation methods are as follows:

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 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; NT 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<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; }//define crossover operation void Xover (int &a,int &b) {int pos;//randomly generated hybridization point namely the first several components are exchanged for each other pos=rand ()%5+1; in n components, randomly determine the POS 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 (); }

Related Keywords:

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.