def linearize(expr): """Returns the tangent approximation to the expression. Gives an elementwise lower (upper) bound for convex (concave) expressions. No guarantees for non-DCP expressions. Args: expr: An expression. Returns: An affine expression. """ if expr.is_affine(): return expr else: tangent = expr.value if tangent is None: raise ValueError( "Cannot linearize non-affine expression with missing variable values." ) grad_map = expr.grad for var in expr.variables(): if grad_map[var] is None: return None if var.ndim > 1: temp = cvx.reshape(cvx.vec(var - var.value), (var.shape[0] * var.shape[1], 1)) flattened = np.transpose(grad_map[var]) * temp tangent = tangent + cvx.reshape(flattened, expr.shape) else: tangent = tangent + np.transpose(grad_map[var])*(var - var.value) return tangent
def test_problem_ubsdp(self): # test problem needs review print 'skipping, needs review' return from cvxpy import (matrix, variable, program, minimize, sum, abs, norm2, log, square, zeros, max, hstack, vstack, eye, eq, trace, semidefinite_cone, belongs, reshape) (m, n) = (self.m, self.n) c = matrix(self.c) A = matrix(self.A) B = matrix(self.B) Xubsdp = matrix(self.Xubsdp) tol_exp = 6 # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # X + S = I # X >= 0, S >= 0. # # c is an n-vector. # A is an m^2 x n-matrix. # B is an m x m-matrix. X = variable(m, n) S = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X) + c[i,0], 0.0) for i in range(n)] constr += [eq(X + S, eye(m))] constr += [belongs(X, semidefinite_cone), belongs(S, semidefinite_cone)] p = program(minimize(trace(B * X)), constr) p.solve(True) np.testing.assert_array_almost_equal( X.value, self.Xubsdp, tol_exp) # Use cvxpy to solve # # minimize tr(B * X) # s.t. tr(Ai * X) + ci = 0, i = 1, ..., n # 0 <= X <= I X2 = variable(m, n) constr = [eq(trace(reshape(A[:,i], (m, m)) * X2) + c[i,0], 0.0) for i in range(n)] constr += [belongs(X2, semidefinite_cone), belongs(eye(m) - X2, semidefinite_cone)] p = program(minimize(trace(B * X2)), constr) p.solve(True) np.testing.assert_array_almost_equal( X2.value, X.value, tol_exp)
def create(**kwargs): m = kwargs["m"] n = kwargs["n"] k = kwargs["k"] A = np.matrix(np.random.rand(m,n)) A -= np.mean(A, axis=0) K = np.array([(A[i].T*A[i]).flatten() for i in xrange(m)]) sigma = cp.Variable(n,n) t = cp.Variable(m) tdet = cp.Variable(1) f = cp.sum_largest(t+tdet, k) z = K*cp.reshape(sigma, n*n, 1) C = [-cp.log_det(sigma) <= tdet, t == z] det_eval = lambda: -cp.log_det(sigma).value z_eval = lambda: (K*cp.reshape(sigma, n*n, 1)).value f_eval = lambda: cp.sum_largest(z_eval()+det_eval(), k).value return cp.Problem(cp.Minimize(f), C), f_eval
def meshy2_one(x1,x2,y,m,k1=[1],k2=[1],interp=0,mesh1=None,mesh2=None,lnorm=1,tune=50,eps=0.01,cvx_solver=0): # In this bivariate meshy solver, it is assumed that we are taking the same number of cuts per covariate: same m. In general though, can use m_1 cuts for x_1 and m_2 cuts for x_2. # interp = 0 : nearest neighbor # interp = 1 : linear weighting of nearest 2+1=3 neighbors # k1 is the order differences applied on x_1, k2 for x_2 # k1 and k2 can be lists, but they must be the same length such that they are paired at each index # possible solvers to choose from: typically CVXOPT works better for simulations thus far. solvers = [cvx.SCS,cvx.CVXOPT] # 0 is SCS, 1 CVXOPT default_iter = [160000,3200][cvx_solver] # defaults are 2500 and 100 n = x1.size # Create D-matrix: specify n and k if mesh1==None: mesh1 = np.linspace(min(x1)-eps,max(x1)+eps,m) if mesh2==None: mesh2 = np.linspace(min(x2)-eps,max(x2)+eps,m) delta1 = np.diff(mesh1)[0] # Regular grids, but x1 and x2 can have different range delta2 = np.diff(mesh2)[0] # Create O-matrix: specify x and number of desired cuts O = utils2.interpO(x1=x1,x2=x2,mesh1=mesh1,mesh2=mesh2,interp=interp) # Solve convex problem. theta = cvx.Variable(m,m) sobo_like = sobbynorm(k1=k1,k2=k2,lnorm=lnorm,theta=theta,delta1=delta1,delta2=delta2,m=m) obj = cvx.Minimize(0.5 * cvx.sum_squares(y - O*cvx.reshape(theta,m*m,1)) + tune * sobo_like) prob = cvx.Problem(obj) prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters = default_iter) counter = 0 while prob.status != cvx.OPTIMAL: maxit = 2*default_iter prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters=maxit) default_iter = maxit counter = counter +1 if counter>4: raise Exception("Solver did not converge with %s iterations! (N=%s,d=%s,k1=%s,k2=%s)" % (default_iter,n,m,k1,k2) ) output = {'mesh1': mesh1, 'mesh2':mesh2,'theta.hat': np.array(theta.value),'fitted':O.dot(np.reshape(np.array(theta.value),m*m,1)),'x1':x1,'x2':x2,'y':y,'k1':k1,'k2':k2,'interp':interp,'eps':eps,'m':m} return output
def minimax_lloyd(domain, M, L=None, maxiter=100, Xhat=None, verbose=True, xtol=1e-5, full=None): r""" A fixed point iteration for a minimax design This algorithm can be interpreted as a block coordinate descent type algorithm for the optimal minimax experimental design on the given domain. SD96. """ # Terminate early if we have a simple case try: return minimax_design_1d(domain, M, L=L) except AssertionError: pass if full is None: if len(domain) < 3: _update_voronoi = _update_voronoi_full else: _update_voronoi = _update_voronoi_sample elif full is True: _update_voronoi = _update_voronoi_full elif full is False: _update_voronoi = _update_voronoi_sample if L is None: L = np.eye(len(domain)) M0 = 10 * len(domain) * M X0 = domain.sample(M0) if Xhat is None: if verbose: print(10 * '=' + " Building Coffeehouse Design " + 10 * '=') Xhat = maximin_coffeehouse(domain, M, L, verbose=verbose) if verbose: print('\n' + 10 * '=' + " Building Maximin Design " + 10 * '=') Xhat = maximin_block(domain, M, L=L, maxiter=50, verbose=verbose, X0=Xhat) # The Shrinkage suggested by Pro17 hasn't been demonstrated to be useful with this initialization, so we avoid it if verbose: print('\n' + 10 * '=' + " Building Minimax Design " + 10 * '=') x = cp.Variable(len(domain)) c = cp.Variable(len(domain)) constraints = domain._build_constraints(x) if verbose: printer = IterationPrinter(it='4d', minimax='18.10e', dx='9.3e') printer.print_header(it='iter', minimax='minimax est', dx='Δx') V = domain.sample(M0) Xhat_new = np.zeros_like(Xhat) for it in range(maxiter): # Compute new Voronoi vertices V = _update_voronoi(domain, Xhat, V, L, M0) D = cdist(Xhat, V, L=L) d = np.min(D, axis=0) for k in range(M): # Identify closest points to Xhat[k] I = np.isclose(D[k], d) # Move the Xhat[k] to the circumcenter ones = np.ones((1, np.sum(I))) obj = cp.mixed_norm( (L @ (cp.reshape(x, (len(domain), 1)) @ ones - V[I].T)).T, 2, np.inf) prob = cp.Problem(cp.Minimize(obj), constraints) prob.solve() Xhat_new[k] = x.value dx = np.max(np.sqrt(np.sum((Xhat_new - Xhat)**2, axis=1))) if verbose: printer.print_iter(it=it, minimax=np.max(d), dx=dx) Xhat[:, :] = Xhat_new[:, :] if dx < xtol: if verbose: print('small change in design') break return Xhat
def __init__(self, m, K): # Variables: self.var = dict() self.var['X'] = cvx.Variable((m.n_x, K)) self.var['U'] = cvx.Variable((m.n_u, K)) self.var['sigma'] = cvx.Variable(nonneg=True) self.var['nu'] = cvx.Variable((m.n_x, K - 1)) self.var['delta_norm'] = cvx.Variable(nonneg=True) self.var['sigma_norm'] = cvx.Variable(nonneg=True) # Parameters: self.par = dict() self.par['A_bar'] = cvx.Parameter((m.n_x * m.n_x, K - 1)) self.par['B_bar'] = cvx.Parameter((m.n_x * m.n_u, K - 1)) self.par['C_bar'] = cvx.Parameter((m.n_x * m.n_u, K - 1)) self.par['S_bar'] = cvx.Parameter((m.n_x, K - 1)) self.par['z_bar'] = cvx.Parameter((m.n_x, K - 1)) self.par['X_last'] = cvx.Parameter((m.n_x, K)) self.par['U_last'] = cvx.Parameter((m.n_u, K)) self.par['sigma_last'] = cvx.Parameter(nonneg=True) self.par['weight_sigma'] = cvx.Parameter(nonneg=True) self.par['weight_delta'] = cvx.Parameter(nonneg=True) self.par['weight_delta_sigma'] = cvx.Parameter(nonneg=True) self.par['weight_nu'] = cvx.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: constraints += [ self.var['X'][:, k + 1] == cvx.reshape(self.par['A_bar'][:, k], (m.n_x, m.n_x)) * self.var['X'][:, k] + cvx.reshape(self.par['B_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k] + cvx.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 = cvx.sum(cvx.square(self.var['X'] - self.par['X_last']), axis=0) du = cvx.sum(cvx.square(self.var['U'] - self.par['U_last']), axis=0) ds = self.var['sigma'] - self.par['sigma_last'] constraints += [cvx.norm(dx + du, 1) <= self.var['delta_norm']] constraints += [cvx.norm(ds, 'inf') <= self.var['sigma_norm']] # Flight time positive: constraints += [self.var['sigma'] >= 0.1] # Objective: model_objective = m.get_objective(self.var['X'], self.var['U'], self.par['X_last'], self.par['U_last']) sc_objective = cvx.Minimize( self.par['weight_sigma'] * self.var['sigma'] + self.par['weight_nu'] * cvx.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 if model_objective is None else sc_objective + model_objective self.prob = cvx.Problem(objective, constraints)
def fit(self, train_vectors, train_labels, train_features_13, test_vectors, test_labels, test_features_13, weights): c0 = 100 c1 = 10 c2 = 10 train_labels = np.expand_dims(train_labels, axis=1) n = len(train_vectors[0]) beta = cp.Variable((n+1, 1)) # lambd = cp.Parameter(nonneg=True) lambd = 1 Y = train_labels X = train_vectors X = cp.hstack([X, np.ones((len(X),1))]) N = len(train_labels) N0 = len(train_labels[train_features_13 == 0]) N1 = len(train_labels[train_features_13 == 1]) x0 = X[train_features_13 == 0] y0 = Y[train_features_13 == 0] x1 = X[train_features_13 == 1] y1 = Y[train_features_13 == 1] # log_likelihood = cp.sum( # (-cp.reshape(cp.multiply(Y, X @ beta), (len(Y),)) + # cp.log_sum_exp(cp.hstack([np.zeros((len(Y), 1)), X @ beta]), axis=1))) + \ # lambd * cp.norm(beta[0:-1], 2) # log_likelihood = cp.sum( # (cp.multiply(-cp.reshape(cp.multiply(Y, X @ beta), (len(Y),)) + # cp.log_sum_exp(cp.hstack([np.zeros((len(Y), 1)), X @ beta]), axis=1), weights))) + \ # lambd * cp.norm(beta[0:-1], 2) log_likelihood = cp.sum(cp.multiply(cp.reshape(cp.logistic(cp.multiply(-Y, X@beta)), (len(Y),)), weights))\ + lambd * cp.norm(beta[0:-1], 2) # print(np.shape(np.zeros((len(y0), 1)))) problem = cp.Problem(cp.Minimize(log_likelihood), [ # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply(y0, (x0 @ beta)))) \ # >= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply(y1, (x1 @ beta))))-c0, # # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply(y0, (x0 @ beta)))) \ # <= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply(y1, (x1 @ beta)))) + c0, # # # # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply((1-y0)/2*y0, (x0 @ beta)))) \ # >= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply((1-y1)/2*y1, (x1 @ beta)))) - c1, # # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply((1-y0)/2*y0, (x0 @ beta)))) \ # <= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply((1-y1)/2*y1, (x1 @ beta)))) + c1, # # # # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply((1 + y0) / 2 * y0, (x0 @ beta)))) \ # >= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply((1 + y1) / 2 * y1, (x1 @ beta)))) - c2, # # (N1 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y0), 1)), cp.multiply((1 + y0) / 2 * y0, (x0 @ beta)))) \ # <= (N0 / float(N)) * cp.sum(cp.minimum( # np.zeros((len(y1), 1)), cp.multiply((1 + y1) / 2 * y1, (x1 @ beta)))) + c2 (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply(y0, (x0 @ beta)))) \ >= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply(y1, (x1 @ beta))))-c0, (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply(y0, (x0 @ beta)))) \ <= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply(y1, (x1 @ beta)))) + c0, (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1-y0)/2.0, cp.multiply(y0, x0 @ beta)))) \ >= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1-y1)/2.0, cp.multiply(y1, x1 @ beta)))) - c1, (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1-y0)/2.0, cp.multiply(y0, x0 @ beta)))) \ <= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1-y1)/2.0,cp.multiply(y1, x1 @ beta)))) + c1, (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1 + y0) / 2.0, cp.multiply( y0, x0 @ beta)))) \ >= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1 + y1) / 2.0 , cp.multiply(y1, x1 @ beta)))) - c2, (N1 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1 + y0) / 2.0,cp.multiply( y0, x0 @ beta)))) \ <= (N0 / float(N)) * cp.sum(cp.minimum( 0, cp.multiply((1 + y1) / 2.0 ,cp.multiply( y1, x1 @ beta)))) + c2 ]) problem.solve(method='dccp') print(beta.value) self.beta = beta.value loss = self.predict(test_vectors, test_labels, test_features_13) print(loss) return loss
def loss_x3(x, y, lambdaVal, d): diff = y - x[0] return (cp.reshape(lambdaVal, (d, 1))).T @ cp.reshape(diff, (d, 1))
def reconstruct(self, measurement_results): from cvxpy import Variable, atoms, abs, reshape, Minimize, Problem, CVXOPT from traceback import print_exc reconstruction_operator_names = [] reconstruction_operators = [] basis_axes_names = self.reconstruction_basis.keys() basis_vector_norms = np.asarray([ np.linalg.norm(self.reconstruction_basis[r]['operator']) for r in basis_axes_names ]) for reconstruction_operator_name, reconstruction_operator in self.reconstruction_basis.items( ): reconstruction_operator_names.append(reconstruction_operator_name) reconstruction_operators.append( reconstruction_operator['operator']) reconstruction_matrix = [] for rot, projection in self.proj_seq.items(): for measurement_name, projection_operator in projection[ 'operators'].items(): reconstruction_matrix.append([ np.sum(projection_operator * np.conj(reconstruction_operator)) / np.sum(np.abs(reconstruction_operator)**2) for reconstruction_operator in reconstruction_operators ]) reconstruction_matrix_pinv = np.linalg.pinv(reconstruction_matrix) reconstruction_matrix = np.asarray(reconstruction_matrix) self.reconstruction_matrix = reconstruction_matrix self.reconstruction_matrix_pinv = reconstruction_matrix_pinv projections = np.dot(reconstruction_matrix_pinv, measurement_results) reconstruction = { str(k): v for k, v in zip(basis_axes_names, projections) } if self.reconstruction_type == 'cvxopt': #x = cvxpy.Variable(len(projections), complex=True) x = Variable(len(projections), complex=True) rmat_normalized = np.asarray(reconstruction_matrix / np.mean(np.abs(measurement_results)), dtype=complex) meas_normalized = np.asarray(measurement_results).ravel( ) / np.mean(np.abs(measurement_results)) #lstsq_objective = cvxpy.atoms.sum_squares(cvxpy.abs(rmat_normalized @ x - meas_normalized)) lstsq_objective = atoms.sum_squares( abs(rmat_normalized @ x - meas_normalized)) matrix_size = int(np.round(np.sqrt(len(projections)))) #x_reshaped = cvxpy.reshape(x, (matrix_size, matrix_size)) x_reshaped = reshape(x, (matrix_size, matrix_size)) psd_constraint = x_reshaped >> 0 hermitian_constraint = x_reshaped.H == x_reshaped # Create two constraints. constraints = [psd_constraint, hermitian_constraint] # Form objective. #obj = cvxpy.Minimize(lstsq_objective) obj = Minimize(lstsq_objective) # Form and solve problem. prob = cvxpy.Problem(obj, constraints) try: prob.solve(solver=cvxpy.CVXOPT, verbose=True) reconstruction = { str(k): v for k, v in zip(basis_axes_names, np.asarray(x.value)) } except ValueError as e: print_exc() if self.reconstruction_output_mode == 'array': it = np.nditer([self.reconstruction_output_array, None], flags=['refs_ok'], op_dtypes=(object, complex)) with it: for x, z in it: z[...] = reconstruction[str(x)] reconstruction = it.operands[1] return reconstruction
def getControllerParams(y, M): """ This function generates the response time optimal controller parameters for a permanent magnet motor, resulting in zero torque ripple under no measurement noise. It is assumed, for simplicity, that the control inputs are the currents instead of voltages. These currents, once known, can be used to determine the control voltage inputs. :param y: This is the torque vs theta function, which makes the permanent magnet motor model nonlinear. :param M: This is the order of the Fourier series approximation of the nonlinearity. :return: Returns the controller parameters, without the proportionality constant. """ plt.figure() plt.plot(np.linspace(-np.pi, np.pi, len(y)), y) plt.title('The plot of f(theta)') p = np.zeros(M) q = np.zeros(M) for k in range(M): p[k] = 2 * (np.cos( (k + 1) * np.linspace(-np.pi, np.pi, len(y))) @ y) / len(y) q[k] = 2 * (np.sin( (k + 1) * np.linspace(-np.pi, np.pi, len(y))) @ y) / len(y) s = np.zeros(len(y)) x = np.linspace(-np.pi, np.pi, len(y)) for k in range(len(y)): s[k] = (np.cos(np.linspace(1, M, M) * x[k]) @ p + np.sin(np.linspace(1, M, M) * x[k]) @ q) plt.figure() plt.plot(s) plt.plot(y) plt.title('Comparison with the Fourier truncation') Z = np.zeros([4 * M + 1, 2 * M]) #cos and cos for k in range(1, M + 1): for l in range(1, M + 1): if (l == k): Z[0, l - 1] = Z[0, l - 1] + p[k - 1] / 2 Z[2 * (k + l) - 1, l - 1] = Z[2 * (k + l) - 1, l - 1] + p[k - 1] / 2 else: Z[2 * (k + l) - 1, l - 1] = Z[2 * (k + l) - 1, l - 1] + p[k - 1] / 2 Z[2 * np.abs(k - l) - 1, l - 1] = Z[2 * abs(k - l) - 1, l - 1] + p[k - 1] / 2 #cos and sin for k in range(1, M + 1): for l in range(1, M + 1): if (l == k): Z[2 * (k + l) + 1 - 1, l + M - 1] = Z[2 * (k + l) + 1 - 1, l + M - 1] + p[k - 1] / 2 else: Z[2 * (k + l) + 1 - 1, l + M - 1] = Z[2 * (k + l) + 1 - 1, l + M - 1] + p[k - 1] / 2 Z[2 * abs(k - l) + 1 - 1, l + M - 1] = Z[2 * abs(k - l) + 1 - 1, l + M - 1] + np.sign(l - k) * p[k - 1] / 2 # sin and cos for k in range(1, M + 1): for l in range(1, M + 1): if (l == k): Z[2 * (k + l) + 1 - 1, l - 1] = Z[2 * (k + l) + 1 - 1, l - 1] + q[k - 1] / 2 else: Z[2 * (k + l) + 1 - 1, l - 1] = Z[2 * (k + l) + 1 - 1, l - 1] + q[k - 1] / 2 Z[2 * np.abs(k - l) + 1 - 1, l - 1] = Z[2 * np.abs(k - l) + 1 - 1, l - 1] + np.sign(k - l) * q[k - 1] / 2 # sin and sin for k in range(1, M + 1): for l in range(1, M + 1): if (l == k): Z[1 - 1, l + M - 1] = Z[1 - 1, l + M - 1] + q[k - 1] / 2 Z[2 * (k + l) - 1, l + M - 1] = Z[2 * (k + l) - 1, l + M - 1] - q[k - 1] / 2 else: Z[2 * (k + l) - 1, l + M - 1] = Z[2 * (k + l) - 1, l + M - 1] - q[k - 1] / 2 Z[2 * abs(k - l) - 1, l + M - 1] = Z[2 * abs(k - l) - 1, l + M - 1] + q[k - 1] / 2 A = np.zeros([4 * M, 4 * M]) for i in range(1, 2 * M + 1): A[2 * (i - 1), 2 * (i - 1)] = np.cos(2 * np.pi * i / 3) A[2 * (i - 1), 2 * i - 1] = np.sin(2 * np.pi * i / 3) A[2 * i - 1, 2 * (i - 1)] = -np.sin(2 * np.pi * i / 3) A[2 * i - 1, 2 * i - 1] = np.cos(2 * np.pi * i / 3) A = np.vstack((np.hstack( (1, np.zeros(4 * M))), np.hstack((np.zeros([4 * M, 1]), A)))) B = np.zeros([4 * M, 4 * M]) for i in range(1, 2 * M + 1): B[2 * (i - 1), 2 * (i - 1)] = np.cos(4 * np.pi * i / 3) B[2 * (i - 1), 2 * i - 1] = np.sin(4 * np.pi * i / 3) B[2 * i - 1, 2 * (i - 1)] = -np.sin(4 * np.pi * i / 3) B[2 * i - 1, 2 * i - 1] = np.cos(4 * np.pi * i / 3) B = np.vstack((np.hstack( (1, np.zeros(4 * M))), np.hstack((np.zeros([4 * M, 1]), B)))) G = np.hstack((Z, A @ Z, B @ Z)) As1 = np.vstack((np.hstack( (np.zeros(M - 1), 0)), np.hstack((np.eye(M - 1), np.zeros([M - 1, 1]))))) Bs1 = np.vstack((1, np.zeros([M - 1, 1]))) As2 = np.vstack((np.hstack( (np.zeros(M - 1), 0)), np.hstack((np.eye(M - 1), np.zeros([M - 1, 1]))))) Bs2 = np.vstack((1, np.zeros([M - 1, 1]))) As3 = np.vstack((np.hstack( (np.zeros(M - 1), 0)), np.hstack((np.eye(M - 1), np.zeros([M - 1, 1]))))) Bs3 = np.vstack((1, np.zeros([M - 1, 1]))) p1 = cvx.Variable((1, M)) q1 = cvx.Variable((1, M)) p2 = cvx.Variable((1, M)) q2 = cvx.Variable((1, M)) p3 = cvx.Variable((1, M)) q3 = cvx.Variable((1, M)) Q1l = cvx.Variable((M, M), hermitian=True) Q2l = cvx.Variable((M, M), hermitian=True) Q3l = cvx.Variable((M, M), hermitian=True) Q1u = cvx.Variable((M, M), hermitian=True) Q2u = cvx.Variable((M, M), hermitian=True) Q3u = cvx.Variable((M, M), hermitian=True) Ds1u = cvx.Variable() Ds2u = cvx.Variable() Ds3u = cvx.Variable() Ds1l = cvx.Variable() Ds2l = cvx.Variable() Ds3l = cvx.Variable() z = cvx.Variable() Cs1u = cvx.Variable((1, M), complex=True) Cs2u = cvx.Variable((1, M), complex=True) Cs3u = cvx.Variable((1, M), complex=True) Cs1l = cvx.Variable((1, M), complex=True) Cs2l = cvx.Variable((1, M), complex=True) Cs3l = cvx.Variable((1, M), complex=True) r1u = cvx.Variable((1, M), complex=True) r2u = cvx.Variable((1, M), complex=True) r3u = cvx.Variable((1, M), complex=True) r1l = cvx.Variable((1, M), complex=True) r2l = cvx.Variable((1, M), complex=True) r3l = cvx.Variable((1, M), complex=True) constraints = [] constraints = constraints + [ G @ (cvx.hstack((p1, q1, p2, q2, p3, q3)).T) == np.vstack( (1, np.zeros([4 * M, 1]))) ] constraints = constraints + [ cvx.real(r1u) == -p1 / 2, cvx.imag(r1u) == q1 / 2 ] constraints = constraints + [ cvx.real(r2u) == -p2 / 2, cvx.imag(r2u) == q2 / 2 ] constraints = constraints + [ cvx.real(r3u) == -p3 / 2, cvx.imag(r3u) == q3 / 2 ] constraints = constraints + [ cvx.real(r1l) == p1 / 2, cvx.imag(r1l) == -q1 / 2 ] constraints = constraints + [ cvx.real(r2l) == p2 / 2, cvx.imag(r2l) == -q2 / 2 ] constraints = constraints + [ cvx.real(r3l) == p3 / 2, cvx.imag(r3l) == -q3 / 2 ] constraints = constraints + [Cs1u == r1u] constraints = constraints + [Cs2u == r2u] constraints = constraints + [Cs3u == r3u] constraints = constraints + [Cs1l == r1l] constraints = constraints + [Cs2l == r2l] constraints = constraints + [Cs3l == r3l] constraints = constraints + [Ds1u == z / 2] constraints = constraints + [Ds2u == z / 2] constraints = constraints + [Ds3u == z / 2] constraints = constraints + [Ds1l == z / 2] constraints = constraints + [Ds2l == z / 2] constraints = constraints + [Ds3l == z / 2] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q1u - (As1.T) @ Q1u @ As1, -(As1.T) @ Q1u @ Bs1)), cvx.hstack((-cvx.conj( (As1.T) @ Q1u @ Bs1).T, -(Bs1.T) @ Q1u @ Bs1)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs1u).T)), cvx.hstack( (-Cs1u, cvx.reshape(Ds1u + Ds1u, [1, 1]))))) >> 0 ] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q2u - (As2.T) @ Q2u @ As2, -(As2.T) @ Q2u @ Bs2)), cvx.hstack((-cvx.conj( (As2.T) @ Q2u @ Bs2).T, -(Bs2.T) @ Q2u @ Bs2)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs2u).T)), cvx.hstack( (-Cs2u, cvx.reshape(Ds2u + Ds2u, [1, 1]))))) >> 0 ] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q3u - (As3.T) @ Q3u @ As3, -(As3.T) @ Q3u @ Bs3)), cvx.hstack((-cvx.conj( (As3.T) @ Q3u @ Bs3).T, -(Bs3.T) @ Q3u @ Bs3)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs3u).T)), cvx.hstack( (-Cs3u, cvx.reshape(Ds3u + Ds3u, [1, 1]))))) >> 0 ] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q1l - (As1.T) @ Q1l @ As1, -(As1.T) @ Q1l @ Bs1)), cvx.hstack((-cvx.conj( (As1.T) @ Q1l @ Bs1).T, -(Bs1.T) @ Q1l @ Bs1)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs1l).T)), cvx.hstack( (-Cs1l, cvx.reshape(Ds1l + Ds1l, [1, 1]))))) >> 0 ] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q2l - (As2.T) @ Q2l @ As2, -(As2.T) @ Q2l @ Bs2)), cvx.hstack((-cvx.conj( (As2.T) @ Q2l @ Bs2).T, -(Bs2.T) @ Q2l @ Bs2)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs2l).T)), cvx.hstack( (-Cs2l, cvx.reshape(Ds2l + Ds2l, [1, 1]))))) >> 0 ] constraints = constraints + [ cvx.vstack( (cvx.hstack((Q3l - (As3.T) @ Q3l @ As3, -(As3.T) @ Q3l @ Bs3)), cvx.hstack((-cvx.conj( (As3.T) @ Q3l @ Bs3).T, -(Bs3.T) @ Q3l @ Bs3)))) + cvx.vstack( (cvx.hstack((np.zeros([M, M]), -cvx.conj(Cs3l).T)), cvx.hstack( (-Cs3l, cvx.reshape(Ds3l + Ds3l, [1, 1]))))) >> 0 ] prob = cvx.Problem(cvx.Minimize(z), constraints) prob.solve(solver=cvx.SCS, verbose=True) return p, q, z.value, p1.value, p2.value, p3.value, q1.value, q2.value, q3.value
Aeq2 = np.zeros((m, n)) beq2 = np.zeros((m, n)) for p in range(m): if (rssi[p, int(prev_out_ap[p] - 1)] > -75): Aeq2[p, int(prev_out_ap[p] - 1)] = 1 beq2[p, int(prev_out_ap[p] - 1)] = 1 variables = cp.Variable((m, n), boolean=True) constraint1 = A1 * variables <= b1 constraint2 = cp.multiply(A2, variables) <= b2 constraint3 = Aeq1 * variables.T == beq1 constraint4 = cp.multiply(Aeq2, variables) == beq2 total_utility = cp.sum(cp.multiply( obj, variables)) - 0.5 * cp.max( cp.sum(cp.reshape(variables, (m, n)), axis=0)) problem = cp.Problem( cp.Maximize(total_utility), [constraint1, constraint2, constraint3, constraint4]) problem.solve(solver=cp.GLPK_MI) for p in range(m): ind = int(np.nonzero(variables.value[p, :])[0][0]) out_ap_step[p, k] = ind + 1 prev_out_ap = out_ap_step[:, k].reshape(m) out_ap = mode(out_ap_step, axis=1)[0].reshape(m) np.savetxt("out_ap.csv", out_ap, fmt="%d") prev_out_ap = out_ap else: d = 1000 * np.ones((m, n))
def PLS_cvxpy(N, TT, y, X, K, lambda_, R, tol=0.0001): p = X.shape[1] beta0 = np.zeros(N * p).reshape(N, p) for i in range(1, N + 1): ind = [] ind.append((i - 1) * TT) ind.append(i * TT) yy = y[ind[0]:ind[1]] XX = X[ind[0]:ind[1]] beta0[i - 1, ] = (np.linalg.inv(XX.T @ XX) @ (XX.T @ yy)).T b_out = np.array([beta0] * K) a_out = np.zeros(K * p).reshape(K, p) b_old = np.ones(N * p).reshape(N, p) a_old = np.ones(p).reshape(1, p) for r in range(1, R + 1): for k in range(1, K + 1): gamma = pen_generate(b_out, a_out, N, p, K, k) X_list = [] for i in range(1, N + 1): ind = [] ind.append((i - 1) * TT) ind.append(i * TT) id_ = [] id_.append((i - 1) * p) id_.append(i * p) X_list.append(X[ind[0]:ind[1]]) b = cp.Variable((p, N)) a = cp.Variable((p, 1)) A = np.ones(N).reshape(1, N) obj1 = cp.norm(b - (a @ A), 2, axis=0) @ gamma XX = block_diag(*X_list) obj = cp.Minimize( cp.sum_squares(y - cp.reshape((XX @ cp.vec(b)), (N * TT, 1))) / (N * TT) + obj1 * (lambda_ / N)) prob = cp.Problem(obj) try: prob.solve(solver=cp.MOSEK) except: prob.solve(solver=cp.ECOS) a_out[k - 1] = a.value.reshape(1, p) b_out[k - 1] = b.value.T a_new = np.copy(a_out[K - 1]) b_new = np.copy(b_out[K - 1]) if (criterion(a_old, a_new, b_old, b_new, tol) == True): break a_old = np.copy(a_out[K - 1]) b_old = np.copy(b_out[K - 1]) a_out_exp = np.transpose(np.array([a_out] * N), (1, 0, 2)) d_temp = (b_out - a_out_exp)**2 dist = np.sqrt(np.apply_along_axis(np.sum, 2, d_temp).T) group_est = np.apply_along_axis(np.argmin, 1, dist) count = np.unique(group_est, return_counts=True) if np.count_nonzero(count[1] > p) == K: a_out = post_lasso(group_est, y, X, K, p, N, TT) b_est = np.empty(N * p).reshape(N, p) for i in range(1, N + 1): group = group_est[i - 1] b_est[i - 1, ] = a_out[group, ] return b_est, a_out, group_est
def factorize(self): """ Compute matrix factorization. Return fitted factorization model. """ for run in range(self.n_run): self.W, self.H = self.seed.initialize( self.V, self.rank, self.options) p_obj = c_obj = sys.float_info.max best_obj = c_obj if run == 0 else best_obj iter = 0 if self.callback_init: self.final_obj = c_obj self.n_iter = iter mffit = mf_fit.Mf_fit(self) self.callback_init(mffit) #CVX code goes here m,n = self.V.shape # Uncomment to normalize matrix summed = 1./np.sum(self.V, axis=0) D= np.diagflat(summed) self.V = self.V @ D x = cvx.Variable([n,n]) kappa = 0.2 beta = 2/self.rank epsilon = kappa * (1-beta)/((self.rank-1)*(1-beta)+1) if self.p is None: p = np.random.rand(n,1) elif self.p.shape != (n,1) and self.p.shape != (1,n): p = np.random.rand(n,1) else: p = self.p if p.shape == (1,n): p = p.T objective = cvx.Minimize(p.T @ cvx.reshape(cvx.diag(x),(n,1))) constraints = [x >= 0] for i in range(0, n): constraints += [ cvx.norm((np.reshape(self.V[:,i], (m,1)) - self.V @ cvx.reshape(x[:,i], (n,1))), p=1) <= 2*epsilon, x[i,i] <= 1 ] for j in range(0,n): constraints += [x[i,j] <= x[i,i]] constraints += [cvx.trace(x) == self.rank] prob = cvx.Problem(objective, constraints) prob.solve() print("Problem Value: " + str(prob.value)) print("X: ") print(x.value) # Create a copy to make sure it's not immutable X = np.array(np.diag(x.value)) K = [] for i in range(0, self.rank): b = np.unravel_index(np.argmax(X), X.shape)[0] K.append(b) X[b] = -1 print("K:") print(K) self.K = K if self.callback: self.final_obj = c_obj self.n_iter = iter mffit = mf_fit.Mf_fit(self) self.callback(mffit) if self.track_factor: self.tracker.track_factor( run, W=self.W, H=self.H, final_obj=c_obj, n_iter=iter) # if multiple runs are performed, fitted factorization model with # the lowest objective function value is retained if c_obj <= best_obj or run == 0: best_obj = c_obj self.n_iter = iter self.final_obj = c_obj mffit = mf_fit.Mf_fit(copy.deepcopy(self)) mffit.fit.tracker = self.tracker return mffit
def simulate_chain(in_prob): # Get a ParamConeProg object reductions = [Dcp2Cone(), CvxAttr2Constr(), ConeMatrixStuffing()] chain = Chain(None, reductions) cone_prog, inv_prob2cone = chain.apply(in_prob) # Dualize the problem, reconstruct a high-level cvxpy problem for the dual. # Solve the problem, invert the dualize reduction. solver = ConicSolver() cone_prog = solver.format_constraints(cone_prog, exp_cone_order=[0, 1, 2]) data, inv_data = a2d.Dualize.apply(cone_prog) A, b, c, K_dir = data[s.A], data[s.B], data[s.C], data['K_dir'] y = cp.Variable(shape=(A.shape[1], )) constraints = [A @ y == b] i = K_dir[a2d.FREE] dual_prims = {a2d.FREE: y[:i], a2d.SOC: []} if K_dir[a2d.NONNEG]: dim = K_dir[a2d.NONNEG] dual_prims[a2d.NONNEG] = y[i:i + dim] constraints.append(y[i:i + dim] >= 0) i += dim for dim in K_dir[a2d.SOC]: dual_prims[a2d.SOC].append(y[i:i + dim]) constraints.append(SOC(y[i], y[i + 1:i + dim])) i += dim if K_dir[a2d.DUAL_EXP]: exp_len = 3 * K_dir[a2d.DUAL_EXP] dual_prims[a2d.DUAL_EXP] = y[i:i + exp_len] y_de = cp.reshape(y[i:i + exp_len], (exp_len // 3, 3), order='C') # fill rows first constraints.append( ExpCone(-y_de[:, 1], -y_de[:, 0], np.exp(1) * y_de[:, 2])) i += exp_len if K_dir[a2d.DUAL_POW3D]: alpha = np.array(K_dir[a2d.DUAL_POW3D]) dual_prims[a2d.DUAL_POW3D] = y[i:] y_dp = cp.reshape(y[i:], (alpha.size, 3), order='C') # fill rows first pow_con = PowCone3D(y_dp[:, 0] / alpha, y_dp[:, 1] / (1 - alpha), y_dp[:, 2], alpha) constraints.append(pow_con) objective = cp.Maximize(c @ y) dual_prob = cp.Problem(objective, constraints) dual_prob.solve(solver='SCS', eps=1e-8) dual_prims[a2d.FREE] = dual_prims[a2d.FREE].value if K_dir[a2d.NONNEG]: dual_prims[a2d.NONNEG] = dual_prims[a2d.NONNEG].value dual_prims[a2d.SOC] = [expr.value for expr in dual_prims[a2d.SOC]] if K_dir[a2d.DUAL_EXP]: dual_prims[a2d.DUAL_EXP] = dual_prims[a2d.DUAL_EXP].value if K_dir[a2d.DUAL_POW3D]: dual_prims[a2d.DUAL_POW3D] = dual_prims[a2d.DUAL_POW3D].value dual_duals = {s.EQ_DUAL: constraints[0].dual_value} dual_sol = cp.Solution(dual_prob.status, dual_prob.value, dual_prims, dual_duals, dict()) cone_sol = a2d.Dualize.invert(dual_sol, inv_data) # Pass the solution back up the solving chain. in_prob_sol = chain.invert(cone_sol, inv_prob2cone) in_prob.unpack(in_prob_sol)
]) #print(gstack.shape) y = scores keras_l = np.sum(k.eval(categorical_hinge(y_train[indices, :], y))) #print(y) #print(y.reshape((-1,1))) wk = np.vstack([param.reshape(-1, 1) for param in weights]) print("SETTING UP CONVEX PROBLEM") w = cp.Variable((num_weights, 1)) #yhat = cp.Variable((batch_size,num_classes)) #const = [yhat == y + cp.reshape((gstack.T@(w - wk)),(batch_size,num_classes))] const = [] yhat = y + cp.reshape((gstack.T @ (w - wk)), (batch_size, num_classes)) #f = cp.sum(-yhat[np.arange(batch_size),true_class] + cp.log_sum_exp(yhat, axis=1)) + cp.norm(w,2) f = cp.sum( cp.pos(yhat - yhat[np.arange(batch_size), [true_class]].T @ np.ones( (1, 3)) + 1)) + 10 * cp.norm(w - wk, 2) objective = cp.Minimize(f) prob = cp.Problem(objective, const) print("LEGGO") r = prob.solve(solver="SCS", verbose=False) print("Current Loss: ", keras_l) print("SOLVED!, l = ", r) w_new = w.value shapes = grads[0] ind = 0 w_ = [] #print(w_new.shape)
def loss_y1(lambdaVal, x, y, workers, d): val = cp.sum([(cp.reshape(lambdaVal[i], (d, 1))).T @ cp.reshape(y[i] - x[i + 1][0], (d, 1)) for i in range(0, workers)]) return 1
def __init__(self, m, K): # Variables: self.var = dict() self.var['X'] = cvx.Variable((m.n_x, K)) self.var['U'] = cvx.Variable((m.n_u, K)) self.var['nu'] = cvx.Variable((m.n_x, K - 1)) self.var['sigma'] = cvx.Variable(nonneg=True) # Parameters: self.par = dict() self.par['A_bar'] = cvx.Parameter((m.n_x * m.n_x, K - 1)) self.par['B_bar'] = cvx.Parameter((m.n_x * m.n_u, K - 1)) self.par['C_bar'] = cvx.Parameter((m.n_x * m.n_u, K - 1)) self.par['S_bar'] = cvx.Parameter((m.n_x, K - 1)) self.par['z_bar'] = cvx.Parameter((m.n_x, K - 1)) self.par['X_last'] = cvx.Parameter((m.n_x, K)) self.par['U_last'] = cvx.Parameter((m.n_u, K)) self.par['sigma_last'] = cvx.Parameter(nonneg=True) self.par['weight_sigma'] = cvx.Parameter(nonneg=True) self.par['weight_nu'] = cvx.Parameter(nonneg=True) self.par['tr_radius'] = cvx.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: constraints += [ self.var['X'][:, k + 1] == cvx.reshape(self.par['A_bar'][:, k], (m.n_x, m.n_x)) * self.var['X'][:, k] + cvx.reshape(self.par['B_bar'][:, k], (m.n_x, m.n_u)) * self.var['U'][:, k] + cvx.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 region: du = self.var['U'] - self.par['U_last'] dx = self.var['X'] - self.par['X_last'] ds = self.var['sigma'] - self.par['sigma_last'] constraints += [ cvx.norm(dx, 1) + cvx.norm(du, 1) + cvx.norm(ds, 1) <= self.par['tr_radius'] ] # Objective: model_objective = m.get_objective(self.var['X'], self.var['U'], self.par['X_last'], self.par['U_last']) sc_objective = cvx.Minimize( self.par['weight_sigma'] * self.var['sigma'] + self.par['weight_nu'] * cvx.norm(self.var['nu'], 1)) objective = sc_objective if model_objective is None else sc_objective + model_objective self.prob = cvx.Problem(objective, constraints)
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 optimize(self, power, solar, prices, Q0, Pmax0, f_p): # f_p is predicted fan power consumption n, T = power.shape cmax = np.tile(self.cmax, (self.n_b, T)) dmax = np.tile(self.dmax, (self.n_b, T)) Qmin = np.tile(self.Qmin, (self.n_b, T + 1)) Qmax = np.tile(self.Qmax, (self.n_b, T + 1)) #fmax = np.tile(self.fmax, (self.n_f, T)) #hmax = np.tile(self.hmax, (self.n_t, T + 1)) solar = np.tile(solar, (self.n_f, 1)) # print(solar.shape) # print(solar) c = cvx.Variable((self.n_b, T)) d = cvx.Variable((self.n_b, T)) #f = cvx.Variable((self.n_f, T)) Q = cvx.Variable((self.n_b, T + 1)) #h = cvx.Variable((self.n_t, T + 1)) # Battery, fan, THI, Constraints constraints = [ c <= cmax, c >= 0, d <= dmax, d >= 0, #f >= 0, #f <= fmax, Q[:, 0] == Q0, Q[:, 1:T + 1] == self.l_eff * Q[:, 0:T] + self.c_eff * c * self.t_res - self.d_eff * d * self.t_res, Q >= Qmin, Q <= Qmax, #h[:, 0] == h0.flatten() ] # THI vs fan power model #for t in range(0, T): # constraints.append( # h[:, t + 1] == self.coeff_h * h[:, t] + np.dot(self.coeff_x, f_exo[:, t]) # + self.coeff_f * f[:, t] + self.coeff_b) # not a constraint, just a definition net = cvx.hstack([ power.reshape((1, power.size)) + c - d + cvx.reshape(cvx.sum(cvx.pos(f_p - solar), axis=0), (1, T)) - Pmax0, np.zeros((1, 1)) ]) obj = cvx.Minimize( cvx.sum( cvx.multiply( prices, cvx.pos( power.reshape((1, power.size)) + c - d + cvx.reshape(cvx.sum(cvx.pos(f_p - solar), axis=0), (1, T))))) # cost min + self.d_price * cvx.max(net) # demand charge + self.b_price * cvx.sum(c + d) # battery degradation # attempt to make battery charge at night * doesnt do anything # + 0.0001 * cvx.sum_squares((power.reshape((1, power.size)) # + c - d + cvx.reshape(cvx.sum(cvx.pos(f_p - solar), axis=0), (1, T)))/np.max(power)) # + self.h_price * cvx.sum_squares(cvx.pos(h - hmax)) # THI penalty ) prob = cvx.Problem(obj, constraints) prob.solve(solver=cvx.ECOS) # calculate expected max power net = power + c.value - d.value + np.sum(np.clip(f_p, 0, None), axis=0) - Pmax0 Pmax_new = np.max(net) + Pmax0 if Pmax_new < Pmax0: Pmax_new = Pmax0 return c.value, d.value, Q.value, prob.status, Pmax_new
alpha = cp.Parameter(shape=1) alpha.value = [packet_arrival_per_device] beta = cp.Parameter(shape=1) beta.value = [nb_devices] gamma = cp.Parameter(shape=np.shape(external_packet_arrival)) gamma.value = np.array(external_packet_arrival) delta = cp.Parameter(shape=len(SF)) delta.value = np.array(time_on_air[:, -1]) G_pure_aloha = cp.multiply(cp.multiply(alpha, beta), p) G_pure_aloha = cp.vstack(G_pure_aloha[i, j] + gamma[i, j] for i in range(nb_sf) for j in range(nb_channels)) G_pure_aloha = cp.reshape(G_pure_aloha, (nb_sf, nb_channels)) G_pure_aloha = cp.vstack(G_pure_aloha[i, j] * delta[i] for i in range(nb_sf) for j in range(nb_channels)) #G_pure_aloha = cp.vstack(G_pure_aloha[i] * cp.exp(G_pure_aloha[i]) for i in range(nb_sf*nb_channels)) G_pure_aloha = cp.reshape(G_pure_aloha, (nb_sf, nb_channels)) # objective = cp.Maximize( cp.sum(cp.log(G_pure_aloha)) - 2 * cp.sum(G_pure_aloha)) #objective = cp.Maximize(cp.sum(G_pure_aloha)) contraints = [ 0 <= p, p <= 1, cp.sum(p) == 1, cp.sum(p[0:, :]) >= np.divide(nb_devices_SF[0], nb_devices), cp.sum(p[0:1, :]) <= np.sum(np.divide(nb_devices_SF, nb_devices)[0:1]),
m = 10 n = 10 k = 3 A = np.matrix(np.random.rand(m,n)) A -= np.mean(A, axis=0) K = np.array([(A[i].T*A[i]).flatten() for i in range(m)]) # Problem construction sigma_inv1 = cp.Variable(n,n) # Inverse covariance matrix t = cp.Variable(m) tdet = cp.Variable(1) f = cp.sum_largest(t+tdet, k) z = K*cp.reshape(sigma_inv1, n*n, 1) C = [-cp.log_det(sigma_inv1) <= tdet, t == z] prob = cp.Problem(cp.Minimize(f), C) # Problem collection # Single problem collection problemDict = { "problemID" : problemID, "problem" : prob, "opt_val" : opt_val } problems = [problemDict]