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

Create a problem with general constraints that use operator abs

Description
Create a simple problem using the modeling methods abs, And and max, as well as their corresponding modeling constructs using the API method problem.addgencons, for creating general constraints.

Further explanation of this example: 'Xpress Python Reference Manual'

general_constraints.zip[download all files]

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





general_constraints_abs.py

# General constraints example using the xpress.abs() operator and
# the problem.addgencons() API method.
#
# Find the point that minimizes the l-1 norm within a given polytope,
# i.e. the sum of the absolute values of the coordinates of a point in
# a polytope.
#
# (C) 1983-2025 Fair Isaac Corporation

import xpress as xp

formulate_using_abs = True  # If True - will use xpress.abs, else - will use problem.addgencons.

p = xp.problem()

# Read data from a problem of MIPLIB 2017.
p.readProb('Data/pk1.mps.gz')

# Retrieve all variables of the original problem.
x = p.getVariable()

if formulate_using_abs:
    # Here we use the abs operator of the Python interface to define the
    # objective function.

    # Change objective function to the l-1 norm of the variable vector, to
    # be minimized.
    p.setObjective (xp.Sum(xp.abs(v) for v in x))
else:
    # Here we do so by using the API function problem.addgencons().

    # First create a set of variables which will be used in the
    # call to problem.addgencons(), and in the objective function later.
    abs_x = [p.addVariable() for v in x]

    N = len(x)

    p.addGenCons([xp.gencons_abs] * N, abs_x, [i for i in range(N)], x)

    # Change objective function to the l-1 norm of the variable vector, to
    # be minimized.
    p.setObjective(xp.Sum(abs_x))

# Set time limit to 5 seconds.
p.controls.timelimit = 5
p.optimize()

print("Solution:", p.getSolution())

Back to examples browserPrevious exampleNext example