Spaces:
Runtime error
Runtime error
Update google_solver/time_window_solver.py
Browse files- google_solver/time_window_solver.py +39 -106
google_solver/time_window_solver.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
from __future__ import print_function
|
| 3 |
from ortools.constraint_solver import routing_enums_pb2
|
| 4 |
from ortools.constraint_solver import pywrapcp
|
|
@@ -6,63 +5,6 @@ from just_time_windows.dataloader import VRP_Dataset
|
|
| 6 |
from just_time_windows.google_solver.convert_data import convert_data
|
| 7 |
|
| 8 |
|
| 9 |
-
dataset = VRP_Dataset(dataset_size=1, num_nodes=20, num_depots=1)
|
| 10 |
-
batch = dataset.get_batch(0, 1)
|
| 11 |
-
|
| 12 |
-
data = convert_data(batch, scale_factor=100)
|
| 13 |
-
data = data[0]
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def create_data_model():
|
| 17 |
-
"""Stores the data for the problem."""
|
| 18 |
-
data = {}
|
| 19 |
-
data['time_matrix'] = [
|
| 20 |
-
[0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7],
|
| 21 |
-
[6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14],
|
| 22 |
-
[9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9],
|
| 23 |
-
[8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16],
|
| 24 |
-
[7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14],
|
| 25 |
-
[3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8],
|
| 26 |
-
[6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5],
|
| 27 |
-
[2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10],
|
| 28 |
-
[3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6],
|
| 29 |
-
[2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5],
|
| 30 |
-
[6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4],
|
| 31 |
-
[6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10],
|
| 32 |
-
[4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8],
|
| 33 |
-
[4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6],
|
| 34 |
-
[5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2],
|
| 35 |
-
[9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9],
|
| 36 |
-
[7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0],
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
data['time_windows'] = [
|
| 41 |
-
(0, 0), # depot
|
| 42 |
-
(7, 12), # 1
|
| 43 |
-
(10, 15), # 2
|
| 44 |
-
(16, 18), # 3
|
| 45 |
-
(10, 13), # 4
|
| 46 |
-
(0, 5), # 5
|
| 47 |
-
(5, 10), # 6
|
| 48 |
-
(0, 4), # 7
|
| 49 |
-
(5, 10), # 8
|
| 50 |
-
(0, 3), # 9
|
| 51 |
-
(10, 16), # 10
|
| 52 |
-
(10, 15), # 11
|
| 53 |
-
(0, 5), # 12
|
| 54 |
-
(5, 10), # 13
|
| 55 |
-
(7, 8), # 14
|
| 56 |
-
(10, 15), # 15
|
| 57 |
-
(11, 15), # 16
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
-
data['num_vehicles'] = 4
|
| 61 |
-
data['depot'] = 0
|
| 62 |
-
return data
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
def compute_total_time(data, routing, assignment):
|
| 67 |
time_dimension = routing.GetDimensionOrDie('Time')
|
| 68 |
total_time = 0
|
|
@@ -75,95 +17,86 @@ def compute_total_time(data, routing, assignment):
|
|
| 75 |
return total_time
|
| 76 |
|
| 77 |
|
| 78 |
-
|
| 79 |
def print_solution(data, manager, routing, assignment):
|
| 80 |
-
"""Prints assignment on console."""
|
| 81 |
time_dimension = routing.GetDimensionOrDie('Time')
|
| 82 |
total_time = 0
|
| 83 |
for vehicle_id in range(data['num_vehicles']):
|
| 84 |
index = routing.Start(vehicle_id)
|
| 85 |
-
plan_output = 'Route for vehicle {}:\n'
|
| 86 |
while not routing.IsEnd(index):
|
| 87 |
time_var = time_dimension.CumulVar(index)
|
| 88 |
-
plan_output += '{
|
| 89 |
-
manager.IndexToNode(index), assignment.Min(time_var),
|
| 90 |
-
assignment.Max(time_var))
|
| 91 |
index = assignment.Value(routing.NextVar(index))
|
| 92 |
time_var = time_dimension.CumulVar(index)
|
| 93 |
-
plan_output += '{
|
| 94 |
-
|
| 95 |
-
assignment.Max(time_var))
|
| 96 |
-
plan_output += 'Time of the route: {}min\n'.format(
|
| 97 |
-
assignment.Min(time_var))
|
| 98 |
print(plan_output)
|
| 99 |
total_time += assignment.Min(time_var)
|
| 100 |
-
print('Total time of all routes: {}min'
|
| 101 |
|
| 102 |
|
| 103 |
-
def adjust_time_windows(
|
| 104 |
-
|
| 105 |
-
for x in D:
|
| 106 |
-
A = (x[0] + 10, x[1] + 10)
|
| 107 |
-
L.append(A)
|
| 108 |
-
return L
|
| 109 |
|
| 110 |
|
| 111 |
def main():
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
routing = pywrapcp.RoutingModel(manager)
|
| 118 |
|
| 119 |
def time_callback(from_index, to_index):
|
| 120 |
-
"""Returns the travel time between the two nodes."""
|
| 121 |
-
# Convert from routing variable Index to time matrix NodeIndex.
|
| 122 |
from_node = manager.IndexToNode(from_index)
|
| 123 |
to_node = manager.IndexToNode(to_index)
|
| 124 |
return data['time_matrix'][from_node][to_node]
|
| 125 |
|
| 126 |
transit_callback_index = routing.RegisterTransitCallback(time_callback)
|
| 127 |
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
|
| 128 |
-
|
| 129 |
routing.AddDimension(
|
| 130 |
transit_callback_index,
|
| 131 |
-
10000,
|
| 132 |
-
10000,
|
| 133 |
-
False,
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
# Add time window constraints for each location except depot.
|
| 138 |
for location_idx, time_window in enumerate(data['time_windows']):
|
| 139 |
-
if location_idx == 0:
|
| 140 |
-
continue
|
| 141 |
index = manager.NodeToIndex(location_idx)
|
| 142 |
-
|
| 143 |
-
time_dimension.CumulVar(index).SetRange(a, b)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
for vehicle_id in range(data['num_vehicles']):
|
| 147 |
index = routing.Start(vehicle_id)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
time_dimension.CumulVar(index).SetRange(a, b)
|
| 151 |
|
| 152 |
for i in range(data['num_vehicles']):
|
| 153 |
-
routing.AddVariableMinimizedByFinalizer(
|
| 154 |
-
|
| 155 |
-
routing.AddVariableMinimizedByFinalizer(
|
| 156 |
-
time_dimension.CumulVar(routing.End(i)))
|
| 157 |
|
| 158 |
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
|
| 159 |
-
search_parameters.first_solution_strategy =
|
| 160 |
-
|
| 161 |
assignment = routing.SolveWithParameters(search_parameters)
|
|
|
|
| 162 |
if assignment:
|
| 163 |
print_solution(data, manager, routing, assignment)
|
| 164 |
total_time = compute_total_time(data, routing, assignment)
|
| 165 |
-
print(total_time)
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
if __name__ == '__main__':
|
| 169 |
-
|
|
|
|
|
|
|
| 1 |
from __future__ import print_function
|
| 2 |
from ortools.constraint_solver import routing_enums_pb2
|
| 3 |
from ortools.constraint_solver import pywrapcp
|
|
|
|
| 5 |
from just_time_windows.google_solver.convert_data import convert_data
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def compute_total_time(data, routing, assignment):
|
| 9 |
time_dimension = routing.GetDimensionOrDie('Time')
|
| 10 |
total_time = 0
|
|
|
|
| 17 |
return total_time
|
| 18 |
|
| 19 |
|
|
|
|
| 20 |
def print_solution(data, manager, routing, assignment):
|
|
|
|
| 21 |
time_dimension = routing.GetDimensionOrDie('Time')
|
| 22 |
total_time = 0
|
| 23 |
for vehicle_id in range(data['num_vehicles']):
|
| 24 |
index = routing.Start(vehicle_id)
|
| 25 |
+
plan_output = f'Route for vehicle {vehicle_id}:\n'
|
| 26 |
while not routing.IsEnd(index):
|
| 27 |
time_var = time_dimension.CumulVar(index)
|
| 28 |
+
plan_output += f'{manager.IndexToNode(index)} Time({assignment.Min(time_var)},{assignment.Max(time_var)}) -> '
|
|
|
|
|
|
|
| 29 |
index = assignment.Value(routing.NextVar(index))
|
| 30 |
time_var = time_dimension.CumulVar(index)
|
| 31 |
+
plan_output += f'{manager.IndexToNode(index)} Time({assignment.Min(time_var)},{assignment.Max(time_var)})\n'
|
| 32 |
+
plan_output += f'Time of the route: {assignment.Min(time_var)} min\n'
|
|
|
|
|
|
|
|
|
|
| 33 |
print(plan_output)
|
| 34 |
total_time += assignment.Min(time_var)
|
| 35 |
+
print(f'Total time of all routes: {total_time} min')
|
| 36 |
|
| 37 |
|
| 38 |
+
def adjust_time_windows(windows, offset=10):
|
| 39 |
+
return [(start + offset, end + offset) for start, end in windows]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def main():
|
| 43 |
+
# إعداد مجموعة بيانات لاختبار نموذج time windows
|
| 44 |
+
dataset = VRP_Dataset(dataset_size=1, num_nodes=20, num_depots=1)
|
| 45 |
+
batch = dataset.get_batch(0, 1)
|
| 46 |
+
data = convert_data(batch, scale_factor=100)[0]
|
| 47 |
+
|
| 48 |
+
manager = pywrapcp.RoutingIndexManager(
|
| 49 |
+
len(data['time_matrix']),
|
| 50 |
+
data['num_vehicles'],
|
| 51 |
+
data['depot']
|
| 52 |
+
)
|
| 53 |
routing = pywrapcp.RoutingModel(manager)
|
| 54 |
|
| 55 |
def time_callback(from_index, to_index):
|
|
|
|
|
|
|
| 56 |
from_node = manager.IndexToNode(from_index)
|
| 57 |
to_node = manager.IndexToNode(to_index)
|
| 58 |
return data['time_matrix'][from_node][to_node]
|
| 59 |
|
| 60 |
transit_callback_index = routing.RegisterTransitCallback(time_callback)
|
| 61 |
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
|
| 62 |
+
|
| 63 |
routing.AddDimension(
|
| 64 |
transit_callback_index,
|
| 65 |
+
10000,
|
| 66 |
+
10000,
|
| 67 |
+
False,
|
| 68 |
+
'Time'
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
time_dimension = routing.GetDimensionOrDie('Time')
|
| 72 |
|
| 73 |
+
# تعيين نوافذ زمنية للعقد
|
|
|
|
| 74 |
for location_idx, time_window in enumerate(data['time_windows']):
|
|
|
|
|
|
|
| 75 |
index = manager.NodeToIndex(location_idx)
|
| 76 |
+
time_dimension.CumulVar(index).SetRange(int(time_window[0]), int(time_window[1]))
|
|
|
|
| 77 |
|
| 78 |
+
# تعيين نوافذ زمنية لنقاط بداية المركبات
|
| 79 |
for vehicle_id in range(data['num_vehicles']):
|
| 80 |
index = routing.Start(vehicle_id)
|
| 81 |
+
depot_window = data['time_windows'][0]
|
| 82 |
+
time_dimension.CumulVar(index).SetRange(int(depot_window[0]), int(depot_window[1]))
|
|
|
|
| 83 |
|
| 84 |
for i in range(data['num_vehicles']):
|
| 85 |
+
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.Start(i)))
|
| 86 |
+
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.End(i)))
|
|
|
|
|
|
|
| 87 |
|
| 88 |
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
|
| 89 |
+
search_parameters.first_solution_strategy = routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
|
| 90 |
+
|
| 91 |
assignment = routing.SolveWithParameters(search_parameters)
|
| 92 |
+
|
| 93 |
if assignment:
|
| 94 |
print_solution(data, manager, routing, assignment)
|
| 95 |
total_time = compute_total_time(data, routing, assignment)
|
| 96 |
+
print(f"\nFinal total time: {total_time} min")
|
| 97 |
+
else:
|
| 98 |
+
print("No solution found.")
|
| 99 |
|
| 100 |
|
| 101 |
if __name__ == '__main__':
|
| 102 |
+
main()
|