def test_robust_regression(self): # # Compare cvxpy solutions to cvxopt ground truth # from cvxpy import (matrix, variable, program, minimize, huber, sum, leq, geq, square, norm2, ones, zeros, quad_form) tol_exp = 5 # Check solution to within 5 decimal places m, n = self.m, self.n A = matrix(self.A) b = matrix(self.b) # Method 1: using huber directly x = variable(n) p = program(minimize(sum(huber(A*x - b, 1.0)))) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xh, tol_exp) # Method 2: solving the dual QP x = variable(n) u = variable(m) v = variable(m) p = program(minimize(0.5*quad_form(u, 1.0) + sum(v)), [geq(u, 0.0), leq(u, 1.0), geq(v, 0.0), leq(A*x - b, u + v), geq(A*x - b, -u - v)]) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xh, tol_exp)
def to_cvxpy(self, weight_var, weight_var_series, init_weights): constraints = [] labels = self.labels groups = self.max_weights.index.union(self.min_weights.index).unique() msg_fmt = '组"{}"最小值"{}"应小于等于最大值"{}"' for g in groups: min_ = self.min_weights[g] max_ = self.max_weights[g] # 只有二方都限定,才比较 limited_min = min_ != 'NotConstrained' limited_max = max_ != 'NotConstrained' if limited_min and limited_max: assert min_ <= max_, msg_fmt.format(g, min_, max_) # 该组涉及到的股票 assets = labels[labels == g].index # 股票对应的变量位置序号 ix = get_ix(weight_var_series, assets) # 如果该组没有对应序号,跳过 if len(ix) == 0: continue var_list = weight_var[ix] # 如果限定该组上限 # if limited_max and len(var_list): if limited_max: constraints.append(cvx.sum(var_list) <= max_) # 如果限定该组下限 # if limited_min and len(var_list): if limited_min: constraints.append(cvx.sum(var_list) >= min_) return constraints
def to_cvxpy(self, weight_var, weight_var_series, init_weights): min_cons, max_cons = [], [] loadings = self.loadings #.loc[weight_var_series.index].dropna() ix = get_ix(weight_var_series, self.loadings.index) for c in loadings.columns: min_ = self.min_exposures[c] max_ = self.max_exposures[c] assert min_ <= max_, '{} 列{}最小值"{}"必须小于等于最大值"{}"'.format( str(self), c, min_, max_) min_cons.append(cvx.sum(weight_var[ix] * loadings[c]) >= min_) max_cons.append(cvx.sum(weight_var[ix] * loadings[c]) <= max_) return min_cons + max_cons
def _cqp(D, o, k, m, N): ''' solves using cvxpy ''' # cast to object. D = cvxpy.matrix(D) N = cvxpy.matrix(N) o = cvxpy.matrix(o) # create variables. f = cvxpy.variable(k, 1, name='f') x=cvxpy.variable(m, 1, name='x') y=cvxpy.variable(m, 1, name='y') # create constraints. geqs = cvxpy.greater_equals(f,0.0) #TO DO: Sum of all f = sum of observed reads classes (and not equal to 1) sum1 = cvxpy.equals(cvxpy.sum(f), 1.0) #sum1 = cvxpy.equals(cvxpy.sum(f), sum_obs_freq) #3 #dev = cvxpy.equals(D*f-o-x,0.0) #4. matrix N (m x m) * x - y = 0 sizeConstr = cvxpy.equals(N*x-y,0.0) #Check now to do N^2 #sizeConstr = cvxpy.equals(N^2*x-y,0.0) #This might not work but try #sizeConstr = cvxpy.equals(x/N-y,0.0) #constrs = [geqs, sum1, dev, sizeConstr] constrs = [geqs, sum1] log.debug('\tin _cqp function: \n\t\tPrint matrices shapes:') log.debug('\t\t\t%s', D.shape) log.debug('\t\t\t%s', f.shape) log.debug('\t\t\t%s', o.shape) # create the program. #p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(y)),constraints=constrs) p = cvxpy.program(cvxpy.minimize(cvxpy.norm2(D*f-o)),constraints=constrs) p.options['abstol'] = 1e-6 ## 'abstol' - Absolute accuracy Default: 1e-7 p.options['reltol'] = 1e-5 ## 'reltol' - Relative accuracy Default: 1e-6 p.options['feastol'] = 1e-5 ## 'feastol' - Tolerance for feasibility conditions Default: 1e-6 p.options['maxiters'] = 500 ## 'maxiters' - Maximum number of iterations Default: 100 # solve the program. p.solve(quiet=True) # return results. #print np.around(f.value, decimals=20) #print "Print using loop" getcontext().prec = 20 #for i in f.value: # temp_fi=str(i).strip('[]') # print temp_fi return f.value
def cvxpy_test(): numpy.random.seed(1) n = 10 mu = numpy.abs(numpy.random.randn(n, 1)) Sigma = numpy.random.randn(n, n) Sigma = Sigma.T.dot(Sigma) w = cvxpy.Variable(n) gamma = cvxpy.Parameter(nonneg=True) ret = mu.T*w risk = cvxpy.quad_form(w, Sigma) result = cvxpy.Problem(cvxpy.Maximize(ret - gamma*risk), [cvxpy.sum(w) == 1, w >= 0]) return f"csvpy test >>> {result}"
def construct_lp_relaxation( demand_hist, M, transport_graph, cost_label='cost' ) : """ demand_hist is a dictionary from ordered station pairs (i,j) -> whole integers M is a membership graph with edges (i,x) for all corresponding stations-state pairs A is a DiGraph on X, with cost given by property 'cost' """ APSP = nx.all_pairs_dijkstra_path_length( transport_graph, weight=cost_label ) cost = 0. constr = [] """ create enroute variables """ S = nx.DiGraph() # service "edges" for (i,j), NUM in demand_hist.iteritems() : VARS = [] for x,y in itertools.product( M.neighbors_iter(i), M.neighbors_iter(j) ) : var = cvxpy.variable() distance = APSP[x][y] cost += var * distance constr.append( cvxpy.geq( var, 0. ) ) # needs to be non-negative S.add_edge( x, y, var=var, distance=distance ) VARS.append( var ) constr.append( cvxpy.eq( cvxpy.sum(VARS), NUM ) ) # total such trips should sum to num """ create balance variables """ B = nx.DiGraph() HEADS = [ h for h, d in S.in_degree_iter() if d > 0 ] TAILS = [ t for t, d in S.out_degree_iter() if d > 0 ] for y,x in itertools.product( HEADS, TAILS ) : var = cvxpy.variable() cost += var * APSP[y][x] constr.append( cvxpy.geq( var, 0. ) ) B.add_edge( y,x, var=var ) """ generate flow conservation constraints """ for x in S.nodes_iter() : sin = sum( [ data['var'] for _,__,data in S.in_edges_iter( x, data=True ) ] ) sout = sum( [ data['var'] for _,__,data in S.out_edges_iter( x, data=True ) ] ) bin = sum( [ data['var'] for _,__,data in B.in_edges_iter( x, data=True ) ] ) bout = sum( [ data['var'] for _,__,data in B.out_edges_iter( x, data=True ) ] ) constr.append( cvxpy.eq( sin, bout ) ) constr.append( cvxpy.eq( sout, bin ) ) #service_vars = S # alias return cost, constr, S
def FindKineticOptimum(self, bounds=None): """Use the power of convex optimization! minimize sum (protein cost) we can do this by using ln(concentration) as variables and leveraging the convexity of exponentials. """ assert self.dG0_f_prime is not None ln_conc, constraints = self._MakeMinimumFeasbileConcentrationsProblem() total_inv_conc = cvxpy.sum(cvxpy.exp(-ln_conc)) program = cvxpy.program(cvxpy.minimize(total_inv_conc), constraints) program.solve(quiet=True) return ln_conc.value, total_inv_conc.value
def test_convexify_obj(self): """ Test convexify objective """ obj = cvx.Maximize(cvx.sum(cvx.square(self.x))) self.x.value = [1,1] obj_conv = convexify_obj(obj) prob_conv = cvx.Problem(obj_conv, [self.x <= -1]) prob_conv.solve() self.assertAlmostEqual(prob_conv.value,-6) obj = cvx.Minimize(cvx.sqrt(self.a)) self.a.value = [1] obj_conv = convexify_obj(obj) prob_conv = cvx.Problem(obj_conv,cvx.sqrt(self.a).domain) prob_conv.solve() self.assertAlmostEqual(prob_conv.value,0.5)
def test_problem_expdesign(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, log, det_rootn, diag, sum, geq, eq, zeros) tol_exp = 6 V = matrix(self.V) n = V.shape[1] x = variable(n) # Use cvxpy to solve the problem above p = program(minimize(-log(det_rootn(V*diag(x)*V.T))), [geq(x, 0.0), eq(sum(x), 1.0)]) p.solve(True) np.testing.assert_array_almost_equal( x.value, self.xd, tol_exp)
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 f_Gamma(c): # For fixed c solve the semidefinite program for Algorithm 4 # Variable Gamma_plus (for \Gamma_{i+1}) d_plus = variable(2*n, 1, name='d_plus') # Constraints constr = [] for j in range(2*n): ctt = less_equals(d_plus[j,0], 0) constr.append( less_equals(-d_plus[j,0], 0)) constr.append( less_equals( d_plus[j,0], d[j,0])) constr.append( less_equals( J[j,j]*d_plus[j,0] + sum([abs(J[i,j])*d_plus[i,0] for i in range(2*n)]) - abs(J[j,j])*d_plus[j,0], c* d_plus[j,0])) # Objective function obj = geo_mean(d_plus) # Find solution p = program(maximize(obj), constr) return d_plus.value
def calculate_accumulation(self): self.accumulation = cvx.sum(self.edge_flows)
def bregman_map_cvxtorch(s_mat, Wk_plus_value, Wk_minus_value, gamma, l1_pen, dagness_pen, dagness_exp): """ Solves argmin g(W) + <grad f (Wk), W-Wk> + 1/gamma * Dh(W, Wk) with new CVXPY layers and PyTorch this is only implemented for a specific penalty and kernel Args: s_mat (np.array): data matrix Wk_plus_value (np.array): current iterate value for W+ Wk_minus_value (np.array): current iterate value for W- gamma (float): Bregman iteration map param l1_pen (float): lambda in paper dagness_pen (float): mu in paper dagness_exp (float): alpha in paper """ n = s_mat.shape[1] W_plus = cp.Variable((n, n), nonneg=True) W_plus.value = Wk_plus_value W_minus = cp.Variable((n, n), nonneg=True) inv_gamma_param = cp.Parameter(nonneg=True) l1_pen_param = cp.Parameter(nonneg=True) Wk_plus_param = cp.Parameter((n, n), nonneg=True) Wk_minus_param = cp.Parameter((n, n), nonneg=True) W_minus.value = Wk_minus_value sum_W = W_plus + W_minus # sum variable obj_ll = cp.norm(s_mat @ (np.eye(n) - W_plus + W_minus), "fro") ** 2 obj_spars = l1_pen_param * cp.sum(W_plus + W_minus) # Compute C sum_Wk = Wk_plus_value + Wk_minus_value C = compute_C(n, sum_Wk, dagness_pen, dagness_exp, inv_gamma_param) obj_trace = cp.trace(C @ sum_W) obj_kernel = inv_gamma_param * (dagness_pen * (n - 1) * (1 + dagness_exp * cp.norm(sum_W, "fro"))**n) obj = obj_ll + obj_spars + obj_trace + obj_kernel prob = cp.Problem(cp.Minimize(obj), [cp.sum(W_plus) + cp.sum(W_minus) >= n/((n-2)*dagness_exp)]) assert prob.is_dpp(), "{}{}{}{}".format((dagness_pen * (n - 1) * (1 + dagness_exp * cp.norm(sum_W, "fro"))**n).is_dpp()) #set_trace() layer = CvxpyLayer(prob, parameters = [inv_gamma_param, l1_pen_param], variables = [W_plus, W_minus]) #TODO allow GPU torch_gamma = torch.tensor(1 / gamma) torch_l1_pen = torch.tensor(l1_pen) x_star = layer(torch_gamma, torch_l1_pen) #W_plus.value, W_minus.value #set_trace() next_W_plus, next_W_minus = x_star[0].numpy(), x_star[1].numpy() tilde_W_plus = np.maximum(next_W_plus - next_W_minus, 0.0) tilde_W_minus = np.maximum(next_W_minus - next_W_plus, 0.0) tilde_sum = tilde_W_plus + tilde_W_minus # if np.sum(tilde_sum) >= n / ((n - 2) * dagness_exp): return tilde_W_plus, tilde_W_minus else: return np.maximum(next_W_plus, 0), np.maximum(next_W_minus, 0)
def total_loss(self, v_0, treated_outcome, treated_covariates, control_outcome, control_covariates, pairwise_difference, pen, placebo, data): ''' Solves for w*(v) that minimizes loss function 1 given v, Returns loss from loss function 2 with w=w*(v) placebo: bool indicates whether the optimization is ran for finding the real synthetic control or as part of a placebo-style validity test. If True, only placebo class attributes are affected. ''' assert placebo in [ False, "in-time", "in-space" ], "TypeError: Placebo must False, 'in-time' or 'in-space'" n_controls = control_outcome.shape[1] if pen == "auto": V = np.diag(v_0[:-1]) pen_coef = v_0[-1] else: V = np.diag(v_0) pen_coef = pen # Construct the problem - constrain weights to be non-negative w = cvx.Variable((n_controls, 1), nonneg=True) #Define the objective #PROBLEM: treated_synth_difference = cvx.sum(V @ cvx.square(treated_covariates.T - control_covariates @ w)) runs better for normal sc, #but it doesnt work at all for in-time placebos, this probably means I am messing up the dimensionality somewhere in the processing #This is a work-around that works, but it ain't pretty if placebo == 'in-time': treated_synth_difference = cvx.sum( V @ cvx.square(treated_covariates - control_covariates @ w)) else: treated_synth_difference = cvx.sum( V @ cvx.square(treated_covariates.T - control_covariates @ w)) pairwise_difference = cvx.sum( V @ (cvx.square(pairwise_difference) @ w)) objective = cvx.Minimize(treated_synth_difference + pen_coef * pairwise_difference) #Add constraint sum of weights must equal one constraints = [cvx.sum(w) == 1] #Solve problem problem = cvx.Problem(objective, constraints) try: #Try solving using current value of V, if it doesn't work return infinite loss result = problem.solve(verbose=False) loss = (treated_outcome - control_outcome @ w.value).T @ ( treated_outcome - control_outcome @ w.value) except: return float(np.inf) #If loss is smaller than previous minimum, update loss, w and v if not placebo: if loss < data.min_loss: data.min_loss = loss data.w = w.value data.v = np.diagonal(V) / np.sum( np.diagonal(V) ) #Make sure its normailzed (sometimes the optimizers diverge from bounds) data.pen = pen_coef data.synth_outcome = data.w.T @ data.control_outcome_all.T #Transpose to make it (n_periods x 1) data.synth_covariates = data.control_covariates @ data.w elif placebo == "in-space": data.in_space_placebo_w = w.value elif placebo == "in-time": data.in_time_placebo_w = w.value #Return loss return loss
def iter_dccp(self, max_iter, tau, mu, tau_max, solver, ep, max_slack_tol, **kwargs): """ ccp iterations :param max_iter: maximum number of iterations in ccp :param tau: initial weight on slack variables :param mu: increment of weight on slack variables :param tau_max: maximum weight on slack variables :param solver: specify the solver for the transformed problem :return value of the objective function, maximum value of slack variables, value of variables """ # split non-affine equality constraints constr = [] for constraint in self.constraints: if str( type(constraint) ) == "<class 'cvxpy.constraints.zero.Equality'>" and not constraint.is_dcp( ): constr.append(constraint.args[0] <= constraint.args[1]) constr.append(constraint.args[0] >= constraint.args[1]) else: constr.append(constraint) obj = self.objective self = cvx.Problem(obj, constr) it = 1 converge = False # keep the values from the previous iteration or initialization previous_cost = float("inf") previous_org_cost = self.objective.value variable_pres_value = [] for var in self.variables(): variable_pres_value.append(var.value) # each non-dcp constraint needs a slack variable var_slack = [] for constr in self.constraints: if not constr.is_dcp(): var_slack.append(cvx.Variable(constr.shape)) while it <= max_iter and all(var.value is not None for var in self.variables()): constr_new = [] # objective convexified_obj = convexify_obj(self.objective) if not self.objective.is_dcp(): # non-sub/super-diff while convexified_obj is None: # damping var_index = 0 for var in self.variables(): var.value = 0.8 * var.value + 0.2 * variable_pres_value[ var_index] var_index += 1 convexified_obj = convexify_obj(self.objective) # domain constraints for dom in self.objective.expr.domain: constr_new.append(dom) # new cost function cost_new = convexified_obj.expr # constraints count_slack = 0 for arg in self.constraints: temp = convexify_constr(arg) if not arg.is_dcp(): while temp is None: # damping var_index = 0 for var in self.variables(): var.value = 0.8 * var.value + 0.2 * variable_pres_value[ var_index] var_index += 1 temp = convexify_constr(arg) newcon = temp[0] # new constraint without slack variable for dom in temp[1]: # domain constr_new.append(dom) constr_new.append(newcon.expr <= var_slack[count_slack]) constr_new.append(var_slack[count_slack] >= 0) count_slack = count_slack + 1 else: constr_new.append(arg) # objective if self.objective.NAME == 'minimize': for var in var_slack: cost_new += tau * cvx.sum(var) obj_new = cvx.Minimize(cost_new) else: for var in var_slack: cost_new -= tau * cvx.sum(var) obj_new = cvx.Maximize(cost_new) # new problem prob_new = cvx.Problem(obj_new, constr_new) # keep previous value of variables variable_pres_value = [] for var in self.variables(): variable_pres_value.append(var.value) # solve if solver is None: prob_new_cost_value = prob_new.solve(**kwargs) else: prob_new_cost_value = prob_new.solve(solver=solver, **kwargs) if prob_new_cost_value is not None: logger.info( "iteration=%d, cost value=%.5f, tau=%.5f, solver status=%s", it, prob_new_cost_value, tau, prob_new.status) else: logger.info( "iteration=%d, cost value=%.5f, tau=%.5f, solver status=%s", it, np.nan, tau, prob_new.status) max_slack = None # print slack if (prob_new._status == "optimal" or prob_new._status == "optimal_inaccurate") and not var_slack == []: slack_values = [v.value for v in var_slack if v.value is not None] max_slack = max([np.max(v) for v in slack_values] + [-np.inf]) logger.info("max slack = %.5f", max_slack) #terminate if prob_new.value is not None and np.abs(previous_cost - prob_new.value) <= ep and np.abs(self.objective.value - previous_org_cost) <= ep \ and (max_slack is None or max_slack <= max_slack_tol): it = max_iter + 1 converge = True else: previous_cost = prob_new.value previous_org_cost = self.objective.value tau = min([tau * mu, tau_max]) it += 1 # return if converge: self._status = "Converged" else: self._status = "Not_converged" var_value = [] for var in self.variables(): var_value.append(var.value) if not var_slack == []: return (self.objective.value, max_slack, var_value, self._status) else: return (self.objective.value, var_value, self._status)
import numpy as np import matplotlib.pyplot as plt import dccp np.random.seed(0) n = 10 r = np.linspace(1, 5, n) 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) 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 # plot plt.figure(figsize=(5,5)) circ = np.linspace(0,2*pi) x_border = [-l/2, l/2, l/2, -l/2, -l/2] y_border = [-l/2, -l/2, l/2, l/2, -l/2] for i in xrange(n): plt.plot(c[i,0].value+r[i]*np.cos(circ),c[i,1].value+r[i]*np.sin(circ),'b') plt.plot(x_border,y_border,'g') plt.axes().set_aspect('equal') plt.xlim([-l/2,l/2]) plt.ylim([-l/2,l/2]) plt.show()
def init_gusto_problem(self): cons = [] # Variables x = cp.Variable((2*self.n,self.N)) # state u = cp.Variable((self.m,self.N-1)) # control xlb_slack_vars = cp.Variable((2*self.n, self.N), nonneg=True) xub_slack_vars = cp.Variable((2*self.n, self.N), nonneg=True) tr_slack_vars = cp.Variable(2*self.N-1, nonneg=True) sdf_slack_vars = cp.Variable(self.N*self.n_obs, nonneg=True) self.gusto_prob_variables = {'x':x, 'u':u, 'xlb_slack_vars':xlb_slack_vars, 'xub_slack_vars':xub_slack_vars, 'tr_slack_vars':tr_slack_vars, 'sdf_slack_vars':sdf_slack_vars} # Parameters x0 = cp.Parameter(2*self.n) xg = cp.Parameter(2*self.n) obstacles = cp.Parameter((4, self.n_obs)) omega = cp.Parameter(1) delta = cp.Parameter(1) x_bar = cp.Parameter((2*self.n, self.N)) # state reference u_bar = cp.Parameter((self.m, self.N-1)) # control reference dists = cp.Parameter(self.N*self.n_obs) sdf_grads = cp.Parameter((self.n, self.N*self.n_obs)) self.gusto_prob_parameters = {'x0': x0, 'xg': xg, 'obstacles': obstacles, 'omega':omega, 'delta':delta, 'x_bar':x_bar, 'u_bar':u_bar, 'dists':dists, 'sdf_grads':sdf_grads} # Initial condition cons += [x[:,0] == x0] # Dynamics constraints for ii in range(self.N-1): cons += [x[:,ii+1] - (self.Ak @ x[:,ii] + self.Bk @ u[:,ii]) == np.zeros(2*self.n)] # Control constraints for i_t in range(self.N-1): cons += [cp.norm(u[:, i_t]) <= self.umax] penalty_cost = 0.0 # State bounds penalties for i_t in range(self.N): for jj in range(self.n): cons += [self.posmin[jj] - x[jj,i_t] <= xlb_slack_vars[jj, i_t]] cons += [x[jj,i_t] - self.posmax[jj] <= xub_slack_vars[jj, i_t]] cons += [self.velmin - x[self.n+jj, i_t] <= xlb_slack_vars[self.n+jj, i_t]] cons += [x[self.n+jj, i_t] - self.velmax <= xub_slack_vars[self.n+jj, i_t]] penalty_cost += omega*cp.sum(xlb_slack_vars) + omega*cp.sum(xub_slack_vars) # Collision avoidance penalty for i_t in range(self.N): for ii_obs in range(self.n_obs): n_hat, dist = sdf_grads[:, self.n_obs*i_t+ii_obs], dists[self.n_obs*i_t+ii_obs] cons += [-(dist + n_hat.T @ (x[:self.n, i_t] - x_bar[:self.n, i_t])) <= sdf_slack_vars[self.n_obs*i_t+ii_obs]] penalty_cost += omega*cp.sum(sdf_slack_vars) # Trust region penalties for i_t in range(self.N): cons += [cp.norm(x[:, i_t] - x_bar[:, i_t], 1) - delta <= tr_slack_vars[i_t]] for i_t in range(self.N-1): cons += [cp.norm(u[:, i_t] - u_bar[:, i_t], 1) - delta <= tr_slack_vars[self.N+i_t]] penalty_cost += omega*cp.sum(tr_slack_vars) lqr_cost = 0. # l2-norm of lqr_cost for i_t in range(self.N): lqr_cost += cp.quad_form(x[:, i_t]-xg, self.Q) for i_t in range(self.N-1): lqr_cost += cp.quad_form(u[:, i_t], self.R) self.gusto_prob = cp.Problem(cp.Minimize(lqr_cost+penalty_cost), cons)
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
X_test_file = "~/development/cvxopt/hwk7/quad_metric_data_norng/X_test.csv" Y_test_file = "~/development/cvxopt/hwk7/quad_metric_data_norng/Y_test.csv" d_test_file = "~/development/cvxopt/hwk7/quad_metric_data_norng/d_test.csv" X = np.loadtxt(open(X_file, "rb"), delimiter=",") Y = np.loadtxt(open(Y_file, "rb"), delimiter=",") d = np.loadtxt(open(d_file, "rb"), delimiter=",") X_test = np.loadtxt(open(X_test_file, "rb"), delimiter=",") Y_test = np.loadtxt(open(Y_test_file, "rb"), delimiter=",") d_test = np.loadtxt(open(d_test_file, "rb"), delimiter=",") N = X.shape[1] Z = X - Y P = cp.Variable((5, 5), symmetric=True) t1 = 1 / N * (d**2).sum() t2 = 1 / N * cp.sum( [-2 * d[i] * cp.quad_form(Z[:, i], P)**(1 / 2) for i in range(N)]) t3 = 1 / N * cp.sum([cp.quad_form(Z[:, i], P) for i in range(100)]) obj = t1 + t2 + t3 p = cp.Problem(cp.Minimize(obj), [P >> 0]) p.solve() Z_test = X_test - Y_test N = X_test.shape[1] errors = [ d_test[i] - (cp.quad_form(Z_test[:, i], P.value)**(1 / 2)).value for i in range(N) ] mse = np.sum(np.array(errors)**2) / N
#solving problem of hw3_1 (extra question) import cvxpy as cp import cvxopt A= cvxopt.matrix([1,2,0,1,0,0,3,1,0,3,1,1,2,1,2,5,1,0,3,2],(4,5)).T c_max = cvxopt.matrix([100,100,100,100,100],(5,1)) p= cvxopt.matrix([3,2,7,6]) p_disc=cvxopt.matrix([2,1,4,2]) q=cvxopt.matrix([4,10,5,10]) u=cp.Variable(4,1) x=cp.Variable(4,1) objective = cp.Maximize(cp.sum(u)) constraints1 =[cp.min(p[i]*x[i],p[i]*q[i]+p_disc[i]*(x[i]-q[i]))>=u[i] for i in range(4)] constraints =[x>=0, A*x<=c_max] constraints1.extend(constraints) prob = cp.Problem(objective,constraints1) prob.solve() print x.value lamb = constraints1[2].dual_value ############################################################# #original fomulation ############################################################# print "equavlent form" xx = cp.Variable(4,1) objective1 = cp.Maximize(sum([cp.min(p[i]*xx[i],p[i]*q[i]+p_disc[i]*(xx[i]-q[i])) for i in range(4)])) constraints2 = [xx>=0, A*xx<=c_max] prob1 = cp.Problem(objective1, constraints2) prob1.solve() print xx.value
a_arr[a_arr < prob] = 0 a_arr_sp = spmatrix(a_arr[a_arr.nonzero()[0], a_arr.nonzero()[1]], a_arr.nonzero()[0], a_arr.nonzero()[1], size=(m, n)) W = cp.Variable(n, 4) constraints = [] constraints.append(W[0,0] == 0) constraints.append(W[:,0] >= 0) lam = 8 beta = 0.5 loss = cp.sum(a_arr_sp[:,0] - a_arr_sp*W[:,0]) l2_reg = 0.5*beta*cp.sum(cp.square(W[:,0])) l1_reg = lam*cp.sum(W[:,0]) obj = cp.Minimize(l2_reg) # TODO No constraints, get error. p = cp.Problem(obj, constraints) import cProfile #cProfile.run('p.solve()') objective, constr_map, dims = p.canonicalize() all_ineq = itertools.chain(constr_map[s.EQ], constr_map[s.INEQ]) var_offsets, x_length = p._get_var_offsets(objective, all_ineq) c, obj_offset = p._constr_matrix([objective], var_offsets, x_length, p._DENSE_INTF,
def to_cvxpy(self, weight_var, weight_var_series, init_weights): return [cvx.sum(cvx.abs(weight_var)) <= self.max_]
def iter_dccp(self, max_iter, tau, mu, tau_max, solver, ep, max_slack_tol, **kwargs): """ ccp iterations :param max_iter: maximum number of iterations in ccp :param tau: initial weight on slack variables :param mu: increment of weight on slack variables :param tau_max: maximum weight on slack variables :param solver: specify the solver for the transformed problem :return value of the objective function, maximum value of slack variables, value of variables """ # split non-affine equality constraints constr = [] for constraint in self.constraints: if str(type(constraint)) == "<class 'cvxpy.constraints.zero.Equality'>" and not constraint.is_dcp(): constr.append(constraint.args[0] <= constraint.args[1]) constr.append(constraint.args[0] >= constraint.args[1]) else: constr.append(constraint) obj = self.objective self = cvx.Problem(obj, constr) it = 1 converge = False # keep the values from the previous iteration or initialization previous_cost = float("inf") previous_org_cost = self.objective.value variable_pres_value = [] for var in self.variables(): variable_pres_value.append(var.value) # each non-dcp constraint needs a slack variable var_slack = [] for constr in self.constraints: if not constr.is_dcp(): var_slack.append(cvx.Variable(constr.shape)) while it <= max_iter and all(var.value is not None for var in self.variables()): constr_new = [] # objective convexified_obj = convexify_obj(self.objective) if not self.objective.is_dcp(): # non-sub/super-diff while convexified_obj is None: # damping var_index = 0 for var in self.variables(): #var_index = self.variables().index(var) var.value = 0.8*var.value + 0.2* variable_pres_value[var_index] var_index += 1 convexified_obj = convexify_obj(self.objective) # domain constraints for dom in self.objective.expr.domain: constr_new.append(dom) # new cost function cost_new = convexified_obj.expr # constraints count_slack = 0 for arg in self.constraints: temp = convexify_constr(arg) if not arg.is_dcp(): while temp is None: # damping for var in self.variables: var_index = self.variables().index(var) var.value = 0.8 * var.value + 0.2 * variable_pres_value[var_index] temp = convexify_constr(arg) newcon = temp[0] # new constraint without slack variable for dom in temp[1]:# domain constr_new.append(dom) constr_new.append(newcon.expr <= var_slack[count_slack]) constr_new.append(var_slack[count_slack] >= 0) count_slack = count_slack + 1 else: constr_new.append(arg) # objective if self.objective.NAME == 'minimize': for var in var_slack: cost_new += tau*cvx.sum(var) obj_new = cvx.Minimize(cost_new) else: for var in var_slack: cost_new -= tau*cvx.sum(var) obj_new = cvx.Maximize(cost_new) # new problem prob_new = cvx.Problem(obj_new, constr_new) # keep previous value of variables variable_pres_value = [] for var in self.variables(): variable_pres_value.append(var.value) # solve if solver is None: logger.info("iteration=%d, cost value=%.5f, tau=%.5f", it, prob_new.solve(**kwargs), tau) else: logger.info("iteration=%d, cost value=%.5f, tau=%.5f", it, prob_new.solve(solver=solver, **kwargs), tau) max_slack = None # print slack if (prob_new._status == "optimal" or prob_new._status == "optimal_inaccurate") and not var_slack == []: slack_values = [v.value for v in var_slack if v.value is not None] max_slack = max([np.max(v) for v in slack_values] + [-np.inf]) logger.info("max slack = %.5f", max_slack) #terminate if prob_new.value is not None and np.abs(previous_cost - prob_new.value) <= ep and np.abs(self.objective.value - previous_org_cost) <= ep \ and (max_slack is None or max_slack <= max_slack_tol ): it = max_iter+1 converge = True else: previous_cost = prob_new.value previous_org_cost = self.objective.value tau = min([tau*mu,tau_max]) it += 1 # return if converge: self._status = "Converged" else: self._status = "Not_converged" var_value = [] for var in self.variables(): var_value.append(var.value) if not var_slack == []: return(self.objective.value, max_slack, var_value) else: return(self.objective.value, var_value)
def to_cvxpy(self, weight_var, weight_var_series, init_weights): total = cvx.sum(weight_var) return [total >= self.min_, total <= self.max_]
value=np.random.randn(len(time_range), price_scenarios) * 10 + 120, shape=(len(time_range), price_scenarios), ) # bid_price = cvx.Variable(len(time_range), name='bid_pri') # offer_price = cvx.Variable(len(time_range), name='off_pri') bid_volume = cvx.Variable((len(time_range), soc_scenarios), name='bid_vol') offer_volume = cvx.Variable((len(time_range), soc_scenarios), name='off_vol') bought_volume = cvx.Variable((len(time_range), soc_scenarios), name='bid_vol') sold_volume = cvx.Variable((len(time_range), soc_scenarios), name='off_vol') soc = cvx.Variable((len(time_range) + 1, soc_scenarios), name='off_pri') battery_cycles = cvx.sum(offer_volume) / BATTERY_ENERGY constraints = [ bid_volume >= 0, offer_volume >= 0, soc >= 0, bid_volume <= BATTERY_POWER / 2, offer_volume <= BATTERY_ENERGY / 2, soc[0] == initial_soc, soc[1:] == soc[:-1] + bid_volume * EFFICIENCY**.5 - offer_volume / EFFICIENCY**.5, soc <= BATTERY_ENERGY, battery_cycles <= MAX_CYCLES / 365 * len(time_range) / 48, ] constraints += [ bid_volume[:, i - 1] == bid_volume[:, i]
def constraints(self, mask, sizing_for_rel=False, find_min_soe=False): """Default build constraint list method. Used by services that do not have constraints. Args: mask (DataFrame): A boolean array that is true for indices corresponding to time_series data included in the subs data set Returns: A list of constraints that corresponds the battery's physical constraints and its service constraints """ constraint_list = [] size = int(np.sum(mask)) ene_target = self.soc_target * self.effective_soe_max # this is init_ene # optimization variables ene = self.variables_dict['ene'] dis = self.variables_dict['dis'] ch = self.variables_dict['ch'] uene = self.variables_dict['uene'] udis = self.variables_dict['udis'] uch = self.variables_dict['uch'] on_c = self.variables_dict['on_c'] on_d = self.variables_dict['on_d'] start_c = self.variables_dict['start_c'] start_d = self.variables_dict['start_d'] if sizing_for_rel: constraint_list += [ cvx.Zero(ene[0] - ene_target + (self.dt * dis[0]) - (self.rte * self.dt * ch[0]) - uene[0] + (ene[0] * self.sdr * 0.01)) ] constraint_list += [ cvx.Zero(ene[1:] - ene[:-1] + (self.dt * dis[1:]) - (self.rte * self.dt * ch[1:]) - uene[1:] + (ene[1:] * self.sdr * 0.01)) ] else: # energy at beginning of time step must be the target energy value constraint_list += [cvx.Zero(ene[0] - ene_target)] # energy evolution generally for every time step constraint_list += [ cvx.Zero(ene[1:] - ene[:-1] + (self.dt * dis[:-1]) - (self.rte * self.dt * ch[:-1]) - uene[:-1] + (ene[:-1] * self.sdr * 0.01)) ] # energy at the end of the last time step (makes sure that the end of the last time step is ENE_TARGET constraint_list += [ cvx.Zero(ene_target - ene[-1] + (self.dt * dis[-1]) - (self.rte * self.dt * ch[-1]) - uene[-1] + (ene[-1] * self.sdr * 0.01)) ] # constraints on the ch/dis power constraint_list += [cvx.NonPos(ch - (on_c * self.ch_max_rated))] constraint_list += [cvx.NonPos((on_c * self.ch_min_rated) - ch)] constraint_list += [cvx.NonPos(dis - (on_d * self.dis_max_rated))] constraint_list += [cvx.NonPos((on_d * self.dis_min_rated) - dis)] # constraints on the state of energy constraint_list += [cvx.NonPos(self.effective_soe_min - ene)] constraint_list += [cvx.NonPos(ene - self.effective_soe_max)] # account for -/+ sub-dt energy -- this is the change in energy that the battery experiences as a result of energy option # if sizing for reliability if sizing_for_rel: constraint_list += [cvx.Zero(uene)] else: constraint_list += [ cvx.Zero(uene + (self.dt * udis) - (self.dt * uch * self.rte)) ] # the constraint below limits energy throughput and total discharge to less than or equal to # (number of cycles * energy capacity) per day, for technology warranty purposes # this constraint only applies when optimization window is equal to or greater than 24 hours if self.daily_cycle_limit and size >= 24: sub = mask.loc[mask] for day in sub.index.dayofyear.unique(): day_mask = (day == sub.index.dayofyear) constraint_list += [ cvx.NonPos( cvx.sum(dis[day_mask] + udis[day_mask]) * self.dt - self.ene_max_rated * self.daily_cycle_limit) ] elif self.daily_cycle_limit and size < 24: TellUser.info( 'Daily cycle limit did not apply as optimization window is less than 24 hours.' ) # note: cannot operate startup without binary if self.incl_startup and self.incl_binary: # startup variables are positive constraint_list += [cvx.NonPos(-start_c)] constraint_list += [cvx.NonPos(-start_d)] # difference between binary variables determine if started up in # previous interval constraint_list += [cvx.NonPos(cvx.diff(on_d) - start_d[1:])] constraint_list += [cvx.NonPos(cvx.diff(on_c) - start_c[1:])] return constraint_list
def test_readme_examples(self): import numpy numpy.random.seed(1) # cvx.Problem data. m = 30 n = 20 A = numpy.random.randn(m, n) b = numpy.random.randn(m) # Construct the problem. x = cvx.Variable(n) objective = cvx.Minimize(cvx.sum_squares(A @ x - b)) constraints = [0 <= x, x <= 1] p = cvx.Problem(objective, constraints) # The optimal objective is returned by p.solve(). p.solve() # The optimal value for x is stored in x.value. print(x.value) # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print(constraints[0].dual_value) #################################################### # Scalar variable. a = cvx.Variable() # Column vector variable of length 5. x = cvx.Variable(5) # Matrix variable with 4 rows and 7 columns. A = cvx.Variable((4, 7)) #################################################### # Positive scalar parameter. m = cvx.Parameter(nonneg=True) # Column vector parameter with unknown sign (by default). cvx.Parameter(5) # Matrix parameter with negative entries. G = cvx.Parameter((4, 7), nonpos=True) # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) # Raises an error for assigning a value with invalid sign. with self.assertRaises(Exception) as cm: G.value = numpy.ones((4, 7)) self.assertEqual(str(cm.exception), "Parameter value must be nonpositive.") #################################################### a = cvx.Variable() x = cvx.Variable(5) # expr is an Expression object after each assignment. expr = 2 * x expr = expr - a expr = cvx.sum(expr) + cvx.norm(x, 2) #################################################### import numpy as np # cvx.Problem data. n = 10 m = 5 A = np.random.randn(n, m) b = np.random.randn(n) gamma = cvx.Parameter(nonneg=True) # Construct the problem. x = cvx.Variable(m) objective = cvx.Minimize( cvx.sum_squares(A @ x - b) + gamma * cvx.norm(x, 1)) p = cvx.Problem(objective) # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value p.solve() return x.value gammas = np.logspace(-1, 2, num=2) # Serial computation. [get_x(value) for value in gammas] #################################################### n = 10 mu = np.random.randn(1, n) sigma = np.random.randn(n, n) sigma = sigma.T.dot(sigma) gamma = cvx.Parameter(nonneg=True) gamma.value = 1 x = cvx.Variable(n) # Constants: # mu is the vector of expected returns. # sigma is the covariance matrix. # gamma is a cvx.Parameter that trades off risk and return. # cvx.Variables: # x is a vector of stock holdings as fractions of total assets. expected_return = mu @ x risk = cvx.quad_form(x, sigma) objective = cvx.Maximize(expected_return - gamma * risk) p = cvx.Problem(objective, [cvx.sum(x) == 1]) p.solve() # The optimal expected return. print(expected_return.value) # The optimal risk. print(risk.value) ########################################### N = 50 M = 40 n = 10 data = [] for i in range(N): data += [(1, np.random.normal(loc=1.0, scale=2.0, size=n))] for i in range(M): data += [(-1, np.random.normal(loc=-1.0, scale=2.0, size=n))] # Construct problem. gamma = cvx.Parameter(nonneg=True) gamma.value = 0.1 # 'a' is a variable constrained to have at most 6 non-zero entries. a = cvx.Variable(n) # mi.SparseVar(n, nonzeros=6) b = cvx.Variable() slack = [ cvx.pos(1 - label * (sample.T @ a - b)) for (label, sample) in data ] objective = cvx.Minimize(cvx.norm(a, 2) + gamma * sum(slack)) p = cvx.Problem(objective) # Extensions can attach new solve methods to the CVXPY cvx.Problem class. # p.solve(method="admm") p.solve() # Count misclassifications. errors = 0 for label, sample in data: if label * (sample.T @ a - b).value < 0: errors += 1 print("%s misclassifications" % errors) print(a.value) print(b.value)
def __init__(self, m, K): # Variables: self.var = dict() self.var['X'] = cvxpy.Variable((m.n_x, K)) self.var['U'] = cvxpy.Variable((m.n_u, K)) self.var['sigma'] = cvxpy.Variable(nonneg=True) self.var['nu'] = cvxpy.Variable((m.n_x, K - 1)) self.var['delta_norm'] = cvxpy.Variable(nonneg=True) self.var['sigma_norm'] = cvxpy.Variable(nonneg=True) # Parameters: self.par = dict() self.par['A_bar'] = cvxpy.Parameter((m.n_x * m.n_x, K - 1)) self.par['B_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1)) self.par['C_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1)) self.par['S_bar'] = cvxpy.Parameter((m.n_x, K - 1)) self.par['z_bar'] = cvxpy.Parameter((m.n_x, K - 1)) self.par['X_last'] = cvxpy.Parameter((m.n_x, K)) self.par['U_last'] = cvxpy.Parameter((m.n_u, K)) self.par['sigma_last'] = cvxpy.Parameter(nonneg=True) self.par['weight_sigma'] = cvxpy.Parameter(nonneg=True) self.par['weight_delta'] = cvxpy.Parameter(nonneg=True) self.par['weight_delta_sigma'] = cvxpy.Parameter(nonneg=True) self.par['weight_nu'] = cvxpy.Parameter(nonneg=True) # Constraints: constraints = [] # Model: constraints += m.get_constraints( self.var['X'], self.var['U'], self.par['X_last'], self.par['U_last']) # Dynamics: # x_t+1 = A_*x_t+B_*U_t+C_*U_T+1*S_*sigma+zbar+nu constraints += [ self.var['X'][:, k + 1] == cvxpy.reshape(self.par['A_bar'][:, k], (m.n_x, m.n_x)) * self.var['X'][:, k] + cvxpy.reshape(self.par['B_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k] + cvxpy.reshape(self.par['C_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k + 1] + self.par['S_bar'][:, k] * self.var['sigma'] + self.par['z_bar'][:, k] + self.var['nu'][:, k] for k in range(K - 1) ] # Trust regions: dx = cvxpy.sum(cvxpy.square( self.var['X'] - self.par['X_last']), axis=0) du = cvxpy.sum(cvxpy.square( self.var['U'] - self.par['U_last']), axis=0) ds = self.var['sigma'] - self.par['sigma_last'] constraints += [cvxpy.norm(dx + du, 1) <= self.var['delta_norm']] constraints += [cvxpy.norm(ds, 'inf') <= self.var['sigma_norm']] # Flight time positive: constraints += [self.var['sigma'] >= 0.1] # Objective: sc_objective = cvxpy.Minimize( self.par['weight_sigma'] * self.var['sigma'] + self.par['weight_nu'] * cvxpy.norm(self.var['nu'], 'inf') + self.par['weight_delta'] * self.var['delta_norm'] + self.par['weight_delta_sigma'] * self.var['sigma_norm'] ) objective = sc_objective self.prob = cvxpy.Problem(objective, constraints)
def test_intro(self): """Test examples from cvxpy.org introduction. """ import numpy # cvx.Problem data. m = 30 n = 20 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m) # Construct the problem. x = cvx.Variable(n) objective = cvx.Minimize(cvx.sum_squares(A @ x - b)) constraints = [0 <= x, x <= 1] prob = cvx.Problem(objective, constraints) # The optimal objective is returned by p.solve(). prob.solve() # The optimal value for x is stored in x.value. print(x.value) # The optimal Lagrange multiplier for a constraint # is stored in constraint.dual_value. print(constraints[0].dual_value) ######################################## # Create two scalar variables. x = cvx.Variable() y = cvx.Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = cvx.Minimize(cvx.square(x - y)) # Form and solve problem. prob = cvx.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) ######################################## # Create two scalar variables. x = cvx.Variable() y = cvx.Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = cvx.Minimize(cvx.square(x - y)) # Form and solve problem. prob = cvx.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) self.assertEqual(prob.status, cvx.OPTIMAL) self.assertAlmostEqual(prob.value, 1.0) self.assertAlmostEqual(x.value, 1.0) self.assertAlmostEqual(y.value, 0) ######################################## # Replace the objective. prob = cvx.Problem(cvx.Maximize(x + y), prob.constraints) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 1.0, places=3) # Replace the constraint (x + y == 1). constraints = prob.constraints constraints[0] = (x + y <= 3) prob = cvx.Problem(prob.objective, constraints) print("optimal value", prob.solve()) self.assertAlmostEqual(prob.value, 3.0, places=2) ######################################## x = cvx.Variable() # An infeasible problem. prob = cvx.Problem(cvx.Minimize(x), [x >= 1, x <= 0]) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEqual(prob.status, cvx.INFEASIBLE) self.assertAlmostEqual(prob.value, np.inf) # An unbounded problem. prob = cvx.Problem(cvx.Minimize(x)) prob.solve() print("status:", prob.status) print("optimal value", prob.value) self.assertEqual(prob.status, cvx.UNBOUNDED) self.assertAlmostEqual(prob.value, -np.inf) ######################################## # A scalar variable. cvx.Variable() # Column vector variable of length 5. x = cvx.Variable(5) # Matrix variable with 4 rows and 7 columns. A = cvx.Variable((4, 7)) ######################################## import numpy # cvx.Problem data. m = 10 n = 5 numpy.random.seed(1) A = numpy.random.randn(m, n) b = numpy.random.randn(m) # Construct the problem. x = cvx.Variable(n) objective = cvx.Minimize(cvx.sum_squares(A @ x - b)) constraints = [0 <= x, x <= 1] prob = cvx.Problem(objective, constraints) print("Optimal value", prob.solve()) print("Optimal var") print(x.value) # A numpy matrix. self.assertAlmostEqual(prob.value, 4.14133859146) ######################################## # Positive scalar parameter. m = cvx.Parameter(nonneg=True) # Column vector parameter with unknown sign (by default). cvx.Parameter(5) # Matrix parameter with negative entries. G = cvx.Parameter((4, 7), nonpos=True) # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) ######################################## # Create parameter, then assign value. rho = cvx.Parameter(nonneg=True) rho.value = 2 # Initialize parameter with a value. rho = cvx.Parameter(nonneg=True, value=2) ######################################## import numpy # cvx.Problem data. n = 15 m = 10 numpy.random.seed(1) A = numpy.random.randn(n, m) b = numpy.random.randn(n) # gamma must be positive due to DCP rules. gamma = cvx.Parameter(nonneg=True) # Construct the problem. x = cvx.Variable(m) error = cvx.sum_squares(A @ x - b) obj = cvx.Minimize(error + gamma * cvx.norm(x, 1)) prob = cvx.Problem(obj) # Construct a trade-off curve of ||Ax-b||^2 vs. ||x||_1 sq_penalty = [] l1_penalty = [] x_values = [] gamma_vals = numpy.logspace(-4, 6) for val in gamma_vals: gamma.value = val prob.solve() # Use expr.value to get the numerical value of # an expression in the problem. sq_penalty.append(error.value) l1_penalty.append(cvx.norm(x, 1).value) x_values.append(x.value) ######################################## import numpy X = cvx.Variable((5, 4)) A = numpy.ones((3, 5)) # Use expr.size to get the dimensions. print("dimensions of X:", X.size) print("dimensions of sum(X):", cvx.sum(X).size) print("dimensions of A @ X:", (A @ X).size) # ValueError raised for invalid dimensions. try: A + X except ValueError as e: print(e)
[-0.0006, -0.0013, 0.0599, 0.0276, 0.0635, 0.0230, 0.0330, 0.0480], [-0.0007, -0.0006, 0.0276, 0.0296, 0.0266, 0.0215, 0.0207, 0.0299], [0.0001, -0.0022, 0.0635, 0.0266, 0.1025, 0.0427, 0.0399, 0.0660], [0.0001, -0.0010, 0.0230, 0.0215, 0.0427, 0.0321, 0.0199, 0.0322], [-0.0004, 0.0014, 0.0330, 0.0207, 0.0399, 0.0199, 0.0284, 0.0351], [-0.0004, -0.0015, 0.0480, 0.0299, 0.0660, 0.0322, 0.0351, 0.0800]], dtype=float) volatility = np.sqrt(np.diag(covar), dtype=float) w = cp.Variable(8) target_volatility = 0.8594 ret = returns.T @ w risk = cp.quad_form(w, covar) prob = cp.Problem(cp.Maximize(ret), [cp.sum(w) == 1, risk <= target_volatility]) #prob = cp.Problem(cp.Maximize(ret),[cp.sum(w) == 1, risk <= target_volatility, w >= 0]) # long only prob.solve() print("\nThe optimal return is {}".format(prob.value)) print("\nThe optimal weights is {}".format(w.value)) print("\nQuiz begins here") ''' Mean-Variance Analysis and CAPM Consider a market with d=3d=3 risky assets and 1 risk-free asset with the following parameters: mu = [6,2,4] V = [[8,-2,4],[-2,2,-2],[4,-2,8]]X10e-3, rf = 1% Note that the mean returns on the assets are in % but the variance is not Note that the asset returns and variances are not representative of realistic market conditions. ''' print(
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]
import cvxpy as cp import numpy as np import matplotlib.pyplot as plt my_data = np.genfromtxt('xy_train.csv', delimiter=',') y = my_data[:, 2] x1 = my_data[:, 0] x2 = my_data[:, 1] my_data = np.delete(my_data, 2, 1) C = 1 nn = 200 p = 2 epsilion_vec = cp.Variable(nn) beta0 = cp.Variable() beta_vec = cp.Variable(p) cost = 0.5 * cp.sum_squares(beta_vec) + C * cp.sum(epsilion_vec) prob = cp.Problem(cp.Minimize(cost), [ epsilion_vec >= 0, cp.multiply(y, my_data @ beta_vec + beta0) + epsilion_vec >= 1 ]) prob.solve() beta0 = beta0.value beta_vec = beta_vec.value print(beta_vec) x = np.linspace(-3, 4, num=50) yy = -(beta_vec[0] * x + beta0) / beta_vec[1] plt.scatter(x1[y == 1], x2[y == 1], marker='+') plt.scatter(x1[y == -1], x2[y == -1], marker='s') plt.plot(x, yy) plt.show()
def total_variation_plus_seasonal_filter(signal, c1=10, c2=500, residual_weights=None, tv_weights=None, use_ixs=None, periodic_detector=False, transition_locs=None, seas_max=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 ''' 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: index_set = ~np.isnan(signal) else: index_set = np.logical_and(use_ixs, ~np.isnan(signal)) s_hat = cvx.Variable(len(signal)) s_seas = cvx.Variable(len(signal)) s_error = cvx.Variable(len(signal)) c1 = cvx.Constant(value=c1) c2 = cvx.Constant(value=c2) #w = len(signal) / np.sum(index_set) if transition_locs is None: objective = cvx.Minimize( # (365 * 3 / len(signal)) * w * # cvx.sum(cvx.huber(cvx.multiply(residual_weights, s_error))) 10 * cvx.norm(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)) # + c2 * .1 * cvx.norm(cvx.diff(s_seas, k=1)) ) else: objective = cvx.Minimize( 10 * cvx.norm(cvx.multiply(residual_weights, s_error)) + c2 * cvx.norm(cvx.diff(s_seas, k=2))) constraints = [ signal[index_set] == s_hat[index_set] + s_seas[index_set] + s_error[index_set], cvx.sum(s_seas[:365]) == 0 ] if len(signal) > 365: constraints.append(s_seas[365:] - s_seas[:-365] == 0) if periodic_detector: constraints.append(s_hat[365:] - s_hat[:-365] == 0) if transition_locs is not None: loc_mask = np.ones(len(signal) - 1, dtype=bool) loc_mask[transition_locs] = False # loc_mask[transition_locs + 1] = False constraints.append(cvx.diff(s_hat, k=1)[loc_mask] == 0) if seas_max is not None: constraints.append(s_seas <= seas_max) problem = cvx.Problem(objective=objective, constraints=constraints) problem.solve() return s_hat.value, s_seas.value
def OPF(ppc, PloadProfile, QloadProfile, u): Yb = Ybus(ppc) #Ybus Matrix nbus = Yb.shape[0] #number of buses ngen = ppc['gen'].shape[0] ngenInfo = ppc['gen'].shape[1] genMatrix = np.zeros((nbus, ngenInfo)) for i in range(ngen): genMatrix[int(ppc['gen'][i][0]) - 1, :] = ppc['gen'][i] genBusIndex = ppc['gen'][:, 0].astype(int) - 1 ck2 = ppc['gencost'][:, 4] ck1 = ppc['gencost'][:, 5] ck0 = ppc['gencost'][:, 6] baseMVA = ppc['baseMVA'] Pload = np.zeros((nbus)) Qload = np.zeros((nbus)) Pload = PloadProfile / baseMVA Qload = QloadProfile / baseMVA Qmax = genMatrix[:, 3] / baseMVA Qmin = genMatrix[:, 4] / baseMVA Pmax = genMatrix[:, 8] / baseMVA Pmin = genMatrix[:, 9] / baseMVA Vmin = 0.94 * np.ones(nbus) Vmax = 1.06 * np.ones(nbus) e = np.eye(nbus, dtype=int) Yac = np.zeros((nbus, nbus, nbus), dtype=complex) OnesMatrix = np.zeros((nbus, nbus, nbus)) for k in range(nbus): a = e[:, k].reshape(-1, 1) b = e[k, :].reshape(1, -1) OnesMatrix[:, :, k] = np.matmul(a, b) Yac[:, :, k] = np.matmul(OnesMatrix[:, :, k], Yb.todense()) Y = np.zeros((2 * nbus, 2 * nbus, 2 * nbus)) Ybar = np.zeros((2 * nbus, 2 * nbus, 2 * nbus)) M = np.zeros((2 * nbus, 2 * nbus, 2 * nbus)) Z = np.zeros((nbus, nbus, nbus)) # print(Yb) # Y=sparse.csr_matrix(np.zeros((2*nbus,2*nbus,2*nbus))) # Ybar=sparse.csr_matrix(np.zeros((2*nbus,2*nbus,2*nbus))) # M=sparse.csr_matrix(np.zeros((2*nbus,2*nbus,2*nbus))) # Z=sparse.csr_matrix(np.zeros((nbus,nbus,nbus))) for k in range(nbus): a1 = np.real(Yac[:, :, k] + np.transpose(Yac[:, :, k])) a2 = np.imag(np.transpose(Yac[:, :, k]) - Yac[:, :, k]) a3 = np.imag(Yac[:, :, k] - np.transpose(Yac[:, :, k])) a4 = np.real(Yac[:, :, k] + np.transpose(Yac[:, :, k])) Y[:, :, k] = 0.5 * np.bmat([[a1, a2], [a3, a4]]) for k in range(nbus): a1 = np.imag(Yac[:, :, k] + np.transpose(Yac[:, :, k])) a2 = np.real(Yac[:, :, k] - np.transpose(Yac[:, :, k])) a3 = np.real(np.transpose(Yac[:, :, k]) - Yac[:, :, k]) a4 = np.imag(Yac[:, :, k] + np.transpose(Yac[:, :, k])) Ybar[:, :, k] = -0.5 * np.bmat([[a1, a2], [a3, a4]]) for k in range(nbus): M[:, :, k] = np.bmat([[OnesMatrix[:, :, k], Z[:, :, k]], [Z[:, :, k], OnesMatrix[:, :, k]]]) W = cp.Variable((2 * nbus, 2 * nbus), symmetric=True) a = cp.Variable((ngen, 1)) DeltaP = cp.Variable((nbus)) weight = 1000 constraints = [W >> 0] constraints += [ cp.trace(Y[:, :, k] @ W) <= u[k] * Pmax[k] - (Pload[k] + DeltaP[k]) for k in range(nbus) ] constraints += [ cp.trace(Y[:, :, k] @ W) >= u[k] * Pmin[k] - (Pload[k] + DeltaP[k]) for k in range(nbus) ] constraints += [ cp.trace(Ybar[:, :, k] @ W) <= Qmax[k] - Qload[k] for k in range(nbus) ] constraints += [ cp.trace(Ybar[:, :, k] @ W) >= Qmin[k] - Qload[k] for k in range(nbus) ] constraints += [ cp.trace(M[:, :, k] @ W) >= Vmin[k]**2 for k in range(nbus) ] constraints += [ cp.trace(M[:, :, k] @ W) <= Vmax[k]**2 for k in range(nbus) ] constraints += [ cp.bmat([[ ck1[np.where(genBusIndex == k)[0][0]] * (cp.trace(Y[:, :, k] @ W) + Pload[k] + DeltaP[k]) * baseMVA - a[np.where(genBusIndex == k)[0][0]] - ck0[np.where(genBusIndex == k)[0][0]], np.sqrt(ck2[np.where(genBusIndex == k)[0][0]]) * (cp.trace(Y[:, :, k] @ W) + Pload[k] + DeltaP[k]) * baseMVA ], [ np.sqrt(ck2[np.where(genBusIndex == k)[0][0]]) * (cp.trace(Y[:, :, k] @ W) + Pload[k] + DeltaP[k]) * baseMVA, -1 ]]) << 0 for k in genBusIndex ] prob = cp.Problem( cp.Minimize(cp.sum(a) + weight * cp.norm(DeltaP, 1) * baseMVA), constraints) prob.solve(verbose=True, warm_start=True) # Print result. print("..............................") print("Number of buses is", nbus) print("...............................") print("Number of generators is", ngen) print("...............................") print("The optimal value (total generation cost plus penalty) is") print(round(prob.value, 3), "= ", round(cp.sum(a).value, 3), " $", " + ", round((weight * cp.norm(DeltaP, 1) * baseMVA).value, 2), "penalty") print("The solution to power level of the generators is") for k in genBusIndex: print( np.round(((cp.trace(Y[:, :, k] @ W) + Pload[k] + DeltaP[k]) * baseMVA).value, 3), "MW") print("...............................") print("The solution to voltage of buses is") for k in range(nbus): print(np.round(cp.sqrt(cp.trace(M[:, :, k] @ W)).value, 3), "pu") print("...............................") print("The solutions to DeltaP is") for k in range(nbus): print(np.round((DeltaP[k] * baseMVA).value, 3), "MW") print("...............................") print("The eigenvalues of matrix W are") print(np.round(np.linalg.eig(W.value)[0], 2))
import pandas as pd #%% u = ut.get_cvx_utility() #%% t = get_data() r = get_target_return(t, months=3) r = r.fillna(0) # A voir si c'est necessaire? #%% n, p = r.shape w_cp = cp.Variable(p) λ = 1 reg = λ * cp.norm1(w_cp) objective = cp.Maximize(1 / n * cp.sum(u(r.values @ w_cp)) - reg) constraints = [w_cp >= 0, cp.sum(w_cp) == 1] prob = cp.Problem(objective, constraints) result = prob.solve(solver="ECOS", verbose=True) # %% w = pd.Series(w_cp.value, index=t.columns) w = w[w >= 0.01] # On ne garde que les titres avec un poids >= 1% w.sort_values(ascending=False).plot.bar() # %% w.index = w.index.map(fund_name) w.sort_values(ascending=False) """
def setup_solver_cvx(self): if self.dataterm != "W1": raise Exception("Only W1 dataterm is implemented in CVX") c = self.constvars imagedims = c['imagedims'] n_image = c['n_image'] d_image = c['d_image'] l_labels = c['l_labels'] m_gradients = c['m_gradients'] s_manifold = c['s_manifold'] l_shm = c['l_shm'] f_flat = self.data.odf.reshape(-1, l_labels).T f = np.array(f_flat.reshape((l_labels, ) + imagedims), order='C') normalize_odf(f, c['b']) f_flat = f.reshape(l_labels, n_image) self.cvx_x = Variable( ('p', (l_labels, d_image, n_image)), ('g', (n_image, m_gradients, s_manifold, d_image)), ('q0', (n_image, )), ('q1', (l_labels, n_image)), ('p0', (l_labels, n_image)), ('g0', (n_image, m_gradients, s_manifold)), ) self.cvx_y = Variable( ('u', (n_image, l_labels)), ('v', (n_image, l_shm)), ('w', (m_gradients, n_image, d_image, s_manifold)), ('w0', (m_gradients, n_image, s_manifold)), ('misc', (n_image * m_gradients, )), ) p, g, q0, q1, p0, g0 = [ cvxVariable(*a['shape']) for a in self.cvx_x.vars() ] self.cvx_vars = p + sum(g, []) + [q0, q1, p0] + g0 self.cvx_obj = cvx.Maximize(-cvx.vec(f_flat).T * cvx.vec(cvx.diag(c['b']) * p0) - cvx.sum(q0)) div_op = sparse_div_op(imagedims) self.cvx_dual = True self.cvx_constr = [] # u_constr for i in range(n_image): for k in range(l_labels): self.cvx_constr.append( c['b'][k]*(q0[i] + p0[k,i] \ - cvxOp(div_op, p[k], i)) - q1[k,i] >= 0) # v_constr for i in range(n_image): for k in range(l_shm): Yk = cvx.vec(c['Y'][:, k]) self.cvx_constr.append(-Yk.T * q1[:, i] == 0) # w_constr for j in range(m_gradients): Aj = c['A'][j, :, :] Bj = c['B'][j, :, :] Pj = c['P'][j, :] for i in range(n_image): for t in range(d_image): for l in range(s_manifold): self.cvx_constr.append( Aj*g[i][j][l,t] == sum([Bj[l,m]*p[Pj[m]][t,i] \ for m in range(3)])) # w0_constr for j in range(m_gradients): Aj = c['A'][j, :, :] Bj = c['B'][j, :, :] Pj = c['P'][j, :] for i in range(n_image): self.cvx_constr.append(Aj * g0[i][j, :].T == Bj * p0[Pj, i]) # additional inequality constraints for i in range(n_image): for j in range(m_gradients): self.cvx_constr.append(cvx.norm(g[i][j], 2) <= c['lbd']) self.cvx_constr.append(cvx.norm(g0[i][j, :], 2) <= 1.0)
r1 = np.sqrt(3.4*np.random.rand(Nt1,1)) # Radius t1 = 2*np.pi*np.random.rand(Nt1,1) # Angle testdata1 = np.concatenate((r1*np.cos(t1), r1*np.sin(t1)), axis=1) # Points r2 = np.sqrt(2.4*np.random.rand(Nt2,1)) # Radius t2 = 2*np.pi*np.random.rand(Nt2,1) # Angle testdata2 = np.concatenate((3+r2*np.cos(t2), r2*np.sin(t2)), axis=1) # points ## training linear SVM based on CVX optimizer X = np.concatenate((data1, data2), axis=0) y = np.concatenate((np.ones((N1, 1)), - np.ones((N2, 1))), axis=0) C = 2 w = cp.Variable((D, 1)) b = cp.Variable() epsilon = cp.Variable((N1+N2, 1)) objective = cp.Minimize(cp.sum(cp.square(w))*0.5 + cp.sum(cp.square(epsilon)*C)) constraints = [cp.multiply(y, (X @ w + b)) >= 1 - epsilon, epsilon >= 0] prob = cp.Problem(objective, constraints) prob.solve() print("status:", prob.status) print("optimal value", prob.value) print("optimal var w = {}, b = {}".format(w.value, b.value)) ## visualize decision boundary for training data d = 0.02 x1 = np.arange(np.min(X[:,0]), np.max(X[:,0]), d) x2 = np.arange(np.min(X[:,1]), np.max(X[:,1]), d) x1Grid, x2Grid = np.meshgrid(x1, x2) xGrid = np.stack((x1Grid.flatten('F'), x2Grid.flatten('F')), axis=1) scores1 = xGrid.dot(w.value) + b.value scores2 = -xGrid.dot(w.value) - b.value
# Define all the edges in the network for i in range(N): for j in range(N): # If an edge exists in the graph, create it and append its nodes if graph[i][j] != 0: new_edge = Edge(graph[i][j]) new_edge.connect(nodes[j], nodes[i]) edges.append(new_edge) # Once all the edges are defined, calculate the accumlation of the network for node in nodes: node.calculate_accumulation() # Create an array of all the flows, as this is what we will be trying to maximize flows = [] for edge in edges: flows.append(edge.flow) flow = np.array(flows) constraints = [] # We create a seperate function for the node contraints since we want to avoid # accidentally contraining the source node for i in range(len(nodes)): if i != source_node: constraints.append(nodes[i].constraints()) for edge in edges: constraints.append(edge.constraints()) obj = cvx.Maximize(cvx.sum(flows)) prob = cvx.Problem(obj, constraints=constraints) print("The max sum of all a_i is", prob.solve())
N = X1.shape[0] M = X0.shape[0] X = np.vstack([X1, X0]) y = np.vstack([np.ones([N,1]), -np.ones([M,1])]) X = np.asmatrix(X) y = np.asmatrix(y) m = N+M z = np.hstack([np.ones([m,1]), np.sqrt(2)*X[:,0], np.sqrt(2)*X[:,1], np.square(X[:,0]), np.sqrt(2)*np.multiply(X[:,0],X[:,1]),np.square(X[:,1])]) w = cvx.Variable([6,1]) obj = cvx.Minimize(cvx.sum(cvx.logistic(-cvx.multiply(y,z*w)))) prob = cvx.Problem(obj).solve() w = w.value # to plot [X1gr, X2gr] = np.meshgrid(np.arange(-3,3,0.1), np.arange(-3,3,0.1)) Xp = np.hstack([X1gr.reshape(-1,1), X2gr.reshape(-1,1)]) Xp = np.asmatrix(Xp) m = Xp.shape[0] Zp = np.hstack([np.ones([m,1]), np.sqrt(2)*Xp[:,0], np.sqrt(2)*Xp[:,1], np.square(Xp[:,0]), np.sqrt(2)*np.multiply(Xp[:,0], Xp[:,1]), np.square(Xp[:,1])]) q = Zp*w # linear boundary B = []
## Nonnegtivity constraints constraints = [y >= 0, x >= 0] constraints.append(y[0] == 0) ## Flow constraint, ie what I produce + what I have in inventory ## should equal demand + inventory for next period for s in scenarios: for t in range(1, T + 1): constraints.append(x[sv(s, t)] + y[sv(s, t - 1)] == Xi[sp(s, t)] + y[sv(s, t)]) ## Objective: for all scenarios, for all time periods add cost objective = cp.Minimize( cp.sum([ prob(s) * (x[sv(s, t)] * p[t - 1] + y[sv(s, t)] * h[t - 1]) for t in range(1, T + 1) for s in scenarios ])) ## Define linear problem and solve prob = cp.Problem(objective, constraints) result = prob.solve(solver=cp.GLPK) ## Save results og_out = sys.stdout with open('results.txt', 'w') as f: sys.stdout = f print('Optimal objective value: %f' % result) for s in scenarios: print() print()
def fit_ellipse_eps_insensitive(x, y): """ fit ellipoid using epsilon-insensitive loss """ x = numpy.array(x) y = numpy.array(y) print "shapes", x.shape, y.shape assert len(x) == len(y) N = len(x) D = 5 dat = numpy.zeros((N, D)) dat[:,0] = x*x dat[:,1] = y*y #dat[:,2] = y*x dat[:,2] = x dat[:,3] = y dat[:,4] = numpy.ones(N) print dat.shape dat = cvxpy.matrix(dat) #### parameters # data X = cvxpy.parameter(N, D, name="X") # parameter for eps-insensitive loss eps = cvxpy.parameter(1, name="eps") #### varibales # parameter vector theta = cvxpy.variable(D, name="theta") # dim = (N x 1) s = cvxpy.variable(N, name="s") t = cvxpy.variable(N, name="t") # simple objective objective = cvxpy.sum(t) # create problem p = cvxpy.program(cvxpy.minimize(objective)) # add constraints # (N x D) * (D X 1) = (N X 1) p.constraints.append(X*theta <= s) p.constraints.append(-X*theta <= s) p.constraints.append(s - eps <= t) p.constraints.append(0 <= t) #p.constraints.append(theta[4] == 1) # trace constraint p.constraints.append(theta[0] + theta[1] == 1) ###### set values X.value = dat eps.value = 0.0 #solver = "mosek" #p.solve(lpsolver=solver) p.solve() cvxpy.printval(theta) w = numpy.array(cvxpy.value(theta)) #cvxpy.printval(s) #cvxpy.printval(t) ## For clarity, fill in the quadratic form variables A = numpy.zeros((2,2)) A[0,0] = w[0] A.ravel()[1:3] = 0#w[2] A[1,1] = w[1] bv = w[2:4] c = w[4] ## find parameters z, a, b, alpha = util.conic2parametric(A, bv, c) return z, a, b, alpha
elif sys.platform.startswith('linux'): FontPath = '/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf' jpfont = FontProperties(fname=FontPath) #%% 収益率データの読み込み R = pd.read_csv('asset_return_data.csv', index_col=0) T = R.shape[0] N = R.shape[1] Mu = R.mean().values Return = R.values / T #%% 期待ショートフォール最小化問題の設定 Weight = cvx.Variable(N) Deviation = cvx.Variable(T) VaR = cvx.Variable() inv_Alpha = cvx.Parameter(nonneg=True) Target_Return = cvx.Parameter(nonneg=True) Risk_ES = cvx.sum(Deviation) * inv_Alpha - VaR Opt_Portfolio = cvx.Problem(cvx.Minimize(Risk_ES), [Weight.T @ Mu == Target_Return, cvx.sum(Weight) == 1.0, Weight >= 0.0, Deviation >= 0.0, Return @ Weight - VaR / T + Deviation >= 0.0]) #%% 最小ESフロンティアの計算 V_Alpha = np.array([0.05, 0.10, 0.25, 0.50]) V_Target = np.linspace(Mu.min(), Mu.max(), num=250) V_Risk = np.zeros((V_Target.shape[0], V_Alpha.shape[0])) for idx_col, Alpha in enumerate(V_Alpha): inv_Alpha.value = 1.0 / Alpha for idx_row, Target_Return.value in enumerate(V_Target): Opt_Portfolio.solve(solver=cvx.ECOS) V_Risk[idx_row, idx_col] = Risk_ES.value
from IPython.core import ultratb sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=1) m = 10 n = 2 npr.seed(0) x = npr.random(m) import cvxpy as cp import numdifftools as nd y = cp.Variable(m) obj = cp.Minimize(-x * y - cp.sum(cp.entr(y)) - cp.sum(cp.entr(1. - y))) cons = [0 <= y, y <= 1, cp.sum(y) == n] prob = cp.Problem(obj, cons) prob.solve(cp.SCS, verbose=True) assert 'optimal' in prob.status y_cp = y.value x = Variable(torch.from_numpy(x), requires_grad=True) x = torch.stack([x, x]) y = LML(N=n)(x) np.testing.assert_almost_equal(y[0].data.numpy(), y_cp, decimal=3) dy0, = grad(y[0, 0], x) dy0 = dy0.squeeze()
# discretize outcomes of R1 and R2 r = np.linspace(r_min, r_max, n) # marginal distributions p1 = norm(mu1, sigma1).pdf(r) p1 = p1 / np.sum(p1) p2 = norm(mu2, sigma2).pdf(r) p2 = p2 / np.sum(p2) # form mask of region where R1 + R2 <= 0 r1p = r * np.ones((1, n)) r2p = r1p.T loss_mask = (r1p + r2p <= 0).T P = cp.Variable((n, n)) objective = cp.Maximize(cp.sum(cp.sum(P[loss_mask]))) constraints = [ P >= 0, cp.sum(P, 1) == p1, cp.sum(P, 0) == p2, (r - mu1).T @ P @ (r - mu2) == rho * sigma1 * sigma2 ] prob = cp.Problem(objective, constraints) result = prob.solve() print(result) P = P.value X = np.linspace(r_min, r_max, n) Y = np.linspace(r_min, r_max, n) X, Y = np.meshgrid(X, Y) Z = P
from cvxpy import Minimize, Variable, Problem, max, abs, sum import numpy as np n = 3 N = 30 A = np.matrix([[-1, 0.4, 0.8], [1, 0, 0], [0, 1, 0]]) b = np.matrix([1, 0, 0.3]).T x0 = zeros((n, 1)) xdes = np.matrix([7, 2, -6]).T x = Variable(n, N + 1) u = Variable(1, N) objective = Minimize(sum(max(abs(u), 2 * abs(u) - 1))) constraints1 = [x[:, 1 : N + 1] == A * x[:, 0:N] + b * u] constraints2 = [x[:, 0] == x0] constraints3 = [x[:, N] == xdes] constraints = constraints1 + constraints2 + constraints3 prob1 = Problem(objective, constraints) prob1.solve() print u.value step(range(30), u.value.T)
def setup_solver_cvx(self): c = self.constvars imagedims = c['imagedims'] n_image = c['n_image'] d_image = c['d_image'] l_labels = c['l_labels'] m_gradients = c['m_gradients'] s_manifold = c['s_manifold'] l_shm = c['l_shm'] self.cvx_x = Variable( ('p', (l_labels, d_image, n_image)), ('g', (n_image, m_gradients, s_manifold, d_image)), ('q0', (n_image,)), ('q1', (l_labels, n_image)), ('q2', (l_labels, n_image)), ) self.cvx_y = Variable( ('u1', (n_image, l_labels)), ('v', (n_image, l_shm)), ('w', (m_gradients, n_image, d_image, s_manifold)), ('misc', (n_image*m_gradients,)), ) p, g, q0, q1, q2 = [cvxVariable(*a['shape']) for a in self.cvx_x.vars()] self.cvx_vars = p + sum(g,[]) + [q0,q1,q2] self.cvx_obj = cvx.Maximize( 0.5*cvx.sum(cvx.diag(c['b'])*cvx.square(c['f'].T)) - 0.5*cvx.sum( cvx.diag(1.0/c['b'])*cvx.square(q2 + cvx.diag(c['b'])*c['f'].T) ) - cvx.sum(q0)) div_op = sparse_div_op(imagedims) self.cvx_dual = True self.cvx_constr = [] # u1_constr for i in range(n_image): for k in range(l_labels): self.cvx_constr.append( c['b'][k]*(q0[i] - cvxOp(div_op, p[k], i)) \ - q1[k,i] >= 0) # v_constr for i in range(n_image): for k in range(l_shm): Yk = cvx.vec(c['Y'][:,k]) self.cvx_constr.append(Yk.T*(c['M'][k]*q2[:,i] + q1[:,i]) == 0) # w_constr for j in range(m_gradients): Aj = c['A'][j,:,:] Bj = c['B'][j,:,:] Pj = c['P'][j,:] for i in range(n_image): for t in range(d_image): for l in range(s_manifold): self.cvx_constr.append( Aj*g[i][j][l,t] == sum([Bj[l,m]*p[Pj[m]][t,i] \ for m in range(3)])) # additional inequality constraints for i in range(n_image): for j in range(m_gradients): self.cvx_constr.append(cvx.norm(g[i][j], 2) <= c['lbd'])
def explicit_minimizer(args): # print("len(args):",len(args)) try: if len(args) == 3: (D, y, pshape) = args # y = np.dot(x,x) x_val = None scale = 2 while x_val is None and scale < 16: x = cp.Variable(D.shape[1]) objective = cp.Minimize( cp.sum(cp.abs(x)) / (float(D.shape[1])**scale)) constraints = [D * x - y.flatten() == 0] prob = cp.Problem(objective, constraints) result = prob.solve(verbose=False, solver='SCS') # result = prob.solve(verbose=False, solver='OSQP') # result = prob.solve(verbose=False) x_val = x.value scale += 0.1 if x_val is None: print("x.value is None. scale:" + str(scale)) # print 'optimal val :', result # return np.append(np.zeros(pshape), x.value) # .reshape(self.pshape+D.shape[1],1) return x.value else: (D, y, params, K, cshape, pshape) = args a = np.append(np.diag(np.ones(pshape)), np.zeros((pshape, D.shape[1] - pshape)), axis=1) for i in range(1, K): b = np.zeros((1, D.shape[1])) b[0, pshape + (i - 1) * cshape:pshape + (i) * cshape] = 1 # print("a.shape, b.shape:", a.shape, b.shape) a = np.append(a, b, axis=0) scale = 2 x_val = None while x_val is None and scale < 16: x = cp.Variable(D.shape[1]) objective = cp.Minimize( cp.sum(cp.abs(x)) / (float(D.shape[1])**scale)) # /float(D.shape[1])+cp.sum_square(a[:self.pshape]*x-params[:self.pshape])/float(self.pshape)+cp.sum_square(a[self.pshape:]*x-params[self.pshape:])/float(K-1)) constraints = [D * x - y.flatten() == 0] constraints = constraints + [a * x - params.flatten() == 0] prob = cp.Problem(objective, constraints) result = prob.solve(solver='SCS') # result = prob.solve() x_val = x.value scale += 0.1 if x_val is None: print("x.value is None. scale:" + str(scale)) # print 'optimal val:', result return x.value # .reshape(D.shape[1],1) except Exception as e: print("???", str(e)) print 'str(e):', str(e) print 'repr(e):\t', repr(e) print 'e.message:\t', e.message print 'traceback.print_exc():', traceback.print_exc() print 'traceback.format_exc():\n%s' % traceback.format_exc() print("D.shape, y.shape", D.shape, y.shape) exit()
def MinimizeConcentration(self, metabolite_index=None, concentration_bounds=None): """Finds feasible concentrations minimizing the concentration of metabolite i. Args: metabolite_index: the index of the metabolite to minimize. if == None, minimize the sum of all concentrations. concentration_bounds: the Bounds objects setting concentration bounds. """ my_bounds = concentration_bounds or self.DefaultConcentrationBounds() ln_conc = cvxpy.variable(m=1, n=self.Ncompounds, name='lnC') ln_conc_lb, ln_conc_ub = my_bounds.GetLnBounds(self.compounds) constr = [cvxpy.geq(ln_conc, cvxpy.matrix(ln_conc_lb)), cvxpy.leq(ln_conc, cvxpy.matrix(ln_conc_ub))] my_dG0_r_primes = np.matrix(self.dG0_r_prime) # Make flux-based constraints on reaction free energies. # All reactions must have negative dGr in the direction of the flux. # Reactions with a flux of 0 must be in equilibrium. S = np.matrix(self.S) for i, flux in enumerate(self.fluxes): curr_dg0 = my_dG0_r_primes[0, i] # if the dG0 is unknown, this reaction imposes no new constraints if np.isnan(curr_dg0): continue rcol = cvxpy.matrix(S[:, i]) curr_dgr = curr_dg0 + RT * ln_conc * rcol if flux == 0: constr.append(cvxpy.eq(curr_dgr, 0.0)) else: constr.append(cvxpy.leq(curr_dgr, 0.0)) objective = None if metabolite_index is not None: my_conc = ln_conc[0, metabolite_index] objective = cvxpy.minimize(cvxpy.exp(my_conc)) else: objective = cvxpy.minimize( cvxpy.sum(cvxpy.exp(ln_conc))) name = 'CONC_OPT' if metabolite_index: name = 'CONC_%d_OPT' % metabolite_index problem = cvxpy.program(objective, constr, name=name) optimum = problem.solve(quiet=True) """ status = problem.solve(quiet=True) if status != 'optimal': status = optimized_pathway.OptimizationStatus.Infeasible( 'Pathway infeasible given bounds.') return ConcentrationOptimizedPathway( self._model, self._thermo, my_bounds, optimization_status=status) """ opt_ln_conc = np.matrix(np.array(ln_conc.value)) result = ConcentrationOptimizedPathway( self._model, self._thermo, my_bounds, optimal_value=optimum, optimal_ln_metabolite_concentrations=opt_ln_conc) return result
def __init__(self, ruleset: RuleSet) -> None: # set membership matrix; how many copies of a given tile are present in # a given set. Each column is a set, each row a tile slen = len(ruleset.sets) smatrix = np.zeros((ruleset.tile_count, slen), dtype=np.uint8) np.add.at( smatrix, ( np.fromiter(chain.from_iterable(ruleset.sets), np.uint8) - 1, np.repeat( np.arange(slen), np.fromiter(map(len, ruleset.sets), np.uint16) ), ), 1, ) # Input parameters: counts for each tile on the table and on the rack table = self.table = cp.Parameter(ruleset.tile_count, "table", nonneg=True) rack = self.rack = cp.Parameter(ruleset.tile_count, "rack", nonneg=True) # Output variables: counts per resulting set, and counts per # tile taken from the rack to be added to the table. sets = self.sets = cp.Variable(len(ruleset.sets), "sets", integer=True) tiles = self.tiles = cp.Variable(ruleset.tile_count, "tiles", integer=True) # Constraints for the optimisation problem numbertiles = tiles joker_constraints = [] if ruleset.jokers: numbertiles, jokers = tiles[:-1], tiles[-1] joker_constraints = [ # You can place multiple jokers from your rack, but there are # never more than *ruleset.jokers* of them. 0 <= jokers, jokers <= ruleset.jokers, ] constraints = [ # placed sets can only be taken from selected rack tiles and what # was already placed on the table. smatrix @ sets == table + tiles, # the selected tiles must all come from your rack tiles <= rack, # A given set could appear multiple times, but never more than # *repeats* times. 0 <= sets, sets <= ruleset.repeats, # You can place multiple tiles with the same colour and number # but there are never more than *ruleset.repeats* of them. 0 <= numbertiles, numbertiles <= ruleset.repeats, # variable joker constraints for the current ruleset *joker_constraints, ] p: dict[SolverMode, cp.Problem] = {} # Problem solver maximising number of tiles placed p[SolverMode.TILE_COUNT] = cp.Problem(cp.Maximize(cp.sum(tiles)), constraints) # Problem solver maximising the total value of tiles placed tilevalue = np.tile( np.arange(ruleset.numbers, dtype=np.uint16) + 1, ruleset.colours ) if ruleset.jokers: tilevalue = np.append(tilevalue, 0) p[SolverMode.TOTAL_VALUE] = cp.Problem( cp.Maximize(cp.sum(tiles @ tilevalue)), constraints ) # Problem solver used for the opening move ("initial meld"). # Initial meld scoring is based entirely on the sets formed, and must # be equal to or higher than the minimal score. Maximize the tile count # _without jokers_. setvalue = np.array(ruleset.setvalues, dtype=np.uint16) initial_constraints = [ *constraints, sets @ setvalue >= ruleset.min_initial_value, ] p[SolverMode.INITIAL] = cp.Problem( cp.Maximize(cp.sum(numbertiles)), initial_constraints ) self._problems = p
def mu_1(J): n, m = J.shape assert n == m return max([J[i,i] + sum([abs(J[i,j]) for j in range(n)]) - abs(J[i,i]) for i in range(n)])
def logistic_loss(weights, Y, K, reg = 1e-5): norm = cp.quad_form(weights, K) n = weights.shape[0] f_x = K@weights return norm*reg/2 + cp.sum(cp.logistic(f_x) - cp.multiply(f_x, Y), axis = 0)/n
def main(): T = 75 N = p.N M = p.M target = np.array([0, 0, 0, np.pi]) A, B = p.linearize(target, np.zeros((M))) Qf = 1000 * np.eye(N) Qf[0, 0] = 10 Qf[1, 1] = 10 R = 1 * np.eye(M) K, _, _ = dlqr(A, B, Qf, R) print("K", K) print("A", A) print("B", B) sleep(1) U = cp.Variable((T, M)) X = cp.Variable((T + 1, N)) control = np.zeros((T, M)) states = np.zeros((T + 1, N)) atempt = 0 while 1: print(atempt) atempt += 1 const = [] const.append(X[0, :] == np.zeros((N))) p.state = np.array(init) states[0, :] = p.state for i in range(T): # print(i) A, B = p.linearize(p.state, control[i]) p.next(control[i]) # print(p.state) states[i + 1, :] = p.state const.append(X[i + 1, :] == A @ X[i, :] + B @ U[i, :]) sleep(0.010) # if atempt > 100: # sleep(0.020) d.update(p.state[1], p.state[3]) if atempt > 100: for i in range(300): u = np.clip(K @ (p.state - target), -1, 1) p.next(-u) sleep(0.02) d.update(p.state[1], p.state[3]) X_p = X + states U_p = U + control const.append(U_p <= 1) const.append(U_p >= -1) const.append(X_p[:, 1] <= 2) const.append(X_p[:, 1] >= -2) const.append(U[:, 0] >= -0.1) const.append(U[:, 0] <= 0.1) # cost_pend = 1000*cp.sum((X_p[-1:,0] - np.pi)**2) + \ # 1000*cp.sum((X_p[-1:,1] )**2) +\ # 10* cp.sum((X_p[:,0]- np.pi)**2) + \ # 0 # 1* cp.sum((U_p)**2) # 0.1* cp.sum((U_p[1:] - U_p[:-1])**2) cost =1000* cp.sum((X_p[-1,:] - target)**2) + \ 1000* cp.sum((X_p[-2,:] - target)**2) + \ 1000* cp.sum((X_p[-3,:] - target)**2) + \ 10* cp.sum((X_p[:,3] - np.pi)**2) + \ 10* cp.sum((X_p[:,2] )**2) +\ 2* cp.sum((X_p[:,1] )**2) +\ 2* cp.sum((X_p[:,0] )**2) +\ 1* cp.sum((U_p[:,0] )**2) +\ 0 prob = cp.Problem(cp.Minimize(cost), const) prob.solve() # print(U.value) # for a,b in zip(U.value+control, X.value+states): # print("angle", b[0], "vel", b[1], "act", a[0]) control += U.value sleep(0.1)
def to_cvxpy(self, weight_var, weight_var_series, init_weights): constraints = [] locs = weight_var_series.index.get_indexer( self.risk_model_loadings.index) # BasicMaterials constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['BasicMaterials']) >= self.min_basic_materials) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['BasicMaterials']) <= self.max_basic_materials) # ConsumerCyclical constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ConsumerCyclical']) >= self.min_consumer_cyclical) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ConsumerCyclical']) <= self.max_consumer_cyclical) # FinancialServices constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['FinancialServices']) >= self.min_financial_services) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['FinancialServices']) <= self.max_financial_services) # RealEstate constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['RealEstate']) >= self.min_real_estate) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['RealEstate']) <= self.max_real_estate) # ConsumerDefensive constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ConsumerDefensive']) >= self.min_consumer_defensive) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ConsumerDefensive']) <= self.max_consumer_defensive) # HealthCare constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['HealthCare']) >= self.min_health_care) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['HealthCare']) <= self.max_health_care) # Utilities constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Utilities']) >= self.min_utilities) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Utilities']) <= self.max_utilities) # CommunicationServices constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['CommunicationServices']) >= self.min_communication_services) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['CommunicationServices']) <= self.max_communication_services) # Energy constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Energy']) >= self.min_energy) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Energy']) <= self.max_energy) # Industrials constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Industrials']) >= self.min_industrials) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Industrials']) <= self.max_industrials) # Technology constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Technology']) >= self.min_technology) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Technology']) <= self.max_technology) # Momentum constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Momentum']) >= self.min_momentum) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Momentum']) <= self.max_momentum) # Value constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Value']) >= self.min_value) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Value']) <= self.max_value) # Size constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Size']) >= self.min_size) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Size']) <= self.max_size) # ShortTermReversal constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ShortTermReversal']) >= self.min_short_term_reversal) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['ShortTermReversal']) <= self.max_short_term_reversal) # Volatility constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Volatility']) >= self.min_volatility) constraints.append( cvx.sum(weight_var[locs] * self.risk_model_loadings['Volatility']) <= self.max_volatility) return constraints