Initializing help system before first use

Solving a quadratically problem


Type: Programming
Rating: 2 (easy-medium)
Description: Solve a quadratic problem
File(s): example_quadratic.py


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")

© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.