File size: 4,043 Bytes
78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac 78691bb bcca2ac |
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 |
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
from just_time_windows.dataloader import VRP_Dataset
from just_time_windows.google_solver.convert_data import convert_data
def compute_total_time(data, routing, assignment):
time_dimension = routing.GetDimensionOrDie('Time')
total_time = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
while not routing.IsEnd(index):
index = assignment.Value(routing.NextVar(index))
time_var = time_dimension.CumulVar(index)
total_time += assignment.Min(time_var)
return total_time
def print_solution(data, manager, routing, assignment):
time_dimension = routing.GetDimensionOrDie('Time')
total_time = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = f'Route for vehicle {vehicle_id}:\n'
while not routing.IsEnd(index):
time_var = time_dimension.CumulVar(index)
plan_output += f'{manager.IndexToNode(index)} Time({assignment.Min(time_var)},{assignment.Max(time_var)}) -> '
index = assignment.Value(routing.NextVar(index))
time_var = time_dimension.CumulVar(index)
plan_output += f'{manager.IndexToNode(index)} Time({assignment.Min(time_var)},{assignment.Max(time_var)})\n'
plan_output += f'Time of the route: {assignment.Min(time_var)} min\n'
print(plan_output)
total_time += assignment.Min(time_var)
print(f'Total time of all routes: {total_time} min')
def adjust_time_windows(windows, offset=10):
return [(start + offset, end + offset) for start, end in windows]
def main():
# إعداد مجموعة بيانات لاختبار نموذج time windows
dataset = VRP_Dataset(dataset_size=1, num_nodes=20, num_depots=1)
batch = dataset.get_batch(0, 1)
data = convert_data(batch, scale_factor=100)[0]
manager = pywrapcp.RoutingIndexManager(
len(data['time_matrix']),
data['num_vehicles'],
data['depot']
)
routing = pywrapcp.RoutingModel(manager)
def time_callback(from_index, to_index):
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['time_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(time_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
routing.AddDimension(
transit_callback_index,
10000,
10000,
False,
'Time'
)
time_dimension = routing.GetDimensionOrDie('Time')
# تعيين نوافذ زمنية للعقد
for location_idx, time_window in enumerate(data['time_windows']):
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(int(time_window[0]), int(time_window[1]))
# تعيين نوافذ زمنية لنقاط بداية المركبات
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
depot_window = data['time_windows'][0]
time_dimension.CumulVar(index).SetRange(int(depot_window[0]), int(depot_window[1]))
for i in range(data['num_vehicles']):
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.Start(i)))
routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.End(i)))
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
assignment = routing.SolveWithParameters(search_parameters)
if assignment:
print_solution(data, manager, routing, assignment)
total_time = compute_total_time(data, routing, assignment)
print(f"\nFinal total time: {total_time} min")
else:
print("No solution found.")
if __name__ == '__main__':
main()
|