Initializing help system before first use

Firestation location problem using SciPy sparse arrays


Type: Programming
Rating: 2 (easy-medium)
Description: Shows how to use the xp.Dot operator with a sparse SciPy matrix for a location problem.
File(s): firestation_scipy.py


firestation_scipy.py
# Fire station location example using SciPy sparse arrays for efficiency.
#
# The fire station location problem attemps to minimize the number of fire stations
# to build amongst a set of towns, with each town being a candidate for
# hosting a fire station. Each town must be served by a fire station built
# on a town with a travel time no longer than a defined threshold (e.g. 15 minutes).
# In this example, we solve the location problem using a SciPy sparse
# matrix with the xpress.Dot() operator for efficiency.
#
# (C) 1983-2025 Fair Isaac Corporation

import xpress as xp
import numpy as np
import scipy

num_towns = 6     # Number of towns.

t_time = np.array([[ 0, 15, 25, 35, 35, 25],     # Travel times between towns.
                   [15,  0, 30, 40, 25, 15],
                   [25, 30,  0, 20, 30, 25],
                   [35, 40, 20,  0, 20, 30],
                   [35, 25, 35, 20,  0, 19],
                   [25, 15, 25, 30, 19,  0]])

avail = (t_time <= 15).astype(int)                # NumPy array of binary values equal to 1 if t_time <= 15.

avail_sparse = scipy.sparse.csr_array(avail)      # Convert to SciPy sparse matrix format.

# Print travel times in both formats for comparison.
print("NumPy format: ", avail, sep="\n")
print("SciPy format: ", avail_sparse, sep="\n")

p = xp.problem()      # Create Xpress problem.

x = p.addVariables(num_towns, vartype=xp.binary)  # Create NumPy array of variables.

# Serve all towns, amongst those eligible to be selected for each.
p.addConstraint(xp.Dot(avail_sparse,x) >= 1)      # Creates set of constraints with RHS = 1.

p.setObjective(xp.Sum(x))                         # Minimize number of towns selected for a fire station.

p.optimize()

# Print solution.
print("Number of stations: ", round(p.attributes.objval))
xsol = p.getSolution(x)
print("Located at towns",[s+1 for s in range(num_towns) if xsol[s] >= 0.99])

© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.