示例#1
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.SOLVERS:
                sys.stdout = StringIO()  # capture output
                p = Problem(Minimize(self.a + self.x[0]),
                            [self.a >= 2, self.x >= 2])
                if solver in s.MIP_CAPABLE:
                    p.constraints.append(Bool() == 0)
                p.solve(verbose=verbose, solver=solver)
                if solver in s.EXP_CAPABLE:
                    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]:
            print(output)
            assert len(output) == 0
示例#2
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
示例#3
0
    def test_mip_print(self):
        """Test to string methods for Bool/Int vars.
        """
        self.assertEqual(repr(self.x_bool), "Bool(1, 1, 'x')")
        self.assertEqual(repr(self.B_int), "Int(2, 3, 'B')")

        x = Bool()
        B = Int(2, 3)

        self.assertEqual(repr(x), "Bool(1, 1)")
        self.assertEqual(repr(B), "Int(2, 3)")
示例#4
0
    def test_partial_optimize_special_var(self):
        x, y = Bool(1), Int(1)

        # Solve the (simple) two-stage problem by "combining" the two stages (i.e., by solving a single linear program)
        p1 = Problem(Minimize(x+y), [x+y >= 3, y >= 4, x >= 5])
        p1.solve()

        # Solve the two-stage problem via partial_optimize
        p2 = Problem(Minimize(y), [x+y >= 3, y >= 4])
        g = cvxpy.partial_optimize(p2, [y], [x])
        p3 = Problem(Minimize(x+g), [x >= 5])
        p3.solve()
        self.assertAlmostEqual(p1.value, p3.value)
示例#5
0
 def setUp(self):
     self.x_bool = Bool(name='x')
     self.y_int = Int()
     self.A_bool = Bool(3, 2)
     self.B_int = Int(2, 3, name='B')
示例#6
0
 def setUp(self):
     self.x_bool = Bool()
     self.y_int = Int()
     self.A_bool = Bool(3, 2)
     self.B_int = Int(2, 3)