Simple implementation and results of genetic algorithm.
This experiment was done for the final assignment of my Professional English class.
This part has been written in haste, please forgive me.
1 #include<stdlib.h> 2 #include<iostream> 3 #include<time.h> 4 #include<Windows.h> 5 #define N 5//种群规模 6 using namespace std; 7 8 int roulettewheelselection(double T[]); 9 10 int main() 11 { 12 double C[5][4]; 13 //double C1[3];//染色体1,以(x1,x2,x3,x4)形式存储 14 //double C2[3];//染色体2 15 //double C3[3];//··· 16 //double C4[3]; 17 //double C5[3]; 18 srand((unsigned)time(NULL));//不同时间种子不同 19 for (int j = 0; j < 5; j++) 20 { 21 for (int i = 0; i < 4; i++) 22 { 23 C[j][i] = rand() / double(RAND_MAX); 24 //C1[i] = rand() / double(RAND_MAX); 25 //C2[i] = rand() / double(RAND_MAX);//0到1的随机值 26 //C3[i] = rand() / double(RAND_MAX);//定义<<0(x1,x2,x3,x4)<<1 27 //C4[i] = rand() / double(RAND_MAX); 28 //C5[i] = rand() / double(RAND_MAX); 29 } 30 } 31 cout << "初始化结果为:" << endl; 32 33 for (int j = 0; j < 5; j++) 34 { 35 for (int i = 0; i < 4; i++) 36 { 37 cout << C[j][i] << endl; 38 /* cout << C1[i] << endl; 39 cout << C2[i] << endl; 40 cout << C3[i] << endl; 41 cout << C4[i] << endl; 42 cout << C5[i] << endl;*/ 43 } 44 } 45 //评估 46 double f[5];//五个染色体对应的函数值 47 double E[5];//五个染色体对应的适应值 48 for (int j = 0; j < 5; j++) 49 { 50 f[j] = 1 / (C[j + 1][0] * C[j + 1][0] + C[j + 1][1] * C[j + 1][1] + C[j + 1][2] * C[j + 1][2] + C[j + 1][3] * C[j + 1][3] + 1); 51 } 52 //f[0] = 1 / (C1[0] * C1[0] + C1[1] * C1[1] + C1[2] * C1[2] + C1[3] * C1[3] + 1);//染色体1的函数值 53 //f[1] = 1 / (C2[0] * C2[0] + C2[1] * C2[1] + C2[2] * C2[2] + C2[3] * C2[3] + 1);//染色体2函数值 54 //f[2] = 1 / (C3[0] * C3[0] + C3[1] * C3[1] + C3[2] * C3[2] + C3[3] * C3[3] + 1);//染色体3的函数值 55 //f[3] = 1 / (C4[0] * C4[0] + C4[1] * C4[1] + C4[2] * C4[2] + C4[3] * C4[3] + 1); 56 //f[4] = 1 / (C5[0] * C5[0] + C5[1] * C5[1] + C5[2] * C5[2] + C5[3] * C5[3] + 1);//染色体5的函数值 57 for (int i = 0; i < 4; i++) 58 { 59 E[i] = 1 / f[i]; 60 } 61 62 double best = 0; 63 for (int i = 0; i < 4; i++) 64 { 65 if (E[i] > best)//找best适应值最大的染色体 66 { 67 best = E[i]; 68 } 69 } 70 cout << "best 为 " << i + 1 << "染色体"<<endl; 71 //选择 72 /* 73 double E_sum;//适用值求和,总适应值 74 for (int i = 0; i < 4; i++) 75 { 76 E_sum = E_sum + E[i]; 77 } 78 */ 79 /* 80 double E_ratio[4];//各染色体适应值与总适应值之比 81 for (int i = 0; i < 4; i++) 82 { 83 E_ratio[i] = E[i]/E_sum; 84 } 85 */ 86 //roulettewheelselection(E); 87 double D[5][4]; 88 for (int j = 0; j < 5; j++)//选择五次,C赋给D,形成新种群 89 { 90 for (int i = 0; i < 4; i++) 91 { 92 D[roulettewheelselection(E)][i] = C[roulettewheelselection(E)][i]; 93 } 94 } 95 //cout << "最终被选择进入种群的染色体是:" << << endl; 96 97 //交配 98 double p = 0.88;//交配概率为0.88 99 srand((unsigned)time(NULL));//不同时间种子不同 100 int i = 0; 101 double s[5]; 102 for(i=0;i<5;i++) 103 { 104 s[i] = rand() / double(RAND_MAX);//0到1的随机值s 105 int m = 0; 106 if (s[i] <= 0.88) 107 { 108 m = m + 1; 109 cout << "染色体" << i + 1 << "参加交配" << endl; 110 } 111 112 } 113 114 115 116 system("pause"); 117 return 0; 118 } 119 120 int roulettewheelselection(double T[])//轮盘赌算法实现(GA算法中) 121 { 122 //int N;//染色体数目 123 124 //http://www.cnblogs.com/guihailiuli/p/4154416.html 125 srand((unsigned)time(NULL));//不同时间种子不同 126 double s = rand() / double(RAND_MAX);//0到1的随机值s 127 128 double m = 0; 129 130 //cout << "请输入染色体数目N(不能超过10哦)" << endl; 131 //cin >> N; 132 //double T[10];//每个染色体体的适应值 133 double P[10];//计算出的概率,决定扇区大小 134 double t = 0;//总适应值,为T[i]的和 135 //cout << "请输入每个染色体的适应值" << endl; 136 for (int i = 0; i < N; i++) 137 { 138 ////输入每个染色体的适应值 139 // cin >> T[i]; 140 t = t + T[i];//求适应值之和 141 } 142 143 for (int i = 0; i < N; i++) 144 { 145 P[i] = T[i] / t;//计算扇区占比 146 } 147 148 for (int i = 0; i < N; i++) 149 { 150 m = m + P[i]; 151 if (m >= s) 152 { 153 return i; 154 break; 155 } 156 } 157 158 }
1 #include<stdlib.h> 2 #include<iostream> 3 #include<time.h> 4 #include<Windows.h> 5 using namespace std; 6 int main() 7 { 8 double D[5][4]; 9 D[0][0] = 0.0182501 10 D[0][1] = 0.286416 11 D[0][2] = 0.730888 12 D[0][3] = 0.99588 13 14 D[2][0] = 0.524155 15 D[2][1] = 0.937254 16 D[2][2] = 0.120579 17 D[2][3] = 0.25721 18 19 D[3][0] = 0.423475 20 D[3][1] = 0.787622 21 D[3][2] = 0.832698 22 D[3][3] = 0.606769 23 24 D[4][0] = 0.0910672 25 D[4][1] = 0.114475 26 D[4][2] = 0.427229 27 D[4][3] = 0.404187 28 //交配位为1.2. 29 //交配 30 D[0][0] = 0.0182501 31 D[0][1] = 0.286416 32 D[0][2] = 0.120579 33 D[0][3] = 0.25721 34 35 D[2][0] = 0.524155 36 D[2][1] = 0.937254 37 D[2][2] = 0.730888 38 D[2][3] = 0.99588 39 40 D[3][0] = 0.423475 41 D[3][1] = 0.787622 42 D[3][2] = 0.427229 43 D[3][3] = 0.404187 44 45 D[4][0] = 0.0910672 46 D[4][1] = 0.114475 47 D[4][2] = 0.832698 48 D[4][3] = 0.606769 49 //染色体2保留 50 D[1][0] = 0.870174 51 D[1][1] = 0.346599 52 D[1][2] = 0.798273 53 D[1][3] = 0.762688 54 //变异 55 p = 0.1;//变异概率为0.1 56 srand((unsigned)time(NULL));//不同时间种子不同 57 double s[5]; 58 for (int i = 0; i < 5; i++) 59 { 60 for (int j = 0; j < 4; j++) 61 { 62 s[i][j] = rand() / double(RAND_MAX);//0到1的随机值s 63 int m = 0; 64 if (s[i][j] < p) 65 { 66 D[i][j]= rand() / double(RAND_MAX); 67 //m = m + 1; 68 //cout << "染色体" << i + 1 << "参加交配" << endl; 69 } 70 } 71 } 72 cout<<"变异后的染色体值为:" 73 for (i = 0; i < 5; i++) 74 { 75 for (j = 0; j < 4; j++) 76 { 77 cout << D[i][j] << endl; 78 } 79 } 80 //重新评价适应值 81 //评估 82 double f[5];//五个染色体对应的函数值 83 double E[5];//五个染色体对应的适应值 84 for (int j = 0; j < 5; j++) 85 { 86 f[j] = 1 / (D[j + 1][0] * D[j + 1][0] + D[j + 1][1] * D[j + 1][1] + D[j + 1][2] * D[j + 1][2] + D[j + 1][3] * D[j + 1][3] + 1); 87 } 88 for (int i = 0; i < 4; i++) 89 { 90 E[i] = 1 / f[i]; 91 } 92 93 double best = 0; 94 for (int i = 0; i < 4; i++) 95 { 96 if (E[i] > best)//找best适应值最大的染色体 97 { 98 best = E[i]; 99 } 100 } 101 cout << "best 为 " << i + 1 << "染色体" << endl; 102 103 system("pause"); 104 return 0; 105 }