def solve(self, p): m = osqp.OSQP() STATUS_MAP = { m.constant('OSQP_SOLVED'): qp.OPTIMAL, m.constant('OSQP_MAX_ITER_REACHED'): qp.MAX_ITER_REACHED, m.constant('OSQP_PRIMAL_INFEASIBLE'): qp.PRIMAL_INFEASIBLE, m.constant('OSQP_DUAL_INFEASIBLE'): qp.DUAL_INFEASIBLE } if p.P is not None: p.P = p.P.tocsc() if p.A is not None: p.A = p.A.tocsc() if p.i_idx is not None: raise ValueError('Cannot solve MIQPs with OSQP') m.setup(p.P, p.q, p.A, p.l, p.u, **self.options) res = m.solve() status = STATUS_MAP.get(res.info.status_val, qp.SOLVER_ERROR) return QuadprogResults(status, res.info.obj_val, res.x, res.y, res.info.run_time, res.info.iter)
P = spa.csc_matrix(np.array([[11., 0.], [0., 0.]])) q = np.array([3., 4.]) A = spa.csc_matrix(np.array([[-1.0, 0.], [0., -1.], [-1., 3.], [2., 5.], [3., 4]])) l = -np.inf * np.ones(A.shape[0]) u = np.array([0., 0., -15., 100., 80.]) n = P.shape[0] m = A.shape[0] # A = None # l = None # u = None # OSQP options osqp_opts = {'rho': 1.0, 'check_termination': 1, 'max_iter': 2500} # Solve problem with OSQP m = osqp.OSQP() m.setup(P, q, A, l, u, **osqp_opts) res = m.solve() # Solve problem with OSQPPUREPY m = osqppurepy.OSQP() m.setup(P, q, A, l, u, **osqp_opts) res = m.solve()
osqp_opts = {'rho': rho, 'adaptive_rho': True, 'adaptive_rho_interval': 25, 'sigma': 1e-06, 'scaled_termination': False, 'check_termination': 1, 'polish': False, 'verbose': True, 'linsys_solver': 'suitesparse ldl' } qp = mpbpy.QuadprogProblem(P, q, A, l, u) res_gurobi = qp.solve(solver=mpbpy.GUROBI, verbose=True) model = osqppurepy.OSQP() model.setup(P=P, q=q, A=A, l=l, u=u, **osqp_opts) res_osqppurepy = model.solve() # Solve with SuiteSparse LDL model = osqp.OSQP() model.setup(P=P, q=q, A=A, l=l, u=u, **osqp_opts) res_osqp = model.solve() # Check difference with gurobi if res_gurobi.status == 'optimal': print("Difference OSQP vs Gurobi") print(" - primal = %.4f" % (np.linalg.norm(res_gurobi.x - res_osqp.x) / np.linalg.norm(res_gurobi.x)))
import pickle # Load one problem with open('./data/%s.pickle' % 'helicopter_scaling_medium', 'rb') as f: problem = pickle.load(f) # OSQP settings osqp_settings = { 'verbose': True, 'scaling': True, 'scaling_iter': 50, 'early_terminate_interval': 1, 'auto_rho': True, 'rho': 0.01, 'polish': False } # Solve with OSQP model = osqp.OSQP() model.setup(problem['P'], problem['q'], problem['A'], problem['l'], problem['u'], **osqp_settings) res_osqp = model.solve() # Solve with GUROBI import mathprogbasepy as mpbpy qp = mpbpy.QuadprogProblem(problem['P'], problem['q'], problem['A'], problem['l'], problem['u']) res_gurobi = qp.solve(solver=mpbpy.GUROBI, verbose=False) print("GUROBI time = %.4e" % res_gurobi.cputime) print("OSQP time = %.4e" % res_osqp.info.run_time)
import osqppurepy import matplotlib.pyplot as plt # Get data f = '../../../../extra/difficult_problems/data/polish_fail.mat' m = spio.loadmat(f) P = m['P'] A = m['A'] q = m['q'].T.flatten() l = m['l'].T.flatten() u = m['u'].T.flatten() # Solve problem osqp_opts = {'polish': True, 'check_termination': 1} prob = osqppurepy.OSQP() prob.setup(P=P, q=q, A=A, l=l, u=u, **osqp_opts) res = prob.solve() # Compute primal and dual residuals for the points obtained from line search N = len(res.linesearch.t) pri_res_ls = np.zeros(N) dua_res_ls = np.zeros(N) X_ls = res.linesearch.X Z_ls = res.linesearch.Z Y_ls = res.linesearch.Y for i in range(N): pri_res_ls[i] = la.norm(A.dot(X_ls[i, :]) - Z_ls[i, :], np.inf) dua_res_ls[i] = la.norm(P.dot(X_ls[i, :]) + q + A.T.dot(Y_ls[i, :]), np.inf)