def find_ideal_pt_for_person_in_ball(center, radius, idealpt_and_radius, constraint="l1"): X = cvxpy.Variable(5) # 1 point for each item fun = 0 y = idealpt_and_radius[0] w = idealpt_and_radius[1] sumsq = math.sqrt(sum([math.pow(w[i], 2) for i in range(5)])) w = [w[i] / sumsq for i in range(5)] for slider in range(5): fun += w[slider] * cvxpy.abs(X[slider] - y[slider]) obj = cvxpy.Minimize(fun) constraints = [X >= 0, X[0] + X[1] + X[2] - X[3] + 162 == X[4]] if constraint == "l1": constraints += [cvxpy.sum_entries( cvxpy.abs(X[0:4] - center[0:4])) <= radius] else: constraints += [cvxpy.sum_entries( cvxpy.square(X[0:4] - center[0:4])) <= radius**2] prob = cvxpy.Problem(obj, constraints) result = prob.solve() items = [X.value[i, 0] for i in range(5)] if constraint == "l1": credits = [abs(items[i] - center[i]) / radius for i in range(4)] else: credits = [(items[i] - center[i])**2 / radius**2 for i in range(4)] deficit = calculate_deficit(items) items.append(deficit) return items, credits
def test_signed_curvature(self): # Convex argument. expr = cvx.abs(1 + cvx.exp(cvx.Variable()) ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( -cvx.entr(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) expr = cvx.abs( -cvx.log(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) # Concave argument. expr = cvx.abs( cvx.log(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) expr = cvx.abs( -cvx.square(cvx.Variable()) ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( cvx.entr(cvx.Variable()) ) self.assertEqual(expr.curvature, s.UNKNOWN) # Affine argument. expr = cvx.abs( cvx.NonNegative() ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( -cvx.NonNegative() ) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.abs( cvx.Variable() ) self.assertEqual(expr.curvature, s.CONVEX)
def linear_mpc_control(xref, xbar, x0, dref): """ linear mpc control xref: reference point xbar: operational point x0: initial state dref: reference steer angle """ x = cvxpy.Variable((NX, T + 1)) u = cvxpy.Variable((NU, T)) cost = 0.0 constraints = [] for t in range(T): cost += cvxpy.quad_form(u[:, t], R) if t != 0: cost += cvxpy.quad_form(xref[:, t] - x[:, t], Q) A, B, C = get_linear_model_matrix( xbar[2, t], xbar[3, t], dref[0, t]) constraints += [x[:, t + 1] == A * x[:, t] + B * u[:, t] + C] if t < (T - 1): cost += cvxpy.quad_form(u[:, t + 1] - u[:, t], Rd) constraints += [cvxpy.abs(u[1, t + 1] - u[1, t]) <= MAX_DSTEER * DT] cost += cvxpy.quad_form(xref[:, T] - x[:, T], Qf) constraints += [x[:, 0] == x0] constraints += [x[2, :] <= MAX_SPEED] constraints += [x[2, :] >= MIN_SPEED] constraints += [cvxpy.abs(u[0, :]) <= MAX_ACCEL] constraints += [cvxpy.abs(u[1, :]) <= MAX_STEER] prob = cvxpy.Problem(cvxpy.Minimize(cost), constraints) prob.solve(solver=cvxpy.ECOS, verbose=False) if prob.status == cvxpy.OPTIMAL or prob.status == cvxpy.OPTIMAL_INACCURATE: ox = get_nparray_from_matrix(x.value[0, :]) oy = get_nparray_from_matrix(x.value[1, :]) ov = get_nparray_from_matrix(x.value[2, :]) oyaw = get_nparray_from_matrix(x.value[3, :]) oa = get_nparray_from_matrix(u.value[0, :]) odelta = get_nparray_from_matrix(u.value[1, :]) else: print("Error: Cannot solve mpc..") oa, odelta, ox, oy, oyaw, ov = None, None, None, None, None, None return oa, odelta, ox, oy, oyaw, ov
def transition_to_action(s0,s1, dynamics, max_dtheta, max_ddx, max_iter=30, max_line_searches=10): """recovers a control signal u that can cause a transition from s0 to s1. specifically, minimize || f(s0,u) - s1 ||^2 as a Sequential Quadratic Program. """ # the current step size along the search direction recovered by the QP step = 1. # initial guess s0 = array(s0) s1 = array(s1) u0 = array((0,0)) def cost(u): "|| f(s0,u) - s1 ||^2" return sum((dynamics(s0,u)['val'] - s1)**2) # the value of the initial guess best_cost = cost(u0) for it in xrange(max_iter): f = dynamics(s0, u0, derivs={'du'}) # linearize || f(s0,u) - s1 ||^2 about u0 and solve as a QP u = CX.Variable(len(u0), name='u') objective = CX.square(CX.norm( array(f['val']) + vstack(f['du'])*(u-u0) - s1 )) p = CX.Problem(CX.Minimize(objective), [CX.abs(u[0]) <= max_ddx, CX.abs(u[1]) <= max_dtheta]) r = p.solve() unew = array(u.value.flat) # line search along unew-u0 from u0 line_search_success = False for line_searches in xrange(max_line_searches): new_cost = cost(u0 + step*(unew-u0)) if new_cost < best_cost: # accept the step best_cost = new_cost u0 = u0 + step*(unew-u0) # grow the step for the next iteration step *= 1.2 line_search_success = True else: # shrink the step size and try again step *= 0.5 if not line_search_success: # convergence is when line search fails. return u0 print 'Warning: failed to converge' return u0
def distance_from_separating_hyperplane(self,tple): no_of_weights=self.dimensions no_of_tuple=self.dimensions weights=cvxpy.Variable(no_of_weights,1) tuple=numpy.array(tple) print "weights:",weights print "tuple:",tuple bias=self.bias svm_function = 0.0 for i in xrange(no_of_weights): svm_function += cvxpy.abs(weights[i,0]) objective=cvxpy.Minimize(cvxpy.abs(svm_function)*0.5) print "============================================" print "Objective Function" print "============================================" print objective constraint=0.0 constraints=[] for i,k in zip(xrange(no_of_weights),xrange(no_of_tuple)): constraint += weights[i,0]*tuple[k] constraint += bias print "constraint:",constraint constraints.append(cvxpy.abs(constraint) >= 1) print "============================================" print "Constraints" print "============================================" print constraints problem=cvxpy.Problem(objective,constraints) print "=====================================" print "Installed Solvers:" print "=====================================" print cvxpy.installed_solvers() print "Is Problem DCCP:",dccp.is_dccp(problem) print "=====================================" print "CVXPY args:" print "=====================================" result=problem.solve(solver=cvxpy.SCS,verbose=True,method='dccp') print "=====================================" print "Problem value:" print "=====================================" print problem.value print "=====================================" print "Result:" print "=====================================" print result return (result,tuple)
def setUp(self): """ Use cvxopt to get ground truth values """ from cvxopt import lapack,solvers,matrix,spdiag,log,div,normal,setseed from cvxopt.modeling import variable,op,max,sum solvers.options['show_progress'] = 0 setseed() m,n = 100,30 A = normal(m,n) b = normal(m,1) b /= (1.1*max(abs(b))) self.m,self.n,self.A,self.b = m,n,A,b # l1 approximation # minimize || A*x + b ||_1 x = variable(n) op(sum(abs(A*x+b))).solve() self.x1 = x.value # l2 approximation # minimize || A*x + b ||_2 bprime = -matrix(b) Aprime = matrix(A) lapack.gels(Aprime,bprime) self.x2 = bprime[:n] # Deadzone approximation # minimize sum(max(abs(A*x+b)-0.5, 0.0)) x = variable(n) dzop = op(sum(max(abs(A*x+b)-0.5, 0.0))) dzop.solve() self.obj_dz = sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) # Log barrier # minimize -sum (log ( 1.0 - (A*x+b)**2)) def F(x=None, z=None): if x is None: return 0, matrix(0.0,(n,1)) y = A*x+b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1-y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0*z[0]*div(1.0+y**2,(1.0-y**2)**2))*A return f,gradf.T,H self.cxlb = solvers.cp(F)['x']
def solve_problem(weights, alpha_vector): constaints = [sum(weights) == 0.0, sum(cvx.abs(weights)) <= 1.0] obj = optimal_holdings_strict_factor._get_obj(weights, alpha_vector) prob = cvx.Problem(obj, constaints) prob.solve(max_iters=500) return np.asarray(weights.value).flatten()
def TRCPA_v2(M,n,tf,q,lam): Avars = [] for i in range(tf-1): Avars.append(cvx.Variable(n,n)) Ltop = cvx.Variable(n,q) Lbottom = np.zeros((n*(tf-1),q)) L = cvx.vstack(Ltop,Lbottom) S = cvx.Variable(n*tf,q) print("Check 1") objective = cvx.Minimize(cvx.norm(L,"nuc") + lam*np.ones((1,n*tf))*cvx.abs(S)*np.ones((q,1))) identity = np.eye(n) print("Check 2") constraints = [np.dot(identity,M[0:n,:])==Ltop+S[0:n,:]] for i in range(tf-1): constraints.append(-1*Avars[i]*M[(i*n):(i+1)*n,:] + np.dot(identity,M[((i+1)*n):(i+2)*n,:])==L[((i+1)*n):(i+2)*n,:]+S[((i+1)*n):(i+2)*n,:]) print("Check 3") print(constraints) prob = cvx.Problem(objective,constraints) print("Check 4") result = prob.solve(verbose = True) print("Check 5") Ltop_final = Ltop.value Avars_final = [] for x in Avars: Avars_final.append(x.value) S_final = S.value return (Avars_final,Ltop_final,Lbottom,S_final)
def _constraints(self, X, S, error_tolerance): """ Parameters ---------- X : np.array Data matrix with missing values S : cvxpy.Variable Representation of solution variable """ missing_values = np.isnan(X) self._check_missing_value_mask(missing_values) # copy the array before modifying it X = X.copy() # zero out the NaN values X[missing_values] = 0 ok_mask = ~missing_values masked_X = cvxpy.mul_elemwise(ok_mask, X) masked_S = cvxpy.mul_elemwise(ok_mask, S) abs_diff = cvxpy.abs(masked_S - masked_X) close_to_data = abs_diff <= error_tolerance constraints = [close_to_data] if self.require_symmetric_solution: constraints.append(S == S.T) if self.min_value is not None: constraints.append(S >= self.min_value) if self.max_value is not None: constraints.append(S <= self.max_value) return constraints
def _constraints(self, X, missing_mask, S, error_tolerance): """ Parameters ---------- X : np.array Data matrix with missing values filled in missing_mask : np.array Boolean array indicating where missing values were S : cvxpy.Variable Representation of solution variable """ ok_mask = ~missing_mask masked_X = cvxpy.mul_elemwise(ok_mask, X) masked_S = cvxpy.mul_elemwise(ok_mask, S) abs_diff = cvxpy.abs(masked_S - masked_X) close_to_data = abs_diff <= error_tolerance constraints = [close_to_data] if self.require_symmetric_solution: constraints.append(S == S.T) if self.min_value is not None: constraints.append(S >= self.min_value) if self.max_value is not None: constraints.append(S <= self.max_value) return constraints
def cbp_taylor(y, F, Delta, penalty=0.1, order=1): """ 1st order taylor approximation """ # generate derivative matrices dF = list() current = F for i in range(order): dF.append( deriv(current,t) ) current = dF[-1] # Construct the problem. Fp = cvxopt.matrix(F) dFp = cvxopt.matrix(dF[0]) yp = cvxopt.matrix(y) gamma = cp.Parameter(sign="positive", name='gamma') gamma.value = penalty x = cp.Variable(F.shape[1],name='x') d = cp.Variable(F.shape[1],name='d') objective = cp.Minimize(sum(cp.square(yp - Fp*x - dFp*d)) + gamma*cp.norm(x, 1)) constraints = [0 <= x, cp.abs(d) <= 0.5*Delta*x] p = cp.Problem(objective, constraints) # solve result = p.solve() # reconstruct yhat = F.dot(np.array(x.value)) + dF[0].dot(np.array(d.value)) return np.array(x.value), yhat, np.array(d.value), p.value
def optimize_pos_basic(self, df): w_target = df['pos']/df['pos'].abs().sum() w_target = w_target.values nassets=df.shape[0] w = cvxpy.Variable(nassets) obj = cvxpy.Minimize(cvxpy.norm(w-w_target)) constraints = [cvxpy.sum_entries(cvxpy.abs(w)) == 1.0] constraints += [cvxpy.abs(cvxpy.sum_entries(w)) <= 0.1] constraints += [cvxpy.abs(w) <= 1.0/nassets] #constraints += [w >= 0] prob = cvxpy.Problem(obj, constraints) prob.solve(verbose=False, method='dccp') return np.reshape(w.value, [nassets])
def get_constr_error(constr): if isinstance(constr, cvx.constraints.EqConstraint): error = cvx.abs(constr.args[0] - constr.args[1]) elif isinstance(constr, cvx.constraints.LeqConstraint): error = cvx.pos(constr.args[0] - constr.args[1]) elif isinstance(constr, cvx.constraints.PSDConstraint): mat = constr.args[0] - constr.args[1] error = cvx.neg(cvx.lambda_min(mat + mat.T)/2) return cvx.sum_entries(error)
def runndCVXPy(A, b, clambda): n_vars = A.shape[1] x = cvxpy.Variable(n_vars) obj = cvxpy.Minimize( clambda * cvxpy.sum_entries(cvxpy.abs(x)) + 0.5 * cvxpy.sum_entries(cvxpy.square(A * x - b))) prob = cvxpy.Problem(obj, []) prob.solve(verbose=False) return x.value
def F(x=None, z=None): if x is None: return 0, matrix(0.0,(n,1)) y = A*x+b if max(abs(y)) >= 1.0: return None f = -sum(log(1.0 - y**2)) gradf = 2.0 * A.T * div(y, 1-y**2) if z is None: return f, gradf.T H = A.T * spdiag(2.0*z[0]*div(1.0+y**2,(1.0-y**2)**2))*A return f,gradf.T,H
def create(**kwargs): m = kwargs["m"] n = kwargs["n"] k = 10 A = [problem_util.normalized_data_matrix(m,n,1) for i in range(k)] B = problem_util.normalized_data_matrix(k,n,1) c = np.random.rand(k) x = cp.Variable(n) t = cp.Variable(k) f = cp.max_entries(t+cp.abs(B*x-c)) C = [] for i in range(k): C.append(cp.pnorm(A[i]*x, 2) <= t[i]) t_eval = lambda: np.array([cp.pnorm(A[i]*x, 2).value for i in range(k)]) f_eval = lambda: cp.max_entries(t_eval() + cp.abs(B*x-c)).value return cp.Problem(cp.Minimize(f), C), f_eval
def test_problem_penalty(self): """ Compare cvxpy solutions to cvxopt ground truth """ from cvxpy import (matrix,variable,program,minimize, sum,abs,norm2,log,square,zeros,max, hstack,vstack) m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # set tolerance to 5 significant digits tol_exp = 5 # l1 approximation x = variable(n) p = program(minimize(sum(abs(A*x + b)))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x1,tol_exp) # l2 approximation x = variable(n) p = program(minimize(norm2(A*x + b))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.x2,tol_exp) # Deadzone approximation - implementation is currently ugly (need max along axis) x = variable(n) Axbm = abs(A*x+b)-0.5 Axbm_deadzone = vstack([max(hstack((Axbm[i,0],0.0))) for i in range(m)]) p = program(minimize(sum(Axbm_deadzone))) p.solve(True) obj_dz_cvxpy = np.sum(np.max([np.abs(A*x.value+b)-0.5,np.zeros((m,1))],axis=0)) np.testing.assert_array_almost_equal(obj_dz_cvxpy,self.obj_dz,tol_exp) # Log barrier x = variable(n) p = program(minimize(-sum(log(1.0-square(A*x + b))))) p.solve(True) np.testing.assert_array_almost_equal(x.value,self.cxlb,tol_exp)
def constraints(self): cons = [] capacity_limit = [cp.abs(self.flow) <= self.capacity] ptdf_constraint = [sum(gv.ptdf_matrix[self.uid,s.uid] * -1 * s.accumulation for s in \ gv.sources + gv.nodes if s.uid != gv.slack) == self.flow] cons.extend(ptdf_constraint) cons.extend(capacity_limit) #ptdf_constraint = [cp.abs(sum(gv.ptdf_matrix[self.uid,s.uid] * -1 * s.accumulation for s in \ #gv.sources + gv.nodes if s.uid != gv.slack)) <= self.capacity] #cons.extend(ptdf_constraint) return cons
def solveMarkowitzConstraint(c, A, Q, qc, v, x0, bxl, bxu, bcl, bcu): """ Solves a convex program with a quadratic constraint and linear abs penalty maximize c'*x - v'*abs{x - x0} subject to bcl <= A*x <= bcu bxl <= x <= bxu sqrt(x'*Q*x) <= qc """ x = cvx.Variable(len(c)) constraints = [A*x <= bcu, bcl <= A*x, bxl <= x, x <= bxu, cvx.quad_form(x, Q) <= qc*qc] objective = x.T*c - cvx.abs(x - x0).T*v ccu.maximize(objective=objective, constraints=constraints) return x.value
def setup_optim(users, xs, L, Ux, constraints, uo, lambda_=1e-2): """Compute all terms needed to define the optimization problem""" (n, d), nx = users.shape, xs.shape[0] weight = np.dot(np.ones(nx), np.dot(xs, xs.T)) laps = [] for m, xm in enumerate(xs): k = np.matrix(np.kron(np.eye(n), xm[:, np.newaxis])) vm = k.T*Ux laps.append(cvx.quad_form(vm, L[m])) weighted_laplacian = sum((w*l for w, l in zip(weight, laps))) visible_users = sorted(set(range(n)) - set(uo)) visible_l1_norm = np.abs(users[visible_users, :]).sum() regul = lambda_*(cvx.sum_entries(cvx.abs(Ux)) - visible_l1_norm) obj = cvx.Minimize(weighted_laplacian + regul) return cvx.Problem(obj, constraints)
def create_fused_lasso(W, g): reg = 0 inds = W.nonzero() rows = np.asarray(inds[0]).T.squeeze() cols = np.asarray(inds[1]).T.squeeze() if rows.size == 0 or len(rows.shape) == 0: return reg for i in range(rows.shape[0]): row = rows[i] col = cols[i] if row == col: continue Wij = W[row,col] ''' if i >= j or Lij == 0: continue ''' reg = reg + Wij*cvx.abs(g[row]-g[col]) return reg
def PROGRAM( roadnet, surplus, objectives ) : """ construct the program """ # optvars assist = dict() cost = dict() DELTA = .00001 # cvxpy isn't quite robust to non-full dimensional optimization for _,__,road in roadnet.edges_iter( keys=True ) : assist[road] = cvxpy.variable( name='z_{%s}' % road ) cost[road] = cvxpy.variable( name='c_{%s}' % road ) #print assist #print cost objfunc = sum( cost.values() ) OBJECTIVE = cvxpy.minimize( objfunc ) CONSTRAINTS = [] # the flow conservation constraints for u in roadnet.nodes_iter() : INFLOWS = [] for _,__,road in roadnet.in_edges( u, keys=True ) : INFLOWS.append( assist[road] + surplus[road] ) OUTFLOWS = [] for _,__,road in roadnet.out_edges( u, keys=True ) : OUTFLOWS.append( assist[road] ) #conserve_u = cvxpy.eq( sum(OUTFLOWS), sum(INFLOWS) ) error_u = sum(OUTFLOWS) - sum(INFLOWS) conserve_u = cvxpy.leq( cvxpy.abs( error_u ), DELTA ) CONSTRAINTS.append( conserve_u ) # the cost-form constraints for road in cost : for f, line in objectives[road].iter_items() : # is this plus or minus alpha? LB = cvxpy.geq( cost[road], line.offset + line.slope * assist[road] ) CONSTRAINTS.append( LB ) prog = cvxpy.program( OBJECTIVE, CONSTRAINTS ) return prog, assist
def calculate_full_elicitation_euclideanpoint(data): X = cvxpy.Variable(5) #1 point for each mechanism fun = 0 for d in data: y = [d['question_data']['slider' + str(slider) + '_loc'] for slider in range(5)] w = [d['question_data']['slider' + str(slider) + '_weight'] for slider in range(5)] sumsq = math.sqrt(sum([math.pow(w[i],2) for i in range(5)])) w = [w[i] / sumsq for i in range(5)] for slider in range(5): fun += w[slider]*cvxpy.abs(X[slider] - y[slider]) obj = cvxpy.Minimize(fun) constraints = [X >= 0, X[0] + X[1] + X[2] - X[3] + INITIALDEFICITADDITIVE == X[4]] prob = cvxpy.Problem(obj, constraints) result = prob.solve() items = [X.value[i,0] for i in range(5)] print 'Optimal full elicitation:', items deficit = items[0] + items[1] + items[2] - items[3] + INITIALDEFICITADDITIVE items.append(deficit) return items
def mixing_sp(y_fit,ref1,ref2): """mix two reference spectra to match the given ones Parameters ---------- y_fit : ndarray, shape m * n an array containing the signals with m datapoints and n experiments ref1 : ndarray, shape m an array containing the first reference signal ref2 : ndarray, shape m an array containing the second reference signal Returns ------- out : ndarray, shape n the fractions of ref1 in the mix Notes ----- Performs the calculation by minimizing the sum of the least absolute value of the objective function: obj = sum(abs(y_fit-(ref1*F1 + ref2*(1-F1)))) Uses cvxpy to perform this calculation """ try: import cvxpy except ImportError: print('ERROR: Install cvxpy>=1.0 to use this function.') ref1 = ref1.reshape(1,-1) ref2 = ref2.reshape(1,-1) F1 = cvxpy.Variable(shape=(y_fit.shape[1],1)) objective = cvxpy.Minimize(cvxpy.sum(cvxpy.abs(F1*ref1 + (1-F1)*ref2 - y_fit.T))) constraints = [0 <= F1, F1 <= 1] prob = cvxpy.Problem(objective, constraints) prob.solve() return np.asarray(F1.value).reshape(-1)
def to_cvxpy(self, weight_var, weight_var_series, init_weights): # 在原权重中分别找出多头与空头权重序列 long_w = init_weights[init_weights >= 0] short_w = init_weights[init_weights < 0] # 为使shape一致,需要使用fillna,以0值填充 common_index = long_w.index.union(short_w.index).unique() # 与其值无关 # long_w = long_w.reindex(common_index).fillna(0) # short_w = short_w.reindex(common_index).fillna(0) # 找到涉及到变量的位置 ix = get_ix(weight_var_series, common_index) # 如果没有相应位置序列,则返回空限制(即无限制) if len(ix) == 0: return [] # 限定其和的绝对值不超过容忍阀值 return [ cvx.abs(cvx.sum(weight_var[ix])) <= self.tolerance, ]
def lr_recover_l1(invecs, intensities, nonneg=True, **kwargs): """Computes the low-rank matrix reconstruction using l1-minimisation .. math:: \min_Z \sum_i \vert \langle a_i| Z | a_i \rangle - y_i \vert \\ \mathrm{s.t.} Z \ge 0 where :math:`a_i` are the input vectors and :math:`y_i` are the measured intensities. For the arguments not listed see :func:`recover` :param bool nonneg: Enfornce the constraint Z >= 0 (default True) :param kwargs: Additional arguemnts passed to `cvx.Problem.solve` :returns: array of shape (dim, dim); Low-rank matrix approximation for given measurements """ dim = invecs.shape[1] # we have to manually convert convex programm to real form since cvxpy # does not support complex programms z, mat_cons = _semidef_complex_as_real(dim) if nonneg else \ _hermitian_as_real(dim) invecs_real = np.concatenate((invecs.real, invecs.imag), axis=1) obj = cvx.Minimize( sum( cvx.abs(cvx.quad_form(a, z) - y) for a, y in zip(invecs_real, intensities))) prob = cvx.Problem(obj, mat_cons) prob.solve(**kwargs) if prob.status not in ['optimal', 'optimal_inaccurate']: raise RuntimeError("Optimization did not converge: " + prob.status) return z.value[:dim, :dim] + 1.j * z.value[dim:, :dim]
def objective(matrices_list): error_terms: Vector = [] is_data_real: bool = data_type in [np.float32, np.float64] for k1, k2, m in product(range(signal_length), repeat=3): other_index = (k2 - k1) % signal_length k1_plus_m = (k1 + m) % signal_length current_term = tri_spectrum[k1, k1_plus_m, (k2 + m) % signal_length] current_term -= matrices_list[other_index][k1, k1_plus_m] current_term -= matrices_list[m][k1, k2] if is_data_real: next_index: int = (k1 + k2 + m) % signal_length current_term -= matrices_list[next_index][ - k2 % signal_length, (-k2 - m) % signal_length] error_terms.append(current_term) if use_cp: fit_score = cp.sum([cp.abs(term) ** 2 for term in error_terms]) else: fit_score = np.sum([np.abs(term) ** 2 for term in error_terms]) return fit_score
def multiple_regress_l1(x, y, reg_param): """ Function to return the fitted coefficients and intercept for a matrix of covariates and a matrix of targets with complete l1 regularization Arguments: - x: matrix of size (n x d_x) - y: matrix of size (n x d_y) - reg_param: float """ n, d_x = x.shape n, d_y = y.shape W = cvx.Variable((d_y, d_x)) b = cvx.Variable((1, d_y)) obj = cvx.sum_squares(y - x @ W.T - np.ones((711, 1)) @ b) reg_obj = obj + reg_param * cvx.sum(cvx.abs(W)) problem = cvx.Problem(cvx.Minimize(reg_obj)) problem.solve() if problem.status == cvx.OPTIMAL: return (W.value, b.value) else: raise Exception("Solver not converged")
def get_constraints(self, X_v, U_v, X_last_p, U_last_p): """ Get model specific constraints. :param X_v: cvx variable for current states :param U_v: cvx variable for current inputs :param X_last_p: cvx parameter for last states :param U_last_p: cvx parameter for last inputs :return: A list of cvx constraints """ # Boundary conditions: constraints = [ X_v[:, 0] == self.x_init, X_v[:, -1] == self.x_final, U_v[:, 0] == 0, U_v[:, -1] == 0 ] # Input conditions: constraints += [ 0 <= U_v[0, :], U_v[0, :] <= self.v_max, cvx.abs(U_v[1, :]) <= self.w_max, ] # State conditions: constraints += [ X_v[0:2, :] <= self.upper_bound - self.robot_radius, X_v[0:2, :] >= self.lower_bound + self.robot_radius, ] # linearized obstacles for j, obst in enumerate(self.obstacles): p = obst[0] r = obst[1] + self.robot_radius lhs = [(X_last_p[0:2, k] - p) / (cvx.norm( (X_last_p[0:2, k] - p)) + 1e-6) * (X_v[0:2, k] - p) for k in range(K)] constraints += [r - cvx.vstack(lhs) <= self.s_prime[j]] return constraints
def build(self, structures, exact=False): """ Update :mod:`cvxpy` optimization based on structure data. Extract dose matrix, target doses, and objective weights from structures. Use doses and weights to add minimization terms to :attr:`SolverCVXPY.problem.objective`. Use dose constraints to extend :attr:`SolverCVXPY.problem.constraints`. (When constraints include slack variables, a penalty on each slack variable is added to the objective.) Arguments: structures: Iterable collection of :class:`Structure` objects. Returns: :obj:`str`: String documenting how data in ``structures`` were parsed to form an optimization problem. """ self.clear() if isinstance(structures, Anatomy): structures = structures.list A, dose, weight_abs, weight_lin = \ self._Solver__gather_matrix_and_coefficients(structures) self.problem.objective = cvxpy.Minimize( weight_abs.T * cvxpy.abs(A * self.__x - dose) + weight_lin.T * (A * self.__x - dose)) for s in structures: self.__add_constraints(s, exact=exact) return self._Solver__construction_report(structures)
def optimizeRelationships(relPred,relNodes,gtNodeNeighbors,penalty=490): #if 'cvxpy' not in sys.modules: import cvxpy useRel = cvxpy.Variable(relPred.size(0),boolean=True) obj =0 huh=0 for i in range(relPred.size(0)): obj += relPred[i].item()*useRel[i] huh +=useRel[i] constraint = [0]*len(gtNodeNeighbors) for i in range(len(gtNodeNeighbors)): relI=0 for a,b in relNodes: j=None if a==i: j=b elif b==i: j=a if j is not None: constraint[i] += useRel[relI] relI+=1 constraint[i] -= gtNodeNeighbors[i] #obj -= cvxpy.power(penalty,(cvxpy.abs(constraint[i]))) #this causes it to not miss on the same node more than once constraint[i] = cvxpy.abs(constraint[i]) obj -= penalty*constraint[i] cs=[] for i in range(len(gtNodeNeighbors)): cs.append(constraint[i]<=1) problem = cvxpy.Problem(cvxpy.Maximize(obj),cs) #problem.solve(solver=cvxpy.GLPK_MI) problem.solve(solver=cvxpy.ECOS_BB) assert(useRel.value is not None) return useRel.value
def test_quad_form(self) -> None: """Test gradient for quad_form. """ # Issue 1260 n = 10 np.random.seed(1) P = np.random.randn(n, n) P = P.T @ P q = np.random.randn(n) # define the optimization problem with the 2nd constraint as a quad_form constraint x = cp.Variable(n) prob = cp.Problem( cp.Maximize(q.T @ x - (1 / 2) * cp.quad_form(x, P)), [ cp.norm(x, 1) <= 1.0, cp.quad_form(x, P) <= 10, # quad form constraint cp.abs(x) <= 0.01 ]) prob.solve(solver=cp.SCS) # access quad_form.expr.grad without error prob.constraints[1].expr.grad
def run_opt(self): """ Run optimization for the worst case risk over possible covariance matrices """ sigma_opt = cp.Variable((self.n, self.n), PSD=True) # positive semi-definite delta = cp.Variable( (self.n, self.n), symmetric=True ) # difference between the input covar and the testing ones risk = cp.quad_form(self.w, sigma_opt) # elementwise delta constrained and must be zero on diagonals constraints_ls = [ sigma_opt == self.sigma + delta, cp.diag(delta) == 0, cp.abs(delta) <= 0.2 ] prob = cp.Problem(cp.Maximize(risk), constraints_ls) prob.solve() return { 'actual_std': cp.sqrt(cp.quad_form(self.w, self.sigma)).value, 'worst_case_std': cp.sqrt(risk).value, 'delta': delta.value }
def nucmin_estimator(A, y, eta=None, **kwargs): """@todo: Docstring for nucmin_estimator. :param A: @todo :param y: @todo :param **kwargs: @todo :returns: @todo """ x_sharp = cvx.Variable(A.shape[1], A.shape[2]) objective = cvx.Minimize(cvx.normNuc(x_sharp)) if eta is None: constraints = [_expval(A, x_sharp) == y] else: constraints = [cvx.abs(_expval(A, x_sharp) - y) < eta] problem = cvx.Problem(objective, constraints) problem.solve(**kwargs) if problem.status not in ['optimal']: raise ValueError("Optimization did not converge: " + problem.status) return np.array(x_sharp.value)
def _init_constraints(self, parameters, init_model_constraints): # Upper constraints from initial model l1_w = init_model_constraints["w_l1"] init_loss = init_model_constraints["loss"] C = parameters["C"] epsilon = parameters["epsilon"] # New Variables self.w = cvx.Variable(shape=(self.d), name="w") self.b = cvx.Variable(name="b") self.slack = cvx.Variable(shape=(self.n), nonneg=True, name="slack") # New Constraints distance_from_plane = cvx.abs(self.y - (self.X * self.w + self.b)) self.loss = cvx.sum(self.slack) self.weight_norm = cvx.norm(self.w, 1) self.add_constraint(distance_from_plane <= epsilon + self.slack) self.add_constraint(self.weight_norm <= l1_w) self.add_constraint(C * self.loss <= C * init_loss) self.feature_relevance = cvx.Variable(nonneg=True, name="Feature Relevance")
def __init__(self, problemType, di=None, kwargs=None, X=None, Y=None, initLoss=None, initL1=None, parameters=None, presetModel=None): super().__init__(problemType, di=di, kwargs=kwargs, X=X, Y=Y, initLoss=initLoss, initL1=initL1, parameters=parameters, presetModel=presetModel) self._constraints.extend([cvx.abs(self.omega[self.di]) <= self.xp]) self._objective = cvx.Minimize(self.xp)
def mixing_sp(y_fit, ref1, ref2): """mix two reference spectra to match the given ones Parameters ---------- y_fit : ndarray, shape m * n an array containing the signals with m datapoints and n experiments ref1 : ndarray, shape m an array containing the first reference signal ref2 : ndarray, shape m an array containing the second reference signal Returns ------- out : ndarray, shape n the fractions of ref1 in the mix Notes ----- Performs the calculation by minimizing the sum of the least absolute value of the objective function: obj = sum(abs(y_fit-(ref1*F1 + ref2*(1-F1)))) Uses cvxpy to perform this calculation """ F1 = cvxpy.Variable(y_fit.shape[1], 1) objective = cvxpy.Minimize( cvxpy.sum_entries( cvxpy.abs((F1 * ref1.reshape(1, -1) + (1 - F1) * ref2.reshape(1, -1)) - np.transpose(y_fit)))) constraints = [0 <= F1, F1 <= 1] prob = cvxpy.Problem(objective, constraints) prob.solve() return np.asarray(F1.value).reshape(-1)
def fit_MIP(self, x, y): n_points = x.shape[0] n_feat = x.shape[1] w = cp.Variable(shape=n_feat) # Additional variables representing the actual predictions. yhat = cp.Variable(shape=n_points, boolean=True) bigM = 1e3 constraints = [ (yhat-1) * bigM <= x @ w, yhat * bigM >= x @ w, ] fairness = 0 for key, Ip in self.I_train.items(): Np = np.sum(Ip) if Np >= 0: # tmp = 2 * (cp.sum(x @ w) / n_points - # cp.sum(cp.multiply(Ip, (x @ w))) / Np) tmp = 2 * (cp.sum(yhat) / n_points - cp.sum(cp.multiply(Ip, yhat)) / Np) fairness += cp.abs(tmp) loglike = -self.log_likelihood(x, y, w) / n_points fair_reg = self.alpha * fairness w_reg = self.gamma * cp.sum_squares(w) obj_fct = loglike + fair_reg + w_reg prob = cp.Problem(cp.Minimize(obj_fct), constraints) prob.solve() # prob.solve(verbose=False, solver=cp.SCS) # prob.solve(verbose=True, solver=cp.ECOS, feastol=1e-5, abstol=1e-5) print("Value of log likelihood: %.3f" % loglike.value) print("Value of fairness regularization: %.3f" % fairness.value) self.w = w.value
def minSet(test, gap): # A = eigentable m = test.shape[1] # https://www.cvxpy.org/examples/applications/sparse_solution.html#iterative-log-heuristic delta = 1e-8 # threshold for 0 NUM_RUNS = 30 nnzs_log = np.array(()) # (cardinality of p) for each run W = cp.Parameter(shape=m, nonneg=True) p = cp.Variable(shape=m, nonneg=True) W.value = np.ones(m) # Initial weights obj = cp.Minimize(W.T * cp.abs(p)) constraints = [cp.sum(p) == 1, test * p <= 1 - gap] prob = cp.Problem(obj, constraints) for k in range(1, NUM_RUNS + 1): # The ECOS solver has known numerical issues with this problem # so force a different solver. prob.solve(solver=cp.GLPK) # Check for error. if prob.status != cp.OPTIMAL: raise Exception("Solver did not converge!") # Display new number of nonzeros in the solution vector. nnz = (np.absolute(p.value) > delta).sum() nnzs_log = np.append(nnzs_log, nnz) # print('Iteration {}: Found a feasible p in R^{}' # ' with {} nonzeros...'.format(k, n, nnz)) # Adjust the weights elementwise and re-iterate W.value = np.ones(m) / (delta * np.ones(m) + np.absolute(p.value)) return (p.value, nnz)
def remove_dc_from_spad(noisy_spad, bin_edges, bin_weight, lam=1e-2, eps_rel=1e-5): """ Works in numpy. :param noisy_spad: Length C array with the raw spad histogram to denoise. :param bin_edges: Length C+1 array with the bin widths in meters of the original bins. :param bin_weight: Length C nonnegative array controlling relative strength of L1 regularization on each bin. :param lam: float value controlling strength of overall L1 regularization on the signal :param eps: float value controlling precision of solver """ 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, ), "noise") print("lam", lam) print("eps_rel", eps_rel) # print("spad_equalized", spad_equalized) orig_sum = np.sum(spad_equalized) spad_normalized = spad_equalized / orig_sum obj = cp.Minimize( cp.sum_squares(spad_normalized - (x + z)) + lam * cp.sum(bin_weight * cp.abs(x))) constr = [ x >= 0, # z >= 0 ] prob = cp.Problem(obj, constr) prob.solve(solver=cp.OSQP, eps_rel=eps_rel) print("z.value", z.value) denoised_spad = np.clip(x.value * bin_widths, a_min=0., a_max=None) return denoised_spad
def solve_cvx(p0, v0, pk, vk, K): '''Solve convex optimization problem for trajectory and thruster direction''' # create problem variables f_k = cp.Variable((K, 2)) v_k = cp.Variable((K + 1, 2)) p_k = cp.Variable((K + 1, 2)) # create problem constrains constraints = [ p_k[0, :] == p0, v_k[0, :] == v0, p_k[-1, :] == pk, v_k[-1, :] == vk ] e2 = np.zeros(2) e2[1] = 1.0 for i in range(K): constraints.append(v_k[i + 1, :] == v_k[i, :] + (h / m) * f_k[i, :] - h * g * e2) constraints.append(p_k[i + 1, :] == p_k[i, :] + (h / 2) * (v_k[i, :] + v_k[i + 1, :])) constraints.append(p_k[i, 1] >= alpha * cp.abs(p_k[i, 0])) constraints.append(cp.norm(f_k[i, :]) <= Fmax) # form objective # minimum fuel descent # obj = cp.Minimize(gamma * h * cp.sum(cp.norm(f_k, axis=1))) # minimum time descent obj = cp.Minimize(0) # form and solve the problem prob = cp.Problem(obj, constraints) prob.solve() # print result # print("status:", prob.status) # print("optimal value", prob.value) # print("") return p_k.T.value, f_k.T.value, prob
def _calculate_objective(self, mu_l, mu_r, tau, l_cs_value, r_cs_value, beta_value, weights, sum_components=True): weights_w1 = np.diag(weights) # Note: Not using cvx.sum and cvx.abs as in following caused # an error at * weights_w1: # ValueError: operands could not be broadcast together with shapes # (288,1300) (1300,1300) # term_f1 = sum((0.5 * abs( # self._power_signals_d - l_cs_value.dot(r_cs_value)) # + (tau - 0.5) # * (self._power_signals_d - l_cs_value.dot(r_cs_value))) # * weights_w1) term_f1 = (cvx.sum((0.5 * cvx.abs( self._power_signals_d - l_cs_value.dot(r_cs_value)) + (tau - 0.5) * (self._power_signals_d - l_cs_value.dot( r_cs_value))) * weights_w1)).value weights_w2 = np.eye(self._rank_k) term_f2 = mu_l * norm((l_cs_value[:-2, :] - 2 * l_cs_value[1:-1, :] + l_cs_value[2:, :]).dot(weights_w2), 'fro') term_f3 = mu_r * norm(r_cs_value[:, :-2] - 2 * r_cs_value[:, 1:-1] + r_cs_value[:, 2:], 'fro') if r_cs_value.shape[1] < 365 + 2: term_f4 = 0 else: # Note: it was cvx.norm. Check if this modification makes a # difference: # term_f4 = (mu_r * norm( # r_cs_value[1:, :-365] - r_cs_value[1:, 365:], 'fro')) term_f4 = ((mu_r * cvx.norm( r_cs_value[1:, :-365] - r_cs_value[1:, 365:], 'fro'))).value components = [term_f1, term_f2, term_f3, term_f4] objective = sum(components) if sum_components: return objective else: return components
def calc_objective(self, sum_components=True): W1 = np.diag(self.weights) f1 = (cvx.sum( (0.5 * cvx.abs(self.D - self.L_cs.value.dot(self.R_cs.value)) + (self.tau - 0.5) * (self.D - self.L_cs.value.dot(self.R_cs.value))) * W1)).value W2 = np.eye(self.k) f2 = self.mu_L * norm( ((self.L_cs[:-2, :]).value - 2 * (self.L_cs[1:-1, :]).value + (self.L_cs[2:, :]).value).dot(W2), 'fro') f3 = self.mu_R * norm( (self.R_cs[:, :-2]).value - 2 * (self.R_cs[:, 1:-1]).value + (self.R_cs[:, 2:]).value, 'fro') if self.R_cs.shape[1] < 365 + 2: f4 = 0 else: f4 = (self.mu_R * cvx.norm( self.R_cs[1:, :-365] - self.R_cs[1:, 365:], 'fro')).value components = [f1, f2, f3, f4] objective = sum(components) if sum_components: return objective else: return components
def relax(self): return [cvx.abs(self) <= self.M]
def objective_state(self): return sum( cvxpy.abs(link.state.flow - link.v_flow) + cvxpy.abs(link.state.density - link.v_dens) for link in self.get_links() )
def opt_actor(l1, l2, l3, max_a, K, A=None, B=None, strict=True): W1 = l1.weight.data.clone().cpu().numpy() W2 = l2.weight.data.clone().cpu().numpy() W3 = l3.weight.data.clone().cpu().numpy() b1 = l1.bias.data.clone().unsqueeze(-1).cpu().numpy() b2 = l2.bias.data.clone().unsqueeze(-1).cpu().numpy() b3 = l3.bias.data.clone().unsqueeze(-1).cpu().numpy() W1p = W1.copy() b1p = b1.copy() idx1 = (b1p < 0).nonzero() idx1 = (idx1[0], np.vstack((idx1[1], idx1[1] + 1))) b1p[b1p < 0] = 0 W1p[idx1] = 0 W2W1p = W2 @ W1p W2b1pb2 = W2 @ b1p + b2 W2W1p_p = W2W1p.copy() W2b1pb2_p = W2b1pb2.copy() idx2 = (W2b1pb2_p < 0).nonzero() idx2 = (idx2[0], np.vstack((idx2[1] + i for i in range(W2W1p_p.shape[1])))) W2b1pb2_p[W2b1pb2_p < 0] = 0 W2W1p_p[idx2] = 0 K = torch.Tensor(K).numpy() Wopt = cp.Variable(W3.shape) bopt = cp.Variable(b3.shape) iterations = 0 if strict: cost = 0 constraints = [] constraints.append(max_a * (Wopt @ W2W1p_p) + K == 0) constraints.append(Wopt @ (W2b1pb2_p) + bopt == 0) cost += cp.norm(Wopt - W3) cost += 0.5 * cp.norm(bopt - b3) obj = cp.Minimize(cost) prob = cp.Problem(obj, constraints) prob.solve() else: eps = 0.0001 nu_k = 1 done = False while not done: iterations += 1 cost = 0 constraints = [] constraints.append(cp.abs(Wopt @ (W2b1pb2_p) + bopt) <= eps) cost += cp.norm(Wopt - W3) cost += nu_k * cp.norm(max_a * (Wopt @ W2W1p_p) + K) obj = cp.Minimize(cost) prob = cp.Problem(obj, constraints) prob.solve() done = np.all( np.linalg.eig(A + B @ (max_a * Wopt.value @ W2W1p_p + Wopt.value @ (W2b1pb2_p) + bopt.value)) [0] < 0) nu_k *= nu_k * 1.1 # print('Optimization complete for Actor.') # print(f'CVX Problem Status: {prob.status}') # print(f'CVX Objective Value: {prob.value}') # print(f'--------------------------------------------') # print(f"Last layer diff: {np.linalg.norm(Wopt.value - W3)}") # print(f"LQR Fit: {np.linalg.norm(max_a*Wopt.value@W2W1p_p + K) + np.linalg.norm(Wopt.value @ (W2b1pb2_p) + bopt.value)}") # print(f"Max Closed Loop Eval: {np.max(np.linalg.eig(A + B @ (max_a*Wopt.value @ W2W1p_p + Wopt.value @ (W2b1pb2_p) + bopt.value))[0])}") # print(f'--------------------------------------------') l3.weight.data = torch.from_numpy(Wopt.value).to(device).float() l3.bias.data = torch.from_numpy(bopt.value).squeeze(-1).to(device).float() return None
def solve(playery, playerVelY, lowerPipes, prev_flaps, prev_path, est_y=0, std_y=100): pipeVelX = -4 # speed in x playerAccY = 1 # players downward accleration playerFlapAcc = -14 # players speed on flapping # unpack path variables y = path[:, 0] vy = path[:, 1] c = [] # init constraint list c += [y <= GROUND, y >= SKY] # constraints for sky and ground c += [y[0] == playery, vy[0] == playerVelY] # initial conditions obj = 0 x = PLAYERX xs = [x] # init x list for t in range(N - 1): # look ahead dt = t // 15 + 1 # let time get coarser further in the look ahead x -= dt * pipeVelX # update x xs += [x] # add to list c += [vy[t + 1] == vy[t] + playerAccY * dt + playerFlapAcc * flap[t] ] # add y velocity constraint, f=ma c += [y[t + 1] == y[t] + vy[t + 1] * dt] # add y constraint, dy/dt = a pipe_c, dist = getPipeConstraintsDistance( x, y[t + 1], lowerPipes) # add pipe constraints c += pipe_c obj += dist # New code for terminal constraint # Pipe Region: if lowerPipes[-1]['x'] < 181 and 181 < lowerPipes[-1]['x'] + 52: c += [ y[t + 1] >= (lowerPipes[-1]['y'] - 100) + cvx.square(vy[t + 1]) / 2 ] else: pass # Btwn-Pipe Region if lowerPipes[-1]['x'] > 181: n = abs((lowerPipes[-1]['x'] - 181) / 4) c += [ y[t + 1] >= (lowerPipes[-1]['y'] - 100) - n * (n - 1) / 2 - vy[t + 1] * n ] pass # Add c1 and check if terminal set within gpr stddev is feasible c1 = c + [y[t + 1] >= est_y - std_y] + [y[t + 1] <= est_y + std_y] objective = cvx.Minimize(cvx.sum(cvx.abs(vy)) + 100 * obj) if std_y < 50: prob = cvx.Problem(objective, c1) # if low std, use the strategy else: prob = cvx.Problem( objective, c) # if high standard deviation, do not solve with the strategy try: # try solving problem with c1, otherwise try using c prob.solve(verbose=False, solver="GUROBI") new_path = list(zip(xs, y.value)) # store the path new_flaps = np.round(flap.value).astype(bool) # store the solution return new_flaps, new_path # return the one-step flap, and the entire path except: try: prob = cvx.Problem(objective, c) prob.solve(verbose=False, solver="GUROBI") new_path = list(zip(xs, y.value)) # store the path new_flaps = np.round(flap.value).astype(bool) # store the solution return new_flaps, new_path # return the one-step flap, and the entire path except: new_flaps = prev_flaps[ 1:] # if we didn't get a solution this round, use the inputs and flaps from the last solve iter new_path = [((x - 4), y) for (x, y) in prev_path[1:]] if len(prev_flaps) < 12: #print('returning false') return [False], [(0, 0), (0, 0)] else: #print('is this what is causing the error') return new_flaps, new_path
for j in range(i + 1, n): constr.append( cvx.norm(cvx.vec(c[i, :] - c[j, :]), 2) >= r[i] + r[j]) # prob = Problem( # Minimize( # cvx.max( # cvx.max(cvx.abs(c), axis=1) + r # max dim = c_max + r # ) # ), # constr # ) prob = Problem( Minimize( cvx.max(cvx.abs(c[:, 0]) + r) + cvx.max(cvx.abs(c[:, 1]) + r)), constr) print(prob.is_dcp(), prob.is_dcp()) print(dccp.is_dccp(prob)) prob.solve(method='dccp', solver='ECOS', ep=1e-2, max_slack=1e-2, verbose=True) l = cvx.max(cvx.max(cvx.abs(c), axis=1) + r).value * 2 pi = np.pi ratio = pi * cvx.sum(cvx.square(r)).value / cvx.square(l).value print("ratio =", ratio)
def reachable(T, s0, sT, cost, dynamics, max_dtheta, max_theta, max_ddx, max_line_search=30, show_results=lambda *a:None): """Find control signals u_1...u_T, u_t=(ddx_t,dtheta_t) and the ensuing states s_1...s_T that minimize L(s_0,...,s_{T-1}) subject to s_t = f(s_{t-1}, u_t) | dtheta_t | < max_dtheta | ddx_t | < max_ddx One of sT or cost must be None. if sT is set, then L = || s_{t-1} - sT || otherwise, it is L = sum_{t=0}^{T-1} cost(s_t) Solves this as a Sequential Quadratic program by approximating L by a quadratic and f by an affine function. """ s0 = array(s0) if sT is not None: sT = array(sT) assert (cost is None) != (sT is None), 'only one of cost or sT may be specified' # initial iterates and objective terms Sv = [s0] * T Uv = [None] + [zeros(2)]*T L = None if cost: L = [cost(s0)['val']] * T last_obj = None # last objective value attained step = 1. # last line search step size iters = 0 n_line_searches = 0 while True: show_results(Sv, L, '%d, %d line searches so far. step size %g'%(iters, n_line_searches, step)) iters += 1 # variables, objective, and constraints of the quadratic problem S = [None] * T U = [None] * T S[0] = CX.Parameter(5, name='s0') S[0].value = s0 constraints = [] if cost: objective = zeros(1) # define the QP for t in xrange(1,T): # f(u_t, s_{t-1}) and its derivatives f = dynamics(Sv[t-1], Uv[t], {'du','ds'}) dfds = vstack(f['ds']) dfdu = vstack(f['du']) # define u_t and s_t U[t] = CX.Variable(2, name='u%d'%t) S[t] = CX.Variable(5, name='s%d'%t) # constraints: # s_t = linearized f(s_t-1, u_t) about previous iterate # and bounds on s_t and u_t constraints += [ S[t] == f['val'] + dfds*(S[t-1]-Sv[t-1]) + dfdu*(U[t]-Uv[t]), CX.abs(U[t][0]) <= max_ddx, CX.abs(U[t][1]) <= max_dtheta, CX.abs(S[t][4]) <= max_theta ] if cost: # accumulate objective c = cost(Sv[t], derivs={'ds','ds2'}) c['ds2'] = make_psd(c['ds2']) objective += c['val'] + (S[t]-Sv[t]).T*c['ds'] + 0.5*CX.quad_form(S[t]-Sv[t], c['ds2']) if sT is not None: # objective is || s_t - sT || objective = CX.square(CX.norm(S[T-1] - sT)) # solve for S and U p = CX.Problem(CX.Minimize(objective), constraints) r = p.solve(solver=CX.CVXOPT, verbose=False) assert isfinite(r) # line search on U, from Uv along U-Uv line_search_failed = True while n_line_searches < max_line_search: n_line_searches += 1 # compute and apply the controls along the step Us = [] Svs = [s0] for u,u0 in zip(U[1:],Uv[1:]): # a step along the search direction us = u0 + step * (ravel(u.value)-u0) # make it feasible us[0] = clip(us[0], -max_ddx, max_ddx) us[1] = clip(us[1], -max_dtheta, max_dtheta) Us.append(us) # apply controls Svs.append( sim.apply_control(Svs[-1], us)['val'] ) # objective value based on the last state if cost: L = [ cost(s)['val'] for s in Svs ] obj = sum(L) else: obj = sum((Svs[-1]-sT)**2) if last_obj is None or obj < last_obj: step *= 1.1 # lengthen the step for the next round line_search_failed = False # converged break else: step *= 0.7 # shorten the step and try again if line_search_failed: # converged break # throw away this iterate else: # accept the iterate Sv = Svs Uv = [None] + Us last_obj = obj return Sv,Uv
def _control(self, reference_state, predicted_state, reference_steer): """ Solve the MPC control problem. :param reference_state: np.array of reference states :param predicted_state: np.array of predicted states obtained using propogated controls :param reference_steer: np.array of reference steering :return: """ # intialize problem x = cvxpy.Variable((self.num_state, self.config['horizon'] + 1)) u = cvxpy.Variable((self.num_input, self.config['horizon'])) cost = constants.Constant(0.0) constraints = [] # iterate over the horizon for t in range(self.config['horizon']): cost += cvxpy.quad_form(u[:, t], self.config['R']) if t != 0: cost += cvxpy.quad_form(reference_state[:, t] - x[:, t], self.config['Q']) matrix_a, matrix_b, matrix_c = self._linearized_model_matrix( predicted_state[2, t], predicted_state[3, t], reference_steer[0, t]) constraints += [ x[:, t + 1] == matrix_a * x[:, t] + matrix_b * u[:, t] + matrix_c ] if t < (self.config['horizon'] - 1): cost += cvxpy.quad_form(u[:, t + 1] - u[:, t], self.config['Rd']) constraints += [ cvxpy.abs(u[1, t + 1] - u[1, t]) <= self.vehicle.config['max_steer_speed'] * self.delta_t ] # set the cost cost += cvxpy.quad_form( reference_state[:, self.config['horizon']] - x[:, self.config['horizon']], self.config['Qf']) # set the constraints constraints += [x[:, 0] == self.vehicle.get_state()] constraints += [x[2, :] <= self.vehicle.config['max_vel']] constraints += [x[2, :] >= self.vehicle.config['min_vel']] constraints += [u[0, :] <= self.vehicle.config['max_accel']] constraints += [u[0, :] >= self.vehicle.config['min_accel']] constraints += [u[1, :] <= self.vehicle.config['max_steer']] constraints += [u[1, :] >= self.vehicle.config['min_steer']] # solve the problem prob = cvxpy.Problem(cvxpy.Minimize(cost), constraints) prob.solve(solver=cvxpy.OSQP, verbose=False, warm_start=True) # keep track of optimality solved = False if prob.status == cvxpy.OPTIMAL or \ prob.status == cvxpy.OPTIMAL_INACCURATE: solved = True # return solution horizon_x = np.array(x.value[0, :]).flatten() horizon_y = np.array(x.value[1, :]).flatten() horizon_vel = np.array(x.value[2, :]).flatten() horizon_yaw = np.array(x.value[3, :]).flatten() horizon_accel = np.array(u.value[0, :]).flatten() horizon_steer = np.array(u.value[1, :]).flatten() return horizon_x, horizon_y, horizon_vel, horizon_yaw, horizon_accel, horizon_steer, solved
def solve_group_SDP( Sigma, groups=None, verbose=False, objective="abs", norm_type=2, num_iter=10, tol=1e-2, **kwargs, ): """ Solves the group SDP problem: extends the formulation from Barber and Candes 2015/ Candes et al 2018 (MX Knockoffs). Note this will be much faster with equal-sized groups and objective="abs." :param Sigma: true covariance (correlation) matrix, p by p numpy array. :param groups: numpy array of length p with integer values between 1 and m. :param verbose: if True, print progress of solver :param objective: How to optimize the S matrix for group knockoffs. (For ungrouped knockoffs, using the objective = 'abs' is strongly recommended.) There are several options: - 'abs': minimize sum(abs(Sigma - S)) between groups and the group knockoffs. - 'pnorm': minimize Lp-th matrix norm. Equivalent to abs when p = 1. - 'norm': minimize different type of matrix norm (see norm_type below). :param norm_type: Means different things depending on objective. - When objective == 'pnorm', i.e. objective is Lp-th matrix norm, which p to use. Can be any float >= 1. - When objective == 'norm', can be 'fro', 'nuc', np.inf, or 1 (i.e. which other norm to use). Defaults to 2. :param num_iter: We do a line search and scale S at the end to make absolutely sure there are no numerical errors. Defaults to 10. :param tol: Minimum eigenvalue of S must be greater than this. """ # By default we lower the convergence epsilon a bit for drastic speedup. if "eps" not in kwargs: kwargs["eps"] = 5e-3 # Default groups p = Sigma.shape[0] if groups is None: groups = np.arange(1, p + 1, 1) # Test corr matrix TestIfCorrMatrix(Sigma) # Check to make sure the objective is valid objective = str(objective).lower() if objective not in OBJECTIVE_OPTIONS: raise ValueError( f"Objective ({objective}) must be one of {OBJECTIVE_OPTIONS}") # Warn user if they're using a weird norm... if objective == "norm" and norm_type == 2: warnings.warn( "Using norm objective and norm_type = 2 can lead to strange behavior: consider using Frobenius norm" ) # Find minimum tolerance, possibly warn user if lower than they specified maxtol = np.linalg.eigh(Sigma)[0].min() / 1.1 if tol > maxtol and verbose: warnings.warn( f"Reducing SDP tol from {tol} to {maxtol}, otherwise SDP would be infeasible" ) tol = min(maxtol, tol) # Figure out sizes of groups m = groups.max() group_sizes = utilities.calc_group_sizes(groups) # Possibly solve non-grouped SDP if m == p: return solve_SDP( Sigma=Sigma, verbose=verbose, num_iter=num_iter, tol=tol, ) # Sort the covariance matrix according to the groups inds, inv_inds = utilities.permute_matrix_by_groups(groups) sortedSigma = Sigma[inds][:, inds] # Create blocks of semidefinite matrix S, # as well as the whole matrix S variables = [] constraints = [] S_rows = [] shift = 0 for j in range(m): # Create block variable gj = int(group_sizes[j]) Sj = cp.Variable((gj, gj), symmetric=True) constraints += [Sj >> 0] variables.append(Sj) # Create row of S if shift == 0 and shift + gj < p: rowj = cp.hstack([Sj, cp.Constant(np.zeros((gj, p - gj)))]) elif shift + gj < p: rowj = cp.hstack([ cp.Constant(np.zeros((gj, shift))), Sj, cp.Constant(np.zeros((gj, p - gj - shift))), ]) elif shift + gj == p and shift > 0: rowj = cp.hstack([cp.Constant(np.zeros((gj, shift))), Sj]) elif gj == p and shift == 0: rowj = cp.hstack([Sj]) else: raise ValueError( f"shift ({shift}) and gj ({gj}) add up to more than p ({p})") S_rows.append(rowj) # Incremenet shift shift += gj # Construct S and Grahm Matrix S = cp.vstack(S_rows) sortedSigma = cp.Constant(sortedSigma) constraints += [2 * sortedSigma - S >> 0] # Construct optimization objective if objective == "abs": objective = cp.Minimize(cp.sum(cp.abs(sortedSigma - S))) elif objective == "pnorm": objective = cp.Minimize(cp.pnorm(sortedSigma - S, norm_type)) elif objective == "norm": objective = cp.Minimize(cp.norm(sortedSigma - S, norm_type)) # Note we already checked objective is one of these values earlier # Construct, solve the problem. problem = cp.Problem(objective, constraints) problem.solve(verbose=verbose, **kwargs) if verbose: print("Finished solving SDP!") # Unsort and get numpy S = S.value if S is None: raise ValueError( "SDP formulation is infeasible. Try decreasing the tol parameter.") S = S[inv_inds][:, inv_inds] # Clip 0 and 1 values for i in range(p): S[i, i] = max(tol, min(1 - tol, S[i, i])) # Scale to make this PSD using binary search S, gamma = scale_until_PSD(Sigma, S, tol, num_iter) if verbose: mineig = np.linalg.eigh(2 * Sigma - S)[0].min() print( f"After SDP, mineig is {mineig} after {num_iter} line search iters. Gamma is {gamma}" ) # Return unsorted S value return S
def area_direct_blockwise_int(tar_image: np.ndarray, src_img: np.ndarray, ksizex: int, ksizey: int, verbose: bool, eps: int, attack_norm: AreaNormEnumType): """ L1/L2 attack variant against Area scaling. Divides the image into several larger blocks where optimization problem is solved to get attack image. Is faster than 'area_direct' (which solves each block)... """ src_img_new = np.zeros(src_img.shape) use_l2: bool = (attack_norm == AreaNormEnumType.L2) # otherwise, use L1 # we need to divide the target image into blocks def get_step(tar_image_sh): for divi in range(20, 2, -1): if tar_image_sh % divi == 0: return int(tar_image_sh / divi) stepx = get_step(tar_image_sh=tar_image.shape[0]) stepy = get_step(tar_image_sh=tar_image.shape[1]) for r in range(0, tar_image.shape[0], stepx): for c in range(0, tar_image.shape[1], stepy): if verbose is True and c == 0: print("Iteration: {}, {}".format(r, c)) target_value = tar_image[r:(r + stepx), c:(c + stepy)] # define optimization problem novelpixels = cp.Variable((ksizex * stepx, ksizey * stepy)) # get region in source image startx = r * ksizex endx = ((r + stepx) * ksizex) starty = c * ksizey endy = ((c + stepy) * ksizey) # objective function obj_vec = novelpixels - src_img[startx:endx, starty:endy] # .reshape(-1) if use_l2: obj = (1 / 2) * cp.sum_squares(obj_vec) else: obj = cp.sum(cp.abs(obj_vec)) # constraint 1: constrs = [] for rb, rtarind in zip(range(0, endx - startx, ksizex), range(stepx)): for cb, ctarind in zip(range(0, endy - starty, ksizey), range(stepy)): # we use zip to obtain the index in src-img (rb, cd) first and # index in target-image (rtarind, ctarind) as 2nd object. # print(rb, cb, rtarind, ctarind) temp_constr = cp.abs(cp.sum(novelpixels[rb:(rb + ksizex), cb:(cb + ksizey)]) - \ (target_value[rtarind, ctarind] * ksizex * ksizey)) <= eps constrs.append(temp_constr) constr2 = novelpixels <= 255 constr3 = novelpixels >= 0 prob = cp.Problem(cp.Minimize(obj), [*constrs, constr2, constr3]) # solve it, we first try default solver, and then if not possible (rarely the case), we try another. # check the docu: https://www.cvxpy.org/tutorial/intro/index.html#infeasible-and-unbounded-problems try: prob.solve() except: if verbose is True: print("QSQP failed at {}, {}".format(r, c)) try: prob.solve(solver=cp.ECOS) except: print( "Could not solve with QSPS and ECOS at {}, {}".format( r, c)) raise Exception("Could not solve at {}, {}".format(r, c)) if prob.status != cp.OPTIMAL and prob.status != cp.OPTIMAL_INACCURATE: print("Could only solve at {}, {} with status: {}".format( r, c, prob.status)) raise Exception( "Only solveable with infeasible/unbounded/optimal_inaccurate solution" ) assert prob is not None and novelpixels.value is not None # actually not needed, just to ensure.. # print(np.round(novelpixels.value.reshape((ksizex, ksizey)))) src_img_new[startx:endx, starty:endy] = np.round(novelpixels.value) return src_img_new
def area_direct_int(tar_image: np.ndarray, src_img: np.ndarray, ksizex: int, ksizey: int, verbose: bool, eps: int, attack_norm: AreaNormEnumType): src_img_new = np.zeros(src_img.shape) use_l2: bool = (attack_norm == AreaNormEnumType.L2) # otherwise, use L1 for r in range(tar_image.shape[0]): for c in range(tar_image.shape[1]): if verbose is True and r % 20 == 0 and c == 0: print("Iteration: {}, {}".format(r, c)) target_value = tar_image[r, c] # define optimization problem novelpixels = cp.Variable(ksizex * ksizey) # ident = np.identity(ksizex*ksizey) startx = r * ksizex endx = ((r + 1) * ksizex) starty = c * ksizey endy = ((c + 1) * ksizey) obj_vec = novelpixels - src_img[startx:endx, starty:endy].reshape(-1) if use_l2: obj = (1 / 2) * cp.quad_form(obj_vec, np.identity(ksizex * ksizey)) else: obj = cp.sum(cp.abs(obj_vec)) # constr1 = cp.sum(novelpixels) == (target_value * ksizex*ksizey) constr1 = cp.abs( cp.sum(novelpixels) - (target_value * ksizex * ksizey)) <= eps constr2 = novelpixels <= 255 constr3 = novelpixels >= 0 prob = cp.Problem(cp.Minimize(obj), [constr1, constr2, constr3]) try: prob.solve() except: if verbose is True: print("QSQP failed at {}, {}".format(r, c)) try: prob.solve(solver=cp.ECOS) except: print( "Could not solve with QSPS and ECOS at {}, {}".format( r, c)) raise Exception("Could not solve at {}, {}".format(r, c)) if prob.status != cp.OPTIMAL and prob.status != cp.OPTIMAL_INACCURATE: print("Could only solve at {}, {} with status: {}".format( r, c, prob.status)) raise Exception( "Only solveable with infeasible/unbounded/optimal_inaccurate solution" ) assert prob is not None and novelpixels.value is not None # actually not needed, just to ensure.. # print(np.round(novelpixels.value.reshape((ksizex, ksizey)))) src_img_new[startx:endx, starty:endy] = np.round( novelpixels.value.reshape((ksizex, ksizey))) return src_img_new
n_srt) + length_setion point_y_e[i] = length - length_setion - length_setion * (i // n_srt) radius_p = 2 * length / np.sqrt(n * np.pi) r = [radius_p for i in range(n)] radius = 1.5 c = cvx.Variable((n, 2)) constr = [] for i in range(n - 1): for j in range(i + 1, n): constr.append( cvx.norm(cvx.vec(c[i, :] - c[j, :]), 2) >= r[i] + r[j]) prob = cvx.Problem(cvx.Minimize(cvx.max(cvx.max(cvx.abs(c), axis=1) + r)), constr) prob.solve(method='dccp', solver='ECOS', ep=1e-2, max_slack=1e-2) point_e = np.column_stack((point_x_e, point_y_e)) point_t = np.column_stack((point_x_t, point_y_t)) point_c = np.column_stack((point_x_c, point_y_c)) z_t = np.zeros((n, 1)) z_e = np.zeros((n, 1)) z_c = np.zeros((n, 1)) firing_position_x_t = np.zeros((n, nsteps)) firing_position_y_t = np.zeros((n, nsteps)) firing_position_x_e = np.zeros((n, nsteps)) firing_position_y_e = np.zeros((n, nsteps))
def total_variation_plus_seasonal_quantile_filter(signal, use_ixs=None, tau=0.995, c1=1e3, c2=1e2, c3=1e2, solver='ECOS', residual_weights=None, tv_weights=None): ''' This performs total variation filtering with the addition of a seasonal baseline fit. This introduces a new signal to the model that is smooth and periodic on a yearly time frame. This does a better job of describing real, multi-year solar PV power data sets, and therefore does an improved job of estimating the discretely changing signal. :param signal: A 1d numpy array (must support boolean indexing) containing the signal of interest :param c1: The regularization parameter to control the total variation in the final output signal :param c2: The regularization parameter to control the smoothness of the seasonal signal :return: A 1d numpy array containing the filtered signal ''' n = len(signal) if residual_weights is None: residual_weights = np.ones_like(signal) if tv_weights is None: tv_weights = np.ones(len(signal) - 1) if use_ixs is None: use_ixs = np.ones(n, dtype=np.bool) # selected_days = np.arange(n)[index_set] # np.random.shuffle(selected_days) # ix = 2 * n // 3 # train = selected_days[:ix] # validate = selected_days[ix:] # train.sort() # validate.sort() s_hat = cvx.Variable(n) s_seas = cvx.Variable(max(n, 366)) s_error = cvx.Variable(n) s_linear = cvx.Variable(n) c1 = cvx.Parameter(value=c1, nonneg=True) c2 = cvx.Parameter(value=c2, nonneg=True) c3 = cvx.Parameter(value=c3, nonneg=True) tau = cvx.Parameter(value=tau) # w = len(signal) / np.sum(index_set) beta = cvx.Variable() objective = cvx.Minimize( # (365 * 3 / len(signal)) * w * cvx.sum(0.5 * cvx.abs(s_error) + (tau - 0.5) * s_error) 2 * cvx.sum(0.5 * cvx.abs(cvx.multiply(residual_weights, s_error)) + (tau - 0.5) * cvx.multiply(residual_weights, s_error)) + c1 * cvx.norm1(cvx.multiply(tv_weights, cvx.diff(s_hat, k=1))) + c2 * cvx.norm(cvx.diff(s_seas, k=2)) + c3 * beta**2) constraints = [ signal[use_ixs] == s_hat[use_ixs] + s_seas[:n][use_ixs] + s_error[use_ixs], cvx.sum(s_seas[:365]) == 0 ] if True: constraints.append(s_seas[365:] - s_seas[:-365] == beta) constraints.extend([beta <= 0.01, beta >= -0.1]) problem = cvx.Problem(objective=objective, constraints=constraints) problem.solve(solver='MOSEK') return s_hat.value, s_seas.value[:n]
def linear_mpc_control(xref, xbar, x0, dref): """ linear mpc control xref: reference point xbar: operational point x0: initial state dref: reference steer angle """ x = cvxpy.Variable((NX, T + 1)) u = cvxpy.Variable((NU, T)) x_obs = cvxpy.Variable((NX, T + 1)) cost = 0.0 constraints = [] x_g = np.array([[75, 75], [65, 65], [1, 1], [0.5, 0.5]]) #x_g = np.stack(x,y) print(x_g) print(type(xref)) for t in range(T): cost += cvxpy.quad_form(u[:, t], R) if t != 0: cost += cvxpy.quad_form(x_g[:, t] - x[:, t], Q) A, B, C = get_linear_model_matrix(xbar[2, t], xbar[3, t], dref[0, t]) constraints += [x[:, t + 1] == A * x[:, t] + B * u[:, t] + C] if t < (T - 1): cost += cvxpy.quad_form(u[:, t + 1] - u[:, t], Rd) constraints += [ cvxpy.abs(u[1, t + 1] - u[1, t]) <= MAX_DSTEER * DT ] cost += cvxpy.quad_form(x_g[:, T] - x[:, T], Qf) constraints += [x[:, 0] == x0] constraints += [x[2, :] <= MAX_SPEED] constraints += [x[2, :] >= MIN_SPEED] constraints += [cvxpy.abs(u[0, :]) <= MAX_ACCEL] constraints += [cvxpy.abs(u[1, :]) <= MAX_STEER] prob = cvxpy.Problem(cvxpy.Minimize(cost), constraints) prob.solve(solver=cvxpy.ECOS, verbose=False) print(xref) if prob.status == cvxpy.OPTIMAL or prob.status == cvxpy.OPTIMAL_INACCURATE: ox = get_nparray_from_matrix(x.value[0, :]) oy = get_nparray_from_matrix(x.value[1, :]) ov = get_nparray_from_matrix(x.value[2, :]) oyaw = get_nparray_from_matrix(x.value[3, :]) oa = get_nparray_from_matrix(u.value[0, :]) odelta = get_nparray_from_matrix(u.value[1, :]) else: print("Error: Cannot solve mpc..") oa, odelta, ox, oy, oyaw, ov = None, None, None, None, None, None return oa, odelta, ox, oy, oyaw, ov
import cvxpy as cp k = 2000 t = [-3.0+6.0*(i)/(k-1) for i in range(k) ] #range start from zero y = np.exp(t) T_powers = np.matrix(np.hstack((np.ones((k,1)),np.matrix(t).T,np.power(np.matrix(t).T,2)))) u = np.exp(3) l = 0 bisection_tol = 1e-3 gamma1 = cp.Parameter(sign='positive') a = cp.Variable(3) b = cp.Variable(2) objective = 0 #constraints = [cp.abs(T_powers[i]*a-y[i]*(T_powers[i]*cp.vstack(1,b)))<=gamma1*(T_powers[i]*cp.vstack(1,b)) for i in range(100)] #constraints = [T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b))<=gamma1*(T_powers*cp.vstack(1,b)), # T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b))>=-gamma1*(T_powers*cp.vstack(1,b))] constraints = [cp.abs(T_powers*a-np.diag(y)*(T_powers*cp.vstack(1,b)))<=gamma1*(T_powers*cp.vstack(1,b))] objective = cp.Minimize(np.ones((1,3))*a) p = cp.Problem(objective,constraints) gamma1.value = (l+u)/2.0 a_opt = 0 b_opt = 0 gamma1.value = (u+l)/2 while (u-l)>=bisection_tol: print p.is_dcp() # p.solve(solver=cp.CVXOPT) p.solve() # p.solve(verbose=True) if p.status is 'optimal': u = gamma1.value a_opt = a.value b_opt = b.value
def compute_compensation(self): """ Compute CoP and normalized leg stiffness compensation. """ Delta_com = self.pendulum.com.p - self.ref_com Delta_comd = self.pendulum.com.pd - self.ref_comd measured_comd = self.pendulum.com.pd lambda_d = self.ref_lambda nu_d = self.ref_vrp omega_d = self.ref_omega r_d_contact = self.ref_cop_contact xi_d = self.ref_dcm height = dot(self.contact.normal, self.pendulum.com.p - self.contact.p) lambda_max = max_force / (mass * height) lambda_min = min_force / (mass * height) omega_max = sqrt(lambda_max) omega_min = sqrt(lambda_min) Delta_lambda = cvxpy.Variable(1) Delta_nu = cvxpy.Variable(3) Delta_omega = cvxpy.Variable(1) Delta_r = cvxpy.Variable(2) u = cvxpy.Variable(3) Delta_xi = Delta_com + Delta_comd / omega_d \ - measured_comd / (omega_d ** 2) * Delta_omega Delta_omegad = 2 * omega_d * Delta_omega - Delta_lambda Delta_r_world = contact.R[:3, :2] * Delta_r r_contact = r_d_contact + Delta_r lambda_ = lambda_d + Delta_lambda omega = omega_d + Delta_omega Delta_xid = ( Delta_lambda * (xi_d - nu_d) + lambda_d * (Delta_xi - Delta_nu) + - Delta_omega * lambda_d * (xi_d - nu_d) / omega_d) / omega_d xi_z = self.ref_dcm[2] + Delta_xi[2] + 1.5 * sim.dt * Delta_xid[2] costs = [] sq_costs = [ (1., u[0]), (1., u[1]), (1e-3, u[2])] for weight, expr in sq_costs: costs.append((weight, cvxpy.sum_squares(expr))) cost = sum(weight * expr for (weight, expr) in costs) prob = cvxpy.Problem( objective=cvxpy.Minimize(cost), constraints=[ Delta_xid == lambda_d / omega_d * ((1 - k_p) * Delta_xi + u), Delta_omegad == omega_d * (1 - k_p) * Delta_omega, Delta_nu == Delta_r_world + gravity * Delta_lambda / lambda_d ** 2, cvxpy.abs(r_contact) <= self.r_contact_max, lambda_ <= lambda_max, lambda_ >= lambda_min, xi_z <= max_dcm_height, xi_z >= min_dcm_height, omega <= omega_max, omega >= omega_min]) prob.solve() Delta_lambda_opt = Delta_lambda.value Delta_r_opt = array(Delta_r.value).reshape((2,)) self.omega = omega_d + Delta_omega.value self.dcm = self.pendulum.com.p \ + self.pendulum.com.pd / self.omega return (Delta_r_opt, Delta_lambda_opt)
def portfolio_optimization_cvx(r, cov=None, Xf=None, F=None, D=None, wb=None, w0=None, c_=None, lambda_=None, sigma=None, delta_=None, wmin=None, wmax=None, A=None, bmin=None, bmax=None, B=None, beq=None, fmin=None, fmax=None, cpct=None, **kwargs): """ 组合优化器 目标函数: -------- maximize x'r - c'|w-w0| - lambda x'Vx subject to w = wb + x x'Vx <= sigma^2 ||w-w0|| <= delta * 2 wmin <= w <= wmax fmin <= Xf'x <= fmax w'(wb>0) >= cpct bmin <= A'x <= bmax B'x == beq 其中 , w 是绝对权重 , x 是主动权重 , 股票协方差 V 由结构化因子模型确定. 如果传入cov, V=cov; 否则,V=V = Xf'(F)Xf + D^2 Parameter: ----------- r: numpy.array(n,) 股票预期收益 cov: numpy.array(n,n) 股票协方差矩阵 Xf: numpy.array(n,m) 风险因子取值 F: numpy.array(m,m) 风险因子收益率协方差矩阵 D: numpy.array(n,) 股票残差风险矩阵 w0: numpy.array(n,) 组合初始权重,None表示首次建仓,初始权重为0 wb: numpy.array(n,) 基准指数权重 c_: float or numpy.array(n,) 换手惩罚参数 lambda_:float 风险惩罚参数 sigma_: float 跟踪误差约束,0.05 delta_: float 换手约束参数,单边 wmin: float or numpy.array(n,) 绝对权重最小值 wmax: float or numpy.array(n,) 绝对权重最大值 fmin: float/list/tuple/numpy.array(m,) 因子暴露最小值 fmax: float/list/tuple/numpy.array(m,) 因子暴露最大值 cpct: float from 0 to 1, 成分股内股票权重占比 A: numpy.array(n,p) 其他线性约束矩阵 kwargs: dict 传入cvxpy.problem.solve()中的参数。 指定优化器 """ n = len(r) # 资产数量 x = cvx.Variable(n) # 设定目标函数 obj = x @ r if wb is None: wb = np.zeros(n, dtype='float') if w0 is None: w0 = np.zeros(n, dtype='float') w = wb + x if isinstance(c_, float): obj = obj - cvx.sum(c_ * cvx.abs(w - w0)) elif isinstance(c_, np.ndarray): obj = obj - c_ @ cvx.abs(w - w0) if cov is not None: risk = cvx.quad_form(x, cov) else: risk = cvx.quad_form(x, Xf.T @ F @ Xf) + cvx.quad_form( x, np.diag(D**2)) if lambda_: obj = obj - lambda_ * risk # 限制条件 constraints = [] if wmin is not None: constraints.append(wmin <= w) if wmax is not None: constraints.append(w <= wmax) if A is not None: ineq = A.T @ x if bmin is not None: constraints.append(ineq >= bmin) if bmax is not None: constraints.append(ineq <= bmax) if B is not None: eq = B.T @ x constraints.append(beq == eq) if cpct is not None and wb is not None: is_member = np.where(wb != 0.0)[0] constraints.append(cvx.sum(w[is_member]) >= cpct) # 优化 prob = cvx.Problem(cvx.Maximize(obj), constraints) prob.solve(**kwargs) if prob.status == 'optimal': result = { 'abswt': x.value + wb, 'relwt': x.value, 'er': x.value @ r, 'sigmal': risk.value } else: result = {} return result
def solveMarkowitzObjective(c, A, Q, v, x0, bxl, bxu, bcl, bcu): """ Solves a convex program with a quadratic constraint and linear abs penalty maximize c'*x - (1/2) x'*Q*x - v'*abs{x - x0} subject to bcl <= A*x <= bcu bxl <= x <= bxu """ x = cvx.Variable(len(c)) constraints = [A*x <= bcu, bcl <= A*x, bxl <= x, x <= bxu] objective = x.T*c -0.5 * cvx.quad_form(x, Q) - cvx.abs(x - x0).T*v ccu.maximize(objective=objective, constraints=constraints, verbose=True) return x.value