Taxi Matching Problem¶
Let's solve the taxi matching problem as an example of a problem that uses both an objective function
and
constraints.
The taxi matching problem is the problem of minimizing the cost of dispatching a taxi given multiple
customers and multiple taxi locations respectively.
The cost of dispatching a taxi can be defined in various ways, but for simplicity, we will assume
that it
is the total distance between the taxi and the customer. By matching taxis and customers, we can
decide
where to dispatch the taxi so as to minimize the total distance between each taxi and the destination
customer.
First of all, the assumption of the problem here is that there are $N$ customers and the same number
of
$N$ taxis. Suppose that we are given the coordinates $(c_{i,x}, c_{i,y})$ of the customers and
$(t_{j,x},
t_{j,y})$ of the taxis with indices $i, j = 0, 1, \cdots, N -1$. From these coordinates, let the
distance
between customer $i$ and taxi $j$ be the following:
$$
d_{ij} = \sqrt{(c_{i,x} - t_{j,x})^2 + (c_{i,y} - t_{j,y})^2}
$$
Decision Variable¶
The relation between customer $i$ and taxi $j$ can be divided into the following two patterns:
- Customer $i$ is assigned a taxi $j$
- Taxi $j$ is not assigned to customer $i$
We use the binary variable $q_{ij}$ to represent these two states.
- When taxi $j$ is assigned to customer $i$, $q_{ij} = 1$
- When no taxi $j$ is assigned to customer $i$, $q_{ij} = 0$
Customer \ Taxi |
$0$ |
$1$ |
... |
$N-1$ |
$0$ |
$q_{0,0}$ |
$q_{0,1}$ |
... |
$q_{0,N-1}$ |
$1$ |
$q_{1,0}$ |
$q_{1,1}$ |
... |
$q_{1,N-1}$ |
$\vdots$ |
$\vdots$ |
$\vdots$ |
... |
$\vdots$ |
$N -1$ |
$q_{N-1,0}$ |
$q_{N-1,1}$ |
... |
$q_{N-1,N-1}$ |
Objective Function¶
Using the above binary variables, the objective function, which is the total distance between the
matched
customer and the taxi, is given by following:
Since the variable $q_{ij}$ means that customer $i$ and taxi $j$ are matched when $1$, we only add up
the
distances that result in $q_{ij} = 1$.
$$
\sum_{i, j=0}^{N-1}d_{ij}q_{ij}
$$
Constraint¶
The next step is to define the constraints.
First, since we always assign one taxi to one customer, we need the following constraint for customer
$i$.
$$
\sum_{j=0}^{N -1}q_{ij} = 1
$$
In addition, since one taxi is always assigned to one customer, we also need the following constraint
for
taxi $j$:
$$
\sum_{i=0}^{N -1}q_{ij} = 1
$$
Implementing the Problem¶
Since we need the coordinates of the customers and the taxies as input data, we will create a
function
that randomly generates the coordinates of the customers and the taxies and calculates the distances
for
all combinations of customers and taxies.