Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
The Vehicle Routing Problem (VRP) is a combinatorial optimization problem that aims to determine the optimal routes for a fleet of vehicles to deliver goods to a set of locations while minimizing the total distance traveled or cost incurred. The problem is NP-hard, meaning that finding an exact solution is computationally infeasible for large instances, and heuristic or approximation algorithms are often used to find near-optimal solutions. The tutorial showcases how to solve the problem using the Quantum Approximate Optimization Algorithm (QAOA). In the problem, each vehicle starts from a different depot, visits a set of cities, and returns to its depot. The goal is to minimize the total travel cost while ensuring that each city is visited exactly once by one vehicle.

Mathematical Formulation

Given:
  • A set of locations: L=0,1,…,n−1L = {0, 1, \dots, n-1} where
  • The first mm locations are depots: D=0,1,…,m−1D = {0, 1, \dots, m-1}
  • The remaining n−mn - m are cities: C=m,m+1,…,n−1C = {m, m+1, \dots, n-1}
  • Each vehicle k∈Dk \in D:
  • Starts and ends its route at its own depot kk
  • Visits a sequence of cities
  • Returns to its depot
  • Each city must be visited exactly once by one vehicle only.
  • Each vehicle route has P=p+2P = p + 2 positions (start + pp inner positions + end).
You can think about the positions as timesteps. The variable are called positions because driving between cities does not take the same amount of time.

Decision Variables

Let xu,vk∈{0,1}x_{u,v}^k \in \{0,1\} be a binary variable defined as xu,vk={1if vehicle k visits location u at position v0otherwisex_{u,v}^k = \begin{cases} 1 & \text{if vehicle } k \text{ visits location } u \text{ at position } v \\ 0 & \text{otherwise} \end{cases} where:
u∈Lu \in L (location: city or depot)
v∈0,1,…,P−1v \in {0, 1, \dots, P - 1}
k∈Dk \in D (vehicle/depot index)

Constraints

  1. Each city is visited exactly once:
∑k∈D∑v=1P−2xu,vk=1    ∀u∈C\sum_{k\in D}\sum_{v=1}^{P-2} x_{u,v}^k = 1 \,\,\,\,\forall u\in C Each city appears in exactly one vehicle’s route, and not at the start or end positions.
2.
Each car visits exactly one city in every position: ∑u∈Cxu,vk=1    ∀v∈{1,…,P−2},  ∀k∈D\sum_{u\in C} x_{u,v}^k = 1 \,\,\,\,\forall v\in \{1,\dots,P-2\},\,\,\forall k\in D Each vehicle has exactly one location assigned to each inner route position.
3.
Depot start and end positions are fixed: xk,0k=xk,P−1k=1    ∀k∈Dx_{k,0}^k = x_{k,P-1}^k = 1 \,\,\,\, \forall k\in D xu,0k=xu,P−1k=0    for   u≠kx_{u,0}^k = x_{u,P-1}^k = 0 \,\,\,\, for \,\,\, u\neq k Each vehicle starts and ends at its own depot only.
  1. No city is at the start or end:
xu,0k=xu,P−1k=1    ∀u∈C    ∀k∈Dx_{u,0}^k = x_{u,P-1}^k = 1 \,\,\,\, \forall u\in C \,\,\,\, \forall k\in D Constraints (3) and (4) do not use qubits in the QAOA circuit, because they are predetermined.

Objective Function

The aim is to minimize total travel cost, defined as min⁡x∑k∈D∑v=0P−2∑u∈L∑w∈Ldist(u,w)⋅xu,vk⋅xw,v+1k\min_x \quad \sum_{k \in D} \sum_{v = 0}^{P - 2} \sum_{u \in L} \sum_{w \in L} \text{dist}(u, w) \cdot x_{u,v}^k \cdot x_{w,v+1}^k where:
  • dist(u,w)\text{dist}(u, w) is the Euclidean distance between locations uu and ww.
  • The sum computes all transitions (u→w)(u \rightarrow w) for each vehicle between consecutive positions.

Constraint Functions

All cities are visited only once: ∑u∈C(∑k∈D∑v=0P−2xu,vk−1)2=0\sum_{u \in C} \left( \sum_{k \in D} \sum_{v=0}^{P-2} x_{u,v}^k -1 \right)^2=0 All positions are occupied only once: ∑k∈D∑v=1P−2(∑u∈Cxu,vk−1)2=0\sum_{k \in D} \sum_{v=1}^{P-2} \left(\sum_{u \in C} x_{u,v}^k -1 \right)^2=0 **These are added as penalty terms and multiplied by a scaling factor later on in the cost function. Ensure they are actually equal to zero by adding them as penalty terms.**

Generating Problem Data

Defining Positions

Assuming the cities are more or less evenly divided among the vehicles, use possible_number_of_cities_per_vehicle:
The total number of positions num_positions (or time slots) in a car route. Add 22 for the start (depot) and end (return to depot) positions:
All valid routes (time slots) positions that a vehicle can take:
  • Starting depot →0\rightarrow 0
  • Visiting cities →1,2,…,\rightarrow 1,2, \ldots, num_positions-1
  • Ending depot →\rightarrow num_positions
Output:
The inner positions are the positions that can be occupied by cities, excluding the start and end depots.
Output:

Setting City Locations

Set random locations in a square of size (0, max_x_y)×\times(0, max_x_y). To have the same result, set the random seed. To have different results, change or delete the seed:
Output:
output
Output:
The maximal cost of any candidate is num_locations×\times max_distance. The minimal cost of any candidate is num_locations×\times min_distance. Hence, this is the maximal difference of eigenvalues of the total cost Hamiltonian:
To set the maximal difference of eigenvalues of the total cost Hamiltonian to 1, scale the distances by DISTANCE_NORMALISATION:
Output:

Normalizing the Cost and Mixer Hamiltonians

This normalization improves the probability of finding good solutions. If this is too much information right now, skip it and focus on the VRP problem itself under “Defining the VRP Objective Function”. The mixer Hamiltonian is effectively X/2X/2 and has eigenvalues −0.5-0.5 and +0.5+0.5. So the difference between minimum and maximum eigenvalues is exactly
  1. You can rescale it using a global scaling parameter:
The cost Hamiltonian should be similar in eigenvalue difference to the mixer Hamiltonian and should be normalized to about
  1. The distance normalization will take care of it later.
To find the exact normalization requires solving this NP-hard problem, so always use approximation. Since it is approximate, you can tweak the relative scaling parameter.
The overall normalization of the total cost is therefore:
Output:
The constraint Hamiltonian has the property that a minimal constraint violation is 1 and no constraint violation is
  1. Normalize the constraint Hamiltonian relative to the total cost Hamiltonian such that the constraint violation is 1~2 x larger than the maximal difference between total cost values:
The overall normalization of the constraint Hamiltonian is similarly:
Output:
The normalization coefficients are hyperparameters that can be tuned to improve the performance of QAOA. Summarize the normalization coefficients together with the cost and mixer Hamiltonians: H=Cgsp⋅(β⋅Hmixer+γ⋅Cron(Hobjective+Crcn⋅Hconstraints))H = C_{gsp} \cdot \left(\beta\cdot H_{mixer} + \gamma \cdot C_{ron} \left(H_{objective} + C_{rcn}\cdot H_{constraints}\right)\right) Where β\beta and γ\gamma are the QAOA’s variational parameters. CgspC_{gsp} is the global scaling parameter, CronC_{ron} is the relative objective normalization, and CrcnC_{rcn} is the relative constraint normalization coefficients. This method normalizes both the entire Hamiltonian and each individual term - including the constraint Hamiltonian to ensure the solution satisfies the constraints, and the objective Hamiltonian to increase the probability of finding the optimal objective value.

Defining the VRP Objective Function

First, define number of functions that comprise the objective function. A major difficulty is to map 3D (city, position, vehicle) to 1D index in the array of decision variables.

Finding the Right Index in the Array

Access the value of the binary decision variable x that encodes whether city (or depot) u is visited at position v by vehicle k.
  • u - city
  • v - position (time slot)
  • x - the array of decision variables
  • k - the vehicle number
Make sure to satisfy these constraints:
  • Each city is assigned to exactly one vehicle and one route position.
  • Each inner position of a route (per vehicle) is filled by exactly one city.

Determining Total Travel Cost for All Vehicle Routes

Defining Constraints

The total cost function for the VRP is the sum of the total travel cost and the constraints:

Building the QAOA Model

Define the number of layers:

Synthesizing the Model

Output:
Output:

Executing

When you have the QAOA circuit, execute it in the hybrid scheme. First, define ExecutionSession for the optimization process and the initial parameters:
The cost function definition for the execution: For each measurement state, the cost function is calculated by the objective function of the VRP problem:
A function estimates the cost function in each iteration. Use the estimate_cost method of the ExecutionSession class:

Running the Optimization Process

Use scipy.optimize.minimize to minimize the cost function. You may increase the max_iterations to have more iterations.
Output:

Analyzing the Results

If you have more iterations you can make a convergence graph:

Optimizing Parameters

Watch for four or more circuit layers:
Output:
output Accumulate the statistics with the final optimized parameters:
Output:

Best Solution

Output:
Output:
Output:
output