Initializing help system before first use

Firestns: A set-covering model for emergency service provision


Type: Set covering
Rating: 1 (simple)
Description: Minimize the total number of firestations that are required to service a set of towns within a specified permissible travel time.
File(s): fstns.py


fstns.py
'''*******************************************************
  * Python Example Problems                             *
  *                                                     *
  * file fstns.py                                       *
  * Example for the use of the Python language          *
  * (Firestation siting problem)                        *
  *                                                     *
  * (c) 2018-2025 Fair Isaac Corporation                *
  *******************************************************'''

import xpress as xp

RTown = range(6)  # Range of towns
TIMELIMIT = 20    # Max. time allowed to reach any town

TIME = [[0, 15, 25, 35, 35, 25], [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]]

'''This sets SERVE(t,s) to true if the time between the two towns is
   within the time limit. We can then use SERVE to define a set of
   constraints (see below). It is as well possible not to use the
   array SERVE and move the test directly into the definition of the
   constraints.
'''

SERVE = [[TIME[t][s] <= TIMELIMIT for t in RTown] for s in RTown]

p = xp.problem()

# 1 if ambulance at town; 0 if not
openst = [p.addVariable(vartype=xp.binary) for _ in RTown]

# Objective: minimize number fire stations
p.setObjective(xp.Sum(openst[s] for s in RTown))

# Serve each town t by an open station s
p.addConstraint(xp.Sum(openst[s] for s in RTown if SERVE[t][s]) >= 1
                for t in RTown)

p.optimize()  # Solve the MIP-problem

# Print out the solution
print("Solution:\n Minimum number of firestations: ", p.attributes.objval)
for s in RTown:
    print(" open(", s, "): ", p.getSolution(openst[s]), sep='')
print("\n      ", end='')
for s in RTown:
    print(s, " ", end='', sep='')
print('')
for t in RTown:
    if p.getSolution(openst[t]) == 1:
        print(" ", t, ": ", end='')
        for s in RTown:
            if SERVE[t][s]:
                print("Y ", end='')
            else:
                print(". ", end='')
        print('')

© 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.