def test_quad_over_lin(self): """Test quad_over_lin atom. """ P = np.array([[10, 1j], [-1j, 10]]) X = Variable((2, 2), complex=True) b = 1 y = Variable(complex=False) value = cvx.quad_over_lin(P, b).value expr = cvx.quad_over_lin(X, y) prob = Problem(cvx.Minimize(expr), [X == P, y == b]) result = prob.solve(solver=cvx.SCS, eps=1e-6, max_iters=7500, verbose=True) self.assertAlmostEqual(result, value, places=3) expr = cvx.quad_over_lin(X - P, y) prob = Problem(cvx.Minimize(expr), [y == b]) result = prob.solve(solver=cvx.SCS, eps=1e-6, max_iters=7500, verbose=True) self.assertAlmostEqual(result, 0, places=3) self.assertItemsAlmostEqual(X.value, P, places=3)
def test_quad_over_lin(self) -> None: """Test gradient for quad_over_lin """ expr = cp.quad_over_lin(self.x, self.a) self.x.value = [1, 2] self.a.value = 2 self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), [1, 2]) self.assertAlmostEqual(expr.grad[self.a], [-1.25]) self.a.value = 0 self.assertAlmostEqual(expr.grad[self.x], None) self.assertAlmostEqual(expr.grad[self.a], None) expr = cp.quad_over_lin(self.A, self.a) self.A.value = np.eye(2) self.a.value = 2 self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), [1, 0, 0, 1]) self.assertAlmostEqual(expr.grad[self.a], [-0.5]) expr = cp.quad_over_lin(self.x, self.a) + cp.quad_over_lin( self.y, self.a) self.x.value = [1, 2] self.a.value = 2 self.y.value = [1, 2] self.a.value = 2 self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), [1, 2]) self.assertItemsAlmostEqual(expr.grad[self.y].toarray(), [1, 2]) self.assertAlmostEqual(expr.grad[self.a], [-2.5])
def test_validation(self): """Test that complex arguments are rejected. """ x = Variable(complex=True) with self.assertRaises(Exception) as cm: (x >= 0) self.assertEqual(str(cm.exception), "Inequality constraints cannot be complex.") with self.assertRaises(Exception) as cm: cvx.quad_over_lin(x, x) self.assertEqual( str(cm.exception), "The second argument to quad_over_lin cannot be complex.") with self.assertRaises(Exception) as cm: cvx.sum_largest(x, 2) self.assertEqual(str(cm.exception), "Arguments to sum_largest cannot be complex.") x = Variable(2, complex=True) for atom in [ cvx.geo_mean, cvx.log_sum_exp, cvx.max, cvx.entr, cvx.exp, cvx.huber, cvx.log, cvx.log1p, cvx.logistic ]: name = atom.__name__ with self.assertRaises(Exception) as cm: print(name) atom(x) self.assertEqual(str(cm.exception), "Arguments to %s cannot be complex." % name) x = Variable(2, complex=True) for atom in [cvx.maximum, cvx.kl_div]: name = atom.__name__ with self.assertRaises(Exception) as cm: print(name) atom(x, x) self.assertEqual(str(cm.exception), "Arguments to %s cannot be complex." % name) x = Variable(2, complex=True) for atom in [cvx.inv_pos, cvx.sqrt, lambda x: cvx.power(x, .2)]: with self.assertRaises(Exception) as cm: atom(x) self.assertEqual(str(cm.exception), "Arguments to power cannot be complex.") x = Variable(2, complex=True) for atom in [cvx.harmonic_mean, lambda x: cvx.pnorm(x, .2)]: with self.assertRaises(Exception) as cm: atom(x) self.assertEqual(str(cm.exception), "pnorm(x, p) cannot have x complex for p < 1.")
def test_quad_over_lin(self) -> None: x = Variable((3, 5)) y = Variable((3, 5)) z = Variable() s = cp.quad_over_lin(x - y, z) self.assertFalse(s.is_constant()) self.assertFalse(s.is_affine()) self.assertFalse(s.is_quadratic()) self.assertTrue(s.is_dcp()) t = cp.quad_over_lin(x + 2 * y, 5) self.assertFalse(t.is_constant()) self.assertFalse(t.is_affine()) self.assertTrue(t.is_quadratic()) self.assertTrue(t.is_dcp())
def test_quad_over_lin(self): # Test quad_over_lin DCP. atom = cp.quad_over_lin(cp.square(self.x), self.a) self.assertEqual(atom.curvature, s.CONVEX) atom = cp.quad_over_lin(-cp.square(self.x), self.a) self.assertEqual(atom.curvature, s.CONVEX) atom = cp.quad_over_lin(cp.sqrt(self.x), self.a) self.assertEqual(atom.curvature, s.UNKNOWN) assert not atom.is_dcp() # Test quad_over_lin shape validation. with self.assertRaises(Exception) as cm: cp.quad_over_lin(self.x, self.x) self.assertEqual(str(cm.exception), "The second argument to quad_over_lin must be a scalar.")
def DR_W2_conditional_mean_variance_long_only_opt_cvx_kernel_new( X_mat, Y_mat, X0, reg_params, epsilon, rho_div_rho_min): """ CVXPY solver kernel for conditional distributionally robust optimization problem: See problem formulation in DR_Conditional_EstimationW2.ipynb """ def compute_rho_min(X_mat, X0, epsilon): X_dist = np.linalg.norm(X_mat - X0, axis=1) X_dist[np.isnan(X_dist)] = 1e8 X_cut = np.quantile(X_dist, q=epsilon, interpolation='higher') return X_dist[X_dist <= X_cut].mean() * epsilon rho = rho_div_rho_min * compute_rho_min(X_mat, X0, epsilon) X_dist = np.linalg.norm(X_mat - X0, axis=1) X_dist[np.isnan(X_dist)] = 1e8 eta = reg_params epsilon_inv = 1 / epsilon N, sample_stock_num = Y_mat.shape m = cp.Variable(N, nonneg=True) beta = cp.Variable(sample_stock_num) alpha = cp.Variable(1) lambda1 = cp.Variable(1, nonneg=True) denom = cp.Variable(1, nonneg=True) lambda2 = cp.Variable(1) linear_expr = Y_mat @ beta - alpha - 0.5 * eta obj = lambda1 * rho + lambda2 * epsilon + cp.sum( cp.pos(epsilon_inv * m - 0.25 * epsilon_inv * eta**2 - epsilon_inv * eta * alpha - lambda1 * X_dist - lambda2)) / N constraints = [ m >= cp.hstack( [cp.quad_over_lin(linear_expr[i], denom) for i in range(N)]), epsilon_inv * cp.quad_over_lin(beta, lambda1) + denom <= 1, beta >= 0, cp.sum(beta) == 1 ] prob = cp.Problem(cp.Minimize(obj), constraints) prob.solve( mosek_params={ mosek.dparam.optimizer_max_time: 100.0, mosek.dparam.intpnt_co_tol_rel_gap: 1e-4 }) assert prob.status == 'optimal' return beta.value
def solve_dual_problem_ptwise(prob_spec: LinearInvDesProblem, radii: np.ndarray) -> float: """Solve the dual problem for a linear objective function.""" ref_fld = np.linalg.solve(prob_spec.lap_op, prob_spec.src) adj_lap_olap = prob_spec.lap_op.conj().T @ prob_spec.olap_vec # Setup the dual variables. The complex variables are setup as a tuple of # two variables corresponding to the real and imaginary part. eta = cvxpy_complex_utils.CvxpyComplexVariable(prob_spec.dim) lam = cvxpy.Variable(prob_spec.dim) sigma = cvxpy_complex_utils.CvxpyComplexVariable(prob_spec.dim) beta = cvxpy.Variable(prob_spec.dim) # Setup the objective function. obj = -cvxpy.sum(beta) - cvxpy.sum(cvxpy.multiply(radii**2, lam)) # Setup the constraints. constraints = [] constraints.append(eta.real == (np.real(prob_spec.lap_op.T) * sigma.real + np.imag(prob_spec.lap_op.T) * sigma.imag) + np.real(adj_lap_olap)) constraints.append(eta.imag == (np.real(prob_spec.lap_op.T) * sigma.imag - np.imag(prob_spec.lap_op.T) * sigma.real) + np.imag(adj_lap_olap)) constraints.append(lam >= 0) for i in range(prob_spec.dim): constraints.append( beta[i] >= (cvxpy.quad_over_lin(eta.real[i], lam[i]) + cvxpy.quad_over_lin(eta.imag[i], lam[i]))) constraints.append(beta[i] >= (cvxpy.quad_over_lin( eta.real[i] - prob_spec.theta_max * sigma.real[i], lam[i]) + cvxpy.quad_over_lin( eta.imag[i] - prob_spec.theta_max * sigma.imag[i], lam[i]) + prob_spec.theta_max * (sigma.real[i] * np.real(ref_fld[i]) + sigma.imag[i] * np.imag(ref_fld[i])))) prob = cvxpy.Problem(cvxpy.Maximize(obj), constraints) prob.solve(cvxpy.ECOS) return obj.value
def _solve_soft_bias_correction_cvxpy(UE, EUT, I, N, B, tuning): X = cvx.Semidef(N.shape[0], name='T') objective = (cvx.Minimize( cvx.sum_squares(EUT * (X - I) * UE) + cvx.quad_over_lin(N.T * X * B.T, tuning))) constraints = [X >> 0] prob = cvx.Problem(objective, constraints) print("Large constants:") for c in prob.constants(): try: print("\tConstant of size:", c.value.shape, type(c.value)) except AttributeError: pass prob.solve(solver=cvx.SCS, verbose=True, gpu=True) return X.value, prob.value
def txprocdurtrans_time_model(self, s_id, datasize, comp_list, num_itres): #datasize: MB, bw: Mbps, proc: Mbps tx_t = (8*datasize)*cp.inv_pos(BWREGCONST*self.s_bw[s_id, 0]) # sec numitfuncs = len(comp_list) quadoverlin_vector = expr((numitfuncs, 1)) for i, comp in enumerate(comp_list): quadoverlin = comp*cp.quad_over_lin(self.s_n[s_id, i], self.s_proc.get((s_id, 0)) ) quadoverlin_vector.set_((i, 0), quadoverlin) # quadoverlin_ = (quadoverlin_vector.agg_to_row()).get((0,0)) # proc_t = num_itres* (8*datasize)*(quadoverlin_) # sec proc_t = (8*datasize)*(quadoverlin_) # sec stage_t = 0 #self.s_dur.get((s_id, 0)) #trans_t = cp.max(tx_t, proc_t) trans_t = tx_t + proc_t #+ stage_t return [tx_t, proc_t, stage_t, trans_t]
def test_consistency(self): """Test case for non-deterministic behavior in cvxopt. """ import cvxpy xs = [0, 1, 2, 3] ys = [51, 60, 70, 75] eta1 = cvxpy.Variable() eta2 = cvxpy.Variable() eta3 = cvxpy.Variable() theta1s = [eta1 + eta3 * x for x in xs] lin_parts = [theta1 * y + eta2 * y ** 2 for (theta1, y) in zip(theta1s, ys)] g_parts = [-cvxpy.quad_over_lin(theta1, -4 * eta2) + 0.5 * cvxpy.log(-2 * eta2) for theta1 in theta1s] objective = reduce(lambda x, y: x + y, lin_parts + g_parts) problem = cvxpy.Problem(cvxpy.Maximize(objective)) problem.solve(verbose=True, solver=cvxpy.SCS) assert problem.status == cvxpy.OPTIMAL, problem.status return [eta1.value, eta2.value, eta3.value]
def test_consistency(self): """Test case for non-deterministic behavior in cvxopt. """ import cvxpy xs = [0, 1, 2, 3] ys = [51, 60, 70, 75] eta1 = cvxpy.Variable() eta2 = cvxpy.Variable() eta3 = cvxpy.Variable() theta1s = [eta1 + eta3*x for x in xs] lin_parts = [theta1 * y + eta2 * y**2 for (theta1, y) in zip(theta1s, ys)] g_parts = [-cvxpy.quad_over_lin(theta1, -4*eta2) + 0.5 * cvxpy.log(-2 * eta2) for theta1 in theta1s] objective = reduce(lambda x,y: x+y, lin_parts + g_parts) problem = cvxpy.Problem(cvxpy.Maximize(objective)) problem.solve(verbose=True, solver=cvxpy.SCS) assert problem.status in [cvxpy.OPTIMAL_INACCURATE, cvxpy.OPTIMAL] return [eta1.value, eta2.value, eta3.value]
def project(self, position, goal, obstacles): """GAA's magical wizardy for the original problem (projection)""" # get constants p = position # p g = goal # g goal point # create cvx program n = len(p) x = cvx.Variable(n) # projection pt t = cvx.Variable(n) # slack L = cvx.Variable(len(obstacles), nonneg=True) # dual on elip # form objective. self.objective = cvx.norm(x - g, 2) self.constraints = [] for i, (center, shape) in enumerate(obstacles): # cnts += [L[i] >= 0] S = shape.reshape((n, n)) # sigma S_inv = la.inv(S) # sigma inv u = center # mu # get eigdecomp D, U = la.eig(S) # constraints Q = np.dot(U.T, S_inv.dot(u)) LHS = 0 for d in range(n): LHS += cvx.quad_over_lin(L[i] * Q[d] + U.T[d, :] * x, L[i] * (D[d]**-1) + 1) self.constraints += [ LHS - 2 * p.T * x <= L[i] * np.dot(u, S_inv.dot(u)) - cvx.sum_squares(p) - L[i] ] # Form and solve problem. prob = cvx.Problem(cvx.Minimize(self.objective), self.constraints) tic = time.time() prob.solve(solver=cvx.ECOS, reltol=1e-3, abstol=1e-3) print("Projection solution time {:2.4f}sec".format(time.time() - tic)) if x.value is None: raise RuntimeError("failed to project") return np.array(x.value).flatten(), self.objective.value
def obj_fun(BETA,XDOT,ALPHA,Ys,YBAR): res = 0.0 for k in range(num_classes): print "Class: " + str(k) vpk = BETA[k,:]*X res = res + cvx.quad_over_lin(vpk,1) res = -0.5*res for i in range(num_classes): print "Class: " + str(i) y = np.asarray(Ys[i,:])[0] ybar = np.asarray(Ybar[i,:])[0] yset = [p for p in range(Y.shape[1]) if y[p] == 1] ybarset = [q for q in range(Y.shape[1]) if ybar[q] == 1] tups = [] for p in yset: for q in ybarset: tups.append((p,q)) for tup in tups: j = tup[0] l = tup[1] res = res + ALPHA[i + 8*j + 8*l] return res
def f(): x = cp.Variable() y = cp.Variable() obj = cp.Minimize(x) constraints = [ (x + y)**2 / cp.sqrt(y) <= x - y + 5 ] problem = cp.Problem(obj, constraints) print(f"f before: {problem.is_dcp()}") x = cp.Variable() y = cp.Variable() a = cp.Variable() b = cp.Variable() obj = cp.Minimize(x) constraints = [ cp.quad_over_lin(a, b) <= x - y + 5, a == x + y, b <= cp.sqrt(y), ] problem = cp.Problem(obj, constraints) print(f"f after: {problem.is_dcp()}") problem.solve()
def estimate_transcript_frequencies_with_cvxopt(observed_array, expected_array, sparse_penalty, sparse_index, verbose=False): from cvxpy import matrix, variable, geq, log, eq, program, maximize, \ minimize, sum, quad_over_lin Xs = matrix(observed_array) ps = matrix(expected_array) thetas = variable(ps.shape[1]) constraints = [eq(sum(thetas), 1), geq(thetas, 0)] if sparse_penalty == None: p = program(maximize(Xs * log(ps * thetas)), constraints) else: p = program( maximize(Xs * log(ps * thetas) - sparse_penalty * quad_over_lin(1., thetas[sparse_index, 0])), constraints) p.options['maxiters'] = 1500 value = p.solve(quiet=not verbose) thetas_values = numpy.array(thetas.value.T.tolist()[0]) return thetas_values
def altminsolve(problem, noquad=False, eqconstraint=False, sthresh=0.0): r"""An alternating minimization solver for operator linear inverse problems. Uses an explicit factorization of the decision variable to solve: .. math:: \operatorname{minimize}_{\mathbf{X}_i,\mathbf{Y}_i} \frac{1}{2} \bigg\lVert \mathbf{b} - \mu\bigg( \sum_{i=1}^r \mathbf{X}_i \otimes \mathbf{Y}_i \bigg) \bigg\rVert^2_{\ell_2} + \lambda \sum_{i=1}^r \lVert \mathbf{X}_i \rVert_X \lVert \mathbf{Y}_i \rVert_Y. The above formulation corresponds to using a `NucNorm_Prod` object with X and Y norms as the regularizer. Parameters ---------- problem : Problem The `Problem` object to solve. noquad : Optional[bool] Whether we replace the quadratic measurement error with a norm. Default value is False. eqconstraint : Optional[bool] Whether we solve the problem with an equality constraint. Default value is False. sthresh : Optional[float] The hard thresholding limit when solving with an equality constraint. Default value is 0 (i.e., no thresholding). Returns ------- SolverOutput or list(SolverOutput) An object with the results of the optimization problem. """ start = time.time() LAMBDA = problem.penconst rank = problem.rank shape = problem.shape relconvtols = np.sort(np.array(problem.relconvergetol, ndmin=1))[::-1] maxiters = np.sort(np.array(problem.maxiters, ndmin=1)) lmeasvec = problem.measurementvec if problem.lmeasurementvec is None \ else problem.lmeasurementvec rmeasvec = problem.measurementvec if problem.rmeasurementvec is None \ else problem.rmeasurementvec lvars = [cvxpy.Variable(shape[0], shape[1]) for r in range(rank)] lparams = [cvxpy.Parameter(shape[2], shape[3]) for r in range(rank)] lconsts = [] rvars = [cvxpy.Variable(shape[2], shape[3]) for r in range(rank)] rparams = [cvxpy.Parameter(shape[0], shape[1]) for r in range(rank)] rconsts = [] lmeas_temp = problem.measurementobj.cvxapply( operators.DyadsOperator(lvars, lparams)) rmeas_temp = problem.measurementobj.cvxapply( operators.DyadsOperator(rparams, rvars)) if eqconstraint: lconsts.append(lmeas_temp == np.array(lmeasvec)) rconsts.append(rmeas_temp == np.array(rmeasvec)) else: if noquad: lmeaserr = cvxpy.norm(lmeas_temp - np.array(lmeasvec)) rmeaserr = cvxpy.norm(rmeas_temp - np.array(rmeasvec)) else: lmeaserr = cvxpy.quad_over_lin(lmeas_temp - np.array(lmeasvec), 2.0) rmeaserr = cvxpy.quad_over_lin(rmeas_temp - np.array(rmeasvec), 2.0) if eqconstraint: lobj = cvxpy.Minimize(problem.norm.norm_altmin(lvars, lparams)) robj = cvxpy.Minimize(problem.norm.norm_altmin(rparams, rvars)) else: lobj = cvxpy.Minimize(LAMBDA * problem.norm.norm_altmin(lvars, lparams) + lmeaserr) robj = cvxpy.Minimize(LAMBDA * problem.norm.norm_altmin(rparams, rvars) + rmeaserr) lprob = cvxpy.Problem(lobj, lconsts) rprob = cvxpy.Problem(robj, rconsts) # If solving the constrained version, set up needed matrices if eqconstraint: # NB: using the normal equations directly could be unstable start_linalg = time.time() measmat = problem.measurementobj.asmatrix() graminv = np.linalg.inv(measmat @ measmat.T) xfeas = measmat.T @ (graminv @ problem.measurementvec) end_linalg = time.time() if problem.solveropts.get('verbose', False): print('linalg time: ', end_linalg - start_linalg) if problem.rfactorsinit is None: # Will initialize lparams. if eqconstraint: temp = operators.ArrayOperator(xfeas.reshape(shape, order='F')) [U, S, V] = operators.kpsvd(temp) for r in range(rank): lparams[r].value = V[r, ].reshape(shape[2:4], order='F') else: # FIXME: if initfrommeas not implemented, resort to random init U, S, V = operators.kpsvd( problem.measurementobj.initfrommeas(lmeasvec)) # only filling up to SOLVE_RANK, won't neeed more for initialization # If SVD = u s v.H, numpy returns U = u, S = s, V = v.H! # So we want to pull the rows of V! for r in range(rank): lparams[r].value = V[r, ].reshape((shape[2], shape[3]), order='F') else: for r in range(rank): lparams[r].value = problem.rfactorsinit[r] end_setup = time.time() iters = 0 objval = -np.Inf outlist = [] time_offset = 0 tol_ix = 0 iters_ix = 0 Sout = np.zeros((1, rank)) # for debugging eqconstraint while True: iters += 1 if problem.solveropts.get('verbose', False): print('Outer iteration: %i' % iters) lprob.solve(solver=problem.solver, **problem.solveropts) if eqconstraint: temp = operators.DyadsOperator( [np.matrix(lvars[r].value) for r in range(rank)], [np.matrix(lparams[r].value) for r in range(rank)]) temp = temp.asArrayOperator().reshape(xfeas.shape, order='F') temp -= measmat.T @ (graminv @ (measmat @ (temp - xfeas))) temp = operators.ArrayOperator(temp.reshape(shape, order='F')) [U, S, V] = operators.kpsvd(temp) Sout = np.vstack((Sout, S)) Ssum = np.sum(S**2) for r in range(rank): S[r] = 0.0 if S[r]**2 / Ssum < sthresh else S[r] rparams[r].value = np.sqrt(S[r]) * U[:, r].reshape(shape[0:2], order='F') if problem.solveropts.get('verbose', False): print( 'feasgap: ', np.linalg.norm((measmat @ temp.flatten(order='F')) - rmeasvec.flatten(order='F'))) print( 'Vinnerprod: ', np.dot( V[:, 0].flatten(order='F'), problem.trueOperator.rfactors[0].flatten(order='F') / np.linalg.norm( problem.trueOperator.rfactors[0].flatten()))) print('spectrum: ', S) else: for r in range(rank): rparams[r].value = lvars[r].value rprob.solve(solver=problem.solver, **problem.solveropts) if eqconstraint: temp = operators.DyadsOperator( [np.matrix(rparams[r].value) for r in range(rank)], [np.matrix(rvars[r].value) for r in range(rank)]) temp = temp.asArrayOperator().reshape(xfeas.shape, order='F') temp -= measmat.T @ (graminv @ (measmat @ (temp - xfeas))) temp = operators.ArrayOperator(temp.reshape(shape, order='F')) [U, S, V] = operators.kpsvd(temp) Sout = np.vstack((Sout, S)) Ssum = np.sum(S**2) for r in range(rank): S[r] = 0.0 if S[r]**2 / Ssum < sthresh else S[r] lparams[r].value = np.sqrt(S[r]) * V[r, :].reshape(shape[2:4], order='F') if problem.solveropts.get('verbose', False): print( 'feasgap: ', np.linalg.norm((measmat @ temp.flatten(order='F')) - lmeasvec.flatten(order='F'))) print( 'Uinnerprod: ', np.dot( U[:, 0].flatten(order='F'), problem.trueOperator.lfactors[0].flatten(order='F') / np.linalg.norm( problem.trueOperator.lfactors[0].flatten()))) print('spectrum: ', S) else: for r in range(rank): lparams[r].value = rvars[r].value objval_new = rprob.objective.value relchange = np.abs(objval - objval_new) / np.max((objval_new, 1)) objval = objval_new if problem.solveropts.get('verbose', False): print('relchange: %f' % relchange) def create_out(relconvtol, maxiters): out = SolverOutput() out.problem = problem out.cvxpy_probs = (lprob, rprob) # When solving the constrained problem, recompute rparams as well if eqconstraint: for r in range(rank): rparams[r].value = np.sqrt(S[r]) * U[:, r].reshape( shape[0:2], order='F') lfactors = [np.matrix(rparams[r].value) for r in range(rank)] rfactors = [np.matrix(lparams[r].value) for r in range(rank)] out.recovered = operators.DyadsOperator(lfactors, rfactors) out.outer_iters = iters out.setup_time = end_setup - start out.solve_time = (end_solve - end_setup) - time_offset out.objval = objval out.relchange = relchange out.relconvtol = relconvtol out.maxiters = maxiters out.debug['S'] = Sout return out # put in [relconvtol] X [maxiter] (need to output all) # 1) Check if we hit an iter limit # - fill in all relconvtols from tol_ix to end # 2) Check if we hit a relconvtol # - fill in from iters_ix to end (can result in duplicate) # 3) Check if we're done iters_ix_curr = iters_ix tol_ix_curr = tol_ix end_solve = time.time() if iters == maxiters[iters_ix_curr]: for tol in relconvtols[tol_ix:]: outlist.append(create_out(tol, maxiters[iters_ix_curr])) iters_ix += 1 while tol_ix < relconvtols.size and relchange <= relconvtols[tol_ix]: for eff_iters in maxiters[iters_ix_curr:]: # If also hit a maxiters, don't ouput duplicate if iters == eff_iters: continue outlist.append(create_out(relconvtols[tol_ix], eff_iters)) tol_ix += 1 time_offset += time.time() - end_solve if ((tol_ix == relconvtols.size) or (np.max(maxiters) == iters)): break assert len(outlist) == maxiters.size * relconvtols.size return outlist if len(outlist) > 1 else outlist[0]
def matsolve(problem, compute_dyads=False, noquad=False, eqconstraint=False): """A solver for operator linear inverse problems in matrix representation. Solves the convex operator recovery problem provided that the `Regularizer` object has a CVXPY implementation. Parameters ---------- problem : Problem The `Problem` object to solve. compute_dyads : Optional[bool] Whether to return the operator in dyadic representation. When True, we compute the SVD of the matrix decision variable and create a `DyadsOperator` using the scaled left/right singular vectors as the left/right factors. Otherwise we reshape the matrix decision variable to have the shape of the operator and return an `ArrayOperator`. Default value is False. noquad : Optional[bool] Whether we replace the quadratic measurement error with a norm. Default value is False. eqconstraint : Optional[bool] Whether we solve the problem with an equality constraint. Default value is False. Returns ------- SolverOutput An object with the results of the optimization problem. """ matshape = (int(np.prod(problem.shape[0:2])), int(np.prod(problem.shape[2:4]))) RANK = min(matshape) start = time.time() X = cvxpy.Variable(*matshape) reg = problem.norm.norm_mat(X) meastemp = problem.measurementobj.matapply(X) if eqconstraint: # solve: min ||X|| s.t. A(X) = A(X_0) prob = cvxpy.Problem(cvxpy.Minimize(reg), [ meastemp == problem.measurementvec, ]) else: # solve: min .5*||A(X) - A(X_0)||_F^2 + LAMBDA*||X|| LAMBDA = problem.penconst if noquad: measerr = cvxpy.norm(meastemp - problem.measurementvec) else: measerr = cvxpy.quad_over_lin(meastemp - problem.measurementvec, 2.0) prob = cvxpy.Problem(cvxpy.Minimize(measerr + LAMBDA * reg)) end_setup = time.time() prob.solve(solver=problem.solver, **problem.solveropts) end_solve = time.time() out = SolverOutput() out.problem = problem out.cvxpy_probs = (prob, ) if compute_dyads: U, S, V = np.linalg.svd(X.value, full_matrices=0) lfactors = [ np.sqrt(S[i]) * U[:, i].reshape(problem.shape[0:2], order='F') for i in range(RANK) ] # just using V.T instead of V.H rfactors = [ np.sqrt(S[i]) * V.T[:, i].reshape(problem.shape[2:4], order='F') for i in range(RANK) ] out.recovered = operators.DyadsOperator(lfactors, rfactors) else: temp = np.array(X.value).reshape(problem.shape, order='F') out.recovered = operators.ArrayOperator(temp) out.setup_time = end_setup - start out.solve_time = end_solve - end_setup out.objval = prob.objective.value return out
def remove_dc_from_spad_test(noisy_spad, bin_edges, bin_weight, use_anscombe, use_quad_over_lin, use_poisson, use_squared_falloff, lam1=1e-2, lam2=1e-1, eps_rel=1e-5): def anscombe(x): return 2 * np.sqrt(x + 3. / 8) def inv_anscombe(x): return (x / 2)**2 - 3. / 8 assert len(noisy_spad.shape) == 1 C = noisy_spad.shape[0] assert bin_edges.shape == (C + 1, ) bin_widths = bin_edges[1:] - bin_edges[:-1] spad_equalized = noisy_spad / bin_widths x = cp.Variable((C, ), "signal") z = cp.Variable((1, ), "dc") nx = cp.Variable((C, ), "signal noise") nz = cp.Variable((C, ), "dc noise") if use_poisson: # Need tricky stuff if use_anscombe: # plt.figure() # plt.bar(range(len(spad_equalized)), spad_equalized, log=True) # plt.title("Before") # d_ans = cp.Variable((C,), "denoised anscombe") # Apply Anscombe Transform to data: spad_ans = anscombe(spad_equalized) # Apply median filter to remove Gaussian Noise spad_ans_filt = scipy.signal.medfilt(spad_ans, kernel_size=15) # Apply Inverse Anscombe Transform spad_equalized = inv_anscombe(spad_ans_filt) # plt.figure() # plt.bar(range(len(spad_equalized)), spad_equalized, log=True) # plt.title("After") if use_quad_over_lin: obj = \ cp.sum([cp.quad_over_lin(nx[i], x[i]) for i in range(C)]) + \ cp.sum([cp.quad_over_lin(nz[i], z) for i in range(C)]) + \ lam2 * cp.sum(bin_weight*cp.abs(x)) constr = [ x >= 0, x + nx >= 0, z >= cp.min(spad_equalized), z + nz >= cp.min(spad_equalized), x + nx + z + nz == spad_equalized ] prob = cp.Problem(cp.Minimize(obj), constr) prob.solve(solver=cp.ECOS, verbose=True, reltol=eps_rel) else: obj = cp.sum_squares(spad_equalized - (x + z)) + lam2 * cp.sum( bin_weight * cp.abs(x)) constr = [x >= 0, z >= 0] prob = cp.Problem(cp.Minimize(obj), constr) prob.solve(solver=cp.OSQP, verbose=True, eps_rel=eps_rel) else: # No need for tricky stuff obj = cp.sum_squares(spad_equalized - (x + z)) + 1e0 * cp.sum(bin_weight * cp.abs(x)) constr = [x >= 0, z >= 0] prob = cp.Problem(cp.Minimize(obj), constr) prob.solve(solver=cp.OSQP, eps_rel=eps_rel) denoised_spad = np.clip(x.value * bin_widths, a_min=0., a_max=None) print("z.value", z.value) return denoised_spad
def cvxpy_objective(current_state, delta_state, current_action): """ Redefining compute_cost for cvxpy """ return 0.5 * cp.quad_over_lin(current_state[0, :2] + delta_state[:2] - target_position, 1)
def link_tt_heuristic(link): ff = link.l / link.fd.v q_max = (link.l / link.fd.q_max) * link.v_dens rho_hat = link.fd.rho_max - link.v_dens cong = link.l / link.fd.w * (quad_over_lin(link.fd.rho_max ** .5, rho_hat) - 1) return cvx_max(hstack([ff, q_max, cong]))
def main(show=False): margin = .05 # margin for drawing box initial_pos = 1 final_pos = 1 n = 100 all_pos = np.linspace(0, 1, n) # Complicated lane example box_size = .18 box_pos = [.2, .5, .8] box_orientation = [-1, 1, -1] x = cp.Variable(n) cons = [x[0] == initial_pos, -2 <= x, x <= 2, x[-1] == -1] obj = 0.0 for i, pos in enumerate(all_pos): obj += sccf.minimum(cp.square(x[i] + 1), 1) obj += sccf.minimum(cp.square(x[i] - 1), 1) for b_pos, b_or in zip(box_pos, box_orientation): if b_pos <= pos and pos <= b_pos + box_size: cons.append(x[i] >= 0 if b_or > 0 else x[i] <= 0) for idx, weight in enumerate([10, 1, .1]): obj += weight * cp.sum_squares(cp.diff(x, k=idx + 1)) prob = sccf.Problem(obj, cons) tic = time.time() result = prob.solve() toc = time.time() print("lane change 2:", obj.value) print("time:", toc - tic) print("iters:", result["iters"]) latexify(fig_width=7, fig_height=2) plt.plot(all_pos * 100, x.value, c='black') plt.ylim(-2, 2) for pos, orientation in zip(box_pos, box_orientation): plt.gca().add_patch( Rectangle((pos * 100, .25 if orientation < 0 else -1.75), (box_size - margin) * 100, 1.5, facecolor='none', edgecolor='k')) plt.axhline(0, ls='--', c='k') plt.savefig("figs/lane_changing.pdf") if show: plt.show() # Lower bound obj = 0 z_top = [cp.Variable(n) for _ in range(n)] z_bottom = [cp.Variable(n) for _ in range(n)] x = cp.Variable(n) cons = [x[0] == initial_pos, -2 <= x, x <= 2, x[-1] == -1] lam_top = cp.Variable(n) lam_bottom = cp.Variable(n) cons.append(0 <= lam_top) cons.append(0 <= lam_bottom) cons.append(lam_top <= 1) cons.append(lam_bottom <= 1) for z, lam in zip(z_top + z_bottom, lam_top + lam_bottom): cons.append(z[0] == initial_pos * lam) cons.append(-2 * lam <= z) cons.append(z <= 2 * lam) cons.append(z[-1] == -1 * lam) for i, pos in enumerate(all_pos): obj += cp.quad_over_lin(z_top[i][i] + lam_top[i], lam_top[i]) + (1 - lam_top[i]) obj += cp.quad_over_lin(z_bottom[i][i] - lam_bottom[i], lam_bottom[i]) + (1 - lam_bottom[i]) for b_pos, b_or in zip(box_pos, box_orientation): if b_pos <= pos and pos <= b_pos + box_size: for z in z_top + z_bottom + [x]: cons.append(z[i] >= 0 if b_or > 0 else z[i] <= 0) for idx, weight in enumerate([10, 1, .1]): for z, lam in zip(z_top + z_bottom, lam_top + lam_bottom): obj += weight * cp.quad_over_lin(cp.diff(z, k=idx + 1), lam) / (2 * n) obj += weight * cp.quad_over_lin(cp.diff(x - z, k=idx + 1), 1 - lam) / (2 * n) prob = cp.Problem(cp.Minimize(obj), cons) obj_value = prob.solve(solver=cp.MOSEK) print("lane change lower bound:", obj_value) # MICP obj = 0 z_top = [cp.Variable(n) for _ in range(n)] z_bottom = [cp.Variable(n) for _ in range(n)] x = cp.Variable(n) cons = [x[0] == initial_pos, -2 <= x, x <= 2, x[-1] == -1] lam_top = cp.Variable(n, boolean=True) lam_bottom = cp.Variable(n, boolean=True) for z, lam in zip(z_top + z_bottom, lam_top + lam_bottom): cons.append(z[0] == initial_pos * lam) cons.append(-2 * lam <= z) cons.append(z <= 2 * lam) cons.append(z[-1] == -1 * lam) for i, pos in enumerate(all_pos): obj += cp.quad_over_lin(z_top[i][i] + lam_top[i], lam_top[i]) + (1 - lam_top[i]) obj += cp.quad_over_lin(z_bottom[i][i] - lam_bottom[i], lam_bottom[i]) + (1 - lam_bottom[i]) for b_pos, b_or in zip(box_pos, box_orientation): if b_pos <= pos and pos <= b_pos + box_size: for z in z_top + z_bottom + [x]: cons.append(z[i] >= 0 if b_or > 0 else z[i] <= 0) for idx, weight in enumerate([10, 1, .1]): for z, lam in zip(z_top + z_bottom, lam_top + lam_bottom): obj += weight * cp.quad_over_lin(cp.diff(z, k=idx + 1), lam) / (2 * n) obj += weight * cp.quad_over_lin(cp.diff(x - z, k=idx + 1), 1 - lam) / (2 * n) prob = cp.Problem(cp.Minimize(obj), cons) import sys while True: answer = input( "Are you sure you would like to solve the MICP (y/n) ").lower() if answer == "y": break elif answer == "n": return else: print("Invalid answer.") continue obj_value = prob.solve(solver=cp.MOSEK, verbose=True) print("global optimum:", obj_value)
import numpy as np import cvxpy as cp from data.correlation_bounds_data import m, n, A, sigma np.set_printoptions(precision=4, suppress=True) Sigma = cp.Variable((n, n), PSD=True) constraints = [] for i in range(m): a = A[:,i] constraints.append(cp.quad_form(a, Sigma) == sigma[i] ** 2) rhos = [] for i in range(n): for j in range(i): denom = cp.geo_mean(cp.hstack([Sigma[i, i], Sigma[j, j]])) rho_ij = cp.quad_over_lin(Sigma[i, j], denom) rhos.append(rho_ij) rho_max = cp.max(cp.hstack(rhos)) obj = cp.Minimize(rho_max) problem = cp.Problem(obj, constraints) problem.solve() print(problem.status) print(Sigma.value) print(rho_max.value)
def test_quad_over_lin(self) -> None: """Test domain for quad_over_lin """ dom = cp.quad_over_lin(self.x, self.a).domain Problem(Minimize(self.a), dom).solve(solver=cp.SCS, eps=1e-6) self.assertAlmostEqual(self.a.value, 0)
def sum_square(A): rows, cols = A.size return sum([cvxpy.quad_over_lin(A[:, i], 1) for i in xrange(cols)])
import numpy as np import cvxpy as cp s = cp.Variable() l = cp.Variable() w = cp.quad_over_lin(s, l) constraints = [ s <= l, l <= s * np.sqrt(2), 20 <= l, l <= 30, w <= 20, s >= np.sqrt(300), ] obj = cp.Minimize(2 * s**2 + 2 * l + np.pi * w) problem = cp.Problem(obj, constraints) problem.solve(solver=cp.ECOS) print('status: ', problem.status) print('total cost: ', problem.value) print('l: ', l.value) print('w: ', w.value) print('filter size: ', s.value**2)
z = cp.Variable() expr = cp.power(x, 2) + cp.power(y, 2) + cp.power(z, 2) <= 2 print((expr.is_dcp())) expr2 = cp.norm(cp.hstack([1, x]), 2) - 3 * x <= y print((expr2.is_dcp())) expr3 = [cp.power(x, -1) + cp.power(y, -1) <= 5, x >= 0, y >= 0] prob = cp.Problem(cp.Minimize(1), expr3) print(prob.is_dcp()) expr4 = [x + 2 * y == 0, x - y == 0] prob1 = cp.Problem(cp.Minimize(1), expr4) print(prob1.is_dcp()) u = cp.Variable() v = cp.Variable() s = cp.Variable() expr5 = [ cp.quad_over_lin(cp.power(u, 2), v) <= s, s == y, u == x + y, v == x - y + 5 ] prob2 = cp.Problem(cp.Minimize(1), expr5) print(prob2.is_dcp()) expr6 = [-cp.log(u) - cp.log(v) <= 0, u == x + z] prob3 = cp.Problem(cp.Minimize(1), expr6) print(prob3.is_dcp()) expr7 = -cp.log(x) - 0.5 * cp.log(y) <= 0 print(expr7.is_dcp()) expr8 = [ cp.log_sum_exp(cp.hstack([u, v])) + cp.exp(x) <= 0, u == y - 1, v == 0.5 * x ] prob4 = cp.Problem(cp.Minimize(1), expr8) print(prob4.is_dcp())
def A_simple_example(): ### (a) x = cp.Variable(1) obj = cp.Minimize(cp.quad_form(x, np.array([[1]])) + 1) constraints = [cp.quad_form(x, np.array([[1]])) -6*x + 8 <= 0] prob = cp.Problem(obj, constraints) prob.solve() p_star = obj.value print("Status: " + str(prob.status)) print("x = " + str(x.value)) print("obj = " + str(p_star)) ### (b) # Plot objective obj_func = lambda x : x**2 + 1 x_vals = np.arange(-1.0, 5.0, 0.1) y_vals = [obj_func(x) for x in x_vals] plt.plot(x_vals, y_vals) # Plot feasible set feasible_x_vals = np.arange(2., 4., 0.1) plt.fill_between(feasible_x_vals, [obj_func(x) for x in feasible_x_vals], 20) # Plot optimal point and value plt.plot(x.value, p_star, 'ro') # Plot Lagrangian lagrangian_func = lambda x, l : x**2 + 1 + l*(x-2)*(x-4) for lamb in [1., 2., 3.]: lamb_y_vals = [lagrangian_func(x, lamb) for x in x_vals] plt.plot(x_vals, lamb_y_vals, color=np.random.rand(3), label="l=" + str(lamb)) # Show plot plt.legend() plt.show() ### (c) ''' Derive Lagrange dual function (1+l)x**2 - 6l*x + 8l +1 Differentiate with respect to x and set equal 0 2(1+l)x -6l = 0 x = 3l/(1+l) Substitute back to original Lagrange dual function (1+l)(3l/(1+l))**2 - 6l*(3l/(1+l) + 8l + 1 = 9l**2/(1+l) - 18l**2/(1+l) + 8l + 1 = -9l**2/(1+l) + 8l + 1 ''' l = cp.Variable(1) dual_obj = cp.Maximize(-9*cp.quad_over_lin(l, 1+l) + 8*l + 1) dual_prob = cp.Problem( dual_obj, [l >= 0]) dual_prob.solve() print("l_star = " + str(l.value)) # l_star = 2 ### (d) '''
def proposal(self): # expected_rewards, stds = params_to_gaussian(self.posterior_parameters) # expected_rewards = np.minimum(expected_rewards, 1.0) posterior_belief = self.sample_from_posterior(self.n_samples) sorted_beliefs = np.sort(posterior_belief, axis=1) thresholds = sorted_beliefs[:, -self.assortment_size].reshape(-1, 1) best_actions = sorted_beliefs[:, -self.assortment_size:] sum_rewards_best = best_actions.sum(1) r_star = sum_rewards_best.mean() expected_rewards = posterior_belief.mean(0) # min_rew = expected_rewards.min() / 1e5 # expected_rewards += np.random.rand(expected_rewards.shape[0]) * min_rew mask = posterior_belief >= thresholds p_star = mask.sum(0) / mask.shape[0] if_star = (posterior_belief * mask).sum(0) / (mask.sum(0) + 1e-12) # else_star = (posterior_belief * (1 - mask)).sum(0) / ( # (1 - mask).sum(0) + 1e-12 # ) # variances = ( # p_star * (if_star - expected_rewards) ** 2 # + (1 - p_star) * (else_star - expected_rewards) ** 2 # ) variances = p_star * (if_star - expected_rewards)**2 # posterior_belief = self.sample_from_posterior(self.n_samples) # sorted_beliefs = np.sort(posterior_belief, axis=1) # thresholds = sorted_beliefs[:, -self.assortment_size].reshape(-1, 1) # mask = posterior_belief >= thresholds # p_star = mask.sum(0) / mask.shape[0] # variances *= p_star variances = np.maximum(variances, 1e-12) # a_star_t = np.sort(expected_rewards)[-self.assortment_size] # a_s = self.posterior_parameters[0] # b_s = self.posterior_parameters[1] # ps = beta.cdf(1 / (a_star_t + 1), a=a_s, b=b_s) # entropies_start = -( # ps * np.log(np.maximum(ps, 1e-12)) # + (1 - ps) * np.log(np.maximum(1 - ps, +1e-12)) # ) # posterior_samples = 1 / beta.rvs(a=a_s, b=b_s) - 1 # new_as = np.ones(self.n_items) # new_as += a_s # new_bs = (geom.rvs(1 / (posterior_samples + 1)) - 1) + b_s # new_ps = beta.cdf(1 / (a_star_t + 1), a=new_as, b=new_bs) # new_entropies = -( # new_ps * np.log(np.maximum(new_ps, 1e-12)) # + (1 - new_ps) * np.log(np.maximum(1 - new_ps, +1e-12)) # ) # reductions = np.maximum(entropies_start - new_entropies, 1e-8) x = cp.Variable(self.n_items, pos=True) # deltas = cp.Parameter(self.n_items, pos=True) rewards = cp.Parameter(self.n_items, ) gains = cp.Parameter(self.n_items, pos=True) # exp_regret = r_star - x @ rewards deltas = r_star - x @ rewards exp_gain = x @ gains information_ratio = cp.quad_over_lin(deltas, exp_gain) objective = cp.Minimize(information_ratio) constraints = [0 <= x, x <= 1, cp.sum(x) == self.assortment_size] prob = cp.Problem( objective, constraints, ) rewards.value = expected_rewards gains.value = variances try: prob.solve(solver="ECOS") zeros_index = (x.value < 1e-3) ones_index = (x.value > 1 - 1e-3) nzeros = zeros_index.sum() nones = ones_index.sum() nitems = x.value.shape[0] logging.debug( f"{nitems - nones - nzeros} nstrict, {nones} ones, {nzeros} zeroes, {nitems} total items" ) if (nitems - nones - nzeros) == 2: all_items = np.arange(nitems) strict_items = all_items[~np. bitwise_or(zeros_index, ones_index)] probas = x.value[~np.bitwise_or(zeros_index, ones_index)] assert strict_items.shape[0] == 2, strict_items assert probas.shape[0] == 2, probas # 2 items to randomize the selection over logging.debug( f"items: {strict_items}, with probas: {probas}", ) rho = probas[0] u = np.random.rand() if rho <= u: remaning_item = strict_items[0] else: remaning_item = strict_items[1] action = np.sort( np.concatenate([ act_optimally(x.value, top_k=self.assortment_size - 1), np.array([remaning_item]) ])) else: action = act_optimally(x.value, top_k=self.assortment_size) if self.c % 5 == 121234: logging.debug( f"a:{action},x:{(100 * x.value).astype(int)},rew:{(100 * expected_rewards).astype(int)},gain:{(100 * np.sqrt(variances)).astype(int)}" ) logging.debug( f"if_optimal: {if_star}, rew:{logar(expected_rewards)}, probas: {logar(p_star)}", ) logging.debug( f"if_optimal: {logar(if_star)}, rew:{logar(expected_rewards)}, probas: {logar(p_star)}", ) logging.debug( f"n{self.posterior_parameters[0]}, v{self.posterior_parameters[1] / self.posterior_parameters[0]}," ) logging.debug(f"obj{prob.value}") except cp.SolverError: logging.warning("solver error") posterior_belief = self.sample_from_posterior(1) action = act_optimally(np.squeeze(posterior_belief), top_k=self.assortment_size) except TypeError: logging.warning("solver error") posterior_belief = self.sample_from_posterior(1) action = act_optimally(np.squeeze(posterior_belief), top_k=self.assortment_size) self.current_action = action self.c += 1 return action
def projectpoly(self, v=None, aMax=1., order=3): """GAA's magical wizardy for the original problem (projection) now with polynomials""" # get constants g = self.goal # g goal point p = self.position # p # get dim, either 2 or 3 n = len(p) if v is None: v = np.zeros(n) goalDist = self.goalDist() minDist = min([ob.estimate.dist(g) for ob in self.obstacles]) if minDist > goalDist: return np.vstack((p, v / 3. + p, g, g)).T, goalDist # create cvx program x = cvx.Variable((n, order + 1)) # projection pt L = cvx.Variable(len(self.obstacles), nonneg=True) # dual on elip obj = cvx.Minimize(cvx.norm(x[:, -1] - g, 2)) cnts = [] for i, ob in enumerate(self.obstacles): estimate = ob.estimate # check if we need to project if ob.estimate.dist(g) > goalDist: cnts += [L[i] == 0] else: cnts += [L[i] >= 0] u = estimate.center # mu S = estimate.body # sigma S_inv = la.inv(S) # sigma inv D, U = la.eig(S) # get eigdecomp # Form objective. Q = np.dot(U.T, S_inv.dot(u)) for j in range(order + 1): cnts += [ np.sum([ cvx.quad_over_lin( L[i] * Q[d] + U.T[d, :] * x[:, j], L[i] * (D[d]**-1) + 1) for d in range(n) ]) - 2 * p.T * x[:, j] <= L[i] * np.dot(u, S_inv.dot(u)) - cvx.sum_squares(p) - L[i] ] # dynamics on polynomial cnts += [x[:, 0] == p] # ic pos cnts += [3 * (x[:, 1] - x[:, 0]) == v] # ic vel cnts += [cvx.norm(6 * (x[:, 2] - 2 * x[:, 1] + x[:, 0]), 2) <= aMax ] # ic acel cnts += [3 * (x[:, -2] - x[:, -1]) == 0] # final vel cnts += [cvx.norm(6 * (x[:, 3] - 2 * x[:, 2] + x[:, 1]), 2) <= aMax ] # final ace # Form and solve problem. prob = cvx.Problem(obj, cnts) # tic = time.time() prob.solve(solver=cvx.ECOS, reltol=1e-3, abstol=1e-3) # print(prob.status) # print(f"Projection solution time {time.time()-tic:2.4f}sec") return np.array(x.value), obj.value
def sdpsolve(problem, compute_dyads=False, noquad=False, eqconstraint=False): """A semidefinite convex solver for operator linear inverse problems. Solve the nuclear norm recovery problem with a semidefinite inequality replacing the decomposition constraint. See `NucNorm_SDR` for more details. Parameters ---------- problem : Problem The `Problem` object to solve. compute_dyads : Optional[bool] Whether to return the operator in dyadic representation. When True, we compute the SVD of the matrix decision variable and create a `DyadsOperator` using the scaled left/right singular vectors as the left/right factors. Otherwise we reshape the matrix decision variable to have the shape of the operator and return an `ArrayOperator`. Default value is False. noquad : Optional[bool] Whether we replace the quadratic measurement error with a norm. Default value is False. eqconstraint : Optional[bool] Whether we solve the problem with an equality constraint. Default value is False. Returns ------- SolverOutput An object with the results of the optimization problem. """ matshape = (int(np.prod(problem.shape[0:2])), int(np.prod(problem.shape[2:4]))) RANK = min(matshape) start = time.time() X = cvxpy.Semidef(matshape[0] + matshape[1]) A = X[0:matshape[0], matshape[0]:] diag = cvxpy.diag(X) ldiag = diag[0:matshape[0]] rdiag = diag[matshape[0]:] reg = problem.norm.norm_sdp(ldiag, rdiag) meastemp = problem.measurementobj.matapply(A) if eqconstraint: prob = cvxpy.Problem(cvxpy.Minimize(reg), [ meastemp == problem.measurementvec, ]) else: LAMBDA = problem.penconst if noquad: measerr = cvxpy.norm(meastemp - problem.measurementvec) else: measerr = cvxpy.quad_over_lin(meastemp - problem.measurementvec, 2.0) prob = cvxpy.Problem(cvxpy.Minimize(measerr + LAMBDA * reg)) end_setup = time.time() prob.solve(solver=problem.solver, **problem.solveropts) end_solve = time.time() out = SolverOutput() out.problem = problem out.cvxpy_probs = (prob, ) if compute_dyads: # TODO: compute_dyads should use the semidefinite representation U, S, V = np.linalg.svd(A.value, full_matrices=0) lfactors = [ np.sqrt(S[i]) * U[:, i].reshape(problem.shape[0:2], order='F') for i in range(RANK) ] # just using V.T instead of V.H rfactors = [ np.sqrt(S[i]) * V.T[:, i].reshape(problem.shape[2:4], order='F') for i in range(RANK) ] out.recovered = operators.DyadsOperator(lfactors, rfactors) else: temp = np.array(A.value).reshape(problem.shape, order='F') out.recovered = operators.ArrayOperator(temp) out.setup_time = end_setup - start out.solve_time = end_solve - end_setup out.objval = prob.objective.value return out
def main(): q = np.array(read_initial_trajectory()) a = [[math.sqrt(pmax) * 0.95] * K] * M r_list = [] iter_time = 0 q_track.append(eval(str(np.ndarray.tolist(q)))) while True: iter_time += 1 # update rate evaluation r_l = [] for n in range(M): r = 0 for k in range(K): addictive_noise = 1 for j in range(K): if j != k: addictive_noise += gamma * (a[n][j]**2) / ( np.linalg.norm(q[n][j] - s[k])**2) r += bandwidth * math.log( 1 + gamma * (a[n][k]**2) / ((np.linalg.norm(q[n][k] - s[k])**2) * addictive_noise), 2) r_l.append(r * 1024) print(r_l) r_list.append(r_l) print(sum(r_l)) if len(r_list) >= minimum_iter_time: if (sum(r_list[-1]) - sum(r_list[-2])) / sum(r_list[-2]) <= ep: break # update ir ir = [[None] * K] * M for n in range(M): for k in range(K): ik = 0 for j in range(K): if j != k: ik += gamma * a[n][j] * a[n][j] / ( np.linalg.norm(q[n][j] - s[k])**2) ir[n][k] = ik # update power and trajectory av = [] qv = [] for n in range(M): av.append(cp.Variable(shape=(K, 1))) qv.append(cp.Variable(shape=(K, 3))) objfunc = [] for n in range(M): for k in range(K): termk = 0 term1 = 1 for j in range(K): term1 += gamma * (2 * a[n][j] * av[n][j]) / ( np.linalg.norm(q[n][j] - s[k])**2) term1 -= gamma * (a[n][j] * a[n][j]) * ( cp.norm(qv[n][j] - s[k])** 2) / (np.linalg.norm(q[n][j] - s[k])**4) termk += (cp.log(term1)) termk += (-1 * math.log(1 + ir[n][k])) termk += (ir[n][k] / (1 + ir[n][k])) term2 = [] for j in range(K): ratio = -1 * gamma / (1 + ir[n][k]) if j != k: det = cp.norm(q[n][j] - s[k])**2 + 2 * ( q[n][j] - s[k]).transpose() * (qv[n][j] - q[n][j]) term2.append(ratio * cp.quad_over_lin(av[n][j], det)) termk += cp.sum(term2) objfunc.append(termk) constr = [] for k in range(K): constr.append(qv[M - 1][k][0] == read_initial_trajectory()[-1][k][0]) constr.append(qv[M - 1][k][1] == read_initial_trajectory()[-1][k][1]) constr.append(qv[M - 1][k][2] == read_initial_trajectory()[-1][k][2]) constr.append(qv[0][k][0] == read_initial_trajectory()[0][k][0]) constr.append(qv[0][k][1] == read_initial_trajectory()[0][k][1]) constr.append(qv[0][k][2] == read_initial_trajectory()[0][k][2]) for n in range(M): for k in range(K): constr.append(qv[n][k][2] <= hmax) constr.append(qv[n][k][2] >= hmin) constr.append(av[n][k] <= math.sqrt(pmax)) constr.append(av[n][k] >= 0) for n in range(1, M): for k in range(K): constr.append( cp.norm(qv[n][k][0:2] - qv[n - 1][k][0:2]) <= 0.5 * vl) constr.append(qv[n][k][2] - qv[n - 1][k][2] <= 0.5 * va) constr.append(qv[n][k][2] - qv[n - 1][k][2] >= -0.5 * va) for n in range(M): for k in range(K): for j in range(k + 1, K): constr.append(2 * (q[n][k] - q[n][j]).transpose() * (qv[n][k] - qv[n][j]) >= (dmin**2)) obj = cp.Maximize(cp.sum(objfunc)) prob = cp.Problem(obj, constr) prob.solve() print("Iteration: {}\tStatus: {}".format(iter_time, prob.status)) a = [v.value for v in av] q = [v.value for v in qv] q_track.append([[qnk for qnk in qn] for qn in q]) for i in range(len(q_track)): for k in range(K): with open('trajectory{}_iteration{}.csv'.format(k, i), mode='w') as csv_file: wr = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for n in range(M): wr.writerow(q_track[i][n][k])
def main(show): # generate data np.random.seed(243) m, n = 5, 1 n_outliers = 1 eta = 0.1 alpha = 0.5 A = np.random.randn(m, n) x_true = np.random.randn(n) b = A @ x_true + 1e-1 * np.random.randn(m) b[np.random.choice(np.arange(m), replace=False, size=n_outliers)] *= -1. # alternating x_alternating = cp.Variable(n) objective = 0.0 for i in range(m): objective += sccf.minimum(cp.square(A[i]@x_alternating-b[i]), alpha) objective += eta * cp.sum_squares(x_alternating) prob = sccf.Problem(objective) prob.solve() # solve relaxed problem x_relaxed = cp.Variable(n) z = [cp.Variable(n) for _ in range(m)] s = cp.Variable(m) objective = 0.0 constraints = [0 <= s, s <= 1] for i in range(m): objective += cp.quad_over_lin(A[i, :] @ z[i] - b[i] * s[i], s[i]) + (1.0 - s[i]) * alpha + \ eta / m * (cp.quad_over_lin(x_relaxed - z[i], 1.0 - s[i]) + eta / m * cp.quad_over_lin(z[i], s[i])) prob = cp.Problem(cp.Minimize(objective), constraints) result = prob.solve(solver=cp.MOSEK) # alternating w/ relaxed initialization x_alternating_perspective = cp.Variable(n) x_alternating_perspective.value = x_relaxed.value objective = 0.0 for i in range(m): objective += sccf.minimum(cp.square(A[i]@x_alternating_perspective-b[i]), alpha) objective += eta * cp.sum_squares(x_alternating_perspective) prob = sccf.Problem(objective) prob.solve(warm_start=True) # brute force evaluate function and perspective xs = np.linspace(-5, 5, 100) f = np.sum(np.minimum(np.square(A * xs - b[:, None]), alpha), axis=0) + eta*xs**2 f_persp = [] for x in xs: z = [cp.Variable(n) for _ in range(m)] s = cp.Variable(m) objective = 0.0 constraints = [0 <= s, s <= 1] for i in range(m): objective += cp.quad_over_lin(A[i, :] @ z[i] - b[i] * s[i], s[i]) + (1.0 - s[i]) * alpha + \ eta / m * (cp.quad_over_lin(x - z[i], 1.0 - s[i]) + eta / m * cp.quad_over_lin(z[i], s[i])) prob = cp.Problem(cp.Minimize(objective), constraints) result = prob.solve(solver=cp.MOSEK) f_persp.append(result) def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() return idx # plot latexify(fig_width=6, fig_height=4) plt.plot(xs, f, '-', label="$L(x)$", c='k') plt.plot(xs, f_persp, '--', label="perspective", c='k') plt.plot(x_alternating.value[0], ) plt.scatter(x_alternating.value[0], f[find_nearest(xs, x_alternating.value[0])], marker='o', label="sccf (no init)", c='k') plt.scatter(x_alternating_perspective.value[0], f[find_nearest(xs, x_alternating_perspective.value[0])], marker='*', label="sccf (persp init)", c='k') plt.legend() plt.xlabel("$x$") plt.savefig("figs/perspective.pdf") if show: plt.show()
def cvx_hoovering(): ini = np.array([[-250, -250, hmin], [-250, 250, hmin], [250, -250, hmin], [250, 250, hmin]]) ipc = np.array([[-250, -250, hmin], [-250, 250, hmin], [250, -250, hmin], [250, 250, hmin]]) ac = [math.sqrt(pmax) * 0.95 for k in range(K)] ir = [None] * K while True: print(ac) print(ipc) R_r = 0 for k in range(K): addnoise = 1 for j in range(K): if j != k: addnoise += gamma * (ac[j] ** 2) / (np.linalg.norm(ipc[j] - s[k]) ** 2) R_r += bandwidth * math.log(1 + (gamma * (ac[k] ** 2)) / (np.linalg.norm(ipc[k] - s[k]) ** 2 * addnoise), 2) for k in range(K): ik = 0 for j in range(K): if j != k: ik += gamma * (ac[j] ** 2) / ((np.linalg.norm(ipc[j] - s[k])) ** 2) ir[k] = ik a = cp.Variable(shape=(K, 1)) q = cp.Variable(shape=(K, 3)) objfunc = [] for k in range(K): term1 = 1 for j in range(K): term1 += gamma * (2 * ac[j] * a[j] / (cp.norm(ipc[j] - s[k]) ** 2)) term1 -= gamma * (ac[j] ** 2) * (cp.norm(q[j] - s[k]) ** 2) / (np.linalg.norm(ipc[j] - s[k]) ** 4) objfunc.append(cp.log(term1)) objfunc.append(-1 * math.log(1 + ir[k], 2)) objfunc.append(ir[k] / (1 + ir[k])) term2 = [] for j in range(K): ratio = -1 * gamma / (1 + ir[k]) if j != k: det = np.linalg.norm(ipc[j] - s[k]) ** 2 + 2 * (ipc[j] - s[k]).transpose() * (q[j] - ipc[j]) term2.append(ratio * cp.quad_over_lin(a[j], det)) objfunc.append(cp.sum(term2)) constr = [] for k in range(K): constr.append(q[k][2] >= hmin) constr.append(q[k][2] <= hmax) constr.append(cp.norm(q[k][0:2] - ini[k][0:2]) <= 0.5 * M * tdv * vl) constr.append(a[k] >= 0) constr.append(a[k] <= math.sqrt(pmax)) for j in range(k + 1, K): constr.append( 2 * (ipc[k] - ipc[j]).transpose() * (q[k] - q[j]) >= cp.norm(ipc[j] - s[k]) ** 2 + dmin ** 2) prob = cp.Problem(cp.Maximize(sum(objfunc)), constr) prob.solve() ac = a.value ipc = q.value
import cvxpy as cp # Create two scalar optimization variables. x = cp.Variable() y = cp.Variable() # Create two constraints. constraints = [ cp.quad_over_lin(x + y, cp.sqrt(y)) <= x - y + 5, y >= 0, x >= y - 5 ] # Form objective. obj = cp.Minimize((x - y + 2)**2) # Form and solve problem. prob = cp.Problem(obj, constraints) prob.solve() # Returns the optimal value. print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x.value, y.value)