-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
203 lines (159 loc) · 5.07 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "bits/stdc++.h"
using namespace std;
std::random_device dev;
std::mt19937 rng(dev());
class subset {
public:
string dna;
int fitness;
subset() {
dna = "";
fitness = 0;
}
subset(const string& _dna, int _fitness) {
dna = _dna;
fitness = _fitness;
}
int get_fitness() {return fitness;}
int calc_fitness(const vector <vector <int>>& profit_vs_weight, const vector <int>& limits) {
int m = limits.size();
fitness = 0;
vector <int> cur_limits(m, 0);
int n = profit_vs_weight.size();
for (int i = 0; i < n; i++) {
if (dna[i] == '0') continue;
for (int j = 0; j < m; j++) {
cur_limits[j] += profit_vs_weight[i][j + 1];
if (cur_limits[j] > limits[j]) {
return fitness = -1;
}
fitness += profit_vs_weight[i][0];
}
}
return fitness;
}
bool operator<(const subset& other) const {
return fitness > other.fitness;
}
};
pair <subset, subset> selection_by_fitness(const set <subset>& st) {
int ttl = 0;
for (const subset& s : st) {
ttl += s.fitness;
}
std::uniform_int_distribution<std::mt19937::result_type> dist(1,ttl);
auto it = st.begin(), jt = st.begin();
int r1 = dist(rng);
int accum = 0;
for (auto kt = st.begin(); kt != st.end(); ++kt) {
accum += kt->fitness;
it = kt;
if (accum >= r1) break;
}
do {
int r2 = dist(rng);
accum = 0;
for (auto kt = st.begin(); kt != st.end(); ++kt) {
accum += kt->fitness;
jt = kt;
if (accum >= r2) break;
}
} while (it != jt);
return {*it, *jt};
}
subset crossover(const subset& parent1, const subset& parent2) {
std::uniform_int_distribution<std::mt19937::result_type> dist1(0,1);
int n = parent1.dna.size();
subset child = parent1;
for (int i = 0; i < n; i++) {
int r = dist1(rng);
if (r == 1) child.dna[i] = parent2.dna[i];
}
return child;
}
void mutation(subset& child, double mutation_rate) {
std::uniform_int_distribution<std::mt19937::result_type> rand_gen(1,1000000);
int mutation_variable = (mutation_rate * 1000000);
int n = child.dna.size();
for (int i = 0; i < n; i++) {
int r = rand_gen(rng);
if (r <= mutation_variable) {
// flip dna
int x = child.dna[i] - '0';
x = 1 - x;
child.dna[i] = '0' + x;
}
}
}
int main() {
int n, m;
cin >> n >> m;
int population_size;
cin >> population_size;
int max_gen;
cin >> max_gen;
vector <vector<int>> data_matrix;
for (int i = 0; i < n; i++) {
data_matrix.push_back(vector <int>(m + 1, 0));
for (int j = 0; j <= m; j++) cin >> data_matrix[i][j];
}
vector <int> limits;
for (int i = 0; i < m; i++) {
int x; cin >> x;
limits.push_back(x);
}
double mutation_rate;
cin >> mutation_rate;
set <subset> st;
std::uniform_int_distribution<std::mt19937::result_type> dist1(0,1);
int ans = 0;
int cnt = 0;
subset candidate;
while (st.size() < population_size and cnt < 1000000) {
cnt++;
string dna = "";
for (int i = 0; i < n; i++) {
int r = dist1(rng);
dna += r + '0';
}
subset s(dna, 0);
if (s.calc_fitness(data_matrix, limits) == -1) continue;
st.insert(s);
if (s.fitness > ans) {
ans = s.fitness;
candidate = s;
}
}
if (st.empty()) {
cout << "NO SUITABLE POPULATION FOUND\n";
return 0;
}
for (int g = 0; g < max_gen; g++) {
cout << "\nGeneration <" << g << "> current_max: " << ans << endl;
cout << "===" << endl;
cnt = 0;
for (const subset& s : st) {
cout << "Population " << cnt++ << " : DNA = [" << s.dna << "] : FITNESS = " << s.fitness << endl;
}
pair <subset, subset> p = selection_by_fitness(st);
cout << "SELECTION COMPLETE" << endl;
subset child = crossover(p.first, p.second);
mutation(child, mutation_rate);
cout << "Child : DNA = [" << child.dna <<"] ";
if (child.calc_fitness(data_matrix, limits) == -1) {
cout << "NOT_SUITABLE" << endl;
continue;
}
cout << "FITNESS = " << child.fitness << endl;
if (child.fitness > ans) {
ans = child.fitness;
candidate = child;
}
st.insert(child);
if (st.size() > population_size) st.erase(prev(st.end()));
cout << "==============" << endl;
}
cout << endl << endl;
cout << "Final Candidate: " << candidate.dna << endl;
cout << "Fitness: " << candidate.fitness << endl;
}