Exemplo n.º 1
0
 def test_div(self):
     """Tests a problem with division.
     """
     obj = Minimize(normInf(self.A/5))
     p = Problem(obj, [self.A >= 5])
     result = p.solve()
     self.assertAlmostEqual(result, 1)
Exemplo n.º 2
0
    def test_norm2(self):
        # Constant argument.
        p = Problem(Minimize(norm2(-2)))
        result = p.solve()
        self.assertAlmostEqual(result, 2)

        # Scalar arguments.
        p = Problem(Minimize(norm2(self.a)), [self.a <= -2])
        result = p.solve()
        self.assertAlmostEqual(result, 2)
        self.assertAlmostEqual(self.a.value, -2)

        # Maximize
        p = Problem(Maximize(-norm2(self.a)), [self.a <= -2])
        result = p.solve()
        self.assertAlmostEqual(result, -2)
        self.assertAlmostEqual(self.a.value, -2)

        # Vector arguments.
        p = Problem(Minimize(norm2(self.x - self.z) + 5),
            [self.x >= [2,3], self.z <= [-1,-4]])
        result = p.solve()
        self.assertAlmostEqual(result, 12.61577)
        self.assertItemsAlmostEqual(self.x.value, [2,3])
        self.assertItemsAlmostEqual(self.z.value, [-1,-4])

        # Row  arguments.
        p = Problem(Minimize(norm2((self.x - self.z).T) + 5),
            [self.x >= [2,3], self.z <= [-1,-4]])
        result = p.solve()
        self.assertAlmostEqual(result, 12.61577)
        self.assertItemsAlmostEqual(self.x.value, [2,3])
        self.assertItemsAlmostEqual(self.z.value, [-1,-4])
Exemplo n.º 3
0
    def test_dual_variables(self):
        p = Problem(Minimize( norm1(self.x + self.z) ),
            [self.x >= [2,3],
             [[1,2],[3,4]]*self.z == [-1,-4],
             norm2(self.x + self.z) <= 100])
        result = p.solve()
        self.assertAlmostEqual(result, 4)
        self.assertItemsAlmostEqual(self.x.value, [4,3])
        self.assertItemsAlmostEqual(self.z.value, [-4,1])
        # Dual values
        self.assertItemsAlmostEqual(p.constraints[0].dual_value, [0, 1])
        self.assertItemsAlmostEqual(p.constraints[1].dual_value, [-1, 0.5])
        self.assertAlmostEqual(p.constraints[2].dual_value, 0)

        T = matrix(2,(2,3))
        c = matrix([3,4])
        p = Problem(Minimize(1),
            [self.A >= T*self.C,
             self.A == self.B,
             self.C == T.T])
        result = p.solve()
        # Dual values
        self.assertItemsAlmostEqual(p.constraints[0].dual_value, 4*[0])
        self.assertItemsAlmostEqual(p.constraints[1].dual_value, 4*[0])
        self.assertItemsAlmostEqual(p.constraints[2].dual_value, 6*[0])
Exemplo n.º 4
0
    def test_pnorm(self):
        import numpy as np

        x = Variable(3, name='x')

        a = np.array([1.0, 2, 3])

        # todo: add -1, .5, .3, -2.3 and testing positivity constraints

        for p in (1, 1.6, 1.3, 2, 1.99, 3, 3.7, np.inf):
            prob = Problem(Minimize(pnorm(x, p=p)), [x.T*a >= 1])
            prob.solve()

            # formula is true for any a >= 0 with p > 1
            if p == np.inf:
                x_true = np.ones_like(a)/sum(a)
            elif p == 1:
                # only works for the particular a = [1,2,3]
                x_true = np.array([0, 0, 1.0/3])
            else:
                x_true = a**(1.0/(p-1))/a.dot(a**(1.0/(p-1)))

            x_alg = np.array(x.value).flatten()
            self.assertTrue(np.allclose(x_alg, x_true, 1e-3), 'p = {}'.format(p))
            self.assertTrue(np.allclose(prob.value, np.linalg.norm(x_alg, p)))
            self.assertTrue(np.allclose(np.linalg.norm(x_alg, p), pnorm(x_alg, p).value))
Exemplo n.º 5
0
    def test_chebyshev_center(self):
        # The goal is to find the largest Euclidean ball (i.e. its center and
        # radius) that lies in a polyhedron described by linear inequalites in this
        # fashion: P = {x : a_i'*x <= b_i, i=1,...,m} where x is in R^2

        # Generate the input data
        a1 = np.matrix("2; 1")
        a2 = np.matrix(" 2; -1")
        a3 = np.matrix("-1;  2")
        a4 = np.matrix("-1; -2")
        b = np.ones([4,1])

        # Create and solve the model
        r = Variable(name='r')
        x_c = Variable(2,name='x_c')
        obj = Maximize(r)
        constraints = [ #TODO have atoms compute values for constants.
            a1.T*x_c + np.linalg.norm(a1)*r <= b[0],
            a2.T*x_c + np.linalg.norm(a2)*r <= b[1],
            a3.T*x_c + np.linalg.norm(a3)*r <= b[2],
            a4.T*x_c + np.linalg.norm(a4)*r <= b[3],
        ]
        
        p = Problem(obj, constraints)
        result = p.solve()
        self.assertAlmostEqual(result, 0.4472)
        self.assertAlmostEqual(r.value, result)
        self.assertAlmostEqual(x_c.value, [0,0])
Exemplo n.º 6
0
 def test_variable_name_conflict(self):
     var = Variable(name='a')
     p = Problem(Maximize(self.a + var), [var == 2 + self.a, var <= 3])
     result = p.solve()
     self.assertAlmostEqual(result, 4.0)
     self.assertAlmostEqual(self.a.value, 1)
     self.assertAlmostEqual(var.value, 3)
Exemplo n.º 7
0
    def test_consistency(self):
        """Test that variables and constraints keep a consistent order.
        """
        import itertools
        num_solves = 4
        vars_lists = []
        ineqs_lists = []
        for k in range(num_solves):
            sum = 0
            constraints = []
            for i in range(100):
                var = Variable(name=str(i))
                sum += var
                constraints.append(var >= i)
            obj = Minimize(sum)
            p = Problem(obj, constraints)
            objective, constr_map, dims = p.canonicalize()
            all_ineq = itertools.chain(constr_map[s.EQ], constr_map[s.INEQ])
            var_info = p._get_var_offsets(objective, all_ineq)
            sorted_vars, var_offsets, x_length = var_info
            vars_lists.append([int(v.name()) for v in sorted_vars])
            ineqs_lists.append(constr_map[s.INEQ])

        # Verify order of variables is consistent.
        for i in range(num_solves):
            self.assertEqual(range(100), vars_lists[i])
        for i in range(num_solves):
            for idx, constr in enumerate(ineqs_lists[i]):
                var = constr.variables()[0]
                self.assertEqual(idx, int(var.name()))
Exemplo n.º 8
0
    def test_parameter_expressions(self):
        """Test that expressions with parameters are updated properly.
        """
        x = Variable()
        y = Variable()
        x0 = Parameter()
        xSquared = x0*x0 + 2*x0*(x - x0)

        # initial guess for x
        x0.value = 2

        # make the constraint x**2 - y == 0
        g = xSquared - y

        # set up the problem
        obj = abs(x - 1)
        prob = Problem( Minimize( obj ), [ g == 0 ] )
        prob.solve()
        x0.value = 1
        prob.solve()
        self.assertAlmostEqual(g.value, 0)

        # Test multiplication.
        prob = Problem( Minimize( x0*x ), [ x == 1 ] )
        x0.value = 2
        prob.solve()
        x0.value = 1
        prob.solve()
        self.assertAlmostEqual(prob.value, 1)
Exemplo n.º 9
0
    def test_vstack(self):
        c = matrix(1, (1,5))
        p = Problem(Minimize(c * vstack(self.x, self.y)),
            [self.x == [1,2],
            self.y == [3,4,5]])
        result = p.solve()
        self.assertAlmostEqual(result, 15)

        c = matrix(1, (1,4))
        p = Problem(Minimize(c * vstack(self.x, self.x)),
            [self.x == [1,2]])
        result = p.solve()
        self.assertAlmostEqual(result, 6)


        c = matrix(1, (2,2))
        p = Problem( Minimize( sum(vstack(self.A, self.C)) ),
            [self.A >= 2*c,
            self.C == -2])
        result = p.solve()
        self.assertAlmostEqual(result, -4)

        c = matrix(1, (1,2))
        p = Problem( Minimize( sum(vstack(c*self.A, c*self.B)) ),
            [self.A >= 2,
            self.B == -2])
        result = p.solve()
        self.assertAlmostEqual(result, 0)

        c = matrix([1,-1])
        p = Problem( Minimize( c.T * vstack(square(self.a), sqrt(self.b))),
            [self.a == 2,
             self.b == 16])
        result = p.solve()
        self.assertAlmostEqual(result, 0)
Exemplo n.º 10
0
    def test_normInf(self):
        # Constant argument.
        p = Problem(Minimize(normInf(-2)))
        result = p.solve()
        self.assertAlmostEqual(result, 2)

        # Scalar arguments.
        p = Problem(Minimize(normInf(self.a)), [self.a >= 2])
        result = p.solve()
        self.assertAlmostEqual(result, 2)
        self.assertAlmostEqual(self.a.value, 2)

        p = Problem(Minimize(3*normInf(self.a + 2*self.b) + self.c),
            [self.a >= 2, self.b <= -1, self.c == 3])
        result = p.solve()
        self.assertAlmostEqual(result, 3)
        self.assertAlmostEqual(self.a.value + 2*self.b.value, 0)
        self.assertAlmostEqual(self.c.value, 3)

        # Maximize
        p = Problem(Maximize(-normInf(self.a)), [self.a <= -2])
        result = p.solve()
        self.assertAlmostEqual(result, -2)
        self.assertAlmostEqual(self.a.value, -2)

        # Vector arguments.
        p = Problem(Minimize(normInf(self.x - self.z) + 5),
            [self.x >= [2,3], self.z <= [-1,-4]])
        result = p.solve()
        self.assertAlmostEqual(result, 12)
        self.assertAlmostEqual(list(self.x.value)[1] - list(self.z.value)[1], 7)
Exemplo n.º 11
0
 def test_cvxopt_errors(self):
     """Tests that cvxopt errors are caught as solver_error.
     """
     # For some reason CVXOPT can't handle this problem.
     expr = 500*self.a + square(self.a)
     prob = Problem(Minimize(expr))
     prob.solve(solver=s.CVXOPT)
     self.assertEqual(prob.status, s.SOLVER_ERROR)
Exemplo n.º 12
0
 def test_ecos_noineq(self):
     """Test ECOS with no inequality constraints.
     """
     T = matrix(1, (2, 2))
     p = Problem(Minimize(1), [self.A == T])
     result = p.solve(solver=s.ECOS)
     self.assertAlmostEqual(result, 1)
     self.assertItemsAlmostEqual(self.A.value, T)
Exemplo n.º 13
0
 def test_sdp(self):
     # Ensure sdp constraints enforce transpose.
     obj = Maximize(self.A[1,0] - self.A[0,1])
     p = Problem(obj, [lambda_max(self.A) <= 100,
                       self.A[0,0] == 2,
                       self.A[1,1] == 2,
                       self.A[1,0] == 2])
     result = p.solve()
     self.assertAlmostEqual(result, 0)
Exemplo n.º 14
0
 def test_sdp(self):
     """Test a problem with semidefinite cones.
     """
     a = sp.rand(100,100,.1, random_state=1)
     a = a.todense()
     X = Variable(100,100)
     obj = at.norm(X, "nuc") + at.norm(X-a,'fro')
     p = Problem(Minimize(obj))
     p.solve(solver="SCS")
Exemplo n.º 15
0
 def test_large_expression(self):
     for n in [10, 20, 30, 40, 50]:
         A = matrix(range(n*n), (n,n))
         x = Variable(n,n)
         p = Problem(Minimize(sum(x)), [x >= A])
         result = p.solve()
         answer = n*n*(n*n+1)/2 - n*n
         print result - answer
         self.assertAlmostEqual(result, answer)
Exemplo n.º 16
0
 def test_mixed_atoms(self):
     p = Problem(Minimize(norm2(5 + norm1(self.z)
                               + norm1(self.x) +
                               normInf(self.x - self.z) ) ),
         [self.x >= [2,3], self.z <= [-1,-4], norm2(self.x + self.z) <= 2])
     result = p.solve()
     self.assertAlmostEqual(result, 22)
     self.assertItemsAlmostEqual(self.x.value, [2,3])
     self.assertItemsAlmostEqual(self.z.value, [-1,-4])
Exemplo n.º 17
0
 def test_mult_by_zero(self):
     """Test multiplication by zero.
     """
     exp = 0*self.a
     self.assertEqual(exp.value, 0)
     obj = Minimize(exp)
     p = Problem(obj)
     result = p.solve()
     self.assertAlmostEqual(result, 0)
Exemplo n.º 18
0
 def test_matrix_socp(self):
     """Test matrix SOCP.
     """
     A = numpy.ones((10, 10))
     X = Variable(10, 10)
     cost = norm(X - 1, 'fro')
     prob = Problem(Minimize(cost), [A*X >= 2])
     result = prob.solve(solver=s.ECOS)
     self.assertAlmostEqual(result, 0)
Exemplo n.º 19
0
 def test_parameters(self):
     """Test the parameters method.
     """
     p1 = Parameter()
     p2 = Parameter(3, sign="negative")
     p3 = Parameter(4, 4, sign="positive")
     p = Problem(Minimize(p1), [self.a + p1 <= p2, self.b <= p3 + p3 + 2])
     params = p.parameters()
     self.assertItemsEqual(params, [p1, p2, p3])
Exemplo n.º 20
0
    def test_slicing(self):
        p = Problem(Maximize(sum(self.C)), [self.C[1:3, :] <= 2, self.C[0, :] == 1])
        result = p.solve()
        self.assertAlmostEqual(result, 10)
        self.assertItemsAlmostEqual(self.C, 2 * [1, 2, 2])

        p = Problem(Maximize(sum(self.C[0:3:2, 1])), [self.C[1:3, :] <= 2, self.C[0, :] == 1])
        result = p.solve()
        self.assertAlmostEqual(result, 3)
        self.assertItemsAlmostEqual(self.C.value[0:3:2, 1], [1, 2])

        p = Problem(
            Maximize(sum((self.C[0:2, :] + self.A)[:, 0:2])),
            [
                self.C[1:3, :] <= 2,
                self.C[0, :] == 1,
                (self.A + self.B)[:, 0] == 3,
                (self.A + self.B)[:, 1] == 2,
                self.B == 1,
            ],
        )
        result = p.solve()
        self.assertAlmostEqual(result, 12)
        self.assertItemsAlmostEqual(self.C.value[0:2, :], [1, 2, 1, 2])
        self.assertItemsAlmostEqual(self.A.value, [2, 2, 1, 1])

        p = Problem(
            Maximize([[3], [4]] * (self.C[0:2, :] + self.A)[:, 0]),
            [
                self.C[1:3, :] <= 2,
                self.C[0, :] == 1,
                [[1], [2]] * (self.A + self.B)[:, 0] == 3,
                (self.A + self.B)[:, 1] == 2,
                self.B == 1,
                3 * self.A[:, 0] <= 3,
            ],
        )
        result = p.solve()
        self.assertAlmostEqual(result, 12)
        self.assertItemsAlmostEqual(self.C.value[0:2, 0], [1, 2])
        self.assertItemsAlmostEqual(self.A.value, [1, -0.5, 1, 1])

        p = Problem(
            Minimize(norm2((self.C[0:2, :] + self.A)[:, 0])),
            [
                self.C[1:3, :] <= 2,
                self.C[0, :] == 1,
                (self.A + self.B)[:, 0] == 3,
                (self.A + self.B)[:, 1] == 2,
                self.B == 1,
            ],
        )
        result = p.solve()
        self.assertAlmostEqual(result, 3)
        self.assertItemsAlmostEqual(self.C.value[0:2, 0], [1, -2])
        self.assertItemsAlmostEqual(self.A.value, [2, 2, 1, 1])
Exemplo n.º 21
0
 def test_large_square(self):
     """Test large number of variables squared.
     """
     for n in [10, 20, 30, 40, 50]:
         A = matrix(list(range(n*n)), (n,n))
         x = Variable(n,n)
         p = Problem(Minimize(at.square(x[0, 0])),
             [x >= A])
         result = p.solve()
         self.assertAlmostEqual(result, 0)
Exemplo n.º 22
0
 def test_variables(self):
     """Test the variables method.
     """
     p = Problem(Minimize(self.a), [self.a <= self.x, self.b <= self.A + 2])
     vars_ = p.variables()
     ref = [self.a, self.x, self.b, self.A]
     if PY2:
         self.assertItemsEqual(vars_, ref)
     else:
         self.assertCountEqual(vars_, ref)
Exemplo n.º 23
0
    def test_numpy_scalars(self):
        n = 6
        eps = 1e-6
        cvxopt.setseed(10)
        P0 = cvxopt.normal(n, n)
        eye = cvxopt.spmatrix(1.0, range(n), range(n))
        P0 = P0.T * P0 + eps * eye

        print P0

        P1 = cvxopt.normal(n, n)
        P1 = P1.T*P1
        P2 = cvxopt.normal(n, n)
        P2 = P2.T*P2
        P3 = cvxopt.normal(n, n)
        P3 = P3.T*P3

        q0 = cvxopt.normal(n, 1)
        q1 = cvxopt.normal(n, 1)
        q2 = cvxopt.normal(n, 1)
        q3 = cvxopt.normal(n, 1)

        r0 = cvxopt.normal(1, 1)
        r1 = cvxopt.normal(1, 1)
        r2 = cvxopt.normal(1, 1)
        r3 = cvxopt.normal(1, 1)

        slack = Variable()
        # Form the problem
        x = Variable(n)
        objective = Minimize( 0.5*quad_form(x,P0) + q0.T*x + r0 + slack)
        constraints = [0.5*quad_form(x,P1) + q1.T*x + r1 <= slack,
                       0.5*quad_form(x,P2) + q2.T*x + r2 <= slack,
                       0.5*quad_form(x,P3) + q3.T*x + r3 <= slack,
        ]

        # We now find the primal result and compare it to the dual result
        # to check if strong duality holds i.e. the duality gap is effectively zero
        p = Problem(objective, constraints)
        primal_result = p.solve()

        # Note that since our data is random, we may need to run this program multiple times to get a feasible primal
        # When feasible, we can print out the following values
        print x.value # solution
        lam1 = constraints[0].dual_value
        lam2 = constraints[1].dual_value
        lam3 = constraints[2].dual_value
        print type(lam1)

        P_lam = P0 + lam1*P1 + lam2*P2 + lam3*P3
        q_lam = q0 + lam1*q1 + lam2*q2 + lam3*q3
        r_lam = r0 + lam1*r1 + lam2*r2 + lam3*r3
        dual_result = -0.5*q_lam.T*P_lam*q_lam + r_lam
        print dual_result[0,0]
        self.assertEquals(dual_result.size, (1,1))
Exemplo n.º 24
0
 def test_duplicate_constraints(self):
     eq = (self.x == 2)
     le = (self.x <= 2)
     obj = 0
     def test(self):
         objective,constr_map,dims = self.canonicalize()
         return (len(constr_map[s.EQ]),len(constr_map[s.INEQ]))
     Problem.register_solve("test", test)
     p = Problem(Minimize(obj),[eq,eq,le,le])
     result = p.solve(method="test")
     self.assertEqual(result, (1,1))
Exemplo n.º 25
0
 def test_presolve_constant_constraints(self):
     """Test that the presolver removes constraints with no variables.
     """
     x = Variable()
     obj = Maximize(sqrt(x))
     prob = Problem(obj)
     c, G, h, dims, A, b = prob.get_problem_data(s.ECOS)
     for row in range(A.shape[0]):
         assert A[row, :].nnz > 0
     for row in range(G.shape[0]):
         assert G[row, :].nnz > 0
Exemplo n.º 26
0
    def test_reshape(self):
        """Tests problems with reshape.
        """
        # Test on scalars.
        self.assertEqual(reshape(1, 1, 1).value, 1)

        # Test vector to matrix.
        x = Variable(4)
        mat = matrix([[1,-1], [2, -2]])
        vec = matrix([1, 2, 3, 4])
        vec_mat = matrix([[1, 2], [3, 4]])
        expr = reshape(x, 2, 2)
        obj = Minimize(sum_entries(mat*expr))
        prob = Problem(obj, [x == vec])
        result = prob.solve()
        self.assertAlmostEqual(result, sum(mat*vec_mat))

        # Test on matrix to vector.
        c = [1, 2, 3, 4]
        expr = reshape(self.A, 4, 1)
        obj = Minimize(expr.T*c)
        constraints = [self.A == [[-1, -2], [3, 4]]]
        prob = Problem(obj, constraints)
        result = prob.solve()
        self.assertAlmostEqual(result, 20)
        self.assertItemsAlmostEqual(expr.value, [-1, -2, 3, 4])
        self.assertItemsAlmostEqual(reshape(expr, 2, 2).value, [-1, -2, 3, 4])

        # Test matrix to matrix.
        expr = reshape(self.C, 2, 3)
        mat = numpy.matrix([[1,-1], [2, -2]])
        C_mat = numpy.matrix([[1, 4], [2, 5], [3, 6]])
        obj = Minimize(sum_entries(mat*expr))
        prob = Problem(obj, [self.C == C_mat])
        result = prob.solve()
        reshaped = numpy.reshape(C_mat, (2, 3), 'F')
        self.assertAlmostEqual(result, (mat.dot(reshaped)).sum())
        self.assertItemsAlmostEqual(expr.value, C_mat)

        # Test promoted expressions.
        c = matrix([[1,-1], [2, -2]])
        expr = reshape(c*self.a, 1, 4)
        obj = Minimize(expr*[1, 2, 3, 4])
        prob = Problem(obj, [self.a == 2])
        result = prob.solve()
        self.assertAlmostEqual(result, -6)
        self.assertItemsAlmostEqual(expr.value, 2*c)

        expr = reshape(c*self.a, 4, 1)
        obj = Minimize(expr.T*[1, 2, 3, 4])
        prob = Problem(obj, [self.a == 2])
        result = prob.solve()
        self.assertAlmostEqual(result, -6)
        self.assertItemsAlmostEqual(expr.value, 2*c)
Exemplo n.º 27
0
 def test_vec(self):
     """Tests problems with vec.
     """
     c = [1, 2, 3, 4]
     expr = vec(self.A)
     obj = Minimize(expr.T*c)
     constraints = [self.A == [[-1, -2], [3, 4]]]
     prob = Problem(obj, constraints)
     result = prob.solve()
     self.assertAlmostEqual(result, 20)
     self.assertItemsAlmostEqual(expr.value, [-1, -2, 3, 4])
Exemplo n.º 28
0
 def test_large_sum(self):
     """Test large number of variables summed.
     """
     for n in [10, 20, 30, 40, 50]:
         A = matrix(list(range(n*n)), (n,n))
         x = Variable(n,n)
         p = Problem(Minimize(at.sum_entries(x)), [x >= A])
         result = p.solve()
         answer = n*n*(n*n+1)/2 - n*n
         print(result - answer)
         self.assertAlmostEqual(result, answer)
Exemplo n.º 29
0
 def test_diag_prob(self):
     """Test a problem with diag.
     """
     C = Variable(3, 3)
     obj = Maximize(C[0, 2])
     constraints = [diag(C) == 1,
                    C[0, 1] == 0.6,
                    C[1, 2] == -0.3,
                    C == Semidef(3)]
     prob = Problem(obj, constraints)
     result = prob.solve()
     self.assertAlmostEqual(result, 0.583151)
Exemplo n.º 30
0
 def test_parameter_problems(self):
     """Test problems with parameters.
     """
     p1 = Parameter()
     p2 = Parameter(3, sign="negative")
     p3 = Parameter(4, 4, sign="positive")
     p = Problem(Maximize(p1*self.a), [self.a + p1 <= p2, self.b <= p3 + p3 + 2])
     p1.value = 2
     p2.value = -numpy.ones((3,1))
     p3.value = numpy.ones((4, 4))
     result = p.solve()
     self.assertAlmostEqual(result, -6)
Exemplo n.º 31
0
 def _value_impl(self):
     from cvxpy.problems.problem import Problem
     from cvxpy.problems.objective import Maximize
     y_val = self.args[0].value.round(decimals=9).ravel(order='F')
     x_flat = self._parent.x.flatten()
     cons = self._parent.constraints
     if len(cons) == 0:
         dummy = Variable()
         cons = [dummy == 1]
     prob = Problem(Maximize(y_val @ x_flat), cons)
     val = prob.solve(solver='SCS', eps=1e-6)
     return val
Exemplo n.º 32
0
 def test_parameter_problems(self):
     """Test problems with parameters.
     """
     p1 = Parameter()
     p2 = Parameter(3, sign="negative")
     p3 = Parameter(4, 4, sign="positive")
     p = Problem(Maximize(p1*self.a), [self.a + p1 <= p2, self.b <= p3 + p3 + 2])
     p1.value = 2
     p2.value = -numpy.ones((3,1))
     p3.value = numpy.ones((4, 4))
     result = p.solve()
     self.assertAlmostEqual(result, -6)
Exemplo n.º 33
0
 def test_large_sum(self):
     """Test large number of variables summed.
     """
     for n in [10, 20, 30, 40, 50]:
         A = np.arange(n * n)
         A = np.reshape(A, (n, n))
         x = Variable((n, n))
         p = Problem(Minimize(at.sum(x)), [x >= A])
         result = p.solve()
         answer = n * n * (n * n + 1) / 2 - n * n
         print(result - answer)
         self.assertAlmostEqual(result, answer)
Exemplo n.º 34
0
 def test_mixed_atoms(self):
     p = Problem(
         Minimize(
             norm2(5 + norm1(self.z) + norm1(self.x) +
                   normInf(self.x - self.z))), [
                       self.x >= [2, 3], self.z <= [-1, -4],
                       norm2(self.x + self.z) <= 2
                   ])
     result = p.solve()
     self.assertAlmostEqual(result, 22)
     self.assertItemsAlmostEqual(self.x.value, [2, 3])
     self.assertItemsAlmostEqual(self.z.value, [-1, -4])
Exemplo n.º 35
0
    def test_redundant_constraints(self):
        obj = Minimize(sum(self.x))
        constraints = [self.x == 2, self.x == 2, self.x.T == 2, self.x[0] == 2]
        p = Problem(obj, constraints)
        result = p.solve(solver=s.CVXOPT)
        self.assertAlmostEqual(result, 4)

        obj = Minimize(sum(square(self.x)))
        constraints = [self.x == self.x]
        p = Problem(obj, constraints)
        result = p.solve(solver=s.CVXOPT)
        self.assertAlmostEqual(result, 0)
Exemplo n.º 36
0
    def grad(self):
        """Gives the (sub/super)gradient of the expression w.r.t. each variable.

        Matrix expressions are vectorized, so the gradient is a matrix.
        None indicates variable values unknown or outside domain.

        Returns:
            A map of variable to SciPy CSC sparse matrix or None.
        """
        # Subgrad of g(y) = min f_0(x,y)
        #                   s.t. f_i(x,y) <= 0, i = 1,..,p
        #                        h_i(x,y) == 0, i = 1,...,q
        # Given by Df_0(x^*,y) + \sum_i Df_i(x^*,y) \lambda^*_i
        #          + \sum_i Dh_i(x^*,y) \nu^*_i
        # where x^*, \lambda^*_i, \nu^*_i are optimal primal/dual variables.
        # Add PSD constraints in same way.

        # Short circuit for constant.
        if self.is_constant():
            return u.grad.constant_grad(self)

        old_vals = {var.id: var.value for var in self.variables()}
        fix_vars = []
        for var in self.dont_opt_vars:
            if var.value is None:
                return u.grad.error_grad(self)
            else:
                fix_vars += [var == var.value]
        prob = Problem(self.args[0].objective,
                       fix_vars + self.args[0].constraints)
        prob.solve(verbose=True)
        # Compute gradient.
        if prob.status in s.SOLUTION_PRESENT:
            sign = self.is_convex() - self.is_concave()
            # Form Lagrangian.
            lagr = self.args[0].objective.args[0]
            for constr in self.args[0].constraints:
                # TODO: better way to get constraint expressions.
                lagr_multiplier = self.cast_to_const(sign*constr.dual_value)
                prod = lagr_multiplier.T*constr.expr
                if prod.is_scalar():
                    lagr += sum(prod)
                else:
                    lagr += trace(prod)
            grad_map = lagr.grad
            result = {var: grad_map[var] for var in self.dont_opt_vars}
        else:  # Unbounded, infeasible, or solver error.
            result = u.grad.error_grad(self)
        # Restore the original values to the variables.
        for var in self.variables():
            var.value = old_vals[var.id]
        return result
Exemplo n.º 37
0
 def test_presolve_constant_constraints(self):
     """Test that the presolver removes constraints with no variables.
     """
     x = Variable()
     obj = Maximize(sqrt(x))
     prob = Problem(obj)
     data = prob.get_problem_data(s.ECOS)
     A = data["A"]
     G = data["G"]
     for row in range(A.shape[0]):
         assert A[row, :].nnz > 0
     for row in range(G.shape[0]):
         assert G[row, :].nnz > 0
Exemplo n.º 38
0
 def test_parameters(self):
     """Test the parameters method.
     """
     p1 = Parameter()
     p2 = Parameter(3, sign="negative")
     p3 = Parameter(4, 4, sign="positive")
     p = Problem(Minimize(p1), [self.a + p1 <= p2, self.b <= p3 + p3 + 2])
     params = p.parameters()
     ref = [p1, p2, p3]
     if PY2:
         self.assertItemsEqual(params, ref)
     else:
         self.assertCountEqual(params, ref)
Exemplo n.º 39
0
    def test_duplicate_constraints(self):
        eq = (self.x == 2)
        le = (self.x <= 2)
        obj = 0

        def test(self):
            objective, constr_map, dims = self.canonicalize()
            return (len(constr_map[s.EQ]), len(constr_map[s.INEQ]))

        Problem.register_solve("test", test)
        p = Problem(Minimize(obj), [eq, eq, le, le])
        result = p.solve(method="test")
        self.assertEqual(result, (1, 1))
Exemplo n.º 40
0
 def test_large_sdp(self):
     """Test for bug where large PSD caused integer overflow in cvxcore.
     """
     SHAPE = (256, 256)
     rows = SHAPE[0]
     cols = SHAPE[1]
     X = Variable(SHAPE)
     Z = Variable((rows + cols, rows + cols))
     prob = Problem(
         Minimize(0.5 * at.trace(Z)),
         [X[0, 0] >= 1, Z[0:rows, rows:rows + cols] == X, Z >> 0, Z == Z.T])
     prob.solve(solver="SCS")
     self.assertAlmostEqual(prob.value, 1.0)
Exemplo n.º 41
0
Arquivo: spoc.py Projeto: stat-ml/SPOC
    def _get_Theta(self, U, F):
        """
        Function to find Theta from U and F via convex optimization in cvxpy
        or euclidean_proj_simplex

        Parameters
        ---------
        U: array-like with shape (n_nodes, n_clusters)

        F: nd.array with shape (n_clusters, n_clusters)
            with coordinates of k pretenders to be pure nodes


        Returns
        -------
        Theta: nd.array with shape (n_nodes, n_clusters)

        where n_nodes == U.shape[0], n_clusters == U.shape[1]


        Requires
        --------
        cvxpy (http://www.cvxpy.org/en/latest/)
        """

        assert U.shape[1] == F.shape[0] == F.shape[1], \
            "U.shape[1] != F.shape"

        n_nodes = U.shape[0]
        n_clusters = U.shape[1]

        if self.use_cvxpy:
            Theta = Variable(rows=n_nodes, cols=n_clusters)
            constraints = [
                sum_entries(Theta[i, :]) == 1 for i in range(n_nodes)
            ]
            constraints += [
                Theta[i, j] >= 0 for i in range(n_nodes)
                for j in range(n_clusters)
            ]
            obj = Minimize(norm(U - Theta * F, 'fro'))
            prob = Problem(obj, constraints)
            prob.solve()
            return np.array(Theta.value)
        else:
            projector = F.T.dot(np.linalg.inv(F.dot(F.T)))
            theta = U.dot(projector)
            theta_simplex_proj = np.array([
                self._euclidean_proj_simplex(x) for x in theta
            ])
            return theta_simplex_proj
Exemplo n.º 42
0
    def test_to_str(self):
        """Test string representations.
        """
        obj = Minimize(self.a)
        prob = Problem(obj)
        self.assertEqual(repr(prob), "Problem(%s, %s)" % (repr(obj), repr([])))
        constraints = [self.x*2 == self.x, self.x == 0]
        prob = Problem(obj, constraints)
        self.assertEqual(repr(prob), "Problem(%s, %s)" % (repr(obj), repr(constraints)))

        # Test str.
        result = "minimize %(name)s\nsubject to %(name)s == 0\n           0 <= %(name)s" % {"name": self.a.name()}
        prob = Problem(Minimize(self.a), [self.a == 0, self.a >= 0])
        self.assertEqual(str(prob), result)
Exemplo n.º 43
0
    def test_verbose(self):
        # From http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python
        # setup the environment
        outputs = {True: [], False: []}
        backup = sys.stdout

        # ####
        for verbose in [True, False]:
            for solver in [s.ECOS, s.CVXOPT, s.SCS]:
                sys.stdout = StringIO()  # capture output
                p = Problem(Minimize(self.a + self.x[0]),
                            [self.a >= 2, self.x >= 2])
                p.solve(verbose=verbose, solver=solver)
                if solver != s.ECOS:
                    p = Problem(Minimize(self.a), [log(self.a) >= 2])
                    p.solve(verbose=verbose, solver=solver)
                out = sys.stdout.getvalue()  # release output
                outputs[verbose].append(out.upper())
        # ####

        sys.stdout.close()  # close the stream
        sys.stdout = backup  # restore original stdout

        for output in outputs[True]:
            assert len(output) > 0
        for output in outputs[False]:
            assert len(output) == 0
Exemplo n.º 44
0
 def test_bad_objective(self):
     """Test using a cvxpy expression as an objective.
     """
     with self.assertRaises(Exception) as cm:
         Problem(self.x + 2)
     self.assertEqual(str(cm.exception),
                      "Problem objective must be Minimize or Maximize.")
Exemplo n.º 45
0
def fix_prob(prob, fix_var, param_list):
    """Fix the given variables in the problem.

        Parameters
        ----------
        expr : Problem
        fix_var : List
            Variables to be fixed.
        params: : List
            List of parameters to replace variables from fix_var

        Returns
        -------
        Problem
        """
    new_cost = fix_expr(prob.objective.expr, fix_var, param_list)
    if prob.objective.NAME == 'minimize':
        new_obj = cvx.Minimize(new_cost)
    else:
        new_obj = cvx.Maximize(new_cost)
    new_constr = []
    for con in prob.constraints:
        fix_con = fix_expr(con.expr, fix_var, param_list)
        if isinstance(con, NonPos):
            new_constr.append(fix_con <= 0)
        elif isinstance(con, PSD):
            new_constr.append(fix_con >> 0)
        else:
            new_constr.append(fix_con == 0)
    new_prob = Problem(new_obj, new_constr)
    return new_prob
Exemplo n.º 46
0
    def test_invalid_solvers(self):
        """Tests that errors occur when you use an invalid solver.
        """
        with self.assertRaises(Exception) as cm:
            Problem(Minimize(-log(self.a))).solve(solver=s.ECOS)
        self.assertEqual(str(cm.exception),
                         "The solver ECOS cannot solve the problem.")

        with self.assertRaises(Exception) as cm:
            Problem(Minimize(lambda_max(self.a))).solve(solver=s.ECOS)
        self.assertEqual(str(cm.exception),
                         "The solver ECOS cannot solve the problem.")

        with self.assertRaises(Exception) as cm:
            Problem(Minimize(self.a)).solve(solver=s.SCS)
        self.assertEqual(str(cm.exception),
                         "The solver SCS cannot solve the problem.")
Exemplo n.º 47
0
 def test_qp_maximization_reduction_path_qp_solver(self):
     qp_maximization = Problem(Maximize(QuadForm(self.x, -self.Q)),
                               [self.x <= -1])
     path = PathFinder().reduction_path(ProblemType(qp_maximization),
                                        [QpSolver])
     self.assertEquals(3, len(path))
     self.assertEquals(path[1], QpMatrixStuffing)
     self.assertEquals(path[2], FlipObjective)
Exemplo n.º 48
0
def test_constant_atoms():
    tests = []
    for atom_list, objective_type in atoms:
        for atom, size, args, obj_val in atom_list:
            for indexer in get_indices(size):
                for solver in SOLVERS_TO_TRY:
                    # Atoms with Constant arguments.
                    prob_val = obj_val[indexer].value
                    const_args = [Constant(arg) for arg in args]
                    problem = Problem(
                        objective_type(atom(*const_args)[indexer]))
                    yield (run_atom,
                           atom,
                           problem,
                           prob_val,
                           solver)
                    # Atoms with Variable arguments.
                    variables = []
                    constraints = []
                    for idx, expr in enumerate(args):
                        variables.append(Variable(intf.shape(expr)))
                        constraints.append(variables[-1] == expr)
                    objective = objective_type(atom(*variables)[indexer])
                    new_obj_val = prob_val
                    if objective_type == Maximize:
                        objective = -objective
                        new_obj_val = -new_obj_val
                    problem = Problem(objective, constraints)
                    yield (run_atom,
                           atom,
                           problem,
                           new_obj_val,
                           solver)
                    # Atoms with Parameter arguments.
                    parameters = []
                    for expr in args:
                        parameters.append(Parameter(intf.shape(expr)))
                        parameters[-1].value = intf.DEFAULT_INTF.const_to_matrix(expr)
                    objective = objective_type(atom(*parameters)[indexer])
                    yield (run_atom,
                           atom,
                           Problem(objective),
                           prob_val,
                           solver)
Exemplo n.º 49
0
    def test_verbose(self):
        import sys
        # From http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python
        # setup the environment
        outputs = {True: [], False: []}
        backup = sys.stdout
        # ####
        for verbose in [True, False]:
            for solver in installed_solvers():
                # Don't test GLPK because there's a race
                # condition in setting CVXOPT solver options.
                if solver in ["GLPK", "GLPK_MI"]:
                    continue
                # if solver == "GLPK":
                #     # GLPK's stdout is separate from python,
                #     # so we have to do this.
                #     # Note: This probably breaks (badly) on Windows.
                #     import os
                #     import tempfile

                #     stdout_fd = 1
                #     tmp_handle = tempfile.TemporaryFile(bufsize = 0)
                #     os.dup2(tmp_handle.fileno(), stdout_fd)
                # else:
                sys.stdout = StringIO()  # capture output

                p = Problem(Minimize(self.a + self.x[0]),
                            [self.a >= 2, self.x >= 2])
                if SOLVERS[solver].MIP_CAPABLE:
                    p.constraints.append(Bool() == 0)
                p.solve(verbose=verbose, solver=solver)
                if SOLVERS[solver].EXP_CAPABLE:
                    p = Problem(Minimize(self.a), [log(self.a) >= 2])
                    p.solve(verbose=verbose, solver=solver)

                # if solver == "GLPK":
                #     # GLPK's stdout is separate from python,
                #     # so we have to do this.
                #     tmp_handle.seek(0)
                #     out = tmp_handle.read()
                #     tmp_handle.close()
                # else:
                out = sys.stdout.getvalue()  # release output

                outputs[verbose].append((out, solver))
        # ####
        sys.stdout.close()  # close the stream
        sys.stdout = backup  # restore original stdout
        for output, solver in outputs[True]:
            print(solver)
            assert len(output) > 0
        for output, solver in outputs[False]:
            print(solver)
            assert len(output) == 0
Exemplo n.º 50
0
    def value(self):
        """Returns the numeric value of the expression.

        Returns:
            A numpy matrix or a scalar.
        """
        old_vals = {var.id: var.value for var in self.variables()}
        fix_vars = []
        for var in self.dont_opt_vars:
            if var.value is None:
                return None
            else:
                fix_vars += [var == var.value]
        prob = Problem(self.args[0].objective, fix_vars + self.args[0].constraints)
        result = prob.solve()
        # Restore the original values to the variables.
        for var in self.variables():
            var.value = old_vals[var.id]
        return result
Exemplo n.º 51
0
Arquivo: spoc.py Projeto: stat-ml/SPOC
    def _get_Q(self, U):
        """
        Get positive semidefinite Q matrix of ellipsoid from U[i,:] vectors
        for any u_i in U[i,:] : abs(u_i.T * Q * u_i) <= 1

        Parameters
        ---------
        U: array-like


        Returns
        -------
        Q: nd.array with shape (U.shape[1], U.shape[1])


        Requires:
        ---------
        cvxpy (http://www.cvxpy.org/en/latest/)
        scipy.spatial.ConvexHull
        """

        n_nodes = U.shape[0]
        k = U.shape[1]
        Q = Semidef(n=k)

        if self.use_convex_hull:
            hull = ConvexHull(U)
            constraints = [
                abs(U[i, :].reshape((1, k)) * Q * U[i, :].reshape((k, 1))) \
                <= 1 for i in hull.vertices
            ]
        else:
            constraints = [
                abs(U[i, :].reshape((1, k)) * Q * U[i, :].reshape((k, 1))) \
                <= 1 for i in range(n_nodes)
            ]

        obj = Minimize(-log_det(Q))
        prob = Problem(obj, constraints)
        _ = prob.solve(solver=self.solver)
        Q = np.array(Q.value)
        return Q
Exemplo n.º 52
0
    def test_variable_promotion(self):
        p = Problem(Minimize(self.a), [self.x <= self.a, self.x == [1, 2]])
        result = p.solve()
        self.assertAlmostEqual(result, 2)
        self.assertAlmostEqual(self.a.value, 2)

        p = Problem(Minimize(self.a),
                    [self.A <= self.a, self.A == [[1, 2], [3, 4]]])
        result = p.solve()
        self.assertAlmostEqual(result, 4)
        self.assertAlmostEqual(self.a.value, 4)

        # Promotion must happen before the multiplication.
        p = Problem(Minimize([[1], [1]] * (self.x + self.a + 1)),
                    [self.a + self.x >= [1, 2]])
        result = p.solve()
        self.assertAlmostEqual(result, 5)
Exemplo n.º 53
0
    def test_dual_variables(self):
        p = Problem(Minimize( norm1(self.x + self.z) ),
            [self.x >= [2,3],
             [[1,2],[3,4]]*self.z == [-1,-4],
             norm2(self.x + self.z) <= 100])
        result = p.solve()
        self.assertAlmostEqual(result, 4)
        self.assertItemsAlmostEqual(self.x.value, [4,3])
        self.assertItemsAlmostEqual(self.z.value, [-4,1])
        # Dual values
        self.assertItemsAlmostEqual(p.constraints[0].dual_value, [0, 1])
        self.assertItemsAlmostEqual(p.constraints[1].dual_value, [-1, 0.5])
        self.assertAlmostEqual(p.constraints[2].dual_value, 0)

        T = matrix(2, (2, 3))
        c = matrix([3,4])
        p = Problem(Minimize(1),
            [self.A >= T*self.C,
             self.A == self.B,
             self.C == T.T])
        result = p.solve()
        # Dual values
        self.assertItemsAlmostEqual(p.constraints[0].dual_value, 4*[0])
        self.assertItemsAlmostEqual(p.constraints[1].dual_value, 4*[0])
        self.assertItemsAlmostEqual(p.constraints[2].dual_value, 6*[0])
Exemplo n.º 54
0
    def value(self):
        """Returns the numeric value of the expression.

        Returns:
            A numpy matrix or a scalar.
        """
        old_vals = {var.id: var.value for var in self.variables()}
        fix_vars = []
        for var in self.dont_opt_vars:
            if var.value is None:
                return None
            else:
                fix_vars += [var == var.value]
        prob = Problem(self.args[0].objective,
                       fix_vars + self.args[0].constraints)
        prob.solve(solver=self.solver, **self._solve_kwargs)
        # Restore the original values to the variables.
        for var in self.variables():
            var.value = old_vals[var.id]
        # Need to get value returned by solver
        # in case of stacked partial_optimizes.
        return prob._solution.opt_val
Exemplo n.º 55
0
def test_constant_atoms(atom_info, objective_type) -> None:

    atom, size, args, obj_val = atom_info

    for indexer in get_indices(size):
        for solver in SOLVERS_TO_TRY:
            # Atoms with Constant arguments.
            prob_val = obj_val[indexer].value
            const_args = [Constant(arg) for arg in args]
            if len(size) != 0:
                objective = objective_type(atom(*const_args)[indexer])
            else:
                objective = objective_type(atom(*const_args))
            problem = Problem(objective)
            run_atom(atom, problem, prob_val, solver)

            # Atoms with Variable arguments.
            variables = []
            constraints = []
            for idx, expr in enumerate(args):
                variables.append(Variable(intf.shape(expr)))
                constraints.append(variables[-1] == expr)
            if len(size) != 0:
                objective = objective_type(atom(*variables)[indexer])
            else:
                objective = objective_type(atom(*variables))
            problem = Problem(objective, constraints)
            run_atom(atom, problem, prob_val, solver)

            # Atoms with Parameter arguments.
            parameters = []
            for expr in args:
                parameters.append(Parameter(intf.shape(expr)))
                parameters[-1].value = intf.DEFAULT_INTF.const_to_matrix(expr)
            if len(size) != 0:
                objective = objective_type(atom(*parameters)[indexer])
            else:
                objective = objective_type(atom(*parameters))
            run_atom(atom, Problem(objective), prob_val, solver)
Exemplo n.º 56
0
 def __init__(self, prob, opt_vars, dont_opt_vars):
     self.opt_vars = opt_vars
     self.dont_opt_vars = dont_opt_vars
     self.args = [prob]
     # Replace the opt_vars in prob with new variables.
     id_to_new_var = {var.id: var.copy() for var in self.opt_vars}
     new_obj = self._replace_new_vars(prob.objective, id_to_new_var)
     new_constrs = [
         self._replace_new_vars(con, id_to_new_var)
         for con in prob.constraints
     ]
     self._prob = Problem(new_obj, new_constrs)
     super(PartialProblem, self).__init__()
Exemplo n.º 57
0
 def test_expression_values(self):
     diff_exp = self.x - self.z
     inf_exp = normInf(diff_exp)
     sum_entries_exp = 5 + norm1(self.z) + norm1(self.x) + inf_exp
     constr_exp = norm2(self.x + self.z)
     obj = norm2(sum_entries_exp)
     p = Problem(Minimize(obj),
         [self.x >= [2,3], self.z <= [-1,-4], constr_exp <= 2])
     result = p.solve()
     self.assertAlmostEqual(result, 22)
     self.assertItemsAlmostEqual(self.x.value, [2,3])
     self.assertItemsAlmostEqual(self.z.value, [-1,-4])
     # Expression values.
     self.assertItemsAlmostEqual(diff_exp.value, self.x.value - self.z.value)
     self.assertAlmostEqual(inf_exp.value,
         LA.norm(self.x.value - self.z.value, numpy.inf))
     self.assertAlmostEqual(sum_entries_exp.value,
         5 + LA.norm(self.z.value, 1) + LA.norm(self.x.value, 1) + \
         LA.norm(self.x.value - self.z.value, numpy.inf))
     self.assertAlmostEqual(constr_exp.value,
         LA.norm(self.x.value + self.z.value, 2))
     self.assertAlmostEqual(obj.value, result)
Exemplo n.º 58
0
def scs_coniclift(x, constraints):
    """
    Return (A, b, K) so that
        {x : x satisfies constraints}
    can be written as
        {x : exists y where A @ [x; y] + b in K}.

    Parameters
    ----------
    x: cvxpy.Variable
    constraints: list of cvxpy.constraints.constraint.Constraint
        Each Constraint object must be DCP-compatible.

    Notes
    -----
    This function DOES NOT work when ``x`` has attributes, like ``PSD=True``,
    ``diag=True``, ``symmetric=True``, etc...
    """
    from cvxpy.problems.problem import Problem
    from cvxpy.problems.objective import Minimize
    from cvxpy.atoms.affine.sum import sum
    prob = Problem(Minimize(sum(x)), constraints)
    # ^ The objective value is only used to make sure that "x"
    # participates in the problem. So, if constraints is an
    # empty list, then the support function is the standard
    # support function for R^n.
    data, chain, invdata = prob.get_problem_data(solver='SCS')
    inv = invdata[-2]
    x_offset = inv.var_offsets[x.id]
    x_indices = np.arange(x_offset, x_offset + x.size)
    A = data['A']
    x_selector = np.zeros(shape=(A.shape[1], ), dtype=bool)
    x_selector[x_indices] = True
    A_x = A[:, x_selector]
    A_other = A[:, ~x_selector]
    A = -sparse.hstack([A_x, A_other])
    b = data['b']
    K = data['dims']
    return A, b, K
Exemplo n.º 59
0
    def test_consistency(self):
        """Test that variables and constraints keep a consistent order.
        """
        import itertools
        num_solves = 4
        vars_lists = []
        ineqs_lists = []
        var_ids_order_created = []
        for k in range(num_solves):
            sum = 0
            constraints = []
            var_ids = []
            for i in range(100):
                var = Variable(name=str(i))
                var_ids.append(var.id)
                sum += var
                constraints.append(var >= i)
            var_ids_order_created.append(var_ids)
            obj = Minimize(sum)
            p = Problem(obj, constraints)
            objective, constr_map = p.canonicalize()
            all_ineq = itertools.chain(constr_map[s.EQ], constr_map[s.LEQ])
            var_offsets, var_sizes, x_length = p._get_var_offsets(
                objective, all_ineq)
            # Sort by offset.
            vars_ = sorted(var_offsets.items(),
                           key=lambda (var_id, offset): offset)
            vars_ = [var_id for (var_id, offset) in vars_]
            vars_lists.append(vars_)
            ineqs_lists.append(constr_map[s.LEQ])

        # Verify order of variables is consistent.
        for i in range(num_solves):
            self.assertEqual(var_ids_order_created[i], vars_lists[i])
        for i in range(num_solves):
            for idx, constr in enumerate(ineqs_lists[i]):
                var_id, _ = lu.get_expr_vars(constr.expr)[0]
                self.assertEqual(var_ids_order_created[i][idx], var_id)
Exemplo n.º 60
0
    def _combined(self):
        """Sum list of problems with sign flip if objective is maximization.
		
		The combined problem's objective is to minimize the sum (with sign flip)
		of the component problem's objectives, subject to the union of all
		the constraints.
		"""
        p_comb = Problem(Minimize(0))
        for prob in self.problems:
            if isinstance(prob.objective, Minimize):
                p_comb += prob
            else:
                p_comb -= prob
        return p_comb