def __init__(self, m, k, formulation='penalizedl1', nonnegative=False): if not cvx_available: raise RuntimeError( 'Cannot initialize when cvxpy is not available.') # Initialize parameters and variables A = cvx.Parameter((m, k)) b = cvx.Parameter((m, 1)) l = cvx.Parameter(nonneg=True) x = cvx.Variable((k, 1)) # Create the problem if formulation == 'penalizedl1': obj_func = 0.5 * cvx.sum_squares(cvx.matmul(A, x) - b) + l * cvx.norm1(x) constraints = [] elif formulation == 'constrainedl1': obj_func = cvx.sum_squares(cvx.matmul(A, x) - b) constraints = [cvx.norm1(x) <= l] elif formulation == 'constrainedl2': obj_func = cvx.norm1(x) constraints = [cvx.norm(cvx.matmul(A, x) - b) <= l] else: raise ValueError("Unknown formulation '{0}'.".format(formulation)) if nonnegative: constraints.append(x >= 0) problem = cvx.Problem(cvx.Minimize(obj_func), constraints) self._formulation = formulation self._A = A self._b = b self._l = l self._x = x self._obj_func = obj_func self._constraints = constraints self._problem = problem
def findPiercingNumber(self): # Finding piercing number using an integer linear program (ILP) formulation # (a linear program (LP) that is constrained to have integer solutions) # First, find constraint matrix/data for LP used to find piercing number # Candidate piercing points are the endpoints of the intervals _, orderedendpts = self.listSetEndpoints() N = self.numVoters M = 2 * N Mat = np.empty([N, M]) for i in np.arange(0, N): for j, p in enumerate(orderedendpts): A = self.approvalSets[i] Mat[i, j] = A.isPointInSet(p) # Next, vector of coefficients of objective function c = np.ones(M) ## Solve ILP using cvxpy x = cp.Variable(M, integer=True) objective = cp.Minimize(cp.matmul(c, x)) constraints = [cp.matmul(Mat, x) >= np.ones(N), 0 <= x, x <= 1] prob = cp.Problem(objective, constraints) val = prob.solve(solver='GLPK_MI') piercingNumber = int(np.round(val)) piercingSet = np.transpose(orderedendpts)[np.round(x.value) > 0] return piercingNumber, piercingSet, x.value, Mat
def estimate_parameters(y, mat1, mat2, gamma=0.1, beta=0.1): th1 = cvx.Variable(mat1.shape[1]) th2 = cvx.Variable(mat2.shape[1]) if gamma is None: gamma = 2 * np.sqrt(2 * np.log(len(y))) cost = (cvx.sum_squares(y - cvx.matmul(mat1, th1) - cvx.matmul(mat2, th2)) + gamma * cvx.norm1(th2) + beta * cvx.norm(th1[1:])) problem = cvx.Problem(cvx.Minimize(cost)) problem.solve() # th1.value[np.abs(th1.value) <= 1e-2 * th1.value[0]] = 0 # th2.value[np.abs(th2.value) <= 1e-2 * np.max(th2.value)] = 0 # sparsity_pattern_1 = np.abs(th1.value) <= 1e-2 sparsity_pattern_2 = np.abs(th2.value) <= 1e-2 cost = (cvx.sum_squares(y - cvx.matmul(mat1, th1) - cvx.matmul(mat2, th2)) + beta * cvx.norm(th1[1:])) constraints = [] # if np.sum(sparsity_pattern_1) > 0: # constraints.append(th1[sparsity_pattern_1] == 0) if np.sum(sparsity_pattern_2) > 0: constraints.append(th2[sparsity_pattern_2] == 0) problem = cvx.Problem(cvx.Minimize(cost), constraints) problem.solve() # th1.value[np.abs(th1.value) < 1e-4] = 0 th2.value[np.abs(th2.value) < 1e-4] = 0 return th1.value, th2.value
def LinProg(A, alpha, r): x = cp.Variable(shape=(len(state_action_pairs), 1), name="x") constraints = [cp.matmul(A, x) == alpha, x >= 0] objective = cp.Maximize(cp.matmul(r, x)) problem = cp.Problem(objective, constraints) return x, problem.solve()
def find_X_sol_agent(Y_s, Q, num_tasks, total_num_agents): X_sol = cp.Variable((num_tasks, total_num_agents), boolean=True) mismatch = Y_s - cp.matmul(X_sol, Q) obj = cp.Minimize(cp.pnorm(mismatch, 2)) constraints = [cp.matmul(X_sol.T, np.ones([num_tasks, 1])) <= np.ones([total_num_agents , 1]), cp.matmul(X_sol, np.ones([total_num_agents, 1])) == 3*np.ones([num_tasks, 1])] opt_prob = cp.Problem(obj, constraints) opt_prob.solve() X_candidate = np.round(X_sol.value) return X_candidate
def __init__(self, nb_measure, alpha=0.95): self.h = cp.Variable(nb_measure, nonneg=True) self.p = cp.Parameter(nb_measure, nonneg=True) self.v = cp.Parameter(nb_measure) objective = cp.Maximize(cp.matmul(self.v, cp.multiply(self.h, self.p))) constraints = [ self.h <= 1 / (1 - alpha), cp.matmul(self.h, self.p) == 1 ] self.problem = cp.Problem(objective, constraints)
def __init__(self, u): self.u = u self.v = np.array([1.0 / (np.log(2 + i)) for i, _ in enumerate(u)]) self.v[range(10, len(self.v))] = 0 self.P = cp.Variable((len(u), len(u))) self.I = np.ones((len(u),)) self.constraints = [cp.matmul(self.I.transpose(), self.P) == self.I.transpose(), cp.matmul(self.P, self.I) == self.I, 0 <= self.P, self.P <= 1 ] self.all_constraints = None
def __init__(self, nb_measure, q=2, c=1): self.h = cp.Variable(nb_measure, nonneg=True) self.p = cp.Parameter(nb_measure, nonneg=True) self.v = cp.Parameter(nb_measure) self.g = cp.multiply(self.p, 1 + self.h - cp.matmul(self.h, self.p)) objective = cp.Maximize(cp.matmul(self.v, self.g)) constraints = [ self.g >= 0, cp.matmul(cp.power(self.h, q), self.p) <= c**q ] self.problem = cp.Problem(objective, constraints)
def get_output(r, alpha): r = np.array(r) alpha = np.array(alpha) x = cp.Variable(shape=(100, 1), name="x") constraints = [cp.matmul(A, x) == alpha, x >= 0] objective = cp.Maximize(cp.matmul(r, x)) problem = cp.Problem(objective, constraints) solution = problem.solve() for i in range(len(x.value)): solutionx.append(x.value[i][0]) return solution
def __init__(self, nb_measure, alpha=0.95): self.alpha = alpha self.z = cp.Variable(nb_measure, nonneg=True) self.p = cp.Parameter(nb_measure, nonneg=True) self.v = cp.Parameter(nb_measure) dkl = cp.matmul(self.p, -cp.entr(self.z)) objective = cp.Maximize(cp.matmul(self.p, cp.multiply(self.v, self.z))) constraints = [ dkl <= np.log(1 / (1 - self.alpha)), cp.matmul(self.p, self.z) == 1 ] self.problem = cp.Problem(objective, constraints)