Beispiel #1
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
Beispiel #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
Beispiel #3
0
from cvxpy.problems.solvers.utilities import SOLVERS
from cvxpy.expressions.variables import Variable
from cvxpy.expressions.constants import Constant, Parameter
from cvxpy.error import SolverError
import cvxpy.interface as intf
import cvxopt
import numpy as np
import numpy.linalg as LA
import math
from nose.tools import assert_raises

ROBUST_CVXOPT = "robust_cvxopt"
SOLVER_TO_TOL = {SCS: 1e-2, ECOS: 1e-7, CVXOPT: 1e-7, ROBUST_CVXOPT: 1e-7}
SOLVERS_TO_TRY = [ECOS, SCS, CVXOPT, ROBUST_CVXOPT]
# Test elemental if installed.
if ELEMENTAL in installed_solvers():
    SOLVERS_TO_TRY.append(ELEMENTAL)
    SOLVER_TO_TOL[ELEMENTAL] = 1e-7

# Test MOSEK if installed.
if MOSEK in installed_solvers():
    SOLVERS_TO_TRY.append(MOSEK)
    SOLVER_TO_TOL[MOSEK] = 1e-6

v = cvxopt.matrix([-1, 2, -2], tc='d')
v_np = np.matrix([-1., 2, -2]).T

# Defined here to be used in KNOWN_SOLVER_ERRORS
log_sum_exp_axis_0 = lambda x: log_sum_exp(x, axis=0)
log_sum_exp_axis_1 = lambda x: log_sum_exp(x, axis=1)
Beispiel #4
0
from cvxpy.error import SolverError
import cvxpy.interface as intf
import cvxopt
import numpy as np
import numpy.linalg as LA
import math
from nose.tools import assert_raises

ROBUST_CVXOPT = "robust_cvxopt"
SOLVER_TO_TOL = {SCS: 1e-2,
                 ECOS: 1e-7,
                 CVXOPT: 1e-7,
                 ROBUST_CVXOPT: 1e-7}
SOLVERS_TO_TRY = [ECOS, SCS, CVXOPT, ROBUST_CVXOPT]
# Test elemental if installed.
if ELEMENTAL in installed_solvers():
    SOLVERS_TO_TRY.append(ELEMENTAL)
    SOLVER_TO_TOL[ELEMENTAL] = 1e-7

# Test MOSEK if installed.
if MOSEK in installed_solvers():
    SOLVERS_TO_TRY.append(MOSEK)
    SOLVER_TO_TOL[MOSEK] = 1e-6

v = cvxopt.matrix([-1,2,-2], tc='d')
v_np = np.matrix([-1.,2,-2]).T

# Defined here to be used in KNOWN_SOLVER_ERRORS
log_sum_exp_axis_0 = lambda x: log_sum_exp(x, axis=0)
log_sum_exp_axis_1 = lambda x: log_sum_exp(x, axis=1)
Beispiel #5
0
from cvxpy.problems.problem import Problem
from cvxpy.problems.solvers.utilities import SOLVERS
from cvxpy.expressions.variables import Variable
from cvxpy.expressions.constants import Constant, Parameter
import cvxpy.interface as intf
import cvxopt
import numpy as np
import numpy.linalg as LA
import math
from nose.tools import assert_raises

ROBUST_CVXOPT = "robust_cvxopt"
SOLVER_TO_TOL = {SCS: 1e-2, ECOS: 1e-7, CVXOPT: 1e-7, ROBUST_CVXOPT: 1e-7}
SOLVERS_TO_TRY = [ECOS, SCS, CVXOPT, ROBUST_CVXOPT]
# Test elemental if installed.
if ELEMENTAL in installed_solvers():
    SOLVERS_TO_TRY.append(ELEMENTAL)
    SOLVER_TO_TOL[ELEMENTAL] = 1e-7

v = cvxopt.matrix([-1, 2, -2], tc="d")
v_np = np.matrix([-1.0, 2, -2]).T

# Atom, solver pairs known to fail.
KNOWN_SOLVER_ERRORS = []

atoms = [
    (
        [
            (abs, (2, 2), [[[-5, 2], [-3, 1]]], Constant([[5, 2], [3, 1]])),
            (diag, (2, 1), [[[-5, 2], [-3, 1]]], Constant([-5, 1])),
            (diag, (2, 2), [[-5, 1]], Constant([[-5, 0], [0, 1]])),
Beispiel #6
0
from cvxpy.problems.problem import Problem
from cvxpy.problems.solvers.utilities import SOLVERS
from cvxpy.expressions.variables import Variable
from cvxpy.expressions.constants import Constant, Parameter
import cvxpy.interface as intf
import cvxopt
import numpy as np
import numpy.linalg as LA
import math
from nose.tools import assert_raises

ROBUST_CVXOPT = "robust_cvxopt"
SOLVER_TO_TOL = {SCS: 1e-2, ECOS: 1e-7, CVXOPT: 1e-7, ROBUST_CVXOPT: 1e-7}
SOLVERS_TO_TRY = [ECOS, SCS, CVXOPT, ROBUST_CVXOPT]
# Test elemental if installed.
if ELEMENTAL in installed_solvers():
    SOLVERS_TO_TRY.append(ELEMENTAL)
    SOLVER_TO_TOL[ELEMENTAL] = 1e-7

v = cvxopt.matrix([-1, 2, -2], tc='d')
v_np = np.matrix([-1., 2, -2]).T

# Atom, solver pairs known to fail.
KNOWN_SOLVER_ERRORS = []

atoms = [
    ([
        (abs, (2, 2), [[[-5, 2], [-3, 1]]], Constant([[5, 2], [3, 1]])),
        (diag, (2, 1), [[[-5, 2], [-3, 1]]], Constant([-5, 1])),
        (diag, (2, 2), [[-5, 1]], Constant([[-5, 0], [0, 1]])),
        (exp, (2, 2), [[[1, 0], [2, -1]]],