-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraints.h
More file actions
285 lines (267 loc) · 7.86 KB
/
Constraints.h
File metadata and controls
285 lines (267 loc) · 7.86 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#pragma once
#define _USE_MATH_DEFINES
#include "Chromosome.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <random>
struct Constraint
{
double max = 0, min = 0;
Chromosome *optimalSol;
int num_k = 0, royal_number = 0;
double **landscape;
bool nk_first = true;
// max, min, optimalSol은 실수 인코딩 문제를 위한 부분
// 바이너리 인코딩 문제라면, 0, 0, NULL인 상태를 유지한다.
// 마찬가지로, num_k, landscape, nk_first, royal_number는
// 바이너리 인코딩 문제를 위한 부분으로
// 실수 인코딩 문제라면 0, 0, NULL, true인 상태를 유지한다.
enum Function
{
Rosenbrock,
Sphere,
Schwefel,
Rastrigin,
Onemax,
Royalroad,
NKlandscape,
Deceptive,
Minimum_sum
};
Function _function;
Constraint(void) {}
~Constraint(void) {}
Constraint(Function f) : _function(f)
{
switch (_function)
{
case Rosenbrock:
max = 5;
min = -5;
break;
case Sphere:
max = 5;
min = -5;
break;
case Schwefel:
max = 5;
min = -5;
break;
case Rastrigin:
max = 5;
min = -5;
break;
case Minimum_sum:
max = 5;
min = -5;
break;
default:
break;
}
}
// 초기화 하는 과정에서 한번만 호출되야 한다.
Chromosome *Make_optimal_solution(const int &chromosome_size)
{
optimalSol = new Chromosome(chromosome_size);
for (int i = 0; i < chromosome_size; i++)
{
/*
double value = rand() / (double(RAND_MAX) + 1) * max;
if (rand() % 2 == 1)
value = value * -1;
optimalSol->setChromosome(i, value);
*/
optimalSol->setChromosome(i, 0);
}
// optimalSol->setChromosomeSize(chromosome_size);
// optimalSol은 적합도와 염색체 크기 필드가 비워져 있다.
return optimalSol;
}
double Fitness_with_noise(const Chromosome &chr)
{
double fitness = Fitness(chr);
double fitness_with_noise = 0.0;
std::default_random_engine generator;
std::normal_distribution<double> distribution(0, 0.1);
fitness_with_noise = fitness + distribution(generator);
return fitness_with_noise;
}
double Fitness(const Chromosome &chr)
{
switch (_function)
{
/* Real encoding problems */
case Rosenbrock:
return rosenbrock(chr);
case Sphere:
return sphere(chr);
case Schwefel:
return schwefel(chr);
case Rastrigin:
return rastrigin(chr);
/* Binary encoding problems */
case Onemax:
return onemax(chr);
case Royalroad:
return royalroad(chr);
case NKlandscape:
return nk(chr);
case Deceptive:
return deceptive(chr);
case Minimum_sum:
return minimum_sum(chr);
}
}
void setParms(const int &num_k, const int &royal_number)
{
this->num_k = num_k;
this->royal_number = royal_number;
}
double rosenbrock(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize() - 1; i++)
{
double diff1 = chr.getChromosome(i) - optimalSol->getChromosome(i);
double diff2 = chr.getChromosome(i + 1) - optimalSol->getChromosome(i + 1);
fitness += 100 * pow((pow(diff1 + 1, 2) - (diff2 + 1)), 2) + pow(diff1, 2);
}
return fitness + 390.0;
}
double sphere(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
{
double diff = chr.getChromosome(i) - optimalSol->getChromosome(i);
fitness += pow(diff, 2);
}
return fitness - 450.0;
}
double schwefel(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
{
double val = 0.0;
for (int j = 0; j <= i; j++)
{
double diff = chr.getChromosome(j) - optimalSol->getChromosome(j);
val += diff;
}
fitness += pow(val, 2);
}
return fitness - 450.0;
}
double rastrigin(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
{
double diff = chr.getChromosome(i) - optimalSol->getChromosome(i);
fitness += (pow(diff, 2) - 10 * cos(2 * M_PI * diff) + 10);
}
return fitness - 330.0;
}
double minimum_sum(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
fitness += chr.getChromosome(i);
return fitness;
}
double onemax(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
fitness += chr.getChromosome(i);
return fitness;
}
double royalroad(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize() / royal_number; i++)
{
bool sol = true;
for (int j = 0; j < royal_number; j++)
if (chr.getChromosome(royal_number * i + j) != 1)
{
sol = false;
break;
}
if (true == sol)
fitness += royal_number;
}
return fitness;
}
double nk(const Chromosome &chr)
{
// num_k = 12;
if (true == nk_first)
{
int powerOfK = pow(2, num_k);
landscape = new double *[chr.GetSize()];
for (int i = 0; i < chr.GetSize(); i++)
landscape[i] = new double[powerOfK * 2];
for (int i = 0; i < chr.GetSize(); i++)
{
for (int j = 0; j < powerOfK * 2; j++)
landscape[i][j] = (rand() / (double)RAND_MAX);
}
nk_first = false;
/*
std::ofstream outFile("landscape50_12.txt");
int k = 12;
for (int i = 0; i < chr.GetSize(); i++)
{
for (int j = 0; j < pow(2, k + 1); j++)
outFile << landscape[i][j] << ",";
outFile << std::endl;
}
outFile.close();
*/
}
else
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
{
int binary = 0;
int k = num_k;
for (int j = 0; j < num_k + 1; j++)
{
int num = 0;
if (i + j >= chr.GetSize())
num = (i + j) % chr.GetSize();
else
num = i + j;
binary += (chr.getChromosome(num) * pow(2, k));
k--;
}
fitness += landscape[i][binary];
}
/*
std::ofstream outFile("nk20_2.csv", std::fstream::app);
for (int i = 0; i < chr.GetSize(); i++)
{
outFile << chr.getChromosome(i) << ",";
}
outFile << fitness << std::endl;
outFile.close();
*/
return fitness;
}
}
double deceptive(const Chromosome &chr)
{
double fitness = 0.0;
for (int i = 0; i < chr.GetSize(); i++)
fitness += chr.getChromosome(i);
if (fitness == chr.GetSize())
return 0;
else
return fitness - 1;
return fitness;
}
};