| 25 | } |
| 26 | |
| 27 | void Solution::load(pyvrp::Solution const &solution) |
| 28 | { |
| 29 | // Determine offsets for vehicle types. |
| 30 | std::vector<size_t> vehicleOffset(data_.numVehicleTypes(), 0); |
| 31 | for (size_t vehType = 1; vehType < data_.numVehicleTypes(); vehType++) |
| 32 | { |
| 33 | auto const prevAvail = data_.vehicleType(vehType - 1).numAvailable; |
| 34 | vehicleOffset[vehType] = vehicleOffset[vehType - 1] + prevAvail; |
| 35 | } |
| 36 | |
| 37 | for (auto const &solRoute : solution.routes()) |
| 38 | { |
| 39 | // Determine index of next route of this type to load, where we rely |
| 40 | // on solution to be valid to not exceed the number of vehicles per |
| 41 | // vehicle type. |
| 42 | auto const idx = vehicleOffset[solRoute.vehicleType()]++; |
| 43 | auto &route = routes[idx]; |
| 44 | |
| 45 | if (route == solRoute) // then the current route is still OK and we |
| 46 | continue; // can skip inserting and updating |
| 47 | |
| 48 | // Else we need to clear the route and insert the updated route from |
| 49 | // the solution. |
| 50 | route.clear(); |
| 51 | |
| 52 | // Routes use a representation with nodes for each client, reload depot |
| 53 | // (one per trip), and start/end depots. The start depot doubles as the |
| 54 | // reload depot for the first trip. |
| 55 | route.reserve(solRoute.size() + solRoute.numTrips() + 1); |
| 56 | |
| 57 | for (size_t tripIdx = 0; tripIdx != solRoute.numTrips(); ++tripIdx) |
| 58 | { |
| 59 | auto const &trip = solRoute.trip(tripIdx); |
| 60 | |
| 61 | if (tripIdx != 0) // then we first insert a trip delimiter. |
| 62 | { |
| 63 | Route::Node depot = {trip.startDepot()}; |
| 64 | route.push_back(&depot); |
| 65 | } |
| 66 | |
| 67 | for (auto const client : trip) |
| 68 | route.push_back(&nodes[client]); |
| 69 | } |
| 70 | |
| 71 | route.update(); |
| 72 | } |
| 73 | |
| 74 | // Finally, we clear any routes that we have not re-used or inserted from |
| 75 | // the solution. |
| 76 | size_t firstOfType = 0; |
| 77 | for (size_t vehType = 0; vehType != data_.numVehicleTypes(); ++vehType) |
| 78 | { |
| 79 | auto const numAvailable = data_.vehicleType(vehType).numAvailable; |
| 80 | auto const firstOfNextType = firstOfType + numAvailable; |
| 81 | for (size_t idx = vehicleOffset[vehType]; idx != firstOfNextType; ++idx) |
| 82 | routes[idx].clear(); |
| 83 | |
| 84 | firstOfType = firstOfNextType; |