# 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())
|
# General constraints example using the Xpress And/Or operators and
# the problem.addgencons() API method.
#
# Solves a simple SAT problem by finding the solution with the fewest
# 'True' variables that satisfy all clauses.
#
# (C) 1983-2025 Fair Isaac Corporation
import xpress as xp
formulate_using_andor = True # If True - will use xpress operators, else - will use problem.addgencons.
p = xp.problem()
N = 10
k = 5
x = [p.addVariable(vartype=xp.binary) for _ in range(N)]
if formulate_using_andor:
# Here we use the And/Or operators of the Python interface to create a new
# optimization problem.
# At most one of each pair can be True.
con0 = [(x[i] & x[i+1]) == 0 for i in range(0, N-1, 2)]
# At least a quarter of all OR clauses on continuous groups of k
# clauses must be True.
con1 = xp.Sum(xp.Or(*(x[i:i+k])) for i in range(N-k)) >= N/4
p.addConstraint(con0, con1)
else:
# Here we use the API function problem.addgencons().
# Creates a continuous list despite the 2 step in range().
y_and = [p.addVariable(vartype=xp.binary) for i in range(0, N - 1, 2)]
y_or = [p.addVariable(vartype=xp.binary) for i in range(N - k)]
p.addGenCons([xp.gencons_and] * (N // 2) + [xp.gencons_or] * (N - k),
y_and + y_or, # two list of resultants
[2 * i for i in range(N // 2)] + # colstart is the list [0, 2, 4...] for the AND constraint
[2 * (N // 2) + k * i for i in range(N - k)], # ... and then the list [0, k, 2*k...] displaced by 2N
x[:2 * (N // 2)] + # consider all original variables in this order
[x[i + j] for j in range(k) for i in range(N - k)]) # and then variables [0..k-1, 1..k, 2..k+1, ...]
# Set time limit to 5 seconds.
p.controls.timelimit = 5
p.optimize()
print("Solution: x = ", p.getSolution())
|
# General constraints example using the xpress.max() operator and
# the problem.addgencons() API method.
#
# Finds the point that minimizes the maximum variable within a given
# polytope.
#
# (C) 1983-2025 Fair Isaac Corporation
import xpress as xp
formulate_using_max = True # If True - will use xpress.max, 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_max:
# Here we use the max operator of the Python interface to create a new
# optimization problem.
# Change objective function to the maximum of all variables, to
# be minimized.
p.setObjective (xp.max(x))
else:
# Here we use the API function problem.addgencons().
# First create a variable that will be used in the
# call to problem.addgencons() and in the objective function.
max_x = p.addVariable()
p.addGenCons([xp.gencons_max], [max_x], [0], x)
# Change objective function to the maximum of all variables, to
# be minimized.
p.setObjective(max_x)
# Set time limit to 5 seconds.
p.controls.timelimit = 5
p.optimize()
print("Solution: x = ", p.getSolution())
|