-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2
122 lines (119 loc) · 4.26 KB
/
2
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
#include "models/gvrp_models/cplex/gvrp_lp_kk_heuristic.hpp"
using namespace models;
using namespace models::gvrp_models;
Gvrp_lp_kk_heuristic::Gvrp_lp_kk_heuristic (const KK_model& kk_model_, const Matrix2DVal& x_, const Matrix3DVal& y_) : Gvrp_heuristic (kk_model_.instance), kk_model(kk_model_), x(x_), y(y_), valid(false) {}
Gvrp_solution Gvrp_lp_kk_heuristic::run() {
list<list<Vertex>> routes;
list<Vertex> route;
size_t curr;
//checking the depot neighboring
set<int> customers;
bool valid = true;
while (true) {
cout<<endl;
double maxFirst = -1,
remainingFuel = kk_model.instance.vehicleFuelCapacity,
remainingTime = kk_model.instance.timeLimit;
int nextCustomer = 0,
nextAFS = 0;
bool found = false;
for (size_t i = 1; i < kk_model.c0.size(); ++i) {
if (!customers.count(i)) {
if (x[0][i] > maxFirst) {
maxFirst = x[0][i];
nextCustomer = i;
found = true;
} else
for (size_t f = 0; f < kk_model.f0.size(); ++f)
if (y[0][f][i] > maxFirst) {
maxFirst = y[0][f][i];
nextAFS = f;
nextCustomer = i;
found = true;
}
}
}
if (!found)
break;
//update dss
route.push_back(Vertex(*kk_model.c0[0]));
if (maxFirst != x[0][nextCustomer]) {
if (kk_model.customerToAfsFuel(0, nextAFS) > remainingFuel || kk_model.afsToCustomerFuel(nextAFS, nextCustomer) > remainingFuel || remainingTime - kk_model.time(0, nextAFS, nextCustomer) < 0) {
valid = false;
break;
}
cout<<nextAFS<<" ";
remainingFuel -= kk_model.afsToCustomerFuel(nextAFS, nextCustomer);
remainingTime -= kk_model.time(0, nextAFS, nextCustomer);
route.push_back(Vertex(*kk_model.f0[nextAFS]));
} else if (kk_model.customersFuel(0, nextCustomer) > remainingFuel || remainingTime - kk_model.time(0, nextCustomer) < 0) {
valid = false;
break;
} else {
cout<<"here"<<endl;
remainingFuel -= kk_model.customersFuel(0, nextCustomer);
cout<<"here"<<endl;
remainingTime -= kk_model.time(0, nextCustomer);
cout<<"here"<<endl;
}
route.push_back(Vertex(*kk_model.c0[nextCustomer]));
customers.insert(nextCustomer);
curr = nextCustomer;
cout<<nextCustomer<<" ";
//dfs
while (curr != 0) {
maxFirst = 0;
nextCustomer = 0;
nextAFS = 0;
found = false;
for (size_t i = 0; i < kk_model.c0.size(); ++i) {
if (!customers.count(i)) {
if (x[curr][i] > maxFirst) {
maxFirst = x[curr][i];
nextCustomer = i;
found = true;
} else
for (size_t f = 0; f < kk_model.f0.size(); ++f)
if (y[curr][f][i] > 0) {
maxFirst = y[curr][f][i];
nextAFS = f;
nextCustomer = i;
found = true;
}
}
}
//repeated customer
if (!found) {
valid = false;
break;
}
//update dss
if (maxFirst != x[curr][nextCustomer]) {
if (kk_model.customerToAfsFuel(curr, nextAFS) > remainingFuel || kk_model.afsToCustomerFuel(nextAFS, nextCustomer) > kk_model.instance.vehicleFuelCapacity || remainingTime - kk_model.time(curr, nextAFS, nextCustomer) < 0) {
valid = false;
break;
}
cout<<nextAFS<<" ";
remainingFuel = kk_model.instance.vehicleFuelCapacity - kk_model.afsToCustomerFuel(nextAFS, nextCustomer);
remainingTime -= kk_model.time(curr, nextAFS, nextCustomer);
route.push_back(Vertex(*kk_model.f0[nextAFS]));
} else if (kk_model.customersFuel(curr, nextCustomer) > remainingFuel || remainingTime - kk_model.time(curr, nextCustomer) < 0) {
valid = false;
break;
} else {
remainingFuel -= kk_model.afsToCustomerFuel(curr, nextCustomer);
remainingTime -= kk_model.time(curr, nextCustomer);
}
cout<<nextCustomer<<" ";
route.push_back(Vertex(*kk_model.c0[nextCustomer]));
customers.insert(nextCustomer);
curr = nextCustomer;
}
if (!valid)
break;
routes.push_back(route);
route = list<Vertex> ();
}
this->valid = valid;
return Gvrp_solution (routes, gvrp_instance);
}