-
Notifications
You must be signed in to change notification settings - Fork 0
/
routermanager.cpp
347 lines (326 loc) · 9.71 KB
/
routermanager.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
341
342
343
344
345
346
347
#include <algorithm>
#include <iostream>
#include <memory>
#include <thread>
#include "routermanager.h"
#include "adopter.h"
static std::ofstream STR;
RouterManager::RouterManager(const std::string &folder) :
mFileReader(folder)
, mFinished(false)
, mProgress(new Progress())
, mStream(STR)
{
#ifdef WITH_LOG
std::cout << folder << std::endl;
#endif
mDistances = mFileReader.getDistances();
mVelocityCalc = mFileReader.getVelocityRestrictions();
mRestrictionChecker = std::shared_ptr<RestrictionChecker>(
new RestrictionChecker(mFileReader.getTrucks(), mFileReader.getTime()));
mCustomers = mFileReader.getCustomers();
mTrucks = mFileReader.getTrucks();
}
RouterManager::RouterManager(const std::vector<std::string> &data, std::ofstream &stream) :
mFinished(false)
, mProgress(new Progress())
, mSamplesParser(data)
, mStream(stream)
{
mDistances = mSamplesParser.getDistances();
mCustomers = mSamplesParser.getCustomers();
mTrucks = mSamplesParser.getTrucks();
mRestrictionChecker = std::shared_ptr<RestrictionChecker>(
new RestrictionChecker(mTrucks));
}
void RouterManager::calculate()
{
mFinished = false;
#ifdef WITH_DEBUG
operator ()();
#else
std::thread calculateThread(std::ref(*this));
calculateThread.detach();
#endif
}
std::vector<unsigned int> RouterManager::getUsedTrucks()
{
return mUsedTrucks;
}
std::vector<unsigned int> RouterManager::getRouteByTruck(unsigned int truckId)
{
return mTruckRoute.at(truckId)->customerIds();
}
int RouterManager::routeNoTruckNum()
{
return mRoutesNoTruck.size();
}
std::vector<unsigned int> RouterManager::getRouteNoTruck(int index)
{
return mRoutesNoTruck[index]->customerIds();
}
double RouterManager::routeLengthByTruck(unsigned int truckId)
{
return routeLength(mTruckRoute.at(truckId)->customerIds());
}
double RouterManager::routeLength(const std::vector<unsigned int> &customerIds)
{
if (customerIds.empty())
{
return 0;
}
double totalDist = mDistances->at(Pair(0, customerIds[0])) + mDistances->at(Pair(customerIds.back(), 0));
for (unsigned int i = 1; i < customerIds.size(); i ++)
{
totalDist += mDistances->at(Pair(customerIds[i - 1], customerIds[i]));
}
return totalDist;
}
double RouterManager::routeBeginTimeByTruck(unsigned int truckId)
{
if (mTruckRoute.at(truckId)->allCustomers().empty())
{
return 0;
}
Customer customer = mTruckRoute.at(truckId)->allCustomers()[0];
double time = customer.timeRestriction.arrivalTime;
double dist = mDistances->at(Pair(0, customer.id));
return time - mVelocityCalc.getTime(dist);
}
double RouterManager::routeEndTimeByTruck(unsigned int truckId)
{
std::vector<Customer> const &customers = mTruckRoute.at(truckId)->allCustomers();
if (customers.empty())
{
return 0;
}
Customer curCustomer = customers.at(0);
time_t time = curCustomer.timeRestriction.arrivalTime + curCustomer.timeRestriction.loadTime;
for (unsigned int i = 1; i < customers.size(); i ++)
{
curCustomer = customers[i];
time += mVelocityCalc.getTime(mDistances->at(Pair(customers[i - 1].id, curCustomer.id)));
time = std::max(curCustomer.timeRestriction.arrivalTime, time);
time += curCustomer.timeRestriction.loadTime;
}
time += mVelocityCalc.getTime(mDistances->at(Pair(customers.back().id, 0)));
return time;
}
bool RouterManager::isFinished()
{
return mFinished;
}
void RouterManager::operator ()()
{
mUsedTrucks.clear();
mRoutesNoTruck.clear();
mTruckRoute.clear();
#ifdef WITH_LOG
std::cout << "number of mTrucks " << mTrucks.size() << std::endl;
#endif
std::unordered_set<std::shared_ptr<Route>> initialRoutes = customersToRoutes(mCustomers);
std::unordered_set<std::shared_ptr<Route>> badRoutes = mRestrictionChecker->initByRoutes(initialRoutes);
for (std::shared_ptr<Route> const &badRoute: badRoutes)
{
initialRoutes.erase(badRoute);
mRoutesNoTruck.push_back(badRoute);
}
mClarkeWrightAlgorithm = ClarkeWrightAlgorithm(mDistances, std::shared_ptr<RestrictionChecker>(mRestrictionChecker), mProgress);
#ifdef WITH_LOG
std::cout << "create clark wright " << std::endl;
#endif
std::unordered_set<std::shared_ptr<Route> > routes = mClarkeWrightAlgorithm.calculate(initialRoutes);
unsigned int size1 = routes.size();
if (routes.size() > mTrucks.size())
{
routes = mClarkeWrightAlgorithm.calculate(routes);
}
#if WITH_LOG
if (size1 > routes.size())
{
std::cout << "routes num became better " << std::endl;
}
else
{
std::cout << "routes num the same" << std::endl;
}
#endif
std::vector<std::vector<int>> graph;
std::unordered_map<int, std::shared_ptr<Route>> routesInGraph;
for (std::shared_ptr<Route> const &route: routes)
{
std::vector<int> possible;
PassTimeInfo info = mRestrictionChecker->getTimeInfo(route);
for (unsigned int j = 0; j < mTrucks.size(); j ++)
{
if (mRestrictionChecker->isSatisfy(route, info, mTrucks[j]))
{
possible.push_back(j);
}
}
graph.push_back(possible);
routesInGraph.insert(std::pair<int, std::shared_ptr<Route>>(graph.size() - 1, route));
}
#ifdef WITH_LOG
std::cout << "finish check routes \n";
#endif
std::vector<int> matching = getMatching(graph, mTrucks.size());
#ifdef WITH_LOG
std::cout << "got matching \n";
#endif
for (unsigned int i = 0; i < matching.size(); i ++)
{
if (matching[i] < 0)
{
continue;
}
unsigned int id = mTrucks[i].id;
mUsedTrucks.push_back(id);
mTruckRoute[id] = routesInGraph[matching[i]];
routes.erase(routesInGraph[matching[i]]);
}
for (auto routeInGraph: routes)
{
mRoutesNoTruck.push_back(routeInGraph);
}
#ifdef WITH_LOG
double sum = 0;
double s1 = 0;
double s2 = 0;
double s3 = 0;
double avLength = 0;
double maxLength = 0;
for (Customer const &cust : mCustomers)
{
if (cust.zone == 0)
{
s3 += cust.package.weight;
}
else if (cust.zone == 1)
{
s1 += cust.package.weight;
}
else if (cust.zone == 3)
{
s2 += cust.package.weight;
}
avLength += mDistances->at(Pair(0, cust.id));
maxLength = std::max(maxLength, mDistances->at(Pair(0, cust.id)));
}
//std::cout
mStream
<< "sum weight in zone 2 is " << s2 << std::endl;
//std::cout
mStream
<< "sum weight in zone 1 is " << s1 << std::endl;
//std::cout
mStream
<< "sum weight in zone 3 is " << s3 << std::endl;
//std::cout
mStream
<< "average distance is " << avLength / mCustomers.size() << " max dist " << maxLength << std::endl;
for (auto pair: mTruckRoute)
{
double truckWeight = 0;
for (Truck const &truck: mTrucks)
{
if (pair.first == truck.id)
{
truckWeight = truck.capacity.weightCapacity;
}
}
sum += routeLength(pair.second->customerIds());
//std::cout
mStream
<< "\n TRUCK ID IS " << pair.first << std::endl;
//std::cout
mStream
<< " max route weight " << pair.second->maxWeight() << std::endl;
//std::cout
mStream
<< " truck max weight " << truckWeight << std::endl;
//std::cout
mStream
<< " max route Volume " << pair.second->maxVolume() << std::endl;
//std::cout
mStream
<< "number of customers in route is " << pair.second->allCustomers().size() << " total length "
<< routeLength(pair.second->customerIds()) << std::endl;
//std::cout
mStream
<< "route zone is " << pair.second->getZone() <<std::endl;
}
//std::cout
mStream
<< "number of used mTrucks " << mUsedTrucks.size() << std::endl;
//std::cout
mStream
<< "number of routes without mTrucks is " << mRoutesNoTruck.size() << std::endl;
for (unsigned int i = 0; i < mRoutesNoTruck.size(); i ++)
{
sum += routeLength(mRoutesNoTruck[i]->customerIds());
//std::cout
mStream
<< "\n number of customers in route without truck is " << mRoutesNoTruck[i]->allCustomers().size() << std::endl;
}
//std::cout
mStream
<< "sum length " << sum << std::endl;
#endif
mFinished = true;
}
int RouterManager::getProgress()
{
return mProgress->getProgress();
}
std::unordered_set<std::shared_ptr<Route> > RouterManager::customersToRoutes(const std::vector<Customer> &customers)
{
std::unordered_set<std::shared_ptr<Route>> routes;
std::unordered_map<unsigned int, Customer> pairCustomers;
for (Customer const &curCustomer: customers)
{
if (curCustomer.type == Customer::Reception)
{
#ifdef WITH_LOG
std::cout << "insert to map customer with id " << curCustomer.id << std::endl;
#endif
std::pair<unsigned int, Customer> pair(curCustomer.id, curCustomer);
pairCustomers.insert(pair);
}
}
for (Customer const &curCustomer: customers)
{
if (curCustomer.type == Customer::DeliveryFromCustomer)
{
#ifdef WITH_LOG
std::cout << "relation package " << curCustomer.id << std::endl;
#endif
if (pairCustomers.find(curCustomer.relationCustomerID) == pairCustomers.end())
{
#ifdef WITH_LOG
std::cout << "CANT FIND RELATION ID!!! " << curCustomer.relationCustomerID << std::endl;
#endif
continue;
}
Customer pairCustomer = pairCustomers.at(curCustomer.relationCustomerID);
pairCustomers.erase(curCustomer.relationCustomerID);
std::shared_ptr<Route> route = std::shared_ptr<Route>(new Route(curCustomer, pairCustomer));
routes.insert(std::shared_ptr<Route>(route));
}
else if (curCustomer.type == Customer::DeliveryFromDepot)
{
std::shared_ptr<Route> route(new Route(curCustomer));
routes.insert(route);
}
}
for (auto const &customer: pairCustomers)
{
Customer const &curCustomer = customer.second;
std::shared_ptr<Route> route(new Route(curCustomer));
routes.insert(route);
}
#ifdef WITH_LOG
std::cout << "converted customers to routes" << std::endl;
#endif
return routes;
}