Files
ortools-clone/examples/tests/issue117.py
2015-12-03 08:58:56 +01:00

64 lines
1.8 KiB
Python

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)
routing = pywrapcp.RoutingModel(3, VEHICLE_COUNT)
routing.SetDepot(0)
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 length(customer1, customer2):
#static just for the sake of the example
return 1
routing.SetCost(length)
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)