(where: Path, data: ProblemData, result: Result)
| 39 | |
| 40 | |
| 41 | def write_solution(where: Path, data: ProblemData, result: Result): |
| 42 | with open(where, "w") as fh: |
| 43 | if data.num_vehicle_types == 1: |
| 44 | for idx, route in enumerate(result.best.routes(), 1): |
| 45 | visits = [str(visit.location) for visit in route.schedule()] |
| 46 | visits = visits[1:-1] # skip start and end depots |
| 47 | fh.write(f"Route #{idx}: {' '.join(visits)}\n") |
| 48 | |
| 49 | fh.write(f"Cost: {round(result.cost(), 2)}\n") |
| 50 | return |
| 51 | |
| 52 | # Since there are multiple vehicle types, we need to take some care |
| 53 | # to assign the routes to the proper vehicle. We print all routes, |
| 54 | # including empty ones. The route indices correspond to the vehicles. |
| 55 | type2vehicle = [ |
| 56 | (int(vehicle) for vehicle in vehicle_type.name.split(",")) |
| 57 | for vehicle_type in data.vehicle_types() |
| 58 | ] |
| 59 | |
| 60 | routes = [f"Route #{idx + 1}:" for idx in range(data.num_vehicles)] |
| 61 | for route in result.best.routes(): |
| 62 | visits = [str(visit.location) for visit in route.schedule()] |
| 63 | visits = visits[1:-1] # skip start and end depots |
| 64 | |
| 65 | vehicle = next(type2vehicle[route.vehicle_type()]) |
| 66 | routes[vehicle] += " " + " ".join(visits) |
| 67 | |
| 68 | fh.writelines(route + "\n" for route in routes) |
| 69 | fh.write(f"Cost: {round(result.cost(), 2)}\n") |
| 70 | |
| 71 | |
| 72 | def _solve( |
no test coverage detected