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

Solving a quadratically problem

Description
Solve a quadratic problem

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


Source Files





example_quadratic.py

#!/bin/env python

# Solve a simple quadratic optimization problem. Given a matrix
# Q and a point x0, minimize the quadratic function
#
# x' (Q + alpha I) x
#
# subject to the linear system Q (x - x0) = 1 and nonnegativity on all
# variables. Report solution if available

from __future__ import print_function

import xpress as xp
import numpy as np

N = 10

Q = np.arange(1, N**2 + 1).reshape(N, N)
x = np.array([xp.var() for i in range(N)])
x0 = np.random.random(N)

p = xp.problem()

p.addVariable(x)

# c1 and c2 are two systems of constraints of size N each written as
#
# (x-x0)' Q = 1
#
# Qx >= 0

c1 = - xp.Dot((x - x0), Q) == 1
c2 = xp.Dot(Q, x) >= 0

# The objective function is quadratic

p.addConstraint(c1, c2)
p.setObjective(xp.Dot(x, Q + N**3 * np.eye(N), x))

p.solve("")

print("nrows, ncols:", p.attributes.rows, p.attributes.cols)
print("solution:", p.getSolution())

p.write("test5-qp", "lp")

Back to examples browserPrevious exampleNext example