forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue117.py
executable file
·65 lines (48 loc) · 1.68 KB
/
issue117.py
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
#!/usr/bin/env python3
from collections import namedtuple
from ortools.constraint_solver import pywrapcp
VEHICLE_COUNT = 30
VEHICLE_CAPACITY = 200
Customer = namedtuple("Customer", ['index', 'demand', 'x', 'y'])
print('Init')
customers = list()
customers.append(Customer(0, 0, 0, 0))
customers.append(Customer(1, 1, 1.0, 1.0))
customers.append(Customer(1, 1, 2.0, 2.0))
customer_count = len(customers)
manager = pywrapcp.RoutingIndexManager(3, VEHICLE_COUNT, 0)
routing = pywrapcp.RoutingModel(manager)
print('Demand Constraint')
demands = []
for i in range(0, customer_count):
demands.append(customers[i][1])
routing.AddVectorDimension(demands, VEHICLE_CAPACITY, True, "Demand")
print('Adding Costs')
def distance_callback(from_index, to_index):
#static just for the sake of the example
return 1
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
routing.CloseModel()
assignment = routing.Solve(None)
# Inspect solution and extract routes
routes = []
for i in range(0, routing.vehicles()):
route_number = i
routes.append([])
node = routing.Start(route_number)
route = []
route.append(0)
if routing.IsVehicleUsed(assignment, i):
while True:
node = assignment.Value(routing.NextVar(node))
if not routing.IsEnd(node):
route.append(int(node))
else:
break
route.append(0)
routes[route_number].append(route)
#This are the routes as list of lists
routes = [el[0] for el in routes]
#Now try to read the routes into a new assigment object fails
assignment2 = routing.ReadAssignmentFromRoutes(routes, True)