遗传算法求x^2+y^2的最大值,C++实现

最近和学院的教授开始学习数据挖掘和机器学习的课程,第一步就是学习简单的遗传算法。

在网上找了篇教程,于是将其实现。文章地址非常好的理解遗传算法的例子

内容是求求f(x1,x2)=x1^2+x2^2的最大值 ,x1∈{1,2,3,4,5,6,7} , x2∈{1,2,3,4,5,6,7} 。

选择样本数量为8,经过100代的迭代,效果还不错

代码如下:

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
43
44
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
84
85
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
119
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
** 遗传算法求f(x1,x2)=x1^2+x2^2的最大值
** x1∈{1,2,3,4,5,6,7} , x2∈{1,2,3,4,5,6,7}
** @Jet-Muffin
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
using namespace std;
#define SAMPLE_NUM 8 //样本数量
#define EVOLVE_NUM 300 //进化代数
#define random(x) (rand()%x) //随机函数
#define VARIATE_RATE 10 //变异率
void init();
void output();
void cross();
void cross_change(int a,int b);
//样本node
struct node{
int bin[6];
int x1,x2;
int f;
double pct;
}group[SAMPLE_NUM];
//样本集合group
node temp[SAMPLE_NUM]; //操作中间变量数组
int choosen[SAMPLE_NUM]; //概率分布区域数组
//初始化,随机生成样本
void init() {
for(int i = 0; i < SAMPLE_NUM; i++) {
//随机生成bin数组
for(int j = 0; j < 6; j++) {
int pct = random(100);
if(pct >= 50)
group[i].bin[j] = 1;
else
group[i].bin[j] = 0;
}
}
}
//计算相应数值
void calcu() {
for(int i = 0; i < SAMPLE_NUM; i++) {
//生成相应x1、x2
int base = 1;
group[i].x1 = group[i].x2 = 0;
for(int j = 6 / 2 - 1; j >= 0; j--) {
group[i].x1 += base * group[i].bin[j];
base *= 2;
}
base = 1;
for(int j = 6 - 1; j >= 6 / 2; j--) {
group[i].x2 += base * group[i].bin[j];
base *= 2;
}
//计算相应适值
group[i].f = group[i].x1 * group[i].x1 + group[i].x2 * group[i].x2;
//计算占总数百分比
int totf = 0;
for(int i = 0; i < SAMPLE_NUM; i++)
totf += group[i].f;
for(int i = 0; i < SAMPLE_NUM; i++)
group[i].pct = group[i].f / (double)totf;
}
}
//选择过程
void choose() {
cout<<endl;
cout<<"Choosen:......."<<endl;
memset(choosen,0,sizeof(choosen));
//计算概率区域
choosen[0] = floor(group[0].pct * 100 + 0.5);
for(int i = 1; i < SAMPLE_NUM - 1; i++) {
choosen[i] = choosen[i-1] + floor(group[i].pct * 100 + 0.5);
}
choosen[SAMPLE_NUM - 1] = 100;
cout<<"Posibility Area: ";
for(int i = 0; i < SAMPLE_NUM; i++)
cout<<choosen[i]<<" ";
cout<<endl;
//生成随机数以模拟选择
for(int i = 0; i < SAMPLE_NUM; i++) {
int pct = random(100);
int choosen_node;
for(int j = 1; j < SAMPLE_NUM; j++)
if(pct < choosen[j]) {
choosen_node = j;
break;
}
cout<<"seed:"<<pct<<", ";
cout<<"node "<<choosen_node + 1<<" was choosen"<<endl;
temp[i] = group[choosen_node];
}
//输出选择结果
cout<<"choosen result:"<<endl;
for(int i = 0; i < SAMPLE_NUM; i++) {
cout<<"temp["<<i + 1<<"]: ";
for(int j = 0; j < 6; j++)
cout<<temp[i].bin[j]<<" ";
cout<<endl;
}
}
//交叉运算
void cross() {
cout<<endl;
cout<<"Crossing:...."<<endl;
cout<<"Matching:..."<<endl;
//用vector的random_shuffle实现随机乱序配对,从vector中取两个node就进行运算,实现随机交叉
vector<int> seeds;
for (int i = 0; i < SAMPLE_NUM; i++)
seeds.push_back(i + 1);
random_shuffle(seeds.begin(), seeds.end());
int cnt = 0;
int pair_fst,pair_sec;
for(vector<int>::iterator it = seeds.begin(); it != seeds.end(); it++) {
cnt++;
if(cnt&1)
pair_fst = *it;
else
pair_sec = *it;
if(cnt == 2) {
cout<<"Matching result: "<<pair_fst<<"-"<<pair_sec<<endl;
cnt = 0;
cross_change(pair_fst - 1,pair_sec - 1);
}
}
//输出交换结果
cout<<"cross result:"<<endl;
for(int i = 0; i < SAMPLE_NUM; i++) {
cout<<"temp["<<i + 1<<"]: ";
for(int j = 0; j < 6; j++)
cout<<temp[i].bin[j]<<" ";
cout<<endl;
}
cout<<endl;
}
//交叉运算过程
void cross_change(int a,int b) {
//生成交叉点
int cross_seed = random(4) + 1;
cout<<"cross position:"<<cross_seed<<endl;
//输出交换前串信息
cout<<"temp["<<a + 1<<"]: ";
for(int i = 0; i < 6; i++) {
if(i == cross_seed) cout<<"| ";
cout<<temp[a].bin[i]<<" ";
}
cout<<endl;
cout<<"temp["<<b + 1<<"]: ";
for(int i = 0; i < 6; i++) {
if(i == cross_seed) cout<<"| ";
cout<<temp[b].bin[i]<<" ";
}
cout<<endl;
//进行交换
for(int i = cross_seed; i < 6; i++) {
int tmp;
tmp = temp[a].bin[i];
temp[a].bin[i] = temp[b].bin[i];
temp[b].bin[i] = tmp;
}
}
//变异运算
void variation() {
cout<<"Variation:"<<endl;
for(int i = 0; i < SAMPLE_NUM; i++) {
cout<<"temp["<<i<<"]:" ;
int position_seed = random(5);
cout<<"variate pos: "<<position_seed<<",";
int var_seed = random(100);
if(var_seed < VARIATE_RATE) {
cout<<" success";
if(temp[i].bin[position_seed])
temp[i].bin[position_seed] = 0;
else
temp[i].bin[position_seed] = 1;
}
cout<<endl;
}
//输出变异结果
cout<<"variate result:"<<endl;
for(int i = 0; i < SAMPLE_NUM; i++) {
cout<<"temp["<<i + 1<<"]: ";
for(int j = 0; j < 6; j++)
cout<<temp[i].bin[j]<<" ";
cout<<endl;
}
cout<<endl;
}
//将运算的temp数组赋值给group数组以方便下一次迭代运算
void trans() {
for(int i = 0; i < SAMPLE_NUM; i++)
group[i] = temp[i];
}
//输出信息
void output() {
for(int i = 0; i < SAMPLE_NUM; i++) {
cout<<"node["<<i + 1<<"]: ";
for(int j = 0; j < 6; j++)
cout<<group[i].bin[j]<<" ";
cout<<" x1: "<<group[i].x1;
cout<<" x2: "<<group[i].x2;
cout<<" f: "<<group[i].f;
cout<<" percent:"<<group[i].pct<<endl;
}
cout<<endl;
}
//总函数接口
void evolution() {
init();
calcu();
output();
for(int i = 0; i < EVOLVE_NUM; i++) {
choose();
cross();
variation();
trans();
calcu();
output();
}
}
int main() {
srand((int)time(0));
evolution();
}

结果:

该函数最大值为98,有7个样本进化到了该数值,还有1个样本为次大值,效果不错。