-
Notifications
You must be signed in to change notification settings - Fork 1
/
MOEADInd.cpp
341 lines (265 loc) · 6.21 KB
/
MOEADInd.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "stdafx.h"
#include "MOEADInd.h"
#include "Chart.h"
extern Fitness fit[1000] ;
CIndividual::CIndividual()
{
x_var = vector<int>(nvar, 0);
y_obj = vector<double>(nobj, 0);
rank = 0;
}
CIndividual::~CIndividual()
{
x_var.clear();
y_obj.clear();
}
void CIndividual::rnd_init( )
{
for( int n = 0 ; n < nvar ; n++ )
x_var[n] = lowBound[n] +
rnd_uni(&rnd_uni_init)*(uppBound[n] - lowBound[n]);
//obj_eval(n) ;
}
void CIndividual::obj_eval( const int& index )
{
int n = this->x_var.size() ;
double A = evaluate_length(n) ;
double B = evaluate_smoothness(n) ;
double C = evaluate_security(n) ;
int D = 0;
int curX, curY, nextX, nextY ;
double kX, kY, nextk ;
int x1, x2, y1, y2 ;
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++ ) {
x1 = x_var[i] % chartWidth ;
y1 = x_var[i] / chartWidth ;
x2 = x_var[i+1] % chartWidth ;
y2 = x_var[i+1] / chartWidth ;
curX = x1 ;
curY = y1 ;
do {
if(x2==x1){
if( y2 > y1 ) {
nextY = curY + 1 ;
nextX = curX ;
} else {
nextY = curY - 1 ;
nextX = curX ;
}
}else if( y2 == y1 ) {
if( x2 > x1 ) {
nextX = curX + 1 ;
nextY = curY ;
} else {
nextX = curX - 1 ;
nextY = curY ;
}
}else{
if( x2 > x1) nextX = curX+1;
else nextX = curX-1;
if( y2>y1) nextY = curY+1;
else nextY = curY-1;
if( x2< x1 ) nextX++;
if( y2 < y1 ) nextY++;
kX = (nextX-x1-0.5)/(x2-x1);
kY = (nextY-y1-0.5)/(y2-y1);
nextk = min(kX,kY);
nextX = floor(x1+0.5+nextk*(x2-x1));
nextY = floor(y1+0.5+nextk*(y2-y1));
if(x2<x1&&kX<=kY) nextX--;
if(y2<y1&&kY<=kX) nextY--;
}
if(chart[nextY][nextX] == 1)//如果通过了障碍物
D++;
curX = nextX; curY = nextY;
count++;
if(count>1000){
nextX=x2;
nextY=y2;
}
}while(( !( nextX == x2 && nextY == y2 )));
count=0;
}
double len = sqrt((double)( (chartWidth-1) * (chartWidth-1)*2) );
fit[n].A = len/A;
if(B == 0)
fit[index].B = 1;
else
fit[index].B = 1/B;
if(C == 0)
fit[index].C = 0;
else
fit[index].C = 1/C;
y_obj[2] = A + 100000*D; //长度
y_obj[1] = B + 100000*D; //平滑度
y_obj[0] = -C + 100000*D; //安全性
return;
}
double CIndividual::evaluate_length(const int& n ) {
double A = 0.0 ;
//#对长度的评价
for ( int j = 1; j < n; j++ ){
//前一个点和当前点在栅格图上的坐标
int x1,x2,y1,y2;
x1 = x_var[j-1] % chartWidth ;
y1 = x_var[j-1] / chartWidth ;
x2 = x_var[j] % chartWidth ;
y2 = x_var[j] / chartWidth ;
A += sqrt( (double)(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );//两点之间的距离
}
return A ;
}
double CIndividual::evaluate_smoothness(const int& n ) {
double B = 0.0 ;
//#对个体光滑度的评价
for (int j = 1; j < n-1; j++){
//前一个点和当前点在栅格图上的坐标
double angle, len1,len2;
int x1, x2, y1, y2, x3, y3,
vectorx1, vectory1, vectorx2, vectory2 ;
x1 = x_var[j-1] % chartWidth;
y1 = x_var[j-1] / chartWidth;
x2 = x_var[j] % chartWidth;
y2 = x_var[j] / chartWidth;
x3 = x_var[j+1] % chartWidth;
y3 = x_var[j+1] / chartWidth;
vectorx1 = x2 - x1 ;
vectory1 = y2 - y1 ;
vectorx2 = x3 - x2 ;
vectory2 = y3 - y2 ;
len1 = sqrt((double)(vectorx1*vectorx1+vectory1*vectory1));
len2 = sqrt((double)(vectorx2*vectorx2+vectory2*vectory2));
angle = (vectorx1*vectorx2+vectory1*vectory2) / (len1*len2);//计算向量的夹角
angle = acos(angle);
B += angle;
}
return B ;
}
double CIndividual::evaluate_security( const int& n ){
double C = 0.0 ;
//#对个体安全性的评价
for(int j = 0; j < n-1; ++j){
//当前点在栅格图上的坐标
int x1, y1, x2, y2, m = 0;
double len = 0.0;
double L = 0.0;
x1 = x_var[j]%chartWidth;
y1 = x_var[j]/chartWidth;
vector<int> vecChart;
for(int i = 0; i < chartWidth; ++i)
for(int k = 0; k < chartWidth; ++k)
if(chart[i][k] == 1)
vecChart.push_back(i*chartWidth + k);
for(int i = 0; i < vecChart.size(); ++i){
x2 = vecChart[i]%chartWidth;
y2 = vecChart[i]/chartWidth;
len = sqrt((double)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
if( check( this, ((y2-1)*chartWidth+x2)) && check( this,(y2*chartWidth+x2-1)) )
m += 1;
else if(check( this, ((y2+1)*chartWidth+x2)) && check( this, (y2*chartWidth+x2-1) ))
m += 1;
else if(check( this, ((y2+1)*chartWidth+x2)) && check( this, (y2*chartWidth+x2+1) ))
m += 1;
else if(check( this, ((y2-1)*chartWidth+x2)) && check( this, (y2*chartWidth+x2+1) ))
m += 1;
L += len;
L = L - m * sqrt((double)(chartWidth*chartWidth));//进行惩罚
if(L < 0)
L = -L;
}
C = L / vecChart.size();
}
return C ;
}
bool CIndividual::check( CIndividual const* ind, const int& i )
{
for(int k = 0; k < ind->x_var.size() ; ++k )
{
if( ind->x_var[k] == i)
return true;
}
return false;
}
void CIndividual::show_objective()
{
for(int n=0; n<nobj; n++)
printf("%f ",y_obj[n]);
printf("\n");
}
void CIndividual::show_variable()
{
for(int n=0; n<nvar; n++)
printf("%f ",x_var[n]);
printf("\n");
}
void CIndividual::operator=(const CIndividual &ind2)
{
x_var = ind2.x_var;
y_obj = ind2.y_obj;
rank = ind2.rank;
}
bool CIndividual::operator<(const CIndividual &ind2)
{
bool dominated = true;
for(int n=0; n<nobj; n++)
{
if(ind2.y_obj[n]<y_obj[n]) return false;
}
if(ind2.y_obj==y_obj) return false;
return dominated;
}
bool CIndividual::operator<<(const CIndividual &ind2)
{
bool dominated = true;
for(int n=0; n<nobj; n++)
{
if(ind2.y_obj[n]<y_obj[n] - 0.0001) return false;
}
if(ind2.y_obj==y_obj) return false;
return dominated;
}
bool CIndividual::operator==(const CIndividual &ind2)
{
if(ind2.y_obj==y_obj) return true;
else return false;
}
TCompare CIndividual::Compare(CIndividual& ind2) {
bool bBetter = false;
bool bWorse = false;
int i = 0;
do {
if(y_obj[i] < ind2.y_obj[i])
bBetter = true;
if(ind2.y_obj[i] < y_obj[i])
bWorse = true;
i++;
}while (!(bWorse && bBetter) && (i < nobj));
if (bWorse) {
if (bBetter)
return _Pareto_Nondominated;
else
return _Pareto_Dominated;
}
else {
if (bBetter)
return _Pareto_Dominating;
else
return _Pareto_Equal;
}
}
// defining subproblem
TSOP::TSOP()
{
}
TSOP::~TSOP()
{
}
void TSOP::operator=(const TSOP&sub2)
{
indiv = sub2.indiv;
table = sub2.table;
namda = sub2.namda;
array = sub2.array;
}
//#endif