def gen_problem(self, m, n, dens_lvl=0.5): """ the basis purusit problem is defined as minimize || x ||_1 subjec to Ax = b Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> """ # Generate data Ad = spspa.random(m, n, density=dens_lvl) x_true = np.multiply((np.random.rand(n) > 0.5).astype(float), np.random.randn(n)) / np.sqrt(n) bd = Ad.dot(x_true) # Construct the problem # minimize np.ones(n).T * t # subject to Ax = b # -t <= x <= t In = spspa.eye(n) P = spspa.csc_matrix((2 * n, 2 * n)) q = np.append(np.zeros(n), np.ones(n)) A = spspa.vstack([ spspa.hstack([Ad, spspa.csc_matrix((m, n))]), spspa.hstack([In, -In]), spspa.hstack([In, In]) ]) lA = np.hstack([bd, -np.inf * np.ones(n), np.zeros(n)]) uA = np.hstack([bd, np.zeros(n), np.inf * np.ones(n)]) # Create a quadprog_problem and return it return QuadprogProblem(P, q, A, lA, uA)
def gen_problem(self, m, n, dens_lvl=0.5, version='sparse'): """ Lasso problem is defined as minimize || Ax - b ||^2 + gamma * || x ||_1 Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> version - QP reformulation ['dense', 'sparse'] """ # Generate data Ad = spspa.random(m, n, density=dens_lvl) x_true = np.multiply((np.random.rand(n) > 0.5).astype(float), np.random.randn(n)) / np.sqrt(n) bd = Ad.dot(x_true) + .5 * np.random.randn(m) gamma = sp.rand() # gamma_max = 0.2 * np.linalg.norm(Ad.T.dot(bd), np.inf) # self._gammas = np.exp(np.linspace(np.log(gamma_max), # np.log(gamma_max * 1e-2), inst)) # self._iter_cnt = 0 # Construct the problem if version == 'dense': # minimize || Ax - b ||^2 + gamma * np.ones(n).T * t # subject to -t <= x <= t P = spspa.block_diag((2 * Ad.T.dot(Ad), spspa.csc_matrix((n, n))), format='csc') q = np.append(-2 * Ad.T.dot(bd), gamma * np.ones(n)) In = spspa.eye(n) A = spspa.vstack([spspa.hstack([In, -In]), spspa.hstack([In, In])]).tocsc() lA = np.append(-np.inf * np.ones(n), np.zeros(n)) uA = np.append(np.zeros(n), np.inf * np.ones(n)) elif version == 'sparse': # minimize y.T * y + gamma * np.ones(n).T * t # subject to y = Ax # -t <= x <= t 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), gamma * 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 quadprog_problem and return in return QuadprogProblem(P, q, A, lA, uA)
def gen_problem(self, n, k, dens_lvl=0.5, version='dense'): """ Portfolio optimization problem is defined as maximize mu.T * x - gamma x.T (F * F.T + D) x subjec to 1.T x = 1 x >= 0 Arguments --------- k, n - Dimensions of matrix F <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> version - QP reformulation ['dense', 'sparse'] """ # Generate data F = spspa.random(n, k, density=dens_lvl, format='csc') D = spspa.diags(np.random.rand(n) * np.sqrt(k), format='csc') mu = np.random.randn(n) gamma = 1 # Construct the problem if version == 'dense': # minimize x.T (F * F.T + D) x - mu.T / gamma * x # subject to 1.T x = 1 # 0 <= x <= 1 P = 2 * (F.dot(F.T) + D) q = -mu / gamma A = spspa.vstack([np.ones((1, n)), spspa.eye(n)]).tocsc() lA = np.append([1.], np.zeros(n)) uA = np.append([1.], np.ones(n)) elif version == 'sparse': # minimize x.T*D*x + y.T*y - mu.T / gamma * x # subject to 1.T x = 1 # F.T x = y # 0 <= x <= 1 P = spspa.block_diag((2 * D, 2 * spspa.eye(k)), format='csc') q = np.append(-mu / gamma, np.zeros(k)) A = spspa.vstack([ spspa.hstack([ spspa.csc_matrix(np.ones((1, n))), spspa.csc_matrix((1, k)) ]), spspa.hstack([F.T, -spspa.eye(k)]), spspa.hstack([spspa.eye(n), spspa.csc_matrix((n, k))]) ]).tocsc() lA = np.hstack([1., np.zeros(k), np.zeros(n)]) uA = np.hstack([1., np.zeros(k), np.ones(n)]) else: assert False, "Unhandled version" # Create a quadprog_problem and return it return QuadprogProblem(P, q, A, lA, uA)
def gen_problem(self, m, n, dens_lvl=1.0): """ Huber fitting problem is defined as minimize sum( huber(ai'x - bi) ), where huber() is the Huber penalty function defined as | 1/2 x^2 |x| <= 1 huber(x) = < | |x| - 1/2 |x| > 1 Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> """ # Generate data A = spspa.random(m, n, density=dens_lvl, format='csc') x_true = np.random.randn(n) / np.sqrt(n) ind95 = (np.random.rand(m) < 0.95).astype(float) b = A.dot(x_true) + np.multiply(0.5*np.random.randn(m), ind95) \ + np.multiply(10.*np.random.rand(m), 1. - ind95) # Construct the problem # minimize 1/2 u.T * u + np.ones(m).T * v # subject to -u - v <= Ax - b <= u + v # 0 <= u <= 1 # v >= 0 Im = spspa.eye(m) P = spspa.block_diag((spspa.csc_matrix( (n, n)), Im, spspa.csc_matrix((m, m))), format='csc') q = np.append(np.zeros(m + n), np.ones(m)) A = spspa.vstack([ spspa.hstack([A, Im, Im]), spspa.hstack([A, -Im, -Im]), spspa.hstack( [spspa.csc_matrix((m, n)), Im, spspa.csc_matrix((m, m))]), spspa.hstack([spspa.csc_matrix((m, n + m)), Im]) ]).tocsc() lA = np.hstack([b, -np.inf * np.ones(m), np.zeros(2 * m)]) uA = np.hstack( [np.inf * np.ones(m), b, np.ones(m), np.inf * np.ones(m)]) # Create a quadprog_problem and store it in a private variable return QuadprogProblem(P, q, A, lA, uA)
def gen_problem(self, m, n, dens_lvl=0.5, version='dense'): """ Nonnegative least-squares problem is defined as minimize || Ax - b ||^2 subjec to x >= 0 Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> version - QP reformulation ['dense', 'sparse'] """ # Generate data Ad = spspa.random(m, n, density=dens_lvl, format='csc') x_true = np.ones(n) / n + np.random.randn(n) / np.sqrt(n) bd = Ad.dot(x_true) + 0.5*np.random.randn(m) # Construct the problem if version == 'dense': # minimize 1/2 x.T (A.T * A) x - (A.T * b).T * x # subject to x >= 0 P = Ad.T.dot(Ad) q = -Ad.T.dot(bd) A = spspa.eye(n) lA = np.zeros(n) uA = np.inf * np.ones(n) elif version == 'sparse': # minimize 1/2 y.T*y # subject to y = Ax - b # x >= 0 Im = spspa.eye(m) P = spspa.block_diag((spspa.csc_matrix((n, n)), Im), format='csc') q = np.zeros(n + m) A = spspa.vstack([ spspa.hstack([Ad, -Im]), spspa.hstack([spspa.eye(n), spspa.csc_matrix((n, m))]), ]).tocsc() lA = np.append(bd, np.zeros(n)) uA = np.append(bd, np.inf * np.ones(n)) else: assert False, "Unhandled version" # Create a quadprog_problem and return it return QuadprogProblem(P, q, A, lA, uA)
def scale_qp(self, qp, settings): # Initialize scaling d = np.ones(qp.n + qp.m) # Define reduced KKT matrix to scale KKT = spa.vstack([ spa.hstack([qp.P, qp.A.T]), spa.hstack([qp.A, spa.csc_matrix((qp.m, qp.m))]) ]) # Run Scaling KKT2 = KKT.copy() if settings['scaling_norm'] == 2: KKT2.data = np.square(KKT2.data) # Elementwise square elif settings['scaling_norm'] == 1: KKT2.data = np.absolute(KKT2.data) # Elementwise abs # Iterate Scaling for i in range(settings['scaling_iter']): # Regularize components KKT2d = KKT2.dot(d) # Prevent division by 0 d = (qp.n + qp.m) * np.reciprocal(KKT2d + 1e-08) # Limit scaling terms d = np.maximum(np.minimum(d, 1e+03), 1e-03) # Obtain Scaler Matrices d = np.power(d, 1. / settings['scaling_norm']) D = spa.diags(d[:qp.n]) if qp.m == 0: E = spa.csc_matrix((0, 0)) else: E = spa.diags(d[qp.n:]) # Scale problem Matrices P = D.dot(qp.P.dot(D)).tocsc() A = E.dot(qp.A.dot(D)).tocsc() q = D.dot(qp.q) l = E.dot(qp.l) u = E.dot(qp.u) # Return scaled problem return QuadprogProblem(P, q, A, l, u)
def gen_problem(self, m, n, dens_lvl=.8): """ Support vector machine problem is defined as minimize || x ||^2 + gamma * 1.T * max(0, diag(b) A x + 1) Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> """ # Generate data if m % 2 == 1: m = m + 1 N = int(m / 2) gamma = 1.0 b = np.append(np.ones(N), -np.ones(N)) A_upp = spspa.random(N, n, density=dens_lvl) A_low = spspa.random(N, n, density=dens_lvl) A = spspa.vstack([ A_upp / np.sqrt(n) + (A_upp != 0.).astype(float) / n, A_low / np.sqrt(n) - (A_low != 0.).astype(float) / n ]).tocsc() # Construct the problem # minimize x.T * x + gamma 1.T * t # subject to t >= diag(b) A x + 1 # t >= 0 P = spspa.block_diag((2 * spspa.eye(n), spspa.csc_matrix((m, m))), format='csc') q = np.append(np.zeros(n), gamma * np.ones(m)) A = spspa.vstack([ spspa.hstack([spspa.diags(b).dot(A), -spspa.eye(m)]), spspa.hstack([spspa.csc_matrix((m, n)), spspa.eye(m)]), ]).tocsc() lA = np.append(-np.inf * np.ones(m), np.zeros(m)) uA = np.append(-np.ones(m), np.inf * np.ones(m)) # Create a quadprog_problem and store it in a private variable return QuadprogProblem(P, q, A, lA, uA)
def gen_problem(self, m, n, dens_lvl=0.5): """ Linear program in the inequality from is defined as minimize c.T * x subjec to Ax <= b Arguments --------- m, n - Dimensions of matrix A <int> osqp_opts - Parameters of OSQP solver dens_lvl - Density level of matrix A <float> """ # Generate data x_true = np.random.randn(n) / np.sqrt(n) A = spspa.random(m, n, density=dens_lvl, format='csc') uA = A.dot(x_true) + 0.1*np.random.rand(m) lA = -np.inf * np.ones(m) q = -A.T.dot(np.random.rand(m)) P = spspa.csc_matrix((n, n)) # Create a quadprog_problem and return it return QuadprogProblem(P, q, A, lA, uA)