Repository navigation
Expand file tree
/
Copy pathGenetic_Algorithm
More file actions
193 lines (168 loc) · 4.24 KB
/
Copy pathGenetic_Algorithm
File metadata and controls
193 lines (168 loc) · 4.24 KB
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
#include <iostream>
#include <ctime>
#include <string>
#include <cmath>
#define POP_NUM 4 //族群数
#define GEN_NUM 4 //基因位数
#define ITE_NUM 1000 //迭代次数
#define CRO_RATE 0.5 //交叉率
#define MUT_RATE 0.1 //变异率
#pragma warning(disable : 4996)
using namespace std;
class Chromosome {
public:
char gene[GEN_NUM + 1];
int fitvalue;
};
int CreatePOP(Chromosome(&pop)[POP_NUM]);
int To_decimal(char input[GEN_NUM + 1]);
int Caculate_fit(int i);
int Print_chromo(Chromosome(&pop)[POP_NUM]);
int UpadatePop(Chromosome(&old_pop)[POP_NUM], Chromosome(new_pop)[POP_NUM]);
int SelectPop(Chromosome(&pop)[POP_NUM]);
int CrossOver(Chromosome(&pop)[POP_NUM]);
int Mutation(Chromosome(&pop)[POP_NUM]);
int main(void) {
Chromosome pop[POP_NUM];
Chromosome next_pop[POP_NUM];
int i;
srand((int)time(NULL));
CreatePOP(pop);
for (i = 0; i < ITE_NUM; i++) {
UpadatePop(next_pop, pop);
SelectPop(next_pop);
CrossOver(next_pop);
Mutation(next_pop);
UpadatePop(pop,next_pop);
}
return 0;
}
//创建初始族群
int CreatePOP(Chromosome (&pop)[POP_NUM]) {
int i, j;
int input;
int value;
for (i = 0; i < POP_NUM; i++) {
for (j = GEN_NUM - 1; j >= 0; j--) {
input = rand() % 2;
pop[i].gene[j] = '0' + input;
}
pop[i].gene[GEN_NUM] = '\0';
value = To_decimal(pop[i].gene);
pop[i].fitvalue = Caculate_fit(value);
}
Print_chromo(pop);
return 0;
}
int To_decimal(char input[GEN_NUM+1]) {
int i;
int output = 0;
for (i = GEN_NUM-1; i >=0 ;i--) {
//cout << input;
output += (input[i]-'0')*((int)pow(2,3-i));
}
return output;
}
int Caculate_fit(int i) {
int output;
output = i * 15 - ((int)pow(i, 2));
return output;
}
int Print_chromo(Chromosome (&pop)[POP_NUM]) {
int i,j;
cout << "族群:" << endl;
for (i = 0; i < POP_NUM; i++) {
cout << "个体" << i << ": " << "\0"+(string)pop[i].gene << "\t值:" << pop[i].fitvalue << endl;
}
return 0;
}
//更新备份
int UpadatePop(Chromosome (&old_pop)[POP_NUM], Chromosome (new_pop)[POP_NUM] ){
int i;
for (i = 0; i < POP_NUM; i++) {
old_pop[i] = new_pop[i];
}
return 0;
}
//选择
int SelectPop(Chromosome (&pop)[POP_NUM]) {
int i, j;
Chromosome temp;
int SUM_FIT = 0;
int rand_num = 0;
double ChoicePos[POP_NUM];
for (i = GEN_NUM; i >0; i--) {
for (j = 0; j < i-1; j++) {
if ( pop[j].fitvalue< pop[j+1].fitvalue) {
temp = pop[j + 1];
pop[j + 1] = pop[j];
pop[j] = temp;
}
}
}
for (j = 0; j < POP_NUM; j++) {
SUM_FIT += pop[j].fitvalue;
}
for (i = 0; i < POP_NUM; i++) {
ChoicePos[i] = (double)pop[i].fitvalue / SUM_FIT;
rand_num = rand() % 100;
if (rand_num > (int)(ChoicePos[i] * 100) ) {
pop[i] = pop[0];//舍弃,换为最优值
}
}
cout << "选择后,族群如下:" << endl;
Print_chromo(pop);
return 0;
}
//交叉
int CrossOver( Chromosome(&pop)[POP_NUM] ) {
int rand_num = 0;
int i;
int position;
int value;
char temp[GEN_NUM+1]="";
rand_num = rand() % 100;
if (rand_num >= (CRO_RATE * 100)) {
cout << "未进行交叉" << endl;
return 1;
}
cout << "交叉前,族群如下:" << endl;
Print_chromo(pop);
for (i = 0; i < GEN_NUM; i = i + 2) {//交叉
position = rand() % (GEN_NUM - 1);
cout << "交换位置:" << position << endl;
strncpy(temp, pop[i].gene + position, GEN_NUM - position);//C++中此函数有警报
strncpy(pop[i].gene + position, pop[i+1].gene + position, GEN_NUM - position);
strncpy(pop[i + 1].gene + position, temp, GEN_NUM - position);
temp[GEN_NUM] = '\0';
pop[i].gene[GEN_NUM] = '\0';
pop[i + 1].gene[GEN_NUM] = '\0';
}
for (i = 0; i < GEN_NUM; i++) {
value = To_decimal(pop[i].gene);
pop[i].fitvalue = Caculate_fit(value);
}
cout << "交叉后,族群如下:" << endl;
Print_chromo(pop);
return 0;
}
//变异
int Mutation(Chromosome (&pop)[POP_NUM]) {
int i;
int rand_num, randRow, randCol;
int value;
rand_num = rand() % 100;
if (rand_num >= (MUT_RATE * 100)) {
cout << "未进行变异" << endl;
return 1;
}
randRow = rand() % POP_NUM;
randCol = rand() % GEN_NUM;
cout << "变异前,族群如下:" << endl;
Print_chromo(pop);
pop[randRow].gene[randCol] = (pop[randRow].gene[randCol] == '0') ? '1' : '0';
pop[randRow].fitvalue=Caculate_fit(To_decimal(pop[randRow].gene));
cout << "变异后,族群如下:" << endl;
Print_chromo(pop);
return 0;
}