def Log_SVM(absDataOrigin, absLabels, cmpDataOrigin, cmpLabels, absWeight, lamda): # This function combined both logistic regression for absolute labels and SVM for comparison labels. # Parameter: # ------------ # absDataOrigin : N by d numpy matrix where N the number of absolute label data and d is the dimension of data # abslabels : (N,) numpy array, +1 means positive label and -1 represents negative labels # lamda : weight on L1 penalty. Large lamda would have more zeros in beta. # Return: # ------------ # beta : the logistic regression model parameter # const : the logistic regression global constant. cmpWeight = 1.0 - absWeight absN, d = np.shape(absDataOrigin) cmpN, _ = np.shape(cmpDataOrigin) beta = cp.Variable(d) const = cp.Variable(1) objective = absWeight*cp.sum_entries(cp.logistic(cp.mul_elemwise(absLabels, absDataOrigin*beta+const))\ -cp.mul_elemwise(absLabels, absDataOrigin*beta+const))+ \ cmpWeight * cp.sum_entries(cp.pos(1 - cp.mul_elemwise(cmpLabels, cmpDataOrigin * beta + const))) \ + lamda * cp.norm(beta, 1) prob = cp.Problem(cp.Minimize(objective)) prob.solve(solver=cp.SCS) return beta.value, const.value
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 cvxpy_get_var_and_objective(mode,dim_v,sample_n,weight,Phi,Psi,y): if 'reg' in mode: alpha = get_key('alpha',mode,float) try: #version of cvxpy (no downward compatibility) var = cp.Variable(dim_v,sample_n) if weight.size != 0: objective = cp.Minimize( alpha * cp.norm(Phi*var-y) + cp.sum_entries(cp.mul_elemwise(weight,cp.abs(Psi*var)))) else: objective = cp.Minimize( alpha * cp.norm(Phi*var-y) + cp.norm(Psi*var,1)) except: var = cp.Variable((dim_v,sample_n)) if weight.size != 0: objective = cp.Minimize( alpha * cp.norm(Phi*var-y) + cp.sum(cp.multiply(weight,cp.abs(Psi*var)))) else: objective = cp.Minimize( alpha * cp.norm(Phi*var-y) + cp.norm(Psi*var,1)) elif 'bestkTermApprox' in mode: x = y objective = cp.Minimize(cp.norm(Psi*var-x)) else: try: #version of cvxpy (no downward compatibility) var = cp.Variable(dim_v,sample_n) if weight.size != 0: objective = cp.Minimize( cp.sum_entries(cp.mul_elemwise(weight,cp.abs(Psi*var)))) else: objective = cp.Minimize(cp.norm(Psi*var,1)) except: var = cp.Variable((dim_v,sample_n)) if weight.size != 0: objective = cp.Minimize(cp.sum(cp.multiply(weight,cp.abs(Psi*var)))) else: objective = cp.Minimize(cp.norm(Psi*var,1)) return (var,objective)
def _initialize_probs(self, A, k, missing_list, regX, regY): # useful parameters m = A[0].shape[0] ns = [a.shape[1] for a in A] if missing_list == None: missing_list = [[]] * len(self.L) # initialize A, X, Y B = self._initialize_A(A, missing_list) X0, Y0 = self._initialize_XY(B, k, missing_list) self.X0, self.Y0 = X0, Y0 # cvxpy problems Xv, Yp = cp.Variable(m, k), [cp.Parameter(k + 1, ni) for ni in ns] Xp, Yv = cp.Parameter(m, k + 1), [cp.Variable(k + 1, ni) for ni in ns] Xp.value = copy(X0) for yj, yj0 in zip(Yp, Y0): yj.value = copy(yj0) onesM = cp.Constant(ones((m, 1))) obj = sum(L(Aj, cp.mul_elemwise(mask, Xv*yj[:-1,:] \ + onesM*yj[-1:,:]) + offset) + ry(yj[:-1,:])\ for L, Aj, yj, mask, offset, ry in \ zip(self.L, A, Yp, self.masks, self.offsets, regY)) + regX(Xv) pX = cp.Problem(cp.Minimize(obj)) pY = [cp.Problem(cp.Minimize(\ L(Aj, cp.mul_elemwise(mask, Xp*yj) + offset) \ + ry(yj[:-1,:]) + regX(Xp))) \ for L, Aj, yj, mask, offset, ry in zip(self.L, A, Yv, self.masks, self.offsets, regY)] self.probX = (Xv, Yp, pX) self.probY = (Xp, Yv, pY)
def image_inpainting_sparse(vu, depths, nH, nW): depth_map_sparse = np.zeros((nH, nW)) vu_int = (vu + 0.5).astype(np.int32) vu_int[0] = np.maximum(np.minimum(vu_int[0], nH - 1), 0) vu_int[1] = np.maximum(np.minimum(vu_int[1], nW - 1), 0) depth_map_sparse[vu_int[0], vu_int[1]] = depths Known = np.zeros((nH, nW)) Known[vu_int[0], vu_int[1]] = 1 U = cp.Variable(nH, nW) obj = cp.Minimize(cp.tv(U)) constraints = [ cp.mul_elemwise(Known, U) == cp.mul_elemwise(Known, depth_map_sparse) ] prob = cp.Problem(obj, constraints) # Use SCS to solve the problem. prob.solve(verbose=True, solver=cp.SCS) depth_map = U.value return depth_map
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 fit(self, X, y, c=1, sigma=0.1, k_matrix=None): """ Train a SVM classifier using Dual problem and Gaussian Kernel. """ self.X_train = X self.y_train = y # parameters self.c = c self.sigma = sigma self.D = X.shape[1] # number of features self.N = X.shape[0] # number of samples # instantiate variables in cvxpy W = cvx.Variable(self.D) b = cvx.Variable() Delta = cvx.Variable(self.N) # Lagrangian multipliers # Set up the dual problem if k_matrix is None: k_matrix = self.kernel_matrix(X, sigma) first_term = cvx.quad_form(cvx.mul_elemwise(y, Delta), k_matrix) second_term = cvx.sum_entries(Delta) loss = -0.5 * first_term + second_term # add constraints constraints = [Delta >= 0, Delta <= c] dual_sum = cvx.sum_entries(cvx.mul_elemwise(y, Delta)) constraints.append(dual_sum == 0) # instantiate the problem prob = cvx.Problem(cvx.Maximize(loss), constraints) prob.solve(max_iters=200) # prob.solve(solver='CVXOPT', kktsolver='ROBUST_KKTSOLVER') # find bias term self.Delta = Delta.value self.find_bias(Delta, X, y) return self.Delta, self.b
def set_objective(model, optvar, optpar): """ This function defines the objective function, which is a minimization of sum of the losses in the network (r*I) and a term representing the distance to the set-points, either with a L-1 or L-2 norm Args : | model : a Model object containing the data necessary for the simulation | optvar : a dictionary containing cvxpy Variable objects | optpar : a dictionary containing cvxpy Parameter objects | objtype : a string representing the type of objective (can take the values 'L1', 'L2' and 'Loss') Returns : A cvxpy Objective object """ objtype = 'L1' if objtype == 'L1': obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(model.NetworkModel.resistance, optvar['I'])) + cvx.norm(optvar['Ppv'] - optpar['pv_set_points'], 1) + cvx.norm(optvar['Pst'] - optpar['storage_set_points'], 1)) elif objtype == 'L2': obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(model.NetworkModel.resistance, optvar['I'])) + cvx.norm(optvar['Ppv'] - optpar['pv_set_points'], 2) + cvx.norm(optvar['Pst'] - optpar['storage_set_points'], 2)) elif objtype == 'Loss': obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(model.NetworkModel.resistance, optvar['I']))) else: raise IOError("Invalid objtype parameter passed to opf_solve, choose between L1, L2 and Loss") return obj
def main(): x = [[1,0],[0,1],[0,-1],[-1,0],[0,2],[0,-2],[-2,0]] y = [-1, -1, -1, 1, 1, 1, 1] Q = np.empty((7,7)) for i in range(0,7): for j in range(0,7): Q[i,j] = y[i]*y[j]*K(x[i],x[j]) p = np.array([-1] * 7) alpha = cp.Variable(7) objective = cp.Minimize(0.5 * cp.quad_form(alpha, Q) + cp.sum_entries(cp.mul_elemwise(p, alpha))) constraints = [cp.sum_entries(cp.mul_elemwise(y,alpha)) == 0] for i in range(0,7): constraints.append(alpha[i]>=0) prob = cp.Problem(objective, constraints) prob.solve() z = [[1,math.sqrt(2)*x1, math.sqrt(2)*x2, x1 ** 2, x2 ** 2] for (x1,x2) in x] w = [0] * 5 for i in range(0,7): w += np.dot(float(alpha.value[i]) * y[i], z[i]) print "w=",sum(w) #choose any support vector (z,y) to calculate the b print "b=",y[1] - np.dot(w, z[1]) print "status:", prob.status print "optimal value", prob.value print alpha.value print sum(alpha.value)
def updateEi2(self,sigmai,ei_): sigmai=np.array(sigmai) ei_=np.array(ei_) ei=cvx.Variable(I) si=cvx.Variable(I) constraints=[0<=si] slacost=cvx.sum_entries(cvx.mul_elemwise(self.lamb,cvx.inv_pos(self.mu-cvx.mul_elemwise(self.lamb,cvx.inv_pos(self.S-si))))) watcost=np.array(self.omegawat)*si for i in range(I): constraints+=[ei[i]==self.gamma[i]*si[i]] objective = cvx.Minimize(-np.transpose(sigmai)*ei+self.omegadc*(slacost+watcost)+rho/2*cvx.sum_squares(ei-np.array(ei_))) prob = cvx.Problem(objective, constraints) result = prob.solve(solver=slv) #print ei.value.A1 #print result self.e=ei.value.A1 mins=self.minServer() maxe=np.zeros(I) for i in range(I): maxe[i]=(self.S[i]-mins[i])*self.gamma[i] if self.e[i]<0: self.e[i]==0 else: if self.e[i]>maxe[i]: self.e[i]=maxe[i] print('\nWarning: reducing si lower than minimum active server!!!!') #print("ei:",self.e) self.setSwitchSever(self.e)
def main(): TARGET = 0 y = [] x = [] for line in open("features.train"): vec = line.strip().split() x.append([float(vec[1]),float(vec[2])]) if int(float(vec[0])) == TARGET: y.append(1) else: y.append(-1) Q = np.empty((len(x),len(x))) p = [-1] * len(x) for i in range(0,len(x)): for j in range(0,len(x)): Q[i,j] = y[i]*y[j] * K(x[i],x[j]) alpha = cp.Variable(len(x)) C = 0.01 objective = cp.Minimize(0.5 * cp.quad_form(alpha,Q) + cp.sum_entries(cp.mul_elemwise(p,alpha))) constraints = [cp.sum_entries(cp.mul_elemwise(y,alpha)) == 0] for i in range(0,len(x)): constraints.append(alpha[i]>=0) prob = cp.Problem(objective, constraints) prob.solve() print "sum(alpha)=",sum(alpha.value) #choose any support vector (z,y) to calculate the b print "status:", prob.status print "optimal value", prob.value
def fit(self, X, Y, sample_weights=None, verbose=False): assert (X.shape[0] == Y.shape[0]) if sample_weights is not None: assert (len(sample_weights.shape) == 1) assert (len(sample_weights) == X.shape[0]) n = X.shape[0] d = X.shape[1] self.cvx_w = cvx.Variable(d) self.cvx_b = cvx.Variable(1) self.cvx_hinge_losses = cvx.Variable(n) if sample_weights is None: sample_weights = np.ones(n) total_sample_weights = np.sum(sample_weights) self.objective = cvx.Minimize( cvx.sum_entries( cvx.mul_elemwise(sample_weights, self.cvx_hinge_losses)) / total_sample_weights + 0.5 * self.weight_decay * cvx.sum_squares(self.cvx_w)) self.constraints = [ cvx.mul_elemwise(Y, X * self.cvx_w + self.cvx_b) >= 1 - self.cvx_hinge_losses, self.cvx_hinge_losses >= 0 ] self.prob = cvx.Problem(self.objective, self.constraints) self.prob.solve(verbose=verbose, solver=cvx.GUROBI) self.coef_ = np.array(self.cvx_w.value).reshape(-1) self.intercept_ = self.cvx_b.value
def create(**kwargs): # m>k k = kwargs['k'] #class m = kwargs['m'] #instance n = kwargs['n'] #dim p = 5 #p-largest X = problem_util.normalized_data_matrix(m, n, 1) Y = np.random.randint(0, k, m) Theta = cp.Variable(n, k) t = cp.Variable(1) texp = cp.Variable(m) f = t + cp.sum_largest(texp, p) + cp.sum_squares(Theta) C = [] C.append(cp.log_sum_exp(X * Theta, axis=1) <= texp) Yi = one_hot(Y, k) C.append(-cp.sum_entries(cp.mul_elemwise(X.T.dot(Yi), Theta)) == t) t_eval = lambda: \ -cp.sum_entries(cp.mul_elemwise(X.T.dot(one_hot(Y, k)), Theta)).value f_eval = lambda: t_eval() \ + cp.sum_largest(cp.log_sum_exp(X*Theta, axis=1), p).value \ + cp.sum_squares(Theta).value return cp.Problem(cp.Minimize(f), C), f_eval
def solve_spectral(prob, *args, **kwargs): """Solve the spectral relaxation with lambda = 1. """ # TODO: do this efficiently without SDP lifting # lifted variables and semidefinite constraint X = cvx.Semidef(prob.n + 1) W = prob.f0.homogeneous_form() rel_obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(W, X))) W1 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '<=']) W2 = sum([f.homogeneous_form() for f in prob.fs if f.relop == '==']) rel_prob = cvx.Problem( rel_obj, [ cvx.sum_entries(cvx.mul_elemwise(W1, X)) <= 0, cvx.sum_entries(cvx.mul_elemwise(W2, X)) == 0, X[-1, -1] == 1 ] ) rel_prob.solve(*args, **kwargs) if rel_prob.status not in [cvx.OPTIMAL, cvx.OPTIMAL_INACCURATE]: raise Exception("Relaxation problem status: %s" % rel_prob.status) (w, v) = LA.eig(X.value) return np.sqrt(np.max(w))*np.asarray(v[:-1, np.argmax(w)]).flatten(), rel_prob.value
def _estimate(self, t, w_plus, z, value): """Estimate holding costs. Args: t: time of estimate wplus: holdings tau: time to estimate (default=t) """ try: w_plus = w_plus[w_plus.index != self.cash_key] w_plus = w_plus.values except AttributeError: w_plus = w_plus[:-1] # TODO fix when cvxpy pandas ready try: self.expression = cvx.mul_elemwise( time_locator(self.borrow_costs, t), cvx.neg(w_plus)) except TypeError: self.expression = cvx.mul_elemwise( time_locator(self.borrow_costs, t).values, cvx.neg(w_plus)) try: self.expression -= cvx.mul_elemwise( time_locator(self.dividends, t), w_plus) except TypeError: self.expression -= cvx.mul_elemwise( time_locator(self.dividends, t).values, w_plus) return cvx.sum_entries(self.expression), []
def _initialize_probs(self, A, k, missing_list, regX, regY): # useful parameters m = A[0].shape[0] ns = [a.shape[1] for a in A] if missing_list == None: missing_list = [[]]*len(self.L) # initialize A, X, Y B = self._initialize_A(A, missing_list) X0, Y0 = self._initialize_XY(B, k, missing_list) self.X0, self.Y0 = X0, Y0 # cvxpy problems Xv, Yp = cp.Variable(m,k), [cp.Parameter(k+1,ni) for ni in ns] Xp, Yv = cp.Parameter(m,k+1), [cp.Variable(k+1,ni) for ni in ns] Xp.value = copy(X0) for yj, yj0 in zip(Yp, Y0): yj.value = copy(yj0) onesM = cp.Constant(ones((m,1))) obj = sum(L(Aj, cp.mul_elemwise(mask, Xv*yj[:-1,:] \ + onesM*yj[-1:,:]) + offset) + ry(yj[:-1,:])\ for L, Aj, yj, mask, offset, ry in \ zip(self.L, A, Yp, self.masks, self.offsets, regY)) + regX(Xv) pX = cp.Problem(cp.Minimize(obj)) pY = [cp.Problem(cp.Minimize(\ L(Aj, cp.mul_elemwise(mask, Xp*yj) + offset) \ + ry(yj[:-1,:]) + regX(Xp))) \ for L, Aj, yj, mask, offset, ry in zip(self.L, A, Yv, self.masks, self.offsets, regY)] self.probX = (Xv, Yp, pX) self.probY = (Xp, Yv, pY)
def solve_sdr(prob, *args, **kwargs): """Solve the SDP relaxation. """ # lifted variables and semidefinite constraint X = cvx.Semidef(prob.n + 1) W = prob.f0.homogeneous_form() rel_obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(W, X))) rel_constr = [X[-1, -1] == 1] for f in prob.fs: W = f.homogeneous_form() lhs = cvx.sum_entries(cvx.mul_elemwise(W, X)) if f.relop == '==': rel_constr.append(lhs == 0) else: rel_constr.append(lhs <= 0) rel_prob = cvx.Problem(rel_obj, rel_constr) rel_prob.solve(*args, **kwargs) if rel_prob.status not in [cvx.OPTIMAL, cvx.OPTIMAL_INACCURATE]: raise Exception("Relaxation problem status: %s" % rel_prob.status) return X.value, rel_prob.value
def create(**kwargs): # m>k k = kwargs['k'] #class m = kwargs['m'] #instance n = kwargs['n'] #dim p = 5 #p-largest q = 10 X = problem_util.normalized_data_matrix(m,n,1) Y = np.random.randint(0, k-1, (q,m)) Theta = cp.Variable(n,k) t = cp.Variable(q) texp = cp.Variable(m) f = cp.sum_largest(t, p)+cp.sum_entries(texp) + cp.sum_squares(Theta) C = [] C.append(cp.log_sum_exp(X*Theta, axis=1) <= texp) for i in range(q): Yi = one_hot(Y[i], k) C.append(-cp.sum_entries(cp.mul_elemwise(X.T.dot(Yi), Theta)) == t[i]) t_eval = lambda: np.array([ -cp.sum_entries(cp.mul_elemwise(X.T.dot(one_hot(Y[i], k)), Theta)).value for i in range(q)]) f_eval = lambda: cp.sum_largest(t_eval(), p).value \ + cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1)).value \ + cp.sum_squares(Theta).value return cp.Problem(cp.Minimize(f), C), f_val
def f_quantile_elemwise(): m = 4 k = 2 alphas = rand(k) A = np.tile(alphas, (m, 1)) X = cp.Variable(m, k) return cp.sum_entries( cp.max_elemwise(cp.mul_elemwise(-A, X), cp.mul_elemwise(1 - A, X)))
def f_quantile_elemwise(): m = 4 k = 2 alphas = rand(k) A = np.tile(alphas, (m, 1)) X = cp.Variable(m, k) return cp.sum_entries(cp.max_elemwise( cp.mul_elemwise( -A, X), cp.mul_elemwise(1-A, X)))
def quantile_loss(alphas, Theta, X, y): m, n = X.shape k = len(alphas) Y = np.tile(y, (k, 1)).T A = np.tile(alphas, (m, 1)) Z = X*Theta - Y return cp.sum_entries( cp.max_elemwise( cp.mul_elemwise( -A, Z), cp.mul_elemwise(1-A, Z)))
def quantile_loss(alphas, Theta, X, y): m, n = X.shape k = len(alphas) Y = np.tile(y.flatten(), (k, 1)).T A = np.tile(alphas, (m, 1)) Z = X*Theta - Y return cp.sum_entries( cp.max_elemwise( cp.mul_elemwise( -A, Z), cp.mul_elemwise(1-A, Z)))
def marginal_optimization(self, seed = None): logging.debug("Starting to merge marginals") # get number of cliques: n node_card = self.node_card; cliques = self.cliques d = self.nodes_num; n = self.cliques_num; m = self.clusters_num # get the junction tree matrix representation: O O = self.jt_rep() # get log_p is the array of numbers of sum(log(attribute's domain)) log_p = self.log_p_func() # get log_node_card: log(C1), log(C2), ..., log(Cd) log_node_card = np.log(node_card) # get value of sum_log_node_card: log(C1 * C2 *...* Cd) sum_log_node_card = sum(log_node_card) # get the difference operator M on cluster number: m M = self.construct_difference() # initial a seed Z prev_Z = seed if prev_Z is None: prev_Z = np.random.rand(n,m) # run the convex optimization for max_iter times logging.debug("Optimization starting...") for i in range(self.max_iter): logging.debug("The optimization iteration: "+str(i+1)) # sum of row of prev_Z tmp1 = cvx.sum_entries(prev_Z, axis=0).value # tmp2 = math.log(tmp1)-1+sum_log_node_card tmp2 = np.log(tmp1)-1+sum_log_node_card # tmp3: difference of pairwise columns = prev_Z * M tmp3 = np.dot(prev_Z,M) # convex optimization Z = cvx.Variable(n,m) t = cvx.Variable(1,m) r = cvx.Variable() objective = cvx.Minimize(cvx.log_sum_exp(t)-self._lambda*r) constraints = [ Z >= 0, Z*np.ones((m,1),dtype=int) == np.ones((n,1), dtype=int), r*np.ones((1,m*(m-1)/2), dtype=int) - 2*np.ones((1,n), dtype=int)*(cvx.mul_elemwise(tmp3, (Z*M))) + cvx.sum_entries(tmp3 * tmp3, axis=0) <= 0, np.ones((1,n),dtype=int)*Z >= 1, log_p*Z-t-np.dot(log_node_card,O)*Z+tmp2+cvx.mul_elemwise(np.power(tmp1,-1), np.ones((1,n), dtype = int)*Z) == 0 ] prob = cvx.Problem(objective, constraints) result = prob.solve(solver='SCS',verbose=False) prev_Z[0:n,0:m] = Z.value return prev_Z, O
def solve(self): dim = self.X.shape[1] w = cvx.Variable(dim) num_nodes = nx.number_of_nodes(self.temp) b = cvx.Variable(num_nodes) loss = cvx.sum_entries(cvx.mul_elemwise(np.array(self.pos_node),cvx.logistic(-cvx.mul_elemwise(self.y, self.X*w+b)))) + self.Lambda*cvx.quad_form(b,self.P) problem = cvx.Problem(cvx.Minimize(loss)) problem.solve(verbose=False) opt = problem.value self.W = w.value self.b = b.value self.value = opt
def max_request_forwarded(self): objective = cp.Maximize(cp.sum_entries(cp.sum_entries(np.sum(map(lambda i:cp.mul_elemwise(self.A[i].T,self.x_bar[i]),range(self.S))), axis=0),axis=1)) capacityConstrain = cp.sum_entries(np.sum(map(lambda i:cp.mul_elemwise(self.A[i],self.x_bar[i].T),range(self.S))), axis=1)<=self.C #row_sums, if axis=0 it would be a column sum constraints = [capacityConstrain] for i in range(self.S): r1 = cp.sum_entries(self.x_bar[i],axis=1)<= self.avg_D[i] constraints.append(r1) r2 = self.x_bar[i]>=0.0 constraints.append(r2) lp1 = cp.Problem(objective,constraints) result = lp1.solve()
def _estimate(self, t, w_plus, z, value): """Estimate tcosts given trades. Args: t: time of estimate z: trades value: portfolio value Returns: An expression for the tcosts. """ try: z = z[z.index != self.cash_key] z = z.values except AttributeError: z = z[:-1] # TODO fix when cvxpy pandas ready constr = [] second_term = time_locator(self.nonlin_coeff, t) * \ time_locator(self.sigma, t) * \ (value / time_locator(self.volume, t))**(self.power - 1) # no trade conditions if np.isscalar(second_term): if np.isnan(second_term): constr += [z == 0] second_term = 0 else: # it is a pd series no_trade = second_term.index[second_term.isnull()] second_term[no_trade] = 0 constr += [ z[second_term.index.get_loc(tick)] == 0 for tick in no_trade ] try: self.expression = cvx.mul_elemwise( time_locator(self.half_spread, t), cvx.abs(z)) except TypeError: self.expression = cvx.mul_elemwise( time_locator(self.half_spread, t).values, cvx.abs(z)) try: self.expression += cvx.mul_elemwise(second_term, cvx.abs(z)**self.power) except TypeError: self.expression += cvx.mul_elemwise(second_term.values, cvx.abs(z)**self.power) return cvx.sum_entries(self.expression), constr
def optimal_power(n, a_val, b_val, P_tot=1.0, W_tot=1.0): ''' Boyd and Vandenberghe, Convex Optimization, exercise 4.62 page 210 Optimal power and bandwidth allocation in a Gaussian broadcast channel. We consider a communication system in which a central node transmits messages to n receivers. Each receiver channel is characterized by its (transmit) power level Pi ≥ 0 and its bandwidth Wi ≥ 0. The power and bandwidth of a receiver channel determine its bit rate Ri (the rate at which information can be sent) via Ri=αiWi log(1 + βiPi/Wi), where αi and βi are known positive constants. For Wi=0, we take Ri=0 (which is what you get if you take the limit as Wi → 0). The powers must satisfy a total power constraint, which has the form P1 + · · · + Pn = Ptot, where Ptot > 0 is a given total power available to allocate among the channels. Similarly, the bandwidths must satisfy W1 + · · · +Wn = Wtot, where Wtot > 0 is the (given) total available bandwidth. The optimization variables in this problem are the powers and bandwidths, i.e., P1, . . . , Pn, W1, . . . ,Wn. The objective is to maximize the total utility, sum(ui(Ri),i=1..n) where ui: R → R is the utility function associated with the ith receiver. ''' # Input parameters: alpha and beta are constants from R_i equation n = len(a_val) if n != len(b_val): print('alpha and beta vectors must have same length!') return 'failed', np.nan, np.nan, np.nan P = cvx.Variable(n) W = cvx.Variable(n) alpha = cvx.Parameter(n) beta = cvx.Parameter(n) alpha.value = np.array(a_val) beta.value = np.array(b_val) # This function will be used as the objective so must be DCP; i.e. element-wise multiplication must occur inside kl_div, not outside otherwise the solver does not know if it is DCP... R=cvx.kl_div(cvx.mul_elemwise(alpha, W), cvx.mul_elemwise(alpha, W + cvx.mul_elemwise(beta, P))) - \ cvx.mul_elemwise(alpha, cvx.mul_elemwise(beta, P)) objective = cvx.Minimize(cvx.sum_entries(R)) constraints = [ P >= 0.0, W >= 0.0, cvx.sum_entries(P) - P_tot == 0.0, cvx.sum_entries(W) - W_tot == 0.0 ] prob = cvx.Problem(objective, constraints) prob.solve() return prob.status, -prob.value, P.value, W.value
def fit(self, X, y, c=1): """ Train a SVM classifier using data (using Primal problem). """ self.X = X self.y = y # parameters D = X.shape[1] # number of features N = X.shape[0] # number of samples # instantiate variables in cvxpy W = cvx.Variable(D) b = cvx.Variable() # loss function of the primal SVM loss = ( 0.5 * cvx.sum_squares(W) + c * cvx.sum_entries(cvx.pos(1 - cvx.mul_elemwise(y, X * W + b)))) # define loss function manually # loss = 0.5 * cvx.sum_squares(W) # for i in range(N): # loss += c * cvx.pos(1 - y[i] * (X[i]*W+b)) # need to minimize loss/N to avoid error prob = cvx.Problem(cvx.Minimize(loss / N)) prob.solve() # save results self.W = W.value self.b = b.value return self.W, self.b
def policy_unU2(G, R, L, d, x_prev, un_U, gamma): # a greedy policy that max next step return using un_U [n,A]=R.shape [m,temp]=d.shape Q = cvxpy.Variable(n,A) M = cvxpy.Variable(n,n) x = cvxpy.Variable(n,1) Kr = cvxpy.kron(np.ones((A,1)),np.eye(n)) # Create two constraints. constraints = [-M + cvxpy.mul_elemwise(G,(np.ones((n, 1)) * cvxpy.vec(Q).T)) * Kr == 0, L * x - d <= 0, x - M * x_prev == 0, Q * np.ones((A, 1)) - np.ones((n, 1)) == 0, Q >= 0, x >= 0, x.T * np.ones((n, 1)) - 1 == 0 ] # Form objective. obj = cvxpy.Minimize(-(x.T) * un_U) # Form and solve problem. prob = cvxpy.Problem(obj, constraints) prob.solve(solver = cvxpy.ECOS, verbose = False, max_iters = 5000) # prob.solve(solver = cvxpy.SCS, verbose = False) return Q.value, M.value, x.value
def M_step(self): expected_LL = 0 for k in range(self.num_classes): w = cvx.Variable(self.dim) b = cvx.Variable(1) loss = cvx.sum_entries( cvx.mul_elemwise( np.array(self.posterior_mat[:, k]), cvx.logistic(-cvx.mul_elemwise( self.Y, self.X * w + np.ones(self.num_nodes) * b)))) problem = cvx.Problem(cvx.Minimize(loss)) problem.solve(verbose=False, solver='SCS') expected_LL -= problem.value self.W[:, k] = np.array(w.value).flatten() self.B[k] = b.value self.expected_LL = expected_LL
def q3(): def K(x1,x2): return (1+ np.inner(x1[:-1],x2[:-1]))**2 Q = np.zeros((len(data),len(data))) for i in range(len(data)): for j in range(len(data)): Q[i,j]= data[i,-1]*data[j,-1]*K(data[i],data[j]) alpha = cvx.Variable(len(data)) obj = cvx.Minimize(0.5*cvx.quad_form(alpha,Q)-cvx.sum_entries(alpha)) constraint = [cvx.sum_entries(cvx.mul_elemwise(data[:,-1],alpha)) == 0, alpha>=0] prob = cvx.Problem(obj,constraint) prob.solve() ret = alpha.value svid = next(i for i,x in enumerate(ret) if x>1e-5) b = data[svid,-1] - sum(ret[i,0]*data[i,-1]*K(data[i],data[svid]) for i in range(len(data)) if ret[i,0]>1e-5) def getvalue(X): XX = np.append(X,[-1]) return sum(ret[i,0]*data[i,-1]*K(XX,data[i]) for i in range(len(data))) + b ks = [(i,j,i**2,j**2) for j in range(10) for i in range(10)] vs = [getvalue(k[:2]) for k in ks] lr = lm.LinearRegression() lr.fit(ks,vs) print ret print lr.coef_*9,lr.intercept_*9
def sdp_partition(graph, constraints, k): if k != 2: raise Exception('SDP only applicable for 2 partitions') adjmat = nx.to_numpy_matrix(graph, weight='weight') n = nx.number_of_nodes(graph) Y = cvx.Semidef(n) obj = cvx.Maximize( cvx.sum_entries(cvx.mul_elemwise(np.tril(adjmat), (1 - Y)))) consts = [Y == Y.T] for i in range(n): consts.append(Y[i, i] == 1) for c1 in constraints.keys(): for c2 in constraints.keys(): if constraints[c1] != constraints[c2]: consts.append(Y[c1, c2] <= math.cos(2 * math.pi / k)) else: consts.append(Y[c1, c2] == 1) prob = cvx.Problem(obj, consts) prob.solve(solver='SCS') vecs = Y.value random_cut = sample_spherical(1, ndim=n) partition = {} signs = [] for i in range(n): signs.append(np.sign(np.dot(vecs[i, :], random_cut))) for con in constraints: for i in range(n): if signs[con] == signs[i]: partition[i] = constraints[con] return partition
def cvxinnerprod(oper1, oper2): """Computes the inner product between two CVXPY operators in dyads form. Parameters ---------- oper1, oper2 : DyadsOperator DyadsOperators whose factors are CVXPY expressions. """ return sum([ cvxpy.sum_entries( cvxpy.mul_elemwise(oper1.lfactors[r1], oper2.lfactors[r2])) * cvxpy.sum_entries( cvxpy.mul_elemwise(oper1.rfactors[r1], oper2.rfactors[r2])) for r1 in range(oper1.nfactors) for r2 in range(oper2.nfactors) ])
def _matinnerprod(mat1, mat2): """Computes inner product between two matrices.""" if isinstance(mat1, (cvxpy.Variable, cvxpy.Parameter)) or isinstance( mat2, (cvxpy.Variable, cvxpy.Parameter)): return cvxpy.sum_entries(cvxpy.mul_elemwise(mat1, mat2)) else: return np.sum(np.multiply(mat1, mat2))
def _train(self, data, label, delta): pos_idx = label == 1 neg_idx = label == -1 pos_num = sum(pos_idx) P_data_size = 0 for i in range(self.s, self.chunk_count - 1): P_data_size += sum(self.stored_label[i] == 1) if pos_num + P_data_size > delta: self.s += 1 P_data = array([]).reshape(-1, self.fea_num) ts = array([]) for i in range(self.s, self.chunk_count): P_data = r_[P_data, self.stored_data[i][self.stored_label[i] == 1]] ts = r_[ts, i * ones(sum(self.stored_label[i] == 1))] N_data = data[neg_idx] pos_num = P_data.shape[0] neg_num = N_data.shape[0] rand_idx = random.permutation(pos_num) P_data = P_data[rand_idx] ts = ts[rand_idx].astype(int) N_data = N_data[random.permutation(neg_num)] pos_train_num = int(self.train_ratio * pos_num) neg_train_num = int(self.train_ratio * neg_num) train_data = r_[P_data[:pos_train_num], N_data[:neg_train_num]] train_label = r_[ones(pos_train_num), -ones(neg_train_num)] train_ts = ts[:pos_train_num] hold_data = r_[P_data[pos_train_num:], N_data[neg_train_num:]] hold_label = r_[ones(pos_num - pos_train_num), -ones(neg_num - neg_train_num)] hold_pred = zeros([hold_label.size, self.fea_group_num]) self.ensemble = list() for fea_comb_i in range(self.fea_group_num): self._learnH(train_data[:, self.fea_comb[fea_comb_i]], train_label, train_ts) hold_pred = self._predict_base(hold_data) # solve convex optimization problem c = ones_like(hold_label) c[hold_label == 1] = sum(hold_label == -1) / sum(hold_label == 1) w = cvxpy.Variable(self.fea_group_num) obj = cvxpy.Minimize( c.T * cvxpy.logistic(-cvxpy.mul_elemwise(hold_label, hold_pred * w))) constraints = [cvxpy.sum_entries(w) == 1, w >= 0] prob = cvxpy.Problem(obj, constraints) try: prob.solve() except (cvxpy.error.SolverError): prob.solve(solver=cvxpy.CVXOPT) self.wd = array(w.value).squeeze()
def weight_expr(self, t, wplus, z=None, v=None): """Returns the estimated alpha. Args: t: time estimate is made. wplus: An expression for holdings. tau: time of alpha being estimated. Returns: An expression for the alpha. """ alpha = cvx.mul_elemwise(time_locator(self.returns, t, as_numpy=True), wplus) alpha -= cvx.mul_elemwise(time_locator(self.delta, t, as_numpy=True), cvx.abs(wplus)) return cvx.sum_entries(alpha)
def softmax_loss(Theta, X, y): m = len(y) n, k = Theta.size Y = sp.coo_matrix((np.ones(m), (np.arange(m), y)), shape=(m, k)) print cp.__file__ return (cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1)) - cp.sum_entries(cp.mul_elemwise(Y, X*Theta)))
def lqr_cp(C, c, F, f, x_init, T, n_state, n_ctrl, u_lower, u_upper): """Solve min_{tau={x,u}} sum_t 0.5 tau_t^T C_t tau_t + c_t^T tau_t s.t. x_{t+1} = A_t x_t + B_t u_t + f_t x_0 = x_init u_lower <= u <= u_upper """ tau = cp.Variable(n_state + n_ctrl, T) assert (u_lower is None) == (u_upper is None) objs = [] x0 = tau[:n_state, 0] u0 = tau[n_state:, 0] cons = [x0 == x_init] for t in range(T): xt = tau[:n_state, t] ut = tau[n_state:, t] objs.append(0.5 * cp.quad_form(tau[:, t], C[t]) + cp.sum_entries(cp.mul_elemwise(c[t], tau[:, t]))) if u_lower is not None: cons += [u_lower[t] <= ut, ut <= u_upper[t]] if t + 1 < T: xtp1 = tau[:n_state, t + 1] cons.append(xtp1 == F[t] * tau[:, t] + f[t]) prob = cp.Problem(cp.Minimize(sum(objs)), cons) # prob.solve(solver=cp.SCS, verbose=True) prob.solve() assert 'optimal' in prob.status return np.array(tau.value), np.array([obj_t.value for obj_t in objs])
def solve(self, verbose=False, solver=None, **kwargs): params = {**self.params, **kwargs} u = params['u'] λ = params['λ'] try: train = params['train'] except KeyError: train = self.train r = train.r.values try: K = params['kernel'] X = K(train.X, train.X) except KeyError: X = train.X.values n, p = X.shape q = cvx.Variable(p) objective = cvx.Maximize( 1 / n * cvx.sum_entries(u.cvx_util(cvx.mul_elemwise(r, X * q))) - λ * cvx.norm(q)**2) problem = cvx.Problem(objective) problem.solve(solver=solver, verbose=verbose) if p == 1: # q is a scalar self.q = np.array([q.value]) if p > 1: # q is a vector self.q = q.value.A1 self.params['q'] = self.q return self.q
def WMD(wvs1, wvs2, d1, d2): n1 = d1.shape[0] n2 = d2.shape[0] assert wvs1.shape[0] == n1 assert wvs2.shape[0] == n2 assert wvs1.shape[1] == wvs2.shape[2] wwdist = np.zeros((n1, n2)) for i in range(n1): for j in range(n2): wwdist[i, j] = np.linalg.norm(wvs1[i] - wvs2[j], ord=2) T = cvx.Variable(n1, n2) obj = cvx.sum_entries(cvx.mul_elemwise(wwdist, T)) cons = [ cvx.sum_entries(T, axis=1) == d1, cvx.sum_entries(T, axis=0) >= d2, 0 <= T ] prob = cvx.Problem(cvx.Maximize(obj), cons) # Solve prob.solve(solver=cvx.ECOS) # , mi_max_iters=100 Topt = T.value senDiff = prob.value return senDiff, Topt, prob.status
def kevin_scaling(cryst): minspots = 100 g = None for im in cryst: num = sum([ im[i].hkl for i in im if 'on' in i and im[i].hkl is not None and len(im[i].hkl.data) > minspots ]) den = sum([ im[i].hkl for i in im if 'off' in i and im[i].hkl is not None and len(im[i].hkl.data) > minspots ]) a = cvx.Variable() if isinstance(num, xds.uncorrectedhkl) and isinstance( den, xds.uncorrectedhkl): idx = num.data.index.intersection(den.data.index) if len(idx) > 0.: w = (num + den).data.loc[idx]['SIGMA(IOBS)'] n, d = num.data.loc[idx]['IOBS'], den.data.loc[idx]['IOBS'] n, d, w = np.array(n), np.array(d), np.array(w) loss = cvx.norm1(cvx.mul_elemwise(1. / w, a * n - d)) p = cvx.Problem(cvx.Minimize(loss)) p.solve() g = pd.concat((g, (a.value * num / den).data)) return g
def optimal_power(n, a_val, b_val, P_tot=1.0, W_tot=1.0): ''' Boyd and Vandenberghe, Convex Optimization, exercise 4.62 page 210 Optimal power and bandwidth allocation in a Gaussian broadcast channel. We consider a communication system in which a central node transmits messages to n receivers. Each receiver channel is characterized by its (transmit) power level Pi ≥ 0 and its bandwidth Wi ≥ 0. The power and bandwidth of a receiver channel determine its bit rate Ri (the rate at which information can be sent) via Ri=αiWi log(1 + βiPi/Wi), where αi and βi are known positive constants. For Wi=0, we take Ri=0 (which is what you get if you take the limit as Wi → 0). The powers must satisfy a total power constraint, which has the form P1 + · · · + Pn = Ptot, where Ptot > 0 is a given total power available to allocate among the channels. Similarly, the bandwidths must satisfy W1 + · · · +Wn = Wtot, where Wtot > 0 is the (given) total available bandwidth. The optimization variables in this problem are the powers and bandwidths, i.e., P1, . . . , Pn, W1, . . . ,Wn. The objective is to maximize the total utility, sum(ui(Ri),i=1..n) where ui: R → R is the utility function associated with the ith receiver. ''' # Input parameters: alpha and beta are constants from R_i equation n=len(a_val) if n!=len(b_val): print('alpha and beta vectors must have same length!') return 'failed',np.nan,np.nan,np.nan P=cvx.Variable(n) W=cvx.Variable(n) alpha=cvx.Parameter(n) beta =cvx.Parameter(n) alpha.value=np.array(a_val) beta.value =np.array(b_val) # This function will be used as the objective so must be DCP; i.e. element-wise multiplication must occur inside kl_div, not outside otherwise the solver does not know if it is DCP... R=cvx.kl_div(cvx.mul_elemwise(alpha, W), cvx.mul_elemwise(alpha, W + cvx.mul_elemwise(beta, P))) - \ cvx.mul_elemwise(alpha, cvx.mul_elemwise(beta, P)) objective=cvx.Minimize(cvx.sum_entries(R)) constraints=[P>=0.0, W>=0.0, cvx.sum_entries(P)-P_tot==0.0, cvx.sum_entries(W)-W_tot==0.0] prob=cvx.Problem(objective, constraints) prob.solve() return prob.status,-prob.value,P.value,W.value
def test_problem(self): """Test problem object. """ X = Variable((4, 2)) B = np.reshape(np.arange(8), (4, 2)) * 1. prox_fns = [norm1(X), sum_squares(X, b=B)] prob = Problem(prox_fns) # prob.partition(quad_funcs = [prox_fns[0], prox_fns[1]]) prob.set_automatic_frequency_split(False) prob.set_absorb(False) prob.set_implementation(Impl['halide']) prob.set_solver('admm') prob.solve() true_X = norm1(X).prox(2, B.copy()) self.assertItemsAlmostEqual(X.value, true_X, places=2) prob.solve(solver="pc") self.assertItemsAlmostEqual(X.value, true_X, places=2) prob.solve(solver="hqs", eps_rel=1e-6, rho_0=1.0, rho_scale=np.sqrt(2.0) * 2.0, rho_max=2**16, max_iters=20, max_inner_iters=500, verbose=False) self.assertItemsAlmostEqual(X.value, true_X, places=2) # CG prob = Problem(prox_fns) prob.set_lin_solver("cg") prob.solve(solver="admm") self.assertItemsAlmostEqual(X.value, true_X, places=2) prob.solve(solver="hqs", eps_rel=1e-6, rho_0=1.0, rho_scale=np.sqrt(2.0) * 2.0, rho_max=2**16, max_iters=20, max_inner_iters=500, verbose=False) self.assertItemsAlmostEqual(X.value, true_X, places=2) # Quad funcs. prob = Problem(prox_fns) prob.solve(solver="admm") self.assertItemsAlmostEqual(X.value, true_X, places=2) # Absorbing lin ops. prox_fns = [norm1(5 * mul_elemwise(B, X)), sum_squares(-2 * X, b=B)] prob = Problem(prox_fns) prob.set_absorb(True) prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6) cvx_X = cvx.Variable(4, 2) cost = cvx.sum_squares(-2 * cvx_X - B) + cvx.norm(5 * cvx.mul_elemwise(B, cvx_X), 1) cvx_prob = cvx.Problem(cvx.Minimize(cost)) cvx_prob.solve(solver=cvx.SCS) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) prob.set_absorb(False) prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2) # Constant offsets. prox_fns = [norm1(5 * mul_elemwise(B, X)), sum_squares(-2 * X - B)] prob = Problem(prox_fns) prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6) self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
def policy(G, R, L, d, un_Q, un_M, un_U, U_next, U_ref, opt_ref, gamma): [n,A]=R.shape [m,temp]=d.shape Q = cvxpy.Variable(n,A) M = cvxpy.Variable(n,n) S = cvxpy.Variable(m,n) K = cvxpy.Variable(m,m) U = cvxpy.Variable(n,1) y = cvxpy.Variable(m,1) r = cvxpy.Variable(n,1) xi = cvxpy.Variable(m,1) z = cvxpy.Variable(1,1) Kr = cvxpy.kron(np.ones((A,1)),np.eye(n)) # Create two constraints. constraints = [(d.T) * y - z <= opt_ref, -M + cvxpy.mul_elemwise(G,(np.ones((n, 1)) * cvxpy.vec(Q).T)) * Kr == 0, -r + cvxpy.mul_elemwise(R,Q) * np.ones((A, 1)) == 0, -L.T * y + z * np.ones((n, 1)) - U_ref <= 0, -U + r + gamma * M.T * U_next == 0, -K * L + L * M + S + xi * np.ones((1, n)) == 0, xi + d - K * d >= 0, Q * np.ones((A, 1)) - np.ones((n, 1)) == 0, Q >= np.zeros((n, A)), y >= np.zeros((m, 1)), S >= np.zeros((m, n)), K >= np.zeros((m, m)), M >= np.zeros((n, n)) ] # Form objective. obj = cvxpy.Minimize(cvxpy.norm((Q - un_Q), 'fro')) # obj = cvxpy.Minimize(cvxpy.norm((M - un_M), 'fro')) # obj = cvxpy.Minimize(cvxpy.norm((U - un_U))) # Form and solve problem. prob = cvxpy.Problem(obj, constraints) prob.solve(solver = cvxpy.ECOS, verbose = False, max_iters = 2000, feastol = 1e-4, reltol = 1e-4, abstol = 1e-4) # prob.solve(solver = cvxpy.CVXOPT, verbose = True) return U.value, Q.value, M.value
def create(m, n, lam): np.random.seed(0) m = int(n) n = int(n) lam = float(lam) A = sp.rand(n,n, 0.01) A = np.asarray(A.T.dot(A).todense() + 0.1*np.eye(n)) L = np.linalg.cholesky(np.linalg.inv(A)) X = np.random.randn(m,n).dot(L.T) S = X.T.dot(X)/m W = np.ones((n,n)) - np.eye(n) Theta = cp.Variable(n,n) return cp.Problem(cp.Minimize( lam*cp.norm1(cp.mul_elemwise(W,Theta)) + cp.sum_entries(cp.mul_elemwise(S,Theta)) - cp.log_det(Theta)))
def policy(G, R, L, d, U_next, gamma): [n,A]=R.shape [m,temp]=d.shape Q = cvxpy.Variable(n,A) M = cvxpy.Variable(n,n) S = cvxpy.Variable(m,n) K = cvxpy.Variable(m,m) U = cvxpy.Variable(n,1) y = cvxpy.Variable(m,1) r = cvxpy.Variable(n,1) xi = cvxpy.Variable(m,1) z = cvxpy.Variable(1,1) Kr = cvxpy.kron(np.ones((A,1)),np.eye(n)) # Create two constraints. constraints = [-M + cvxpy.mul_elemwise(G, (np.ones((n, 1)) * cvxpy.vec(Q).T)) * Kr == 0, -r + cvxpy.mul_elemwise(R,Q) * np.ones((A, 1)) == 0, -L.T * y + z * np.ones((n, 1)) - U <= 0, -U + r + gamma * M.T * U_next == 0, -K * L + L * M + xi * np.ones((1, n)) <= 0, xi + d - K * d >= 0, Q * np.ones((A, 1)) - np.ones((n, 1)) == 0, Q >= 0, #np.zeros((n, A)), y >= 0, #np.zeros((m, 1)), K >= 0, #np.zeros((m, m)), ] # Form objective. obj = cvxpy.Minimize((d.T) * y - z) # Form and solve problem. prob = cvxpy.Problem(obj, constraints) # prob.solve(solver = cvxpy.MOSEK, verbose = True) # prob.solve(solver = cvxpy.CVXOPT, verbose = True) prob.solve(solver = cvxpy.ECOS, verbose = True, max_iters = 2000, feastol = 1e-4, reltol = 1e-4, abstol = 1e-4) # prob.solve(solver = cvxpy.SCS, verbose = False) return U.value, Q.value, M.value, ((d.T) * y - z).value
def create_matrix_completion_convex_problem( n_alleles, n_peptides, known_mask, known_values, tolerance=0.001): A = cvxpy.Variable(n_alleles, n_peptides, name="A") # nuclear norm minimization should create a low-rank matrix objective = cvxpy.Minimize(cvxpy.norm(A, "nuc")) masked_A = cvxpy.mul_elemwise(known_mask, A) masked_known = cvxpy.mul_elemwise(known_mask, known_values) diff = masked_A - masked_known close_to_data = diff ** 2 <= tolerance lower_bound = (A >= 0) upper_bound = (A <= 1) constraints = [close_to_data, lower_bound, upper_bound] problem = cvxpy.Problem(objective, constraints) return A, problem
def test_conv_prob(self): """Test a problem with convolution. """ import cvxpy as cvx import numpy as np N = 5 y = np.asmatrix(np.random.randn(N, 1)) h = np.asmatrix(np.random.randn(2, 1)) x = cvx.Variable(N) v = cvx.conv(h, x) obj = cvx.Minimize(cvx.sum_entries(cvx.mul_elemwise(y, v[0:N]))) print(cvx.Problem(obj, []).solve())
def create(m, n, density=0.1): np.random.seed(0) mu = np.exp(0.01*np.random.randn(n))-1 # returns D = np.random.rand(n)/10; # idiosyncratic risk F = sp.rand(n,m,density) # factor model F.data = np.random.randn(len(F.data))/10 gamma = 1 B = 1 x = cp.Variable(n) f = mu.T*x - gamma*(cp.sum_squares(F.T.dot(x)) + cp.sum_squares(cp.mul_elemwise(D, x))) C = [cp.sum_entries(x) == B, x >= 0] return cp.Problem(cp.Maximize(f), C)
def policy_unU(G, R, L, d, x_prev, un_U, gamma): [n,A]=R.shape [m,temp]=d.shape Q = cvxpy.Variable(n,A) M = cvxpy.Variable(n,n) S = cvxpy.Variable(m,n) K = cvxpy.Variable(m,m) # U = cvxpy.Variable(n,1) # y = cvxpy.Variable(m,1) # r = cvxpy.Variable(n,1) xi = cvxpy.Variable(m,1) # z = cvxpy.Variable(1,1) x = cvxpy.Variable(n,1) Kr = cvxpy.kron(np.ones((A,1)),np.eye(n)) # Create two constraints. constraints = [#(d.T) * y - z <= opt_ref, -M + cvxpy.mul_elemwise(G,(np.ones((n, 1)) * cvxpy.vec(Q).T)) * Kr == 0, # -r + cvxpy.mul_elemwise(R,Q) * np.ones((A, 1)) == 0, # -L.T * y + z * np.ones((n, 1)) - U_ref <= 0, # -U + r + gamma * M.T * U_next == 0, -K * L + L * M + xi * np.ones((1, n)) <= 0, xi + d - K * d >= 0, x - M * x_prev == 0, Q * np.ones((A, 1)) - np.ones((n, 1)) == 0, Q >= 0, #np.zeros((n, A)), # y >= 0, # np.zeros((m, 1)), K >= 0, #np.zeros((m, m)), x >= 0, x.T * np.ones((n, 1)) - 1 == 0 ] # Form objective. obj = cvxpy.Minimize(-(x.T) * un_U) # Form and solve problem. prob = cvxpy.Problem(obj, constraints) # prob.solve(solver = cvxpy.MOSEK, verbose = True) prob.solve(solver = cvxpy.ECOS, verbose = False, max_iters = 1000) # prob.solve(solver = cvxpy.SCS, verbose = False) return Q.value, M.value, x.value
def loss(self, A, U): return cp.sum_entries(cp.pos(ones(A.shape)-cp.mul_elemwise(cp.Constant(A), U))) def decode(self, A): return sign(A) # return back to Boolean
k = 201 t = np.asmatrix(np.linspace(-3,3,k)).T y = np.exp(t) one = np.asmatrix(np.ones((k,1))) Tpow = cvx.hstack(one, t, cvx.square(t)) u = np.exp(3) l = 0 tol = 1e-3 while u-l >= tol: mid = (l+u)/2 a = cvx.Variable(3) b = cvx.Variable(2) obj = cvx.Minimize(0) constraints = [cvx.abs(Tpow*a - cvx.mul_elemwise(y, Tpow*cvx.vstack(1, b))) <= mid * Tpow*cvx.vstack(1, b)] prob = cvx.Problem(obj, constraints) sol = prob.solve(solver=cvx.CVXOPT) if prob.status == cvx.OPTIMAL: print('gamma = {}', format(mid)) u = mid a_opt = a b_opt = b objval_opt = mid else: l = mid y_fit = cvx.mul_elemwise(Tpow*a_opt.value, cvx.inv_pos(Tpow*cvx.vstack(1, b_opt.value))) plt.figure(0)
def loss(self, A, U): return cp.sum_entries(sum(cp.mul_elemwise(1*(b >= A),\ cp.pos(U-b*ones(A.shape))) + cp.mul_elemwise(1*(b < A), \ cp.pos(-U + (b+1)*ones(A.shape))) for b in range(int(self.Amin), int(self.Amax))))
def multiclass_hinge_loss(Theta, X, y): k = Theta.size[1] Y = one_hot(y, k) return (cp.sum_entries(cp.max_entries(X*Theta + 1 - Y, axis=1)) - cp.sum_entries(cp.mul_elemwise(X.T.dot(Y), Theta)))
def softmax_loss(Theta, X, y): k = Theta.size[1] Y = one_hot(y, k) return (cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1)) - cp.sum_entries(cp.mul_elemwise(X.T.dot(Y), Theta)))
def test_absorb_lin_op(self): """Test absorb lin op operator. """ # norm1. tmp = Variable(10) v = np.arange(10) * 1.0 - 5.0 fn = norm1(mul_elemwise(-v, tmp), alpha=5.) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) self.assertItemsAlmostEqual(x, np.sign(v) * np.maximum(np.abs(v) - 5. * np.abs(v) / rho, 0)) fn = norm1(mul_elemwise(-v, mul_elemwise(2 * v, tmp)), alpha=5.) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) self.assertItemsAlmostEqual(x, np.sign(v) * np.maximum(np.abs(v) - 5. * np.abs(v) / rho, 0)) new_prox = absorb_lin_op(new_prox)[0] x = new_prox.prox(rho, v.copy()) new_v = 2 * v * v self.assertItemsAlmostEqual(x, np.sign(new_v) * np.maximum(np.abs(new_v) - 5. * np.abs(new_v) / rho, 0)) # nonneg. tmp = Variable(10) v = np.arange(10) * 1.0 - 5.0 fn = nonneg(mul_elemwise(-v, tmp), alpha=5.) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) self.assertItemsAlmostEqual(x, fn.prox(rho, -np.abs(v))) # sum_squares. tmp = Variable(10) v = np.arange(10) * 1.0 - 5.0 alpha = 5. val = np.arange(10) fn = sum_squares(mul_elemwise(-v, tmp), alpha=alpha, c=val) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) cvx_x = cvx.Variable(10) prob = cvx.Problem(cvx.Minimize(cvx.sum_squares(cvx_x - v) * (rho / 2) + 5 * cvx.sum_squares(cvx.mul_elemwise(-v, cvx_x)) + (val * -v).T * cvx_x )) prob.solve() self.assertItemsAlmostEqual(x, cvx_x.value, places=3) # Test scale. tmp = Variable(10) v = np.arange(10) * 1.0 - 5.0 fn = norm1(10 * tmp) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) cvx_x = cvx.Variable(10) prob = cvx.Problem(cvx.Minimize(cvx.sum_squares(cvx_x - v) + cvx.norm(10 * cvx_x, 1))) prob.solve() self.assertItemsAlmostEqual(x, cvx_x.value, places=3) val = np.arange(10) fn = norm1(10 * tmp, c=val, b=val, gamma=0.01) rho = 2 new_prox = absorb_lin_op(fn)[0] x = new_prox.prox(rho, v.copy()) cvx_x = cvx.Variable(10) prob = cvx.Problem(cvx.Minimize(cvx.sum_squares(cvx_x - v) + cvx.norm(10 * cvx_x - val, 1) + 10 * val.T * \ cvx_x + cvx.sum_squares(cvx_x) )) prob.solve() self.assertItemsAlmostEqual(x, cvx_x.value, places=2) # sum_entries tmp = Variable(10) v = np.arange(10) * 1.0 - 5.0 fn = sum_entries(sum([10 * tmp, mul_elemwise(v, tmp)])) funcs = absorb.absorb_all_lin_ops([fn]) c = __builtins__['sum']([func.c for func in funcs]) self.assertItemsAlmostEqual(c, v + 10, places=3)
def train(self, ct=0.01, theta=1, p=2): """ :param ct: 松弛变量正则化系数 :param theta: 高斯核函数参数 :param p: 多项式核函数参数 :return: None """ def kg(a, b, value_theta=theta): # 高斯核函数 sim = np.exp( -1 * (a - b).dot(a - b) / (2 * value_theta ** 2)) # sim = np.dot(a, b) return sim def kp(a, b, value_p=p): # 多项式核函数 sim = (np.dot(a, b) + 1) ** value_p return sim def k_mat(x, k_func=kg): # 核矩阵生成函数 m = x.shape[0] mat = np.zeros((m, m)) for i in range(m): for j in range(m): mat[i, j] = k_func(x[i, :], x[j, :]) return mat if self.kernel == "linear": # 线性核 w = cvx.Variable(self.n) e = cvx.Variable(self.m) b = cvx.Variable() c = cvx.Parameter(sign="positive") c.value = ct obj = cvx.Minimize(0.5 * cvx.norm(w,2) + c * cvx.sum_entries(e)) constraints = [e >= 0, cvx.mul_elemwise(self.y, self.x * w + b) - 1 + e >= 0] prob = cvx.Problem(obj, constraints) prob.solve() self.w = np.array(w.value) self.b = b.value return None elif self.kernel == "gaussian": # 高斯核 self.k = k_mat(self.x, k_func=kg) self.k_func = kg else: # 多项式核 self.k = k_mat(self.x, k_func=kp) self.k_func = kp a = cvx.Variable(self.m) b = cvx.Variable() e = cvx.Variable(self.m) c = cvx.Parameter(sign="positive") c.value = ct obj = cvx.Minimize(0.5 * cvx.quad_form(a, self.k) + c * cvx.sum_entries(e)) constraints = [e >= 0, self.y * b + cvx.mul_elemwise(self.y, self.k * a) - 1 + e >= 0] prob = cvx.Problem(obj, constraints) # quadratic programming 二次规划 prob.solve() self.a = np.array(a.value) self.b = b.value return None
constraints.append( cvx.sum_entries(N[:,j]) == I[j] ) prob = cvx.Problem(obj, constraints) prob.solve(solver=cvx.SCS) s1 = cvx.pos(q-cvx.diag(Acontr.T*N*Tcontr)); optimal_net_profit = prob.value optimal_penalty = p.T*s1 optimal_revenue = optimal_penalty + optimal_net_profit print "status:", prob.status print "optimal net profit:", optimal_net_profit print "optimal penalty:", prob.value print "optimal revenue:", prob+p.T*prob.value ''' # Greedy Aproach displaying without contracts Nignore = cvx.Variable(n,T) obj2 = cvx.Maximize(cvx.sum_entries(cvx.mul_elemwise(R, Nignore))) #np.reshape(R,n*T).T *Nignore[:,:]) constraints2 = [Nignore >= 0] for j in range(T): constraints2.append( cvx.sum_entries(Nignore[:,j]) == I[j] ) prob2 = cvx.Problem(obj2, constraints2) prob2.solve() s2 = cvx.pos(q-cvx.diag(Acontr.T*Nignore*Tcontr)) greedy_net_profit = prob2.value - p.T*s2 greedy_penalty = p.T*s2 print "greedy status:", prob2.status print "greedy net_profit:", greedy_net_profit.value print "greedy penalty:", greedy_penalty.value print "greedy revenue:", prob2.value