-
Notifications
You must be signed in to change notification settings - Fork 1
/
savingsAlgorithm.cpp
259 lines (209 loc) · 7.18 KB
/
savingsAlgorithm.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <time.h>
#include <random>
using namespace std;
vector<vector<int>> data;
vector<vector<int>> distanceVec;
int capacity = 50;
vector<int> calculatePathSum(vector<vector<int>> _Scurrent){
vector<vector<int>> Scurrent;
vector<int> shortestPathVec;
Scurrent = _Scurrent;
for(int i = 0; i < Scurrent.size(); i ++){
int pathSum = 0;
for(int j = 0; j < Scurrent[i].size() - 1; j ++){
pathSum += distanceVec[Scurrent[i][j] - 1][Scurrent[i][j + 1] - 1];
}
shortestPathVec.push_back(pathSum);
}
return shortestPathVec;
}
vector<vector<int>> randomCreateSolution(){
vector<int> randomSolution;
vector<vector<int>> randomSolutionVec;
//create a initial solution
for(int i = 2; i <= data.size(); i ++){
randomSolution.push_back(i);
}
//shuffle the initial solution
random_shuffle(randomSolution.begin(), randomSolution.end());
bool flag = true;
randomSolutionVec.push_back(randomSolution);
while(flag){
int i = 0;
while(i <= randomSolutionVec.size()){
//if is the end of the randomSolutionVec then stop
if(i == randomSolutionVec.size()){
flag = false;
break;
} else {
//caculate the sum of the demand
int sum = 0;
for(int j = 0; j < randomSolutionVec[i].size(); j++){
sum += data[randomSolutionVec[i][j] - 1][3];
}
if(sum > capacity) {
vector<int> temp;
randomSolutionVec.push_back(temp);
//split the route in half
int mid = (randomSolutionVec[i].size()) / 2;
for(int j = mid; j < randomSolutionVec[i].size(); j++){
randomSolutionVec[randomSolutionVec.size()-1].push_back(randomSolutionVec[i][j]);
}
randomSolutionVec[i].erase(randomSolutionVec[i].begin() + mid, randomSolutionVec[i].end());
i = i;
} else {
i ++;
}
}
}
}
for(int i = 0; i < randomSolutionVec.size(); i ++){
randomSolutionVec[i].insert(randomSolutionVec[i].begin(), 1);
randomSolutionVec[i].push_back(1);
}
return randomSolutionVec;
}
void savingsAlgorithm(){
vector<vector<int>> savingsVec;
vector<vector<int>> routeDemandLength;
//caculate savingsVec
for(int i = 2; i < data.size(); i ++){
for(int j = i + 1; j < data.size() + 1; j ++){
vector<int> temp;
savingsVec.push_back(temp);
int savings = distanceVec[i - 1][0] + distanceVec[0][j - 1] - distanceVec[i - 1][j - 1];
savingsVec[savingsVec.size() - 1].push_back(savings);
savingsVec[savingsVec.size() - 1].push_back(i);
savingsVec[savingsVec.size() - 1].push_back(j);
}
}
//sort the savingsVec by savings
sort(savingsVec.rbegin(), savingsVec.rend());
//create initial solution
for(int i = 2; i < data.size() + 1; i ++){
vector<int> temp;
routeDemandLength.push_back(temp);
routeDemandLength[i - 2].push_back(1);
routeDemandLength[i - 2].push_back(i);
routeDemandLength[i - 2].push_back(1);
routeDemandLength[i - 2].push_back(data[i - 1][3]);
routeDemandLength[i - 2].push_back(2 * distanceVec[i - 1][0]);
}
cout << "Initial routeDemandLength" << endl;
for(int i = 0; i < routeDemandLength.size(); i ++){
for(int j = 0; j < routeDemandLength[i].size(); j ++){
cout << routeDemandLength[i][j] << " ";
}
cout << endl;
}
int shortestPath = 0;
for(int i = 0; i < routeDemandLength.size(); i ++){
shortestPath += routeDemandLength[i][routeDemandLength[i].size() - 1];
}
cout << "shortestPath => " << shortestPath << endl;
cout << endl;
//merge the route accroding to the savingsVec
for(int i = 0; i < savingsVec.size(); i ++){
int customer_1 = savingsVec[i][1];
int customer_2 = savingsVec[i][2];
int route_1;
int route_2;
for(int j = 0; j < routeDemandLength.size(); j ++){
for(int k = 0; k < routeDemandLength[j].size() - 2; k ++){
if(routeDemandLength[j][k] == customer_1) {
route_1 = j;
}
if(routeDemandLength[j][k] == customer_2) {
route_2 = j;
}
}
}
if(route_1 == route_2) continue;
int tempRoute;
if(route_1 > route_2) {
tempRoute = route_1;
route_1 = route_2;
route_2 = tempRoute;
}
int demandSum = 0;
for(int j = 0; j < routeDemandLength[route_1].size() - 2; j ++){
demandSum += data[routeDemandLength[route_1][j] - 1][3];
}
for(int j = 0; j < routeDemandLength[route_2].size() - 2; j ++){
demandSum += data[routeDemandLength[route_2][j] - 1][3];
}
if(demandSum > capacity) continue;
vector<int> greaterRoute;
vector<int> lowerRoute;
if(routeDemandLength[route_1].size() >= routeDemandLength[route_2].size()) {
greaterRoute = {routeDemandLength[route_1].begin(), routeDemandLength[route_1].end() - 3};
lowerRoute = {routeDemandLength[route_2].begin() + 1, routeDemandLength[route_2].end() - 2};
} else {
greaterRoute = {routeDemandLength[route_2].begin(), routeDemandLength[route_2].end() - 3};
lowerRoute = {routeDemandLength[route_1].begin() + 1, routeDemandLength[route_1].end() - 2};
}
//merge two route
greaterRoute.insert(greaterRoute.end(), lowerRoute.begin(), lowerRoute.end());
int origPathSum = routeDemandLength[route_1][routeDemandLength[route_1].size() - 1] + routeDemandLength[route_2][routeDemandLength[route_2].size() - 1];
int mergePathSum = 0;
for(int j = 0; j < greaterRoute.size() - 1; j ++){
mergePathSum += distanceVec[greaterRoute[j] - 1][greaterRoute[j + 1] - 1];
}
if(origPathSum < mergePathSum) continue;
routeDemandLength.erase(routeDemandLength.begin() + route_2);
routeDemandLength.erase(routeDemandLength.begin() + route_1);
greaterRoute.push_back(demandSum);
greaterRoute.push_back(mergePathSum);
routeDemandLength.push_back(greaterRoute);
}
cout << "Final routeDemandLength" << endl;
for(int i = 0; i < routeDemandLength.size(); i ++){
for(int j = 0; j < routeDemandLength[i].size() - 2; j ++){
cout << routeDemandLength[i][j] << " ";
}
cout << endl;
}
shortestPath = 0;
for(int i = 0; i < routeDemandLength.size(); i ++){
shortestPath += routeDemandLength[i][routeDemandLength[i].size() - 1];
}
cout << "shortestPath => " << shortestPath << endl;
}
int main(){
ifstream dataFile;
dataFile.open("Solomon's_instance_1_depot_10_customers.txt");
//read file
if(dataFile.is_open()){
cout << "File open successed" << endl;
string line;
int dataIndex = 0;
while(getline(dataFile, line)){
float num;
istringstream istr(line);
vector<int> temp;
data.push_back(temp);
while(istr >> num){
data[dataIndex].push_back(num);
}
dataIndex ++;
}
} else {
cout << "File open failed" << endl;
}
//caculate distance between the customer
for(int i = 0; i < data.size(); i ++){
vector<int> temp;
distanceVec.push_back(temp);
for(int j = 0; j < data.size(); j ++){
distanceVec[i].push_back(sqrt(pow(data[i][1] - data[j][1], 2) + pow(data[i][2] - data[j][2], 2)));
}
}
//execute Savings Algorithm
savingsAlgorithm();
}