Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if example == 'random':
# Random Example
nx = 100
neq = 10
nineq = 20
# Generate random Matrices
Qt = sp.randn(nx, nx)
Q = spspa.csc_matrix(np.dot(Qt.T, Qt))
c = sp.randn(nx)
Aeq = spspa.csc_matrix(sp.randn(neq, nx))
beq = sp.randn(neq)
Aineq = spspa.csc_matrix(sp.randn(nineq, nx))
bineq = 100 * sp.rand(nineq)
lb = 0. * np.ones(nx)
ub = 5. * np.ones(nx)
p = qp.quadprogProblem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
else:
assert False, "Unknown example"
for i in range(2):
if example == 'random' and i >= 1:
# Reuse factorizations
c = sp.randn(nx)
beq = sp.randn(neq)
bineq = 100 * sp.rand(nineq)
p = qp.quadprogProblem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
# Solve with GUROBI
resultsGUROBI = p.solve(solver=GUROBI, OutputFlag=0)
# Solve with OSQP
if i == 0:
# Load file
m = spio.loadmat(f)
# Convert matrices
Q = m['Q'].astype(float)
c = m['c'].T.flatten().astype(float)
Aeq = m['A'].astype(float)
beq = m['ru'].T.flatten().astype(float)
lb = m['lb'].T.flatten().astype(float)
ub = m['ub'].T.flatten().astype(float)
nx = Q.shape[0]
Aineq = spspa.csc_matrix(np.zeros((1, nx)))
bineq = np.array([0.0])
# Define problem
p = qp.quadprogProblem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
return p
beq = sp.randn(neq)
Aineq = spspa.csc_matrix(sp.randn(nineq, nx))
bineq = 100 * sp.rand(nineq)
lb = 0. * np.ones(nx)
ub = 5. * np.ones(nx)
p = qp.quadprogProblem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
else:
assert False, "Unknown example"
for i in range(2):
if example == 'random' and i >= 1:
# Reuse factorizations
c = sp.randn(nx)
beq = sp.randn(neq)
bineq = 100 * sp.rand(nineq)
p = qp.quadprogProblem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
# Solve with GUROBI
resultsGUROBI = p.solve(solver=GUROBI, OutputFlag=0)
# Solve with OSQP
if i == 0:
options = {'eps_abs': 0.,
'eps_rel': 0.,
'splitting': 2,
# 'kkt_method': 'indirect',
# 'kkt_ind_alg': 'gmres',
# 'kkt_ind_tol': 1e-5,
# 'kkt_ind_maxiter': 10
}
probOSQP = osqp.OSQP(**options)
probOSQP.problem(Q, c, Aeq, beq, Aineq, bineq, lb, ub)
# q = np.array([1., 1.])
A = spspa.csc_matrix(np.array([[-1, 0], [0, -1], [-1, -3],
[2, 5], [3, 4]]))
uA = np.array([0., 0., -15, 100, 80])
# uA = np.array([-2., 0., -20, 100, 80])
lA = -np.inf * np.ones(len(uA))
p = qp.quadprogProblem(P, q, A, lA, uA)
elif example == 'infeasible':
# Infeasible example
# P = spspa.eye(2)
P = spspa.csc_matrix((2, 2))
q = np.ones(2)
A = spspa.csc_matrix(np.array([[1, 0], [0, 1], [1, 1]]))
lA = np.array([0., 0., -1.])
uA = np.array([np.inf, np.inf, -1.])
p = qp.quadprogProblem(P, q, A, lA, uA)
elif example == 'unbounded':
# Unbounded example
P = spspa.csc_matrix((2, 2))
q = np.array([2, -1])
A = spspa.eye(2)
lA = np.array([0., 0.])
uA = np.array([np.inf, np.inf])
p = qp.quadprogProblem(P, q, A, lA, uA)
elif example == 'random':
# Random Example
n = 30
m = 50
# Generate random Matrices
Pt = sp.randn(n, n)
P = spspa.csc_matrix(np.dot(Pt.T, Pt))
q = sp.randn(n)
#!/usr/bin/env python
# Test QP solver against Maros Mezaros Benchmark suite
import sys
import scipy.io as spio
import scipy.sparse as spspa
import scipy as sp
import numpy as np
import ipdb
import quadprog.problem as qp
from quadprog.solvers.solvers import *
reload(qp)
def load_maros_meszaros_problem(f):
# Load file
m = spio.loadmat(f)
# Convert matrices
P = m['Q'].astype(float)
n = P.shape[0]
q = m['c'].T.flatten().astype(float)
A = m['A'].astype(float)
A = spspa.vstack([A, spspa.eye(n)])
uA = np.append(m['ru'].T.flatten().astype(float),
m['ub'].T.flatten().astype(float))
lA = np.append(m['rl'].T.flatten().astype(float),
m['lb'].T.flatten().astype(float))
P = spspa.block_diag((spspa.csc_matrix((n, n)), 2*spspa.eye(m),
spspa.csc_matrix((n, n))), format='csc')
q = np.append(np.zeros(m + n), self._gammas[0]*np.ones(n))
In = spspa.eye(n)
Onm = spspa.csc_matrix((n, m))
A = spspa.vstack([spspa.hstack([Ad, -spspa.eye(m),
spspa.csc_matrix((m, n))]),
spspa.hstack([In, Onm, -In]),
spspa.hstack([In, Onm, In])]).tocsc()
lA = np.hstack([bd, -np.inf * np.ones(n), np.zeros(n)])
uA = np.hstack([bd, np.zeros(n), np.inf * np.ones(n)])
else:
assert False, "Unhandled version"
# Create a quadprogProblem and store it in a private variable
self._prob = qp.quadprogProblem(P, q, A, lA, uA)
# Create an OSQP object and store it in a private variable
self._osqp = osqp.OSQP(**osqp_opts)
self._osqp.problem(P, q, A, lA, uA)