def sdpRho(self): ''' Build and solve optimisation for rho With parameters, we could build it only once. ''' constraints = [] n = self.dimension**self.nbJoueurs rho = cp.Variable((n, n), hermitian=True) constraints += [rho >> 0] constraints += [cp.trace(rho) == 1] socialWelfaire = cp.Constant(0) winrate = cp.Constant(0) for question in self.game.questions(): for answer in self.game.validAnswerIt(question): proba = self.probaRho(answer, question, rho) socialWelfaire += self.game.questionDistribution * self.game.answerPayoutWin( answer) * proba winrate += self.game.questionDistribution * proba sdp = cp.Problem(cp.Maximize(cp.real(socialWelfaire)), constraints) sdp.solve(solver=cp.MOSEK, verbose=False) return rho
def addDependentTerm(self,uc_type,dep_model,dim=None,L=None): """ Add a new q term, i.e. a state and/or input dependent term. Parameters ---------- uc_type : str Uncertainy type. Must be either of "input", "state" or "process". dep_model : DependencyDescription Description of the state and input dependency (see class above). dim : int, optional Dimension of this q term. Obligatory if S is not provided. L : array, optional Optional specification of mapping matrix for set, {L*q : ||q||<=phi(||x||,||u||)}. Obligatory if dim is not provided. """ if L is None: L = np.eye(dim) else: dim = L.shape[1] self.mtx_map.append(L) self.L.append(np.vstack((np.zeros((self.W.shape[0],dim)),L))) self.dependency.append(dep_model) ball = NormBall(dim,dep_model.pq) self.sets.append(ball) sampler = lambda x,u: ball(dep_model.phi_direct(cvx.Constant(x),cvx.Constant(u)).value) self.sample_q.append(sampler) self.sample_sim.append(lambda t,x,u: L.dot(sampler(x,u))) self.type.append(uc_type) self.is_dependent.append(True) # Extend the other matrices self.W = np.vstack((self.W,np.zeros((L.shape[0],self.W.shape[1])))) for i in range(len(self.L)-1): self.L[i] = np.vstack((self.L[i],np.zeros((L.shape[0],self.L[i].shape[1]))))
def total_variation_plus_seasonal_filter(signal, c1=10, c2=500): ''' 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 ''' 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) index_set = ~np.isnan(signal) w = len(signal) / np.sum(index_set) objective = cvx.Minimize((365 * 3 / len(signal)) * w * cvx.sum(cvx.huber(s_error)) + c1 * cvx.norm1(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))) constraints = [ signal[index_set] == s_hat[index_set] + s_seas[index_set] + s_error[index_set], s_seas[365:] - s_seas[:-365] == 0, cvx.sum(s_seas[:365]) == 0 ] problem = cvx.Problem(objective=objective, constraints=constraints) problem.solve() return s_hat.value, s_seas.value
def __init__(self, I, J, v, b, Ab=None): """ Args: I : ndarray of bools, shape (nfuns, npoints) J : ndarray of ints, shape (nregions, nsubpoints) v : ndarray of floats, shape (npoints, ndim) b : ndarray of floats, shape (nfuns, npoints) """ Operator.__init__(self) assert I.shape == b.shape assert I.shape[1] == v.shape[0] nfuns, npoints = I.shape nregions, nsubpoints = J.shape ndim = v.shape[1] self.I, self.J, self.v, self.b = I, J, v, b self.Ab = epigraph_Ab(I, J, v, b) if Ab is None else Ab self.x = Variable((nregions, nfuns, ndim + 1)) self.y = self.x import cvxpy as cp self.cp_tol = 1e-7 self.cp_x = cp.Parameter(self.x.size) self.cp_y = cp.Variable(self.y.size) self.cp_prob = cp.Problem( cp.Minimize(cp.sum_squares(self.cp_y - self.cp_x)), [cp.Constant(self.Ab[0]) * self.cp_y <= cp.Constant(self.Ab[1])])
def convex_cleanup(phrase_probs, lex_probs, group_indices): size = phrase_probs.size try: assert (size == lex_probs.size) except AssertionError: print("The probability distribution does not have the same size", file=stderr) return uniq_entries = group_indices.shape[0] group_indices = cvx.Constant(group_indices) marginals = cvx.Constant(np.ones(uniq_entries)) sparse_dist = cvx.Variable(size) lambd = cvx.Parameter(sign="positive") reg = cvx.norm(sparse_dist, 1) phrase_cost = cvx.sum_entries(cvx.kl_div(sparse_dist, phrase_probs)) lex_cost = cvx.sum_entries(cvx.kl_div(sparse_dist, lex_probs)) prob_violations = cvx.sum_entries(group_indices * sparse_dist - marginals) cost = phrase_cost + lex_cost + reg * lambd + prob_violations constraints = [ sparse_dist >= 0, # Positive probability values \ # cvx.sum_entries(group_indices*sparse_dist) == marginals.T, \ # Sum of probabilities is 1 \ # making this a constraint seems to slow down the optimizer, # adding this to cost instead; ] for reg_param in np.arange(0.0, 1.0, 0.05): lambd.value = reg_param opt_instance = cvx.Problem(cvx.Minimize(cost), constraints) opt_instance.solve() yield sparse_dist
def run(self, bases, observation, verbose=True): """Runs the model calculations. Bases and observations are loaded into proper polarized data sets. In this step, arguments are checked to ensure polarization angles match in value and order. Any bases that have not been built already are built with their corresponding ``build()`` method. Bases and observations of multiple polarizations are then concatenated and given to cvxpy and MOSEK for solving. Results are returned and processed in inherited ``Model`` attributes. Args: bases (list of Basis): The basis objects (built or not) of several polarizations, to be used for fitting. observation (list of Observation): The observation objects of several polarizations, to be used for fitting. verbose (bool): The console verbosity of the called solver (MOSEK). """ super().run(bases, observation) print('Bases and observations loaded in model') t0 = time.time() print('Forming Regularized problem:') print(' Setting up basis and observation matrices') A = sp.vstack([self.data_set(angle).basis.basis_matrix for angle in self.polarization_angles]) basis_rows = A.shape[0] n = A.shape[1] + 1 o = sp.csc_matrix((np.ones(basis_rows, ), (np.arange(basis_rows), np.zeros(basis_rows, )))) A = sp.hstack((A, o)) H = cvx.Constant(A) b = np.vstack([self.data_set(angle).observation.data.reshape((180 * 1024, 1), order='F') for angle in self.polarization_angles]) b = cvx.Constant(b) x = cvx.Variable(n) print(' Defining regularization term with alpha = {0}'.format(self.alpha)) D = np.diag(np.ones((A.shape[1] - 1,)), k=1) - np.diag(np.ones((A.shape[1],))) D = cvx.Constant(D[:-1, :]) alpha = self.alpha print(' Defining objective') objective = cvx.Minimize(cvx.norm2(H * x - b) + alpha * cvx.norm2(D * x)) print(' Defining constraints') constraints = [x[:-1] >= 0] print(' Defining problem') prob = cvx.Problem(objective, constraints) print('Problem Formulation DONE\n\nCalling the solver: ' + cvx.MOSEK) ts0 = time.time() prob.solve(solver=cvx.MOSEK, verbose=verbose) ts1 = time.time() print(cvx.MOSEK + ' done in {0:.2f} seconds.'.format(ts1 - ts0)) if x.value is not None: print('\nProcessing solution') self.solver_result = np.array(x.value) self.background = self.solver_result[-1] self._process_result(self.solver_result[:-1]) t1 = time.time() print('Fitting DONE:\n Elapsed time: {0:.2f} s'.format(t1-t0))
def set_environmental_variables(self): """ Slices cast values of the environmental values for the current timestep. :return: None """ start_slice = self.start_hour_index + self.timestep end_slice = start_slice + self.horizon + 1 # Need to extend 1 timestep past horizon for OAT slice # Get the current values from a list of all values self.ghi_current = self.all_ghi[start_slice:end_slice] self.ghi_current_ev = deepcopy(self.ghi_current) ghi_noise = np.multiply( self.ghi_current[1:], 0.01 * np.power(1.3 * np.ones(self.horizon), np.arange(self.horizon))) self.ghi_current_ev[1:] = np.add(self.ghi_current[1:], ghi_noise) self.oat_current = self.all_oat[start_slice:end_slice] self.oat_current_ev = deepcopy(self.oat_current) oat_noise = np.multiply( np.power(1.1 * np.ones(self.horizon), np.arange(self.horizon)), np.random.randn(self.horizon)) self.oat_current_ev[1:] = np.add(self.oat_current[1:], oat_noise) self.tou_current = self.all_tou[start_slice:end_slice] self.base_price = np.array(self.tou_current, dtype=float) # Set values as cvxpy values self.oat_forecast = cp.Constant(self.oat_current) self.ghi_forecast = cp.Constant(self.ghi_current) self.cast_redis_curr_rps()
def construct_state_equations_and_storage_constraint(self): """ Implementation of state equations, and states capacities (storages) :return: list of cvx.Constraint """ constraint_state_eq = [] # 1) every intermediate / output state starts with a quantity of 0 kg for s in range(self.S): if s not in self.input_states: constraint_state_eq.append( self.y_s[s] == 0 ) # 2) state equations and storage capacities for t in range(self.T): for s in range(self.S): self.y_st_inflow[s,t] = cvx.Constant(0) self.y_st_outflow[s,t] = cvx.Constant(0) for i in range(self.I): for j in range(self.J): if self.J_i[i,j]: # set inflows if (t-self.P[j,s] >= 0): self.y_st_inflow[s,t] += self.rho_out[j,s]*self.y_ijt[i,j,t-self.P[j,s]] # set outflows self.y_st_outflow[s,t] += self.rho_in[j,s]*self.y_ijt[i,j,t] constraint_state_eq.append( self.C_min[s] <= self.y_s[s] + sum( [self.y_st_inflow[(s,tt)] for tt in range(t+1)] ) - sum( [self.y_st_outflow[(s,tt)] for tt in range(t+1)] )) constraint_state_eq.append( self.C_max[s] >= self.y_s[s] + sum( [self.y_st_inflow[(s,tt)] for tt in range(t+1)] ) - sum( [self.y_st_outflow[(s,tt)] for tt in range(t+1)] )) return constraint_state_eq
def sdpPlayer(self, playerId, Qeq): ''' Build and solve optimisation for playerId With parameters, we could build it only once. ''' constraints = [] varDict = {} for type in ["0", "1"]: for answer in [str(a) for a in range(self.dimension)]: varMatrix = cp.Variable((self.dimension, self.dimension), hermitian=True) constraints += [varMatrix >> 0] #We must create a matrix by hand, cp.Variabel((2,2)) can't be used as first arguement of cp.kron(a, b) or np.kron(a, b) #var = [[varMatrix[0, 0], varMatrix[0, 1]], [varMatrix[1, 0], varMatrix[1, 1]]] varDict[answer + type] = varMatrix #Erreur toqito constraint0 = varDict["00"] constraint1 = varDict["01"] for a in range(1, self.dimension): constraint0 += varDict[str(a) + "0"] constraint1 += varDict[str(a) + "1"] constraints += [cp.bmat(constraint0) == np.eye(self.dimension)] constraints += [cp.bmat(constraint1) == np.eye(self.dimension)] socialWelfare = cp.Constant(0) playerPayout = cp.Constant(0) winrate = cp.Constant(0) for question in self.game.questions(): for answer in self.game.validAnswerIt(question): proba = self.probaPlayer(answer, question, playerId, varDict) socialWelfare += self.game.questionDistribution * self.game.answerPayoutWin( answer) * proba playerPayout += self.game.questionDistribution * self.game.playerPayoutWin( answer, playerId) * proba winrate += self.game.questionDistribution * proba #If Qeq flag, we optimise the player's payout, not the QSW. if Qeq: sdp = cp.Problem(cp.Maximize(cp.real(playerPayout)), constraints) else: sdp = cp.Problem(cp.Maximize(cp.real(socialWelfare)), constraints) sdp.solve(solver=cp.MOSEK, verbose=False) self.QSW = cp.real(socialWelfare).value self.winrate = cp.real(winrate).value self.lastDif = np.abs( cp.real(playerPayout).value - self.playersPayout[playerId]) print("player payout {} updateDiff {}".format( cp.real(playerPayout).value, self.lastDif)) self.playersPayout[playerId] = cp.real(playerPayout).value return varDict
def get_initial_conditions(self): self.water_draws() if self.timestep == 0: self.temp_in_init = cp.Constant(self.t_in_init) self.temp_wh_init = cp.Constant( (self.t_wh_init * (self.wh_size - self.draw_size[0]) + self.tap_temp * self.draw_size[0]) / self.wh_size) if 'battery' in self.type: self.e_batt_init = cp.Constant( float(self.home["battery"]["e_batt_init"])) self.p_batt_ch_init = cp.Constant(0) self.counter = 0 else: self.temp_in_init = cp.Constant( float(self.prev_optimal_vals["temp_in_opt"])) self.temp_wh_init = cp.Constant( (float(self.prev_optimal_vals["temp_wh_opt"]) * (self.wh_size - self.draw_size[0]) + self.tap_temp * self.draw_size[0]) / self.wh_size) if 'battery' in self.type: self.e_batt_init = cp.Constant( float(self.home["battery"]["e_batt_init"])) self.e_batt_init = cp.Constant( float(self.prev_optimal_vals["e_batt_opt"])) self.p_batt_ch_init = cp.Constant( float(self.prev_optimal_vals["p_batt_ch"]) - float(self.prev_optimal_vals["p_batt_disch"])) self.counter = int(self.prev_optimal_vals["solve_counter"])
def cvxpy_partial_trace_im(rho_re, rho_im, dims, axis=0): if not isinstance(rho_re, Expression): rho_re = cvxpy.Constant(shape=rho_re.shape, value=rho_re) if not isinstance(rho_im, Expression): rho_im = cvxpy.Constant(shape=rho_im.shape, value=rho_im) rho_np_re = expr_as_np_array(rho_re) rho_np_im = expr_as_np_array(rho_im) traced_rho = true_np.imag(np_partial_trace(rho_np_re, dims, axis)) \ + true_np.real(np_partial_trace(rho_np_im, dims, axis)) traced_rho = np_array_as_expr(traced_rho) return traced_rho
def test_matmul_canon(self): X = cvxpy.Constant(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])) Y = cvxpy.Constant(np.array([[1.0], [2.0], [3.0]])) Z = cvxpy.matmul(X, Y) canon_matrix, constraints = dgp_atom_canon.mulexpression_canon( Z, Z.args) self.assertEqual(len(constraints), 0) self.assertEqual(canon_matrix.shape, (2, 1)) first_entry = np.log(np.exp(2.0) + np.exp(4.0) + np.exp(6.0)) second_entry = np.log(np.exp(5.0) + np.exp(7.0) + np.exp(9.0)) self.assertAlmostEqual(first_entry, canon_matrix[0, 0].value) self.assertAlmostEqual(second_entry, canon_matrix[1, 0].value)
def setup_pv_problem(self): """ Adds CVX variables for photovoltaic subsystem in pv and battery_pv homes. :return: None """ # Define constants self.pv_area = cp.Constant(float(self.home["pv"]["area"])) self.pv_eff = cp.Constant(float(self.home["pv"]["eff"])) # Define PV Optimization variables self.p_pv = cp.Variable(self.horizon) self.u_pv_curt = cp.Variable(self.horizon)
def solve_lsq(M, y, regularize=0.0, K=None): n = M.shape[1] x = cp.Variable(n) t = cp.Variable(K.shape[0]) M_cp = cp.Constant(M) obj = cp.sum_squares(M_cp @ x - y) constraints = [x >= 0] if regularize: K_cp = cp.Constant(K) obj += regularize * cp.sum_squares(t) constraints += [t >= 0, K_cp @ x <= t] objective = cp.Minimize(obj) prob = cp.Problem(objective, constraints) prob.solve(cp.SCS, verbose=True) return x.value
def cost(self): noload = cvx.Constant(self.no_load_cost) if len(self.bid_curve) == 0: return noload p = -self.terminals[0].power_var offset = self.no_load_cost segments = [offset + p * self.bid_curve[0][1]] if len(self.bid_curve) == 1: return segments[0] offset += self.bid_curve[0][1] * self.bid_curve[0][0] for i in range(len(self.bid_curve) - 1): b = self.bid_curve[i + 1][1] segments.append(b * (p - self.bid_curve[i][0]) + offset) offset += b * (self.bid_curve[i + 1][0] - self.bid_curve[i][0]) return cvx.max_elemwise(*segments) #gen = GeneratorWithBidCurve( # no_load_cost=290.42, # bid_curve=[ # (30, 21.21), # (33.1, 21.43), # (36.2, 21.66), # (39.4, 21.93), # (42.5, 22.22), # (45.6, 22.53), # (48.8, 22.87), # (51.9, 23.22), # (55, 23.6)]) #load = FixedLoad(power=43) #net = Net([gen.terminals[0], load.terminals[0]]) #network = Group([gen, load], [net]) #print(network.optimize())
def symvar_kronl(self, param): # Use a symmetric matrix variable X = cp.Variable(shape=(2, 2), symmetric=True) b_val = 1.5 * np.ones((1, 1)) if param: b = cp.Parameter(shape=(1, 1)) b.value = b_val else: b = cp.Constant(b_val) L = np.array([[0.5, 1], [2, 3]]) U = np.array([[10, 11], [12, 13]]) kronX = cp.kron(X, b) # should be equal to X objective = cp.Minimize(cp.sum(X.flatten())) constraints = [U >= kronX, kronX >= L] prob = cp.Problem(objective, constraints) prob.solve() self.assertItemsAlmostEqual(X.value, np.array([[0.5, 2], [2, 3]]) / 1.5) objective = cp.Maximize(cp.sum(X.flatten())) prob = cp.Problem(objective, constraints) prob.solve() self.assertItemsAlmostEqual(X.value, np.array([[10, 11], [11, 13]]) / 1.5) pass
def ILP_protocol_w_compression(reference_summary: str, sent_units: List[str], compression: List[dict], min_word_limit=30, max_word_limit=40, step=3): print("Compression") constraint_list = [] ref_toks = reference_summary.split(" ") ref_toks = [x.lower() for x in ref_toks] ref_toks_set = list(set(ref_toks)) uniq_tok_num = len(ref_toks_set) y_tok = cp.Variable(shape=(uniq_tok_num), boolean=True) len_doc = len(sent_units) sent_var = cp.Variable(shape=len_doc, boolean=True) len_of_each_sentence = cp.Constant([len(x) for x in sent_units]) length_constraint_sents = sent_var * len_of_each_sentence constraint_list.append(length_constraint_sents <= max_word_limit) obj = cp.Maximize(cp.sum(ref_toks)) prob = cp.Problem(obj, constraints=constraint_list) print(prob) # prob.solve(solver=cp.GLPK_MI) prob.solve() print(prob.status) print(obj.value) print(y_tok.value) print(sent_var.value) exit()
def test_dcp_curvature(self): expr = 1 + cvx.exp(cvx.Variable()) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.Parameter()*cvx.Variable(nonneg=True) self.assertEqual(expr.curvature, s.AFFINE) f = lambda x: x**2 + x**0.5 # noqa E731 expr = f(cvx.Constant(2)) self.assertEqual(expr.curvature, s.CONSTANT) expr = cvx.exp(cvx.Variable())**2 self.assertEqual(expr.curvature, s.CONVEX) expr = 1 - cvx.sqrt(cvx.Variable()) self.assertEqual(expr.curvature, s.CONVEX) expr = cvx.log(cvx.sqrt(cvx.Variable())) self.assertEqual(expr.curvature, s.CONCAVE) expr = -(cvx.exp(cvx.Variable()))**2 self.assertEqual(expr.curvature, s.CONCAVE) expr = cvx.log(cvx.exp(cvx.Variable())) self.assertEqual(expr.is_dcp(), False) expr = cvx.entr(cvx.Variable(nonneg=True)) self.assertEqual(expr.curvature, s.CONCAVE) expr = ((cvx.Variable()**2)**0.5)**0 self.assertEqual(expr.curvature, s.CONSTANT)
def FusedLassoFilterQuat(D,smoothness, degree, order): """ The idea here is to use a relaxation of the quadratically constrained smoothing problem of the quaternions. """ # params n = D.shape[0] A = fd_matrix_parsi(n, degree, order, dt=1.0) # Localize NaN nonNan = cp.Constant(sp.diags(1 - np.any(np.isnan(D), axis=1))) # Create scalar optimization variables. q0 = cp.Variable(shape=n, value=D[:, 0]) qx = cp.Variable(shape=n, value=D[:, 1]) qy = cp.Variable(shape=n, value=D[:, 2]) qz = cp.Variable(shape=n, value=D[:, 3]) l = cp.Parameter(nonneg=True, value=smoothness) # Form objective. loss = lambda x, i: [email protected]((x - D[:, i])) + l*cp.atoms.norm1(A @ x) obj = cp.Minimize(loss(q0, 0) + loss(qx, 1) + loss(qy, 2) + loss(qz, 3)) con = [q0**2 + qx**2 + qy**2 + qz**2 == 1] # Form and solve problem. prob = cp.Problem(obj, con) prob.solve(warm_start=True) # assert(prob.status == "optimal"), "Filtering did not converg." return np.vstack((q0.value, qx.value, qy.value, qz.value))
def bregman_projection_cvx(w, density_parameter, distribution=True): """ Bregman projection for a discrete distribution or a discrete bounded measure, using CVX package Parameters ---------- w : array-like, dtype=float, shape=n Discrete probability distribution or discrete bounded measure density_parameter: float Projection parameter alpha or gamma (based on being a distribution or a measure) distribution: bool Determines whether w should be treated as a distribution """ n = len(w) V = cp.Variable(shape=n) W = cp.Parameter(shape=n) density_parameter = cp.Constant(value=density_parameter) W.value = np.array(w) objective = cp.Minimize(cp.sum(cp.kl_div(V, W))) if distribution: constraints = [V >= density_parameter / n, cp.sum(V) == 1.0] else: constraints = [cp.sum(V) >= density_parameter * n, V <= 1] prob = cp.Problem(objective, constraints) prob.solve() return prob.status, V.value
def predict_log_likelihoods(S, R, T, mu0, uptake_met_concs, excreted_met_concs ): m,n = S.shape rxns = S.columns mets = S.index log_c = cvx.Variable(m) log_likelihood = cvx.Variable(n) deltaG0 = S.T.dot(mu0) log_Q = S.as_matrix().T*log_c log_K = cvx.Constant(-1.0/(R*T))*deltaG0 mu = R*T*log_c + mu0.values uptake_met_indices = S.index.get_loc(uptake_met_concs.index) excreted_met_indices = S.index.get_loc(excreted_met_concs.index) obj = cvx.Maximize(cvx.sum_entries( cvx.entr( log_likelihood ))) constraints = [log_c[uptake_met_indices] == uptake_met_concs.values, log_c[excreted_met_indices] == excreted_met_concs.values, log_likelihood == log_K - log_Q, mu >= np.sum(mu[excreted_met_indices]), mu <= np.sum(mu[uptake_met_indicies]) ] prob = cvx.Problem(obj, constraints) prob.solve(verbose=True) return pd.DataFrame(log_c.value, index=mets), pd.DataFrame(log_likelihood.value, index=rxns)
def test_analytic_param_in_exponent(self) -> None: # construct a problem with solution # x^\star(\alpha) = 1 - 2^alpha, and derivative # x^\star'(\alpha) = -log(2) * 2^\alpha base = 2.0 alpha = cp.Parameter() x = cp.Variable(pos=True) objective = cp.Maximize(x) constr = [cp.one_minus_pos(x) >= cp.Constant(base)**alpha] problem = cp.Problem(objective, constr) alpha.value = -1.0 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-6) self.assertAlmostEqual(x.value, 1 - base**(-1.0)) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -np.log(base) * base**(-1.0)) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) alpha.value = -1.2 alpha.delta = 1e-5 problem.solve(solver=cp.DIFFCP, gp=True, requires_grad=True, eps=1e-6) self.assertAlmostEqual(x.value, 1 - base**(-1.2)) problem.backward() problem.derivative() self.assertAlmostEqual(alpha.gradient, -np.log(base) * base**(-1.2)) self.assertAlmostEqual(x.delta, alpha.gradient * 1e-5, places=3) gradcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3) perturbcheck(problem, gp=True, solve_methods=[s.SCS], atol=1e-3)
def solve_mpc(self): """ Sets the objective function of the Home Energy Management System to be the minimization of cost over the MPC time horizon and solves via CVXPY. Used for all home types. :return: None """ self.cost = cp.Variable(self.horizon) self.wh_weighting = 10 self.objective = cp.Variable(self.horizon) self.constraints += [ self.cost == cp.multiply(self.total_price, self.p_grid) ] # think this should work self.weights = cp.Constant( np.power(self.discount * np.ones(self.horizon), np.arange(self.horizon))) self.obj = cp.Minimize( cp.sum(cp.multiply(self.cost, self.weights)) ) #+ self.wh_weighting * cp.sum(cp.abs(self.temp_wh_max - self.temp_wh_ev))) #cp.sum(self.temp_wh_sp - self.temp_wh_ev)) self.prob = cp.Problem(self.obj, self.constraints) if not self.prob.is_dcp(): self.log.error("Problem is not DCP") try: self.prob.solve(solver=self.solver, verbose=self.verbose_flag) self.solved = True except: self.solved = False
def water_draws(self): draw_sizes = (self.horizon // self.dt + 1) * [0] + self.home["wh"]["draw_sizes"] raw_draw_size_list = draw_sizes[(self.timestep // self.dt):(self.timestep // self.dt) + (self.horizon // self.dt + 1)] raw_draw_size_list = (np.repeat(raw_draw_size_list, self.dt) / self.dt).tolist() draw_size_list = raw_draw_size_list[:self.dt] for i in range(self.dt, self.h_plus): draw_size_list.append(np.average(raw_draw_size_list[i - 1:i + 2])) self.draw_size = draw_size_list df = np.divide(self.draw_size, self.wh_size) self.draw_frac = cp.Constant(df) self.remainder_frac = cp.Constant(1 - df)
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 cvxpy_partial_trace(rho, dims, axis=0): if not isinstance(rho, Expression): rho = cvxpy.Constant(shape=rho.shape, value=rho) rho_np = expr_as_np_array(rho) traced_rho = np_partial_trace(rho_np, dims, axis) traced_rho = np_array_as_expr(traced_rho) return traced_rho
def socp_3(axis): x = cp.Variable(shape=(2,)) c = np.array([-1, 2]) root2 = np.sqrt(2) u = np.array([[1 / root2, -1 / root2], [1 / root2, 1 / root2]]) mat1 = np.diag([root2, 1 / root2]) @ u.T mat2 = np.diag([1, 1]) mat3 = np.diag([0.2, 1.8]) X = cp.vstack([mat1 @ x, mat2 @ x, mat3 @ x]) # stack these as rows t = cp.Constant(np.ones(3, )) objective = cp.Minimize(c @ x) if axis == 0: con = cp.constraints.SOC(t, X.T, axis=0) con_expect = [ np.array([0, 1.16454469e+00, 7.67560451e-01]), np.array([[0, -9.74311819e-01, -1.28440860e-01], [0, 6.37872081e-01, 7.56737724e-01]]) ] else: con = cp.constraints.SOC(t, X, axis=1) con_expect = [ np.array([0, 1.16454469e+00, 7.67560451e-01]), np.array([[0, 0], [-9.74311819e-01, 6.37872081e-01], [-1.28440860e-01, 7.56737724e-01]]) ] obj_pair = (objective, -1.932105) con_pairs = [(con, con_expect)] var_pairs = [(x, np.array([0.83666003, -0.54772256]))] sth = SolverTestHelper(obj_pair, var_pairs, con_pairs) return sth
def expcone_socp_1(): """ A random risk-parity portfolio optimization problem. """ sigma = np.array([[1.83, 1.79, 3.22], [1.79, 2.18, 3.18], [3.22, 3.18, 8.69]]) L = np.linalg.cholesky(sigma) c = 0.75 t = cp.Variable(name='t') x = cp.Variable(shape=(3,), name='x') s = cp.Variable(shape=(3,), name='s') e = cp.Constant(np.ones(3, )) objective = cp.Minimize(t - c * e @ s) con1 = cp.norm(L.T @ x, p=2) <= t con2 = cp.constraints.ExpCone(s, e, x) # SolverTestHelper data obj_pair = (objective, 4.0751197) var_pairs = [ (x, np.array([0.576079, 0.54315, 0.28037])), (s, np.array([-0.55150, -0.61036, -1.27161])), ] con_pairs = [ (con1, 1.0), (con2, [np.array([-0.75, -0.75, -0.75]), np.array([-1.16363, -1.20777, -1.70371]), np.array([1.30190, 1.38082, 2.67496])] ) ] sth = SolverTestHelper(obj_pair, var_pairs, con_pairs) return sth
def test_sdp_var(self): """Test sdp var. """ const = cp.Constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) X = cp.Variable((3, 3), PSD=True) prob = cp.Problem(cp.Minimize(0), [X == const]) prob.solve(solver=cp.SCS) self.assertEqual(prob.status, cp.INFEASIBLE)
def test_trace_canon(self): X = cvxpy.Constant(np.array([[1.0, 5.0], [9.0, 14.0]])) Y = cvxpy.trace(X) canon, constraints = dgp_atom_canon.trace_canon(Y, Y.args) self.assertEqual(len(constraints), 0) self.assertTrue(canon.is_scalar()) expected = np.log(np.exp(1.0) + np.exp(14.0)) self.assertAlmostEqual(expected, canon.value)