Forum Discussion
FICO
3 years agoCommunity Manager
How to improve Xpress solver efficiency and time for VRP projects
Hi all,
I have a Vehicle Routing Problem (VRP) which has 7 constraints (including subtour elimination constraint, flow conservation constraint, etc). Xpress tries to apply many heuristic methods before actually solving the problem. This takes a long time, and it is not desirable.
For a typical VRP problem like this, what types of controls do you suggest me to use in order to have good optimality but also be able to solve it faster (maybe in a couple hours)?
Here is a sample of the code:
# Generate random coordinates
coordinates = np.random.rand(N_total, 2)
# Calculate Euclidean distances
dist = lambda i, j: np.sqrt((coordinates[i, 0] - coordinates[j, 0])**2 + (coordinates[i, 1] - coordinates[j, 1])**2)
distances = np.array([[dist(i, j) for j in range(N_total)] for i in range(N_total)])
# Problem data
N_pickup = 40
N_delivery = 5
N_total = N_pickup + N_delivery
max_stops_per_route = 5
num_routes = N_pickup
# Model
prob = xp.problem("Vehicle Routing")
# Decision variables
x = {(i, j, k): xp.var(vartype=xp.binary, name=f'x_{i}_{j}_{k}') for i in range(N_total) for j in range(N_total) for k in range(num_routes) if i != j}
u = {i: xp.var(vartype=xp.continuous, lb=0, ub=max_stops_per_route, name=f'u_{i}') for i in range(N_total)}
prob.addVariable(x,u)
prob.setControl('miprelstop', 0.68)
# Objective function
prob.setObjective(xp.Sum(distances[i, j] * x[i, j, k] for i in range(N_total) for j in range(N_total) for k in range(num_routes) if i != j), sense=xp.minimize)
#Constraints
# 1. At least one outgoing connection from each pickup stop
for i in range(N_pickup):
prob.addConstraint(xp.Sum(x[i,j,k] for j in range(N_total) for k in range(num_routes) if i != j) >= 1)
# 2. Each stop can be visited only once per route
for i in range(N_pickup):
for k in range(num_routes):
prob.addConstraint(xp.Sum(x[i, j, k] for j in range(N_total) if i != j) <= 1)
# 3. Flow conservation:
for k in range (num_routes):
for i in range(N_pickup):
prob.addConstraint(xp.Sum(x[i, j, k] for j in range(N_pickup) if i != j) == xp.Sum(x[ j,i, k] for j in range(N_pickup) if i != j))
# 4. To ensure there are no round-trip connections:
for k in range (num_routes):
for i in range(N_total):
for j in range(N_total):
if i != j:
prob.addConstraint(x[i, j, k] + x[ j,i, k]<=1)
# 5. All routes should end at a delivery stop
for k in range (num_routes):
prob.addConstraint(xp.Sum(x[i, j, k] for i in range(N_total) for j in range(N_pickup, N_total) if i != j) == 1)
# 6. No outbound service should come from a delivery stop
for j in range(N_pickup, N_total):
prob.addConstraint(xp.Sum(x[j, i, k] for i in range(N_total) for k in range(num_routes) if i != j) == 0)
# 7. Subtour elimination
for k in range(num_routes):
for i in range(N_pickup):
for j in range(N_total):
if i != j:
prob.addConstraint(u[i] - u[j] + N_total * x[i, j,k] <= N_total - 1)
# Solve the problem
prob.solve()
# for i in range(N_total):
# for j in range(N_total):
# for k in range(num_routes):
# if i != j and prob.getSolution(x[i, j, k]) > 1e-5:
# print(f"{i} -> {j} on route {k}")
Thank you
8 Replies
- FICOCommunity Manager
Can you please either attach data so that can generate the same model or provide the log output of the optimizer?
Without any of the two it will be hard to suggest anything.
The number of subtour elimination constraints is huge, isn't it? Did you try to separate them from a callback? Or mark them as delayed rows (see here)?
- FICOCommunity Manager
@Daniel Junglas Thank you. The code above generates random coordinates (first line) for this problem. So it basically generates the data, and the script above can be used as a complete model (I cannot provide the actual data for privacy reasons, but the model above shows the same behavior seen using real data).
Indeed, the last constraint is the one that makes the model run for a long time (i.e. if I remove it, the model generates result within 1-2 hours). But I need to introduce subtour elimination constraint otherwise the model generates subtour.
I did not try the delayed rows method, but I believe constraint (7) cannot be delayed and the model should utilize it from beginning (the chance that the model generates subtour is high). Please correct me if I am wrong.
- FICOCommunity Manager
When I run your code here (after moving the definition of N_total to the top of the file so that generation of coordinates actually works), the model solves in less than a second.
You are right that it is quite likely that the optimizer will have to use some of the subtour elimination constraints. The point is that most likely it will not need all of them (and maybe not even many of them). That is why marking them as delayed or separating them might help: this way only the constraints that are actually violated are taken into account and non-violated constraints do not slow down the solution process.
- FICOCommunity Manager
Thank you @Daniel Junglas . You are correct, N_total has to be moved to the top of the code.
In the example in OP, I put an arbitrary number for N_total (40). However, in the real model, N_total is 180 (we have 180 pick up points). If we replace 40 with 180 in N_total, the model will take a long time to find a solution. I replaced 40 with 180, and attached the output after running for more than 1 hour (and still running), and there is still no solution.
I will try to delay the subtour elimination constraint as you mentioned. I went through the documentation link you provided (problem.loaddelayedrows) to learn how to implement it. However, I am not sure how to select the specific rows that are related to the subtour elimination constraint. Can you please let me know how to see the full matrix of the problem, or how to pinpoint specific rows related to specific constraint?
Thanks
- FICOCommunity Manager
With delayed rows, your constraint 7 would look like this (one line added before and one line added after the loop):
# 7. Subtour elimination first_subtour = prob.attributes.rows for k in range(num_routes): for i in range(N_pickup): for j in range(N_total): if i != j: prob.addConstraint(u[i] - u[j] + N_total * x[i, j,k] <= N_total - 1) prob.loaddelayedrows(range(first_subtour, prob.attributes.rows))In your log file you can see that Xpress spends all the time solving the root LP relaxation and is still not finished. That is something that I think should go a lot faster once you mark the subtour elimination constraints as delayed rows (or separate them through a callback).
- FICOCommunity Manager
Thanks @Daniel Junglas . I incorporated the delayed rows as you indicated above, and I ran the code with actual data (real coordinates of 180 pick up stops), but after a couple of hours, the solver is still performing tree search. Please see the output (so far) attached.
Is there anyway to see from the output whether or not the delaying function has kicked in? Or could it be because almost all the routes have subtours, and the model needs to consider them all at the end of the day?
- FICOCommunity Manager
It looks like Xpress has a hard time finding a feasible solution. However, for your model there is a trivial solution available: since you have as many routes as pickups, you can create one route for each pickup that just goes from the pickup to the closest delivery stop:
if num_routes >= N_pickup: # Create very simple routes that just go from the pickup to the # closest delivery print('Creating trivial solution') variables = [] for i in range(num_routes): j = min(range(N_pickup, N_total), key=lambda x: distances[i, x]) variables.append(x[i, j, i]) prob.addmipsol([1] * len(variables), variables, 'trivial1')Could you try whether things get any better with this?
I also looked at your model and I am wondering some things:
- Isn't the generation of constraint 4 generating each constraint twice? Shouldn't the condition in the loop be i<j rather than i!=j? Otherwise you will generate the same constraint for (i,j) and (j,i).
- Is your model actually correct? Suppose you go from pickup p1 to pickup p2 on a route. Then your flow conservation constraint 3 requires a route p1->p2->...->p1 that does not include any delivery stop. However, constraint 5 requires that a route enters a delivery stop and constraint 6 requires that a route can never leave a delivery stop. Maybe I am missing something but it feels like your flow conservation constraint is not correct. Could you test on small models what happens if you set num_routes=N_pickup-1, i.e., if you forbid the trivial solution I have outlined above? I have the feeling that in this case your problem will be infeasible.
- FICOCommunity Manager
Thank you @Daniel Junglas . You made valid points in your post.
With regards to the trivial solution, you are correct but we are not interested in routes with only 1 stop. Also, the number of routes could change in the future (and may not be the same as number of stops). So the trivial solution will not work.
With regards to the other points:
- The distance matrix shows the distance from each stop to each stop. It is a 180 x 180 matrix. So the cells where i=j is zero in the matrix (i.e. the distance from one stop to itself is zero). That is why in the variables, I made sure that x does not include stops where i!=j. That's why we mentioned i!=j in constraint 4 (because x(i,j) where i=j is not defined in our variables and a connection from a stop to itself should not exist). But you are right, this constraint will create duplicates. So I changed the last loop to: for j in range(i+1, N_total) to prevent duplicates.
- You are correct, constraint 3 creates a loop, but constraint 6 tries to avoid loops (by ending routes at delivery stops). I will remove constraint 6 for now (to allow for loops). Also, I believe that constraint 5 should not impose any issues, as it ensures that there is a delivery stop in the route. So in this case, we can keep the current flow conservation constraint (but just eliminate constraint 6). I ran this modified model but still it takes a long time to find a feasible solution. Do you believe the subtour elimination constraint is in conflict with flow conservation constraint (i.e. former tried to avoid tours, second one creates loops/tours)?
Related Content
Related Content
How to improve Bestbound?
e.g. my optimal solution for this dataset is 116. it reaches 116 within 1000 seconds but bestbound is still close to 0. Optimization part: multiplier=ub_nal * (QUARTERS[-1] - 1) + 1# spread of coef...7 months agoPlace FICO® Xpress Optimization ForumFICO® Xpress Optimization Forum4 unread comments4 unread30Views0likes4Comments- 30Views0likes1Comment
