示例#1
0
    def test_elemwise_arg_count(self) -> None:
        """Test arg count for max and min variants.
        """
        error_message = r"__init__\(\) missing 1 required positional argument: 'arg2'"
        with pytest.raises(TypeError, match=error_message):
            cp.maximum(1)

        with pytest.raises(TypeError, match=error_message):
            cp.minimum(1)
示例#2
0
    def test_elemwise_arg_count(self):
        """Test arg count for max and min variants.
        """
        with self.assertRaises(Exception) as cm:
            cp.maximum(1)
        self.assertTrue(str(cm.exception) in (
            "__init__() takes at least 3 arguments (2 given)",
            "__init__() missing 1 required positional argument: 'arg2'"))

        with self.assertRaises(Exception) as cm:
            cp.minimum(1)
        self.assertTrue(str(cm.exception) in (
            "__init__() takes at least 3 arguments (2 given)",
            "__init__() missing 1 required positional argument: 'arg2'"))
    def test_psd_var(self):
        """Test PSD variable.
        """
        s = cp.Variable((2, 2), PSD=True)
        var_dict = {s.id: s}
        obj = cp.Maximize(cp.minimum(s[0, 1], 10))
        const = [cp.diag(s) == np.ones(2)]
        problem = cp.Problem(obj, const)
        data, _, _ = problem.get_problem_data(solver=cp.SCS)
        param_cone_prog = data[cp.settings.PARAM_PROB]
        solver = SCS()
        raw_solution = solver.solve_via_data(data,
                                             warm_start=False,
                                             verbose=False,
                                             solver_opts={})['x']
        sltn_dict = param_cone_prog.split_solution(raw_solution,
                                                   active_vars=var_dict)
        self.assertEqual(sltn_dict[s.id].shape, s.shape)
        sltn_value = sltn_dict[s.id]
        adjoint = param_cone_prog.split_adjoint(sltn_dict)
        self.assertEqual(adjoint.shape, raw_solution.shape)
        self.assertTrue(any(sltn_value[0, 0] == adjoint))
        self.assertTrue(any(sltn_value[1, 1] == adjoint))
        # off-diagonals of adjoint will be scaled by two
        self.assertTrue(any(2 * np.isclose(sltn_value[0, 1], adjoint)))
        self.assertTrue(any(2 * np.isclose(sltn_value[1, 0], adjoint)))

        problem.solve(solver=cp.SCS)
        self.assertItemsAlmostEqual(s.value, sltn_value)
示例#4
0
    def test_minimum(self):
        x = cp.Variable(pos=True)
        y = cp.Variable(pos=True)

        alpha = cp.Parameter(pos=True, value=1.0, name='alpha')
        beta = cp.Parameter(pos=True, value=3.0, name='beta')
        prod1 = x * y**alpha
        prod2 = beta * x * y**alpha
        posy = prod1 + prod2
        obj = cp.Maximize(cp.minimum(prod1, prod2, 1 / posy))
        constr = [x == alpha, y == 4.0]

        dgp = cp.Problem(obj, constr)
        dgp.solve(SOLVER, gp=True, enforce_dpp=True)
        # prod1 = 1*4, prod2 = 3*4 = 12, 1/posy = 1/(3 +12)
        self.assertAlmostEqual(dgp.value, 1.0 / (4.0 + 12.0))
        self.assertAlmostEqual(x.value, 1.0)
        self.assertAlmostEqual(y.value, 4.0)

        alpha.value = 2.0
        # prod1 = 2*16, prod2 = 3*2*16 = 96, 1/posy = 1/(32 +96)
        dgp.solve(SOLVER, gp=True, enforce_dpp=True)
        self.assertAlmostEqual(dgp.value, 1.0 / (32.0 + 96.0))
        self.assertAlmostEqual(x.value, 2.0)
        self.assertAlmostEqual(y.value, 4.0)
示例#5
0
    def test_minimum(self) -> None:
        """Test domain for minimum.
        """
        b = Variable()
        expr = cp.minimum(self.a, b)
        self.a.value = 2
        b.value = 4
        self.assertAlmostEqual(expr.grad[self.a], 1)
        self.assertAlmostEqual(expr.grad[b], 0)

        self.a.value = 3
        b.value = 0
        self.assertAlmostEqual(expr.grad[self.a], 0)
        self.assertAlmostEqual(expr.grad[b], 1)

        self.a.value = -1
        b.value = 2
        self.assertAlmostEqual(expr.grad[self.a], 1)
        self.assertAlmostEqual(expr.grad[b], 0)

        y = Variable(2)
        expr = cp.minimum(self.x, y)
        self.x.value = [3, 4]
        y.value = [5, -5]
        val = np.zeros((2, 2)) + np.diag([1, 0])
        self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), val)
        val = np.zeros((2, 2)) + np.diag([0, 1])
        self.assertItemsAlmostEqual(expr.grad[y].toarray(), val)

        expr = cp.minimum(self.x, y)
        self.x.value = [-1e-9, 4]
        y.value = [1, 4]
        val = np.zeros((2, 2)) + np.diag([1, 1])
        self.assertItemsAlmostEqual(expr.grad[self.x].toarray(), val)
        val = np.zeros((2, 2)) + np.diag([0, 0])
        self.assertItemsAlmostEqual(expr.grad[y].toarray(), val)

        expr = cp.minimum(self.A, self.B)
        self.A.value = [[1, 2], [3, 4]]
        self.B.value = [[5, 1], [3, 2.3]]
        val = np.zeros((4, 4)) + np.diag([1, 0, 1, 0])
        self.assertItemsAlmostEqual(expr.grad[self.A].toarray(), val)
        val = np.zeros((4, 4)) + np.diag([0, 1, 0, 1])
        self.assertItemsAlmostEqual(expr.grad[self.B].toarray(), val)
示例#6
0
    def test_basic_minimum(self):
        x, y = cp.Variable(2)
        expr = cp.minimum(cp.ceil(x), cp.ceil(y))

        problem = cp.Problem(cp.Maximize(expr), [x >= 11.9, x <= 15.8, y >= 17.4])
        self.assertTrue(problem.is_dqcp())
        problem.solve(qcp=True)
        self.assertEqual(problem.objective.value, 16.0)
        self.assertLess(x.value, 16.0)
        self.assertGreater(x.value, 14.9)
        self.assertGreater(y.value, 17.3)
示例#7
0
    def test_minimum(self):
        x = cvxpy.Variable(pos=True)
        y = cvxpy.Variable(pos=True)

        prod1 = x * y**0.5
        prod2 = 3.0 * x * y**0.5
        posy = prod1 + prod2
        obj = cvxpy.Maximize(cvxpy.minimum(prod1, prod2, 1 / posy))
        constr = [x == 1.0, y == 4.0]

        dgp = cvxpy.Problem(obj, constr)
        dgp.solve(SOLVER, gp=True)
        self.assertAlmostEqual(dgp.value, 1.0 / (2.0 + 6.0))
        self.assertAlmostEqual(x.value, 1.0)
        self.assertAlmostEqual(y.value, 4.0)
示例#8
0
 def test_psd_constraint(self):
     """Test PSD constraint.
     """
     s = cp.Variable((2, 2))
     obj = cp.Maximize(cp.minimum(s[0, 1], 10))
     const = [s >> 0, cp.diag(s) == np.ones(2)]
     prob = cp.Problem(obj, const)
     r = prob.solve(solver=cp.SCS)
     s = s.value
     print(const[0].residual)
     print("value", r)
     print("s", s)
     print("eigs", np.linalg.eig(s + s.T)[0])
     eigs = np.linalg.eig(s + s.T)[0]
     self.assertEqual(np.all(eigs >= 0), True)
示例#9
0
def revenue(x, p, p_disc, q, vec=False):
    r = np.zeros_like(x)
    # idx = np.where(x <= q)
    # r[idx] = (p * x)[idx]
    # idx = np.where(x >= q)
    # r[idx] = (p * q + p_disc * (x - q))[idx]

    if vec:
        r = np.minimum(p * x, p * q + p_disc * (x - q))
        return r
    else:
        r = 0
        for i in range(len(p)):
            r += cp.minimum(p[i] * x[i],
                            p[i] * q[i] + p_disc[i] * (x[i] - q[i]))
        return r
示例#10
0
    def test_minimum(self):
        x = cvxpy.Variable(pos=True)
        y = cvxpy.Variable(pos=True)
        z = cvxpy.Variable(pos=True)
        monomial = 5.0 * (x**0.1) * y**(-0.1) * z**(3)
        posynomial = 5.0 * x * y + 1.2 * y * y
        another_posynomial = posynomial * posynomial
        expr = cvxpy.minimum(monomial, 1 / posynomial, 1 / another_posynomial)
        self.assertTrue(expr.is_dgp())
        self.assertTrue(not expr.is_log_log_convex())
        self.assertTrue(expr.is_log_log_concave())

        expr = (1 / posynomial) * expr
        self.assertTrue(expr.is_dgp())
        self.assertTrue(not expr.is_log_log_convex())
        self.assertTrue(expr.is_log_log_concave())

        expr = expr**2
        self.assertTrue(expr.is_dgp())
        self.assertTrue(not expr.is_log_log_convex())
        self.assertTrue(expr.is_log_log_concave())
示例#11
0
    def opt(d, u):
        x = cp.Variable(d)

        # Predictions
        ux = cp.multiply(d, (x @ u[:-1] + u[-1]))
        if abs_act:
            y = cp.sum(ux)
        else:
            y = cp.sum(cp.multiply((d + 1) / 2, (x @ u[:-1] + u[-1])))

        # Constraints
        constraints = [ux >= 0, x <= upper, x >= lower]

        prob = cp.Problem(cp.Minimize(cp.minimum(y)), constraints)
        t0 = time.time()
        prob.solve()
        print(
            f'Status: {prob.status}, Value: {prob.value}, Time: {time.time()-t0}s'
        )
        if prob.status.lower() == 'infeasible':
            return None
        return x.value, y.value
示例#12
0
    def stability_lmi(self,
                      cA0,
                      cA1,
                      cA2,
                      cB,
                      synthesize_control=True,
                      epsilon=1e-9):
        """
            Solve an LMI to check the stability of an interval controller
        :param cA0: extended nominal matrix
        :param cA1: extended state matrix uncertainty
        :param cA2: extended state matrix uncertainty
        :param cB: extended control matrix
        :param synthesize_control: if true, the controls will be synthesized via the LMI
        :param epsilon: accuracy for checking that a matrix is positive definite.
        :return: whether the controlled system is stable
        """
        import cvxpy as cp
        np.set_printoptions(precision=2)
        p = cB.shape[0] // 2
        q = cB.shape[1]

        # Optimisation variables
        P = cp.Variable((2 * p, 2 * p), diag=True)
        Q = cp.Variable((2 * p, 2 * p), diag=True)
        Qp = cp.Variable((2 * p, 2 * p), diag=True)
        Qn = cp.Variable((2 * p, 2 * p), diag=True)
        Zp = cp.Variable((2 * p, 2 * p), diag=True)
        Zn = cp.Variable((2 * p, 2 * p), diag=True)
        Psi = cp.Variable((2 * p, 2 * p), diag=True)
        Psi_p = cp.Variable((2 * p, 2 * p), diag=True)
        Psi_n = cp.Variable((2 * p, 2 * p), diag=True)
        Gamma = cp.Variable((2 * p, 2 * p), diag=True)

        Omega = Q + cp.minimum(Qp, Qn) + 2 * cp.minimum(Psi_p, Psi_n)

        # Constraints
        if synthesize_control:
            # In fact P is P^{-1}
            # In fact Zp is Zp^{-1}
            # In fact Zn is Zn^{-1}
            U0 = cp.Variable((q, 2 * p))
            U1 = cp.Variable((q, 2 * p))
            U2 = cp.Variable((q, 2 * p))

            Pi_11 = P * cA0.T + cA0 * P + U0.T * cB.T + cB * U0 + Q
            Pi_12 = cA1 * Zp + cB * U1 + P * cA0.T + U0.T * cB.T + Psi_p
            Pi_13 = cA2 * Zn + cB * U2 - P * cA0.T - U0.T * cB.T - Psi_n
            Pi_22 = Zp * cA1.T + cA1 * Zp + U1.T * cB.T + cB * U1 + Qp
            Pi_23 = cA2 * Zn + cB * U2 - Zp * cA1.T - U1.T * cB.T + Psi
            Pi_33 = Qn - Zn * cA2.T - cA2 * Zn - U2.T * cB.T - cB * U2
            Id = np.eye(2 * p)
            Pi = cp.bmat([  # Block matrix
                [Pi_11, Pi_12, Pi_13, Id], [Pi_12.T, Pi_22, Pi_23, Id],
                [Pi_13.T, Pi_23.T, Pi_33, -Id], [Id, Id, -Id, -Gamma]
            ])
            constraints = [
                P >= epsilon, Zp >= epsilon, Zn >= epsilon, Gamma >= epsilon,
                Omega >= epsilon, Pi << 0
            ]
        else:
            Ups_11 = cA0.T * P + P * cA0 + Q
            Ups_12 = cA0.T * Zp + P * cA1 + Psi_p
            Ups_13 = P * cA2 - cA0.T * Zn - Psi_n
            Ups_22 = Zp * cA1 + cA1.T * Zp + Qp
            Ups_23 = Zp * cA2 - cA1.T * Zn + Psi
            Ups_33 = Qn - Zn * cA2 - cA2.T * Zn
            Ups = cp.bmat([  # Block matrix
                [Ups_11, Ups_12, Ups_13, P], [Ups_12.T, Ups_22, Ups_23, Zp],
                [Ups_13.T, Ups_23.T, Ups_33, -Zn], [P, Zp, -Zn, -Gamma]
            ])
            U0, U1, U2 = None, None, None
            constraints = [
                P >= epsilon,
                P + cp.minimum(Zp, Zn) >= epsilon,
                Ups << 0,
                Gamma >= epsilon,
                Omega >= epsilon,
            ]

        prob = cp.Problem(cp.Minimize(0), constraints=constraints)
        prob.solve(solver=cp.SCS, verbose=True)

        logger.debug("Status: {}".format(prob.status))
        success = prob.status == "optimal"
        if success:
            logger.debug("- P + min(Zp, Zn): {}".format(
                np.diagonal(
                    P.value.todense() +
                    np.minimum(Zp.value.todense(), Zn.value.todense()))))
            logger.debug("- Gamma: {}".format(Gamma.value))
            logger.debug("- Omega: {}".format(Omega.value))
            P = P.value.todense()
            Zp = Zp.value.todense()
            Zn = Zn.value.todense()

            if synthesize_control:
                P = np.linalg.inv(P)
                Zp = np.linalg.inv(Zp)
                Zn = np.linalg.inv(Zn)
                logger.debug("- U0:".format(U0.value))
                logger.debug("- U1:".format(U1.value))
                logger.debug("- U2:".format(U2.value))
                self.K0 = U0.value @ P
                self.K1 = U1.value @ Zp
                self.K2 = U2.value @ Zn

            self.compute_attraction_basin(cB, Gamma.value.todense(),
                                          Omega.value, P, Zp, Zn)
        return success
示例#13
0
    def get_allocation(self, unflattened_throughputs, scale_factors,
                       unflattened_priority_weights, cluster_spec):
        all_throughputs, index = \
            self.flatten(d=unflattened_throughputs,
                         cluster_spec=cluster_spec,
                         priority_weights=unflattened_priority_weights)
        if all_throughputs is None or len(all_throughputs) == 0: return None
        (m, n) = all_throughputs[0].shape
        (job_ids, single_job_ids, worker_types, relevant_combinations) = index
        x = cp.Variable((m, n))

        # Row i of scale_factors_array is the scale_factor of job
        # combination i repeated len(worker_types) times.
        scale_factors_array = self.scale_factors_array(scale_factors, job_ids,
                                                       m, n)

        throughputs_no_packed_jobs = np.zeros((len(single_job_ids), n))
        for i, single_job_id in enumerate(single_job_ids):
            for j, worker_type in enumerate(worker_types):
                throughputs_no_packed_jobs[i, j] = \
                    unflattened_throughputs[single_job_id][worker_type]
        proportional_throughputs = self._proportional_policy.get_throughputs(
            throughputs_no_packed_jobs, (single_job_ids, worker_types),
            cluster_spec)

        objective_terms = []
        # Multiply throughputs by scale_factors to ensure that scale_factor
        # is taken into account while allocating times to different jobs.
        # A job run on 1 GPU should receive `scale_factor` more time than
        # a job run on `scale_factor` GPUs.
        for i in range(len(all_throughputs)):
            indexes = relevant_combinations[single_job_ids[i]]
            proportional_throughput = proportional_throughputs[i]
            objective_terms.append(
                cp.sum(
                    cp.multiply(
                        np.multiply(all_throughputs[i][indexes],
                                    scale_factors_array[indexes]), x[indexes]))
                / proportional_throughput)
        if len(objective_terms) == 1:
            objective = cp.Maximize(objective_terms[0])
        else:
            objective = cp.Maximize(cp.minimum(*objective_terms))

        # Make sure the allocation can fit in the cluster.
        constraints = self.get_base_constraints(x, single_job_ids,
                                                scale_factors_array,
                                                relevant_combinations)

        # Explicitly constrain all allocation values with an effective scale
        # factor of 0 to be 0.
        # NOTE: This is not strictly necessary because these allocation values
        # do not affect the optimal allocation for nonzero scale factor
        # combinations.
        for i in range(m):
            for j in range(n):
                if scale_factors_array[i, j] == 0:
                    constraints.append(x[i, j] == 0)
        cvxprob = cp.Problem(objective, constraints)
        result = cvxprob.solve(solver=self._solver)

        if cvxprob.status != "optimal":
            print('WARNING: Allocation returned by policy not optimal!')

        return self.unflatten(x.value.clip(min=0.0).clip(max=1.0), index)
示例#14
0
 def left(self):
     return cvx.minimum(self.b1.x, self.b2.x)
示例#15
0
 def bottom(self):
     return cvx.minimum(self.b1.y, self.b2.y)
示例#16
0
 def u(_r):
     u = cp.minimum(*(y(_r) for y in ys))
     return u
def train_erm_min_welfare(X, L_mat, U_mat, groups, lamb=1000):
    L_X = np.matmul(X, L_mat.T)
    U_X = np.matmul(X, U_mat.T)
    n, d = L_X.shape
    n, m = X.shape

    # For each group, compute its U_X and L_X matrix
    num_groups = len(groups.keys())
    group_ids = groups.keys()
    group_sizes = [groups[i].shape[0] for i in range(num_groups)]
    group_LX, group_UX = {}, {}
    for i in range(num_groups):
        group_LX[i] = np.matmul(groups[i], L_mat.T)
        group_UX[i] = np.matmul(groups[i], U_mat.T)

    # Constructing argmax/min labels used repeatedly
    y = np.argmin(L_X, axis=1)
    s, b = [], []
    for i in range(num_groups):
        s.append(np.argmin(group_UX[i], axis=1))
        b.append(np.argmax(group_UX[i], axis=1))

    learned_betas = []
    learned_predictions = {h: [] for h in range(num_groups)}
    learned_predictions_all = []
    def_alphas = get_default_alpha_arr(K)

    def get_min_welf_estimate(given_Beta):
        for i in range(num_groups):
            # First compute the utility group i has for itself
            USFii = 0
            concave_p = 0
            min_welf = 100
            for t in range(k):
                for li in range(group_sizes[i]):
                    USFii += def_alphas[t] * group_UX[i][
                        li, learned_predictions[i][t][li]]

            for li in range(group_sizes[i]):
                concave_version_p = np.min(group_UX[i][li, :] - np.matmul(given_Beta,groups[i][li,:])) + \
                        np.matmul(given_Beta[b[i][li],:],groups[i][li,:])
                concave_p += def_alphas[k] * concave_version_p

            USFii = USFii * (1 / group_sizes[i])
            concave_p = concave_p * (1 / group_sizes[i])
            min_welf = min(min_welf, USFii + concave_p)
        return min_welf

    # Run code to compute the mixture iteratively
    for k in range(K):
        Beta = cp.Variable(
            (d, m))  # The parameters of the one-vs-all classifier

        # Solve relaxed convexified optimization problem
        loss_objective = 0
        for i in range(n):
            # construct list with entries L(x_i, y) + beta_y^T x_i - beta_{y_i}^T x_i; for each y and y_i defined appropriately
            loss_objective += get_convex_version(X, L_X, Beta, y, i)
        loss_objective = (1 / n) * loss_objective

        # Our Envy-Free Objective is over groups - so iterate over them
        for i in range(num_groups):
            # First compute the utility group i has for itself
            USFii = 0
            concave_p = 0
            min_welf = 100
            for t in range(k):
                for li in range(group_sizes[i]):
                    USFii += def_alphas[t] * group_UX[i][
                        li, learned_predictions[i][t][li]]

            for li in range(group_sizes[i]):
                concave_version_p = cp.min(group_UX[i][li, :] - cp.matmul(Beta,groups[i][li,:])) + \
                        cp.matmul(Beta[b[i][li],:],groups[i][li,:])
                concave_p += def_alphas[k] * concave_version_p

            USFii = USFii * (1 / group_sizes[i])
            concave_p = concave_p * (1 / group_sizes[i])
            min_welf = cp.minimum(min_welf, USFii + concave_p)

        #objective = cp.Maximize((1/100)*((-1/10)*loss_objective + lamb*min_welf))
        objective = cp.Maximize(lamb * min_welf)
        prob = cp.Problem(objective)

        # Solving the problem
        try:
            #results = prob.solve(solver=cp.SCS, verbose=False)#, feastol=1e-5, abstol=1e-5)
            resuls = prob.solve(verbose=False)
        except:
            return 0, 0, 0, 0
        Beta_value = np.array(Beta.value)
        #print("beta: ", Beta_value)
        learned_betas.append(Beta_value)

        min_welfare_estimate = get_min_welf_estimate(Beta_value)
        #print("Min welfare estimate: " , min_welfare_estimate)

        all_predictions = predictions(Beta_value, X)
        learned_predictions_all.append(all_predictions)

        for h in range(num_groups):
            learned_predictions[h].append(predictions(Beta_value, groups[h]))

    # We now solve for the optimal alpha values
    alphas = cp.Variable(K)
    alpha_loss = 0
    alpha_losses = []
    min_welfares = []
    for k in range(K):
        alpha_loss = 0
        for i in range(n):
            alpha_loss += L_X[i, learned_predictions_all[k][i]]
        alpha_losses.append(alpha_loss)
        #print("Hello: ", alpha_losses)
        for i in range(num_groups):
            utls = []
            total_ii_utl = 0
            for li in range(group_sizes[i]):
                total_ii_utl += group_UX[i][li, learned_predictions[i][k][li]]
            #print("k: ", k, "i: ", i, "utl: ", total_ii_utl)
            utls.append(total_ii_utl / group_sizes[i])
        min_welfares.append(min(utls))

    objective = cp.Maximize(-cp.sum(cp.multiply(alpha_losses, alphas))\
            + lamb*cp.sum(cp.multiply(min_welfares, alphas)))

    constraints = []
    for k in range(K):
        constraints.append(alphas[k] >= 0)
    constraints.append(cp.sum(alphas) == 1)

    prob = cp.Problem(objective, constraints)
    #try:
    results = prob.solve(cp.SCS, verbose=False)  #, feastol=1e-5, abstol=1e-5)
    #except:
    #return 0,0,0,0
    opt_alphas = np.array(alphas.value).flatten()
    #print("XIS")
    #print(np.array(xis.value))
    return learned_betas, learned_predictions_all, learned_predictions, opt_alphas
示例#18
0
# w - how much AD puts in A
# x - how much BC puts in C

a = u +v +w + 100
b = x + 100 - u
c = 200 -v -x
d = 100-w

#a,b,c,d - how much budget each item gets

num_of_participants = 5
donations=[]
for i in range(num_of_participants):
    donations.append(100)

utilities = [cvxpy.minimum(a,b), cvxpy.minimum(a,c), cvxpy.minimum(a,d), cvxpy.minimum(b,c), a]


# here is another example -
# a = v +w + 100
# b = x + u
# c = 200 -v -x
# d = 100-w + 100 - u
#
# num_of_participants = 5
# donations=[]
# for i in range(num_of_participants):
#     donations.append(100)

# utilities = [cvxpy.minimum(b,d), cvxpy.minimum(a,c), cvxpy.minimum(a,d), cvxpy.minimum(b,c), a]
    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
示例#20
0
P_ad = np.random.uniform(size=(m, 1))
P_time = np.random.uniform(size=(1, n))
P = P_ad.dot(P_time)

T = np.sin(np.linspace(-2 * np.pi / 2, 2 * np.pi - 2 * np.pi / 2, n)) * SCALE
T += -np.min(T) + SCALE
c = np.random.uniform(size=(m,))
c *= 0.6 * T.sum() / c.sum()
c = 1000 * np.round(c / 1000)
R = np.array([np.random.lognormal(c.min() / c[i]) for i in range(m)])

# Form and solve the optimal advertising problem.
import cvxpy as cp

D = cp.Variable((m, n))
Si = [cp.minimum(R[i] * P[i, :] * D[i, :].T, B[i]) for i in range(m)]
prob = cp.Problem(cp.Maximize(cp.sum(Si)),
                  [D >= 0,
                   D.T @ np.ones(m) <= T,
                   D @ np.ones(n) >= c])
prob.solve()

# Plot traffic.
import matplotlib.pyplot as plt

# %matplotlib inline
# %config InlineBackend.figure_format = 'svg'
plt.plot(T)
plt.xlabel('Hour')
plt.ylabel("Traffic")
plt.show()
示例#21
0
import numpy as np
import cvxpy as cp
from data.opt_funding_data import n, T, rp, rn, E, C, P, M, A
np.set_printoptions(precision=6, suppress=True)

x = cp.Variable(n)
E = [0] + list(E)
I = [0] + list(A @ x)
B = [None] * (T + 1)
B[0] = cp.Variable()
for t in range(T + 1):
    before = B[t] - E[t] + I[t]
    B[t + 1] = cp.minimum((1 + rp) * before, (1 + rn) * before)
#B = cp.hstack(B)
constraints = [
        B[-1] + I[-1] - E[-1] >= 0,
        B[0] >= 0,
        x >= 0,
        ]

obj = cp.Minimize(x @ P + B[0])
problem = cp.Problem(obj, constraints)
problem.solve()
print(problem.value)
示例#22
0
 def u(r):
     u = cp.minimum(beta0 * r + intercept1, r, beta2 * r,
                    beta3 * r + intercept)
     return u
示例#23
0
文件: min.py 项目: YeTian-93/disropt
 def _to_cvxpy(self):
     import cvxpy as cvx
     return cvx.minimum(self.f1._to_cvxpy(), self.f2._to_cvxpy())
def get_constraint_list_cov(x_train, y_train, x_control_train, sensitive_attrs_to_cov_thresh, cons_type, w):

    """
    get the list of constraints to be fed to the minimizer

    cons_type == 0: means the whole combined misclassification constraint (without FNR or FPR)
    cons_type == 1: FPR constraint
    cons_type == 2: FNR constraint
    cons_type == 4: both FPR as well as FNR constraints

    sensitive_attrs_to_cov_thresh: is a dict like {s: {cov_type: val}}
    s is the sensitive attr
    cov_type is the covariance type. contains the covariance for all misclassifications, FPR and for FNR etc
    """
    constraints = []
    for attr in sensitive_attrs_to_cov_thresh.keys():

        attr_arr = x_control_train[attr]
        attr_arr_transformed, index_dict = ut.get_one_hot_encoding(attr_arr)
                
        if index_dict is None: # binary attribute, in this case, the attr_arr_transformed is the same as the attr_arr

            s_val_to_total = {ct:{} for ct in [0,1,2]} # constrain type -> sens_attr_val -> total number
            s_val_to_avg = {ct:{} for ct in [0,1,2]}
            cons_sum_dict = {ct:{} for ct in [0,1,2]} # sum of entities (females and males) in constraints are stored here

            for v in set(attr_arr):
                s_val_to_total[0][v] = sum(x_control_train[attr] == v)
                s_val_to_total[1][v] = sum(np.logical_and(x_control_train[attr] == v, y_train == -1)) # FPR constraint so we only consider the ground truth negative dataset for computing the covariance
                s_val_to_total[2][v] = sum(np.logical_and(x_control_train[attr] == v, y_train == +1))


            for ct in [0,1,2]:
                s_val_to_avg[ct][0] = s_val_to_total[ct][1] / float(s_val_to_total[ct][0] + s_val_to_total[ct][1]) # N1/N in our formulation, differs from one constraint type to another
                s_val_to_avg[ct][1] = 1.0 - s_val_to_avg[ct][0] # N0/N

            
            for v in set(attr_arr):

                idx = x_control_train[attr] == v                


                #################################################################
                # #DCCP constraints
                dist_bound_prod = cvxpy.multiply(y_train[idx], x_train[idx] @ w) # y.f(x)

                cons_sum_dict[0][v] = cvxpy.sum( cvxpy.minimum(0, dist_bound_prod) ) * (s_val_to_avg[0][v] / len(x_train)) # avg misclassification distance from boundary
                cons_sum_dict[1][v] = cvxpy.sum( cvxpy.minimum(0, cvxpy.multiply( (1 - y_train[idx])/2.0, dist_bound_prod) ) ) * (s_val_to_avg[1][v] / sum(y_train == -1)) # avg false positive distance from boundary (only operates on the ground truth neg dataset)
                cons_sum_dict[2][v] = cvxpy.sum( cvxpy.minimum(0, cvxpy.multiply( (1 + y_train[idx])/2.0, dist_bound_prod) ) ) * (s_val_to_avg[2][v] / sum(y_train == +1)) # avg false negative distance from boundary
                #################################################################

                
            if cons_type == 4:
                cts = [1,2]
            elif cons_type in [0,1,2]:
                cts = [cons_type]
            
            else:
                raise Exception("Invalid constraint type")


            #################################################################
            #DCCP constraints
            for ct in cts:
                thresh = abs(sensitive_attrs_to_cov_thresh[attr][ct][1] - sensitive_attrs_to_cov_thresh[attr][ct][0])
                constraints.append( cons_sum_dict[ct][1] <= cons_sum_dict[ct][0]  + thresh )
                constraints.append( cons_sum_dict[ct][1] >= cons_sum_dict[ct][0]  - thresh )

            #################################################################


            
        else: # otherwise, its a categorical attribute, so we need to set the cov thresh for each value separately
            # need to fill up this part
            raise Exception("Fill the constraint code for categorical sensitive features... Exiting...")
            sys.exit(1)
            

    return constraints
示例#25
0
# x - how much BC puts in C

a = u + v + w + 100
b = x + 100 - u
c = 200 - v - x
d = 100 - w

#a,b,c,d - how much budget each item gets

num_of_participants = 5
donations = []
for i in range(num_of_participants):
    donations.append(100)

utilities = [
    cvxpy.minimum(a, b),
    cvxpy.minimum(a, c),
    cvxpy.minimum(a, d),
    cvxpy.minimum(b, c), a
]

# here is another example -
# a = v +w + 100
# b = x + u
# c = 200 -v -x
# d = 100-w + 100 - u
#
# num_of_participants = 5
# donations=[]
# for i in range(num_of_participants):
#     donations.append(100)
示例#26
0
    def _init_edge_deps(self):
        self.delays = defaultdict(lambda: cvxpy.Variable(integer=True))
        self.delay_violations = []
        for edge in self.edges:
            src_node = self.id_to_node_lookup[edge.src]
            dst_node = self.id_to_node_lookup[edge.dst]
            # if they're not in the same partition, then guarantee inequality.
            enforce_inequality: int
            if not self._possible_in_same_partition(src_node, dst_node):
                enforce_inequality = 1
            else:
                dst_candidates = self._candidate_partitions(dst_node)
                inequalities = []
                for partition_type in self._candidate_partitions(src_node):
                    src_row = self._get_row(partition_type, src_node)
                    if partition_type not in dst_candidates:
                        inequalities.append(cvxpy.sum(src_row))
                        continue
                    dst_row = self._get_row(partition_type, dst_node)
                    inequalities.append(
                        cvxpy.sum(cvxpy.maximum(src_row - dst_row, 0)))
                enforce_inequality = cvxpy.maximum(*inequalities,
                                                   0) if inequalities else 0
            self.delay_violations.append(
                self._project_to_bool(
                    cvxpy.maximum(
                        self.delays[src_node] + enforce_inequality -
                        self.delays[dst_node], 0), self.num_nodes * 2))

        # for each node, compute its partition delay. Then constrain that the partition delay and actual delay are
        # close.
        partition_delays = {
            partition_type: cvxpy.Variable(shape=count, nonneg=True)
            for partition_type, count in self.partition_counts.items()
        }

        max_delay = 4 * sum(self.partition_counts.values())

        for partition_delay in partition_delays.values():
            self._add_constraint(partition_delay <= max_delay)

        for node in self.nodes:
            target_delay_min = [max_delay]
            target_delay_max = [0]
            for partition_type, node_to_loc in self.node_to_loc_map.items():
                if node not in node_to_loc:
                    continue

                loc = node_to_loc[node]
                row = self.partition_matrices[partition_type][loc, :]
                activation = row * -max_delay + max_delay
                target_delay_min.append(
                    cvxpy.min(partition_delays[partition_type] + activation))

                activation = row * max_delay - max_delay
                target_delay_max.append(
                    cvxpy.max(partition_delays[partition_type] + activation))
            target_min = cvxpy.minimum(*target_delay_min)
            self._add_pseudo_constraint(
                cvxpy.maximum(self.delays[node] - target_min, 0))

            target_max = cvxpy.maximum(*target_delay_max)
            self._add_pseudo_constraint(
                cvxpy.maximum(target_max - self.delays[node], 0))
示例#27
0
    def test_minimum_sign(self):
        # Two args.
        self.assertEqual(cp.minimum(1, 2).sign, s.NONNEG)
        self.assertEqual(cp.minimum(1, Variable()).sign, s.UNKNOWN)
        self.assertEqual(cp.minimum(1, -2).sign, s.NONPOS)
        self.assertEqual(cp.minimum(1, 0).sign, s.ZERO)

        self.assertEqual(cp.minimum(Variable(), 0).sign, s.NONPOS)
        self.assertEqual(cp.minimum(Variable(), Variable()).sign, s.UNKNOWN)
        self.assertEqual(cp.minimum(Variable(), -2).sign, s.NONPOS)

        self.assertEqual(cp.minimum(0, 0).sign, s.ZERO)
        self.assertEqual(cp.minimum(0, -2).sign, s.NONPOS)

        self.assertEqual(cp.minimum(-3, -2).sign, s.NONPOS)

        # Many args.
        self.assertEqual(
            cp.minimum(-2, Variable(), 0, -1, Variable(), 1).sign, s.NONPOS)

        # Promotion.
        self.assertEqual(cp.minimum(-1, Variable(2)).sign, s.NONPOS)
        self.assertEqual(cp.minimum(-1, Variable(2)).shape, (2, ))
示例#28
0
def integer_opt(number_of_pet, number_of_pcs, pet_power_demand, block_cdq,
                pet_state, pet_lat, pet_lon, number_of_region, pet_region,
                pet_soc, pet_pick_up_probability, block_plq, plq_arrival_rate,
                delay_aware_arrival_rate, block_delay_aware,
                pet_remaining_power, V, per_service_fee, pev_arrival_rate,
                cdq_service_rate, pcs_cost, max_soc, acceptance):
    # Variable.
    y = cv.Variable((number_of_pcs, number_of_pet), boolean=True)
    x = cv.multiply(acceptance, y)
    # Objective function
    # Section: cdq
    cdq_arrival_rate = cv.sum(cv.multiply(pet_power_demand, x),
                              axis=1,
                              keepdims=True) + pev_arrival_rate
    section_cdq_1 = cdq_arrival_rate - cdq_service_rate
    section_cdq = cv.sum(cv.multiply(section_cdq_1, block_cdq))
    # section_plq
    # pick up constraint
    pet = pd.DataFrame(pet_state, columns=['state'])
    pet['soc'] = pet_soc
    pet['state'] = pet_state
    pet['pick_up_probability'] = pet_pick_up_probability
    # pet_pick_up_ini = pet.apply(lambda x: np.random.choice([0, 1], p=[
    #     1-x['pick_up_probability'], x['pick_up_probability']]) if ((x['state'] == 0) & (x['soc'] > 1.5)) else 0, axis=1).values.reshape(1, number_of_pet)
    pet_region_matrix = np.zeros((number_of_region, number_of_pet))
    for i in range(number_of_pet):
        for j in range(number_of_region):
            if pet_region[i] == j:
                pet_region_matrix[j, i] = 1
            pass
        pass
    pass
    pet_recommended = cv.sum(x, axis=0, keepdims=True)
    pet_non_recommended = (np.ones((1, number_of_pet)) - pet_recommended)
    pet_available_ini = np.where(((pet['state'] == 0) & (pet['soc'] > 0.15)),
                                 1, 0).reshape(1, number_of_pet)
    pet_available = cv.multiply(pet_available_ini, pet_non_recommended)
    # pet_pick_up = cv.multiply(pet_non_recommended, pet_pick_up_ini)
    pet_available_region = pet_region_matrix @ pet_available.T
    pet_pick_up_region = cv.minimum(pet_available_region, block_plq)
    section_plq_2 = plq_arrival_rate - pet_pick_up_region
    section_plq = cv.sum(cv.multiply(block_plq, section_plq_2))
    # Section: Delay aware
    section_delay_aware = cv.sum(
        cv.multiply(block_delay_aware,
                    (delay_aware_arrival_rate - pet_pick_up_region)))
    # section: profit
    section_profit = cv.sum(
        cv.multiply(cdq_arrival_rate, per_service_fee) - pcs_cost)
    # objects = - V * section_profit
    # objects = section_cdq - V * section_profit
    objects = (section_plq + section_delay_aware +
               section_cdq) - V * section_profit
    constraints_2_right = np.full((number_of_pet, 1), 1)
    state_test = np.where((pet_state == 0), 1, 0)
    # SOC
    constraints_4_right = np.full((number_of_pcs, number_of_pet), 0)
    soc_test = np.where((pet_remaining_power < 0), 1, 0)
    # rem * x ==0
    # column sum
    constraints_5_right = np.full((1, number_of_pet), 1)
    constraints_6_right = np.full((number_of_pet, 1), 1)
    constraints_7_right = np.full((number_of_pet, 1), 1)
    soc_range_test = np.where((pet_soc > 0.1), 1, 0)
    soc_state_test = np.where((pet_state == 2), 1, 0)
    soc_1 = np.where((pet_soc > max_soc), 1, 0)
    constraints = [
        state_test + pet_non_recommended.T >= constraints_2_right,
        cv.multiply(soc_test, x) == constraints_4_right,
        pet_recommended <= constraints_5_right,
        soc_1 + pet_recommended.T <= constraints_6_right,
        soc_range_test + soc_state_test + pet_recommended.T >=
        constraints_7_right
    ]
    # constraints = [cv.multiply(soc_test, x) == constraints_4_right]
    problem = cv.Problem(cv.Minimize(objects), constraints)
    problem.solve(solver=cv.CPLEX)
    return x.value, pet_pick_up_region.value