import numpy
import scipy.sparse
import osqp
Q = numpy.matrix([[1e4, 0], [0, 0]])
P = scipy.sparse.block_diag([scipy.sparse.kron(scipy.sparse.eye(3), Q), scipy.sparse.kron(scipy.sparse.eye(2), 1e-6)])
q = numpy.zeros(8)
Leq = numpy.matrix([[130, -26000, 0., 0., 0., 0.]])
Ueq = Leq
Lineq = numpy.matrix([[-numpy.inf, -numpy.inf]])
Uineq = numpy.matrix([[numpy.inf, numpy.inf]])
L = numpy.hstack([Leq, Lineq])
U = numpy.hstack([Ueq, Uineq])
L_cons = L.copy()
L_cons[0, -2] = L_cons[0, -1] = -20000
U_cons = U.copy()
U_cons[0, -2] = U_cons[0, -1] = 20000
AA = numpy.matrix([[1, -6e-5], [4, 1]])
AAeq = scipy.sparse.block_diag([scipy.sparse.kron(scipy.sparse.eye(3), -numpy.eye(2))])
AAeq += scipy.sparse.block_diag([scipy.sparse.kron(scipy.sparse.eye(3, k=-1), AA)])
BB = numpy.matrix([[-2e-9], [8e-5]])
BBeq = scipy.sparse.vstack([numpy.zeros([2, 2]), scipy.sparse.kron(scipy.sparse.eye(2), BB)])
Aeq = scipy.sparse.hstack([AAeq, BBeq])
AAineq = numpy.zeros([2, 6])
BBineq = scipy.sparse.eye(2)
Aineq = scipy.sparse.hstack([AAineq, BBineq])
A = scipy.sparse.vstack([Aeq, Aineq])
prob1 = osqp.OSQP()
prob1.setup(P, q.T, A, L.T, U.T, warm_start=True)
x1 = prob1.solve().x
prob2 = osqp.OSQP()
prob2.setup(P, q.T, A, L_cons.T, U_cons.T, warm_start=True)
x2 = prob2.solve().x
print("\tx1\t\t\t\tx2")
for i in range(len(x1)):
if abs(x1[i] - x2[i]) < 100:
print("{:10.2f}\t\t{:10.2f}".format(x1[i], x2[i]))
else:
print("{:10.2f}\t\t{:10.2f}\t*".format(x1[i], x2[i]))
print("\n\n* - large differences")
-----------------------------------------------------------------
problem: variables n = 8, constraints m = 8
nnz(P) + nnz(A) = 25
settings: linear system solver = suitesparse ldl,
eps_abs = 1.0e-03, eps_rel = 1.0e-03,
eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive)
sigma = 1.00e-06, alpha = 1.60, max_iter = 4000
check_termination: on (interval 25)
scaling: on, scaled_termination: off
warm start: on, polish: off
iter objective pri res dua res rho time
1 0.0000e+00 2.60e+04 1.28e+08 1.00e-01 4.48e-05s
25 2.5955e+08 8.00e+00 6.05e+02 1.00e-01 6.62e-05s
status: solved
number of iterations: 25
optimal objective: 259550024.4092
run time: 7.55e-05s
optimal rho estimate: 5.76e-02
-----------------------------------------------------------------
OSQP v0.2.1 - Operator Splitting QP Solver
(c) Bartolomeo Stellato, Goran Banjac
University of Oxford - Stanford University 2017
-----------------------------------------------------------------
problem: variables n = 8, constraints m = 8
nnz(P) + nnz(A) = 25
settings: linear system solver = suitesparse ldl,
eps_abs = 1.0e-03, eps_rel = 1.0e-03,
eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
rho = 1.00e-01 (adaptive)
sigma = 1.00e-06, alpha = 1.60, max_iter = 4000
check_termination: on (interval 25)
scaling: on, scaled_termination: off
warm start: on, polish: off
iter objective pri res dua res rho time
1 0.0000e+00 2.60e+04 1.28e+08 1.00e-01 3.20e-05s
25 2.5955e+08 8.00e+00 6.05e+02 1.00e-01 5.17e-05s
status: solved
number of iterations: 25
optimal objective: 259550096.1118
run time: 6.06e-05s
optimal rho estimate: 5.76e-02
x1 x2
-129.98 -129.98
25992.00 25992.00
-131.55 -131.55
25471.16 25472.09
-133.08 -133.08
24944.75 24945.90
-11681.23 -4.59 *
-2661.77 -1.05 *
* - large differences
It would be expected that adding constraints that are wider than the output given by x1 should not affect x2.