-
Notifications
You must be signed in to change notification settings - Fork 0
/
routeSetEvalFunc.h
193 lines (173 loc) · 6.32 KB
/
routeSetEvalFunc.h
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
/*
* File: routeSetEvalFunc.h
* Author: MAN
*
* Created on September 7, 2013, 6:44 PM
*/
#ifndef _ROUTESETEVALFUNC_H
#define _ROUTESETEVALFUNC_H
#include <queue>
#include "routeSet.h"
//#include "eoEvalFunc.h"
//#include "inputData.h"
void floydWarshall(vector< vector<int> > & sDist, int vertexNo, vector< vector<int> > & transfer)
{
for (int k = 0; k < vertexNo; k++)
{
for (int i = 0; i < vertexNo; i++)
{
for (int j = i + 1; j < vertexNo; j++)
{
if (sDist[i][k] == INFINITY || sDist[k][j] == INFINITY)
{
continue;
}
if ((sDist[i][k] + sDist[k][j]) < sDist[i][j])
{
sDist[j][i] = sDist[i][j] = sDist[i][k] + sDist[k][j];
transfer[i][j] = transfer[j][i] = transfer[i][k] + transfer[k][j];
}
}
}
}
}
template <class EOT>
class RouteSetEvalFunc : public eoEvalFunc<EOT>
{
public:
/// Ctor - no requirement
// START eventually add or modify the anyVariable argument
RouteSetEvalFunc()
{
// START Code of Ctor of an RouteSetEvalFunc object
// END Code of Ctor of an RouteSetEvalFunc object
}
/** Actually compute the fitness
*
* @param EOT & _eo the EO object to evaluate
* it should stay templatized to be usable
* with any fitness type
*/
void operator()(EOT & _eo)
{
// test for invalid to avoid recomputing fitness of unmodified individuals
if (_eo.invalid())
{
// START Code of computation of fitness of the RouteSet object
vector< Route<double> > & routeSet = _eo.mutableRs();
int dist[VERTICES_NO][VERTICES_NO];
int transfer[VERTICES_NO][VERTICES_NO];
list<int> O2E[VERTICES_NO];
vector<int> E2O;
vector<int> routeNo;
vector< vector<int> > newMap(VERTICES_NO, vector<int> (routeSet.size(), 0));
for (int r = 0; r < routeSet.size(); r++)
{
list<int>::const_iterator it = routeSet[r].R().begin();
for (; it != routeSet[r].R().end(); it++)
{
O2E[*it].push_back(E2O.size());
newMap[*it][r] = E2O.size();
E2O.push_back(*it);
routeNo.push_back(r);
}
}
const int newVertexCount = E2O.size();
vector< vector<int> > eDist(newVertexCount, vector<int>(newVertexCount, 0));
vector< vector<int> > eTransfer(newVertexCount, vector<int>(newVertexCount, 0));
for (int i = 0; i < newVertexCount; i++)
{
eDist[i][i] = 0;
for (int j = i + 1; j < newVertexCount; j++)
{
int I = E2O[i], J = E2O[j];
if (I == J)
{
eDist[i][j] = eDist[j][i] = TRANSFER_PENALTY;
eTransfer[i][j] = 1;
}
else
{
eDist[i][j] = eDist[j][i] = INFINITY;
}
}
}
for (int r = 0; r < routeSet.size(); r++)
{
list<int>::const_iterator next = routeSet[r].R().begin();
list<int>::const_iterator it = next++;
for (; next != routeSet[r].R().end(); it++, next++)
{
int i = *it;
int j = *next;
int I = newMap[i][r];
int J = newMap[j][r];
eDist[J][I] = eDist[I][J] = tr[i][j];
}
}
floydWarshall(eDist, newVertexCount, eTransfer);
for (int i = 0; i < VERTICES_NO; i++)
{
dist[i][i] = 0;
for (int j = i + 1; j < VERTICES_NO; j++)
{
int min = INFINITY, minTransfer = INFINITY;
list<int>::iterator iIt = O2E[i].begin();
for (; iIt != O2E[i].end(); iIt++)
{
list<int>::iterator jIt = O2E[j].begin();
for (; jIt != O2E[j].end(); jIt++)
{
if (eDist[*iIt][*jIt] < min)
{
min = eDist[*iIt][*jIt];
minTransfer = eTransfer[*iIt][*jIt];
}
}
}
dist[i][j] = dist[j][i] = min;
transfer[i][j] = transfer[j][i] = minTransfer;
}
}
double D[] = {0, 0, 0}, Dun = 0, ATT = 0, totalDemand = 0, totalSatisfied = 0, T = 0;
for (int i = 0; i < VERTICES_NO; i++)
{
for (int j = i + 1; j < VERTICES_NO; j++)
{
totalDemand += d[i][j];
if (dist[i][j] == INFINITY)
{
Dun += d[i][j];
}
else if (transfer[i][j] > 2)
{
Dun += d[i][j];
//ATT += d[i][j] * dist[i][j];
//totalSatisfied += d[i][j];
// cout << "more than 2";
}
else
{
D[transfer[i][j]] += d[i][j];
ATT += d[i][j] * dist[i][j];
T += d[i][j] * dist[i][j];
totalSatisfied += d[i][j];
}
}
}
T += Dun * (parameters["OptimalATT"] + 50);
//_eo.fitness(-T); //minimize
_eo.actualFitness = -T;
_eo.D[0] = D[0] / totalDemand;
_eo.D[1] = D[1] / totalDemand;
_eo.D[2] = D[2] / totalDemand;
_eo.Dun = Dun / totalDemand;
_eo.ATT = ATT / totalSatisfied;
}
}
private:
// START Private data of an RouteSetEvalFunc object
// varType anyVariable; // for example ...
// END Private data of an RouteSetEvalFunc object
};
#endif /* _ROUTESETEVALFUNC_H */