-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
207 lines (181 loc) · 4.94 KB
/
main.cpp
File metadata and controls
207 lines (181 loc) · 4.94 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "Genes.h"
#include <fstream>
void createGenes();
// 유전자 리스트
vector<Genes> GenesList;
// 물건의 가치보관
vector<int> Properties;
// 지식별 배분 비율
vector<double> Child;
// 비율로 계산한 목표 재산값
vector<int> Target;
bool compare(const Genes& p1, const Genes& p2) {
return p1.getFit() < p2.getFit();
}
int main() {
// 재산 데이터와 비율 데이터 입력
int inputType = 0, propertiSum = 0;
ifstream devideFile, propertyFile;
string dFile = "./divide", pFile = "./res/properties"; // GEN_LENGTH
dFile += to_string(NUM_OF_CHILD) + ".txt";
pFile += to_string(GEN_LENGTH) + ".txt";
devideFile.open(dFile);
propertyFile.open(pFile);
// 재산 데이터 입출력
if (propertyFile.is_open()) {
cout << pFile << "파일 read \n";
int num;
while (propertyFile >> num) {
Properties.push_back(num);
propertiSum += num;
}
propertyFile.close();
}
else {
cout << pFile << "파일 read 실패 : 랜덤 데이터 생성\n";
// 재산 데이터 설정
for (int i = 0; i < GEN_LENGTH; i++) {
int temp = rand() % 128;
Properties.push_back(temp);
propertiSum += temp;
}
}
// 자식 데이터 입력
if (devideFile.is_open()) {
cout << dFile << "파일 read \n";
double num;
while (devideFile >> num) {
Child.push_back(num);
// 정답 데이터 설정
Target.push_back(propertiSum * num);
}
devideFile.close();
}
else {
cout << dFile << "파일 read 실패 : 랜덤 데이터 생성\n";
int per = 100;
// 비율 데이터 설정
for (int i = 0; i < NUM_OF_CHILD; i++) {
int temp = rand() % per;
if (temp > 20) {
temp = 10 + temp % 10;
}
if (i == NUM_OF_CHILD - 1) {
temp = per;
}
per -= temp;
Child.push_back(temp / 100.0);
// 정답 데이터 설정
Target.push_back(propertiSum * temp / 100);
}
}
//cin >> inputType;
//if (inputType == 1) {
// // 재산 데이터 설정
// srand(static_cast<unsigned int>(std::time(0)));
// for (int i = 0; i < GEN_LENGTH; i++) {
// int temp = rand() % 128;
// Properties.push_back(temp);
// propertiSum += temp;
// }
// int per = 100;
// // 비율 데이터 설정
// for (int i = 0; i < NUM_OF_CHILD; i++) {
// int temp = rand() % per;
// if (temp > 20) {
// temp = 10 + temp%10;
// }
// if (i == NUM_OF_CHILD - 1) {
// temp = per;
// }
// per -= temp;
// Child.push_back(temp / 100.0);
// // 정답 데이터 설정
// Target.push_back(propertiSum * temp / 100);
// }
//}
//else {
// int temp;
// cout << "재산 데이터 입력 : \n";
// for (int i = 0; i < GEN_LENGTH; i++) {
// cin >> temp;
// Properties.push_back(temp);
// propertiSum += temp;
// }
// cout << "비율 데이터 입력 : \n";
// for (int i = 0; i < NUM_OF_CHILD; i++) {
// cin >> temp;
// Child.push_back(temp);
// // 정답 데이터 설정
// Target.push_back(propertiSum * temp);
// }
//}
/*
cout << "[ ";
for (int i = 0; i < GEN_LENGTH; i++) {
cout << Properties[i] << " ";
}
cout << "]\n";
cout << "[ ";
for (int i = 0; i < NUM_OF_CHILD; i++) {
cout << Child[i] << " ";
}
cout << "]\n";
cout << "[ ";
for (int i = 0; i < NUM_OF_CHILD; i++) {
cout << Target[i] << " ";
}
cout << "]\n";*/
// 초기 유전자 생성
for (int i = 0; i < N_OF_GEN; i++) {
Genes g;
GenesList.push_back(g);
}
// 반복 지점 : 세대 반복 - ITER
for (int g = 0; g <= ITER; g++) {
// 선택 : 엘리트 탐색
for (int i = 0; i < N_OF_GEN; i++) {
int fit = GenesList[i].calFitness(Properties, Target);
}
sort(GenesList.begin(), GenesList.end(), compare);
if (g % SKIP == 0) {
cout << g << "-fit : " << GenesList[0].getFit() << endl;
GenesList[0].printGenes();
}
// 한 4퍼센트는 엘리트로 둠
int eliteIdx = N_OF_GEN * ELITE / 100;
bool isin[GEN_LENGTH][GEN_LENGTH] = { 0, };
// 교차
for (int i = eliteIdx + 1; i < N_OF_GEN - 1; ) {
vector<int> c1, c2;
c1 = GenesList[i].getGenes();
c2 = GenesList[rand() % 30].getGenes();
int idx1 = rand() % GEN_LENGTH;
int idx2 = rand() % GEN_LENGTH;
if (idx2 >= GEN_LENGTH - 1) {
idx2 = GEN_LENGTH - 1;
}
if (idx2 < idx1) {
int temp = idx2; idx2 = idx1; idx1 = temp;
}
GenesList[i].setHalfIn(c2, idx1, idx2);
GenesList[i + 1].setHalfIn(c1, idx1, idx2);
i += 2;
}
// 돌연변이
for (int i = eliteIdx + 1; i < N_OF_GEN; i++) {
// 돌연변이 확률
if (getRandom()) {
GenesList[i].mutate();
}
}
}
cout << "휴우";
}
// 각 세대 유전자 수 만큼 생성
void createGenes() {
for (int i = 0; i < N_OF_GEN; i++) {
Genes temp;
GenesList.push_back(temp);
}
}