FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Indicator constraints

Description
Examples of formulating indicator constraints with the Xpress Solver Python API.

Further explanation of this example: Whitepaper 'MIP formulations and linearizations', Section 'Indicator constraints'


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
indicators.py[download]





indicators.py

"""
   Xpress Python Example Problems
   ======================

   file indicators.py
    ```````````````
   Indicator constraint definition example.
   - Example discussed in mipformref whitepaper -

   (c) 2024 Fair Isaac Corporation
       author: B. Vieira, Sep. 2024
"""

import xpress as xp
from xpress.enums import SolStatus

N = 2

p = xp.problem()

# Create the decision variables
x = [p.addVariable(name="x_{}".format(i)) for i in range(N)]
b = [p.addVariable(name="b_{}".format(i),vartype=xp.binary) for i in range(N)]

# b[0] = 1 -> x[0]+x[1] >= 12
p.addIndicator(b[0] == 1, x[0] + x[1] >= 12)
# b[1] = 0 -> x[1] <= 5
p.addIndicator(b[1] == 0, x[1] <= 5)

# Set objective
p.setObjective(b[0] + b[1], sense=xp.maximize)

# Write out the model in case we want to look at it.
p.write("indicators.lp")

# Solve the problem
p.optimize()

match p.attributes.solstatus:
    case SolStatus.FEASIBLE | SolStatus.OPTIMAL:
        print("MIP solution: ", p.attributes.objval)
    case SolStatus.INFEASIBLE:
        print("Problem is infeasible")
    case SolStatus.UNBOUNDED:
        print("LP unbounded")
    case SolStatus.NOTFOUND:
        print("Solution not found")

for i in range(N):
    print(x[i].name,": ",p.getSolution(x[i]))
Back to examples browserPrevious exampleNext example