-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
182 lines (141 loc) · 3.11 KB
/
main.cpp
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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <time.h>
using namespace std;
const int chromeNum = 9*9;
const int unit_num = 100;
int ps = 10;//mutation possibility
const int genmax = 500;// iteration times
int rawInput[10][10] = {
{ 0, 1, 1272, 2567, 1653, 2097, 1425, 1177, 3947, 1 },
{ 1, 0, 1, 2511, 1633, 2077, 1369, 1157, 3961, 1518 },
{ 1272, 1, 0, 1, 380, 1490, 821, 856, 3660, 385 },
{ 2567, 2511, 1, 0, 1, 2335, 1562, 2165, 3995, 933 },
{ 1653, 1633, 380, 1, 0, 1, 1041, 1135, 3870, 456 },
{ 2097, 2077, 1490, 2335, 1, 0, 1, 920, 2170, 1920 },
{ 1425, 1369, 821, 1562, 1041, 1, 0, 1, 4290, 626 },
{ 1177, 1157, 856, 2165, 1135, 920, 1, 0, 1, 1290 },
{ 3947, 3961, 3660, 3995, 3870, 2170, 4290, 1, 0, 1 },
{ 1, 1518, 385, 993, 456, 1920, 626, 1290, 1, 0 }
};
class Unit
{
public:
int chrome[chromeNum];
int length;
int val;
bool operator < (const Unit & that) const
{
return this->val < that.val;
}
};
class Group
{
private:
void qfind(vector<Unit>& a, int l, int r, int k)
{
int i = l, j = r;
auto t = a[rand() % (r - l + 1) + l];
do
{
while (a[i] < t) i++;
while (t < a[j]) j--;
if (i <= j)
swap(a[i++], a[j--]);
} while (i <= j);
if (k <= j - l)
qfind(a, l, j, k);
if (k >= i - l)
qfind(a, i, r, k - i + l);
}
public:
vector<Unit> group;
Unit best;
int best_gen;
Group()
{
// Randomization here
}
void assess()
{
}
void unit_sort()
{
for (int i = 0; i < unit_num; i++)
{
for (int j = i + 1; j < unit_num; j++)
{
if (group[i].length > group[j].length)
{
Unit temp;
memcpy(&temp, &group[i], sizeof(Unit));
memcpy(&group[i], &group[j], sizeof(Unit));
memcpy(&group[j], &temp, sizeof(Unit));
}
}
}
}
//cross
void cross(int parent1, int parent2)
{
int child;
int midpoint = int(random(best.length));
for (int i = 0; i < best.length; i++) {
//Before midpoint copy genes from one parent, after midpoint copy genes from the other parent
if (i > midpoint) child.best_gen[i] = best_gen[i];
else child.best_gen[i] = best_gen[i];
}
return child;
}
//mutation
void mutation(Unit &t)
{
}
//
void print()
{
}
//ÖÖȺ½ø»¯
void evolve()
{
for (int i = 0; i < genmax; i++)
{
// Increase mutaion when generatin
if (i > 20)
ps *= 3;
assess();//assess
sort(group.begin(), group.end());
if (best.length > group[0].length)
{
memcpy(&best, &group[0], sizeof(group[0]));
best_gen = i;
}
for (int j = 0; j + 2 < unit_num; j += 3)
group[j + 2] = cross(group[j], group[j + 1]);
for (int j = 0; j < chromeNum; j++)//mutation
mutation(group[j]);
}
}
};
Unit group[unit_num];
Unit bestone;// record the best one
int generation_num;// recode the generation
int main()
{
srand((int)time(0));
for (int i = 0; i < 20; i++)
{
Group g;
g.evolve();
printf("Num", i + 1);
for (int j = 0; j < chromeNum; j++)
printf("%d ", g.best.chrome[j]);
printf(";All Evaluation£º%d; %dth;\n", g.best.length, g.best_gen);
}
system("pause");
return 0;
}