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

Writing and reading problem matrix files

Description
The file write_read.py creates a few variables, then builds a problem and saves it to a file before re-reading that file into a new problem. The file getmatrix.py shows how to retrieve the coefficient matrix, objective coefficients, and constraints' right-hand sides for a given problem.

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

WriteRead_python.zip[download all files]

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





getmatrix.py

# Example to show how to retrieve the coefficient matrix from a
# problem.
#
# (C) 1983-2025 Fair Isaac Corporation

import xpress as xp
import scipy.sparse

p = xp.problem()

p.readProb('Data/prob1.lp')

# Obtain matrix representation of the coefficient matrix for problem.

beg, ind, coef = p.getRows(0, p.attributes.rows - 1)

# Create a Compressed Sparse Row (CSR) format matrix using the data
# from getRows.

A = scipy.sparse.csr_matrix((coef, ind, beg))

# Convert the CSR matrix to a NumPy array of arrays, so that each row
# is a (non-compressed) array.

M = A.toarray()

print(A)
print(M)

c = p.getObj(0, p.attributes.cols - 1)
b = p.getRHS(0, p.attributes.rows - 1)

print(b)
print(c)

Back to examples browserPrevious exampleNext example