Skip to content

Commit

Permalink
finished making tests flake8 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Diamond committed Mar 27, 2019
1 parent a88000c commit c6ed808
Show file tree
Hide file tree
Showing 19 changed files with 604 additions and 631 deletions.
371 changes: 187 additions & 184 deletions cvxpy/tests/test_atoms.py

Large diffs are not rendered by default.

119 changes: 61 additions & 58 deletions cvxpy/tests/test_cbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,107 @@
limitations under the License.
"""

from cvxpy import *
import cvxpy as cp
from cvxpy.tests.base_test import BaseTest


class TestSolvers(BaseTest):
""" Unit tests for solver specific behavior. """

def setUp(self):
self.a = Variable(name='a')
self.b = Variable(name='b')
self.c = Variable(name='c')
self.a = cp.Variable(name='a')
self.b = cp.Variable(name='b')
self.c = cp.Variable(name='c')

self.x = Variable(2, name='x')
self.y = Variable(3, name='y')
self.z = Variable(2, name='z')
self.x = cp.Variable(2, name='x')
self.y = cp.Variable(3, name='y')
self.z = cp.Variable(2, name='z')

self.A = Variable((2,2), name='A')
self.B = Variable((2,2), name='B')
self.C = Variable((3,2), name='C')
self.A = cp.Variable((2, 2), name='A')
self.B = cp.Variable((2, 2), name='B')
self.C = cp.Variable((3, 2), name='C')

def test_lp(self):
"""Tests basic LPs. (from test_elemental.py)
"""
if CBC in installed_solvers():
prob = Problem(Minimize(0), [self.x == 2])
prob.solve(verbose=False, solver=CBC)
if cp.CBC in cp.installed_solvers():
prob = cp.Problem(cp.Minimize(0), [self.x == 2])
prob.solve(verbose=False, solver=cp.CBC)
self.assertAlmostEqual(prob.value, 0)
self.assertItemsAlmostEqual(self.x.value, [2, 2])

prob = Problem(Minimize(-self.a), [self.a <= 1])
prob.solve(verbose=False, solver=CBC)
prob = cp.Problem(cp.Minimize(-self.a), [self.a <= 1])
prob.solve(verbose=False, solver=cp.CBC)
self.assertAlmostEqual(prob.value, -1)
self.assertAlmostEqual(self.a.value, 1)

def test_lp_2(self):
"""Test a basic LP. (from test_solver.py::test_cvxopt_glpk)
"""
# Either the problem is solved or CBC is not installed.
if CBC in installed_solvers():
prob = Problem(Minimize(norm(self.x, 1)), [self.x == 0])
prob.solve(verbose=False, solver=CBC)
# Either the problem is solved or cp.CBC is not installed.
if cp.CBC in cp.installed_solvers():
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)), [self.x == 0])
prob.solve(verbose=False, solver=cp.CBC)
self.assertAlmostEqual(prob.value, 0)
self.assertItemsAlmostEqual(self.x.value, [0, 0])

# Example from http://cvxopt.org/userguide/coneprog.html?highlight=solvers.lp#cvxopt.solvers.lp
objective = Minimize(-4 * self.x[0] - 5 * self.x[1])
# Example from
# http://cvxopt.org/userguide/coneprog.html?highlight=solvers.lp#cvxopt.solvers.lp
objective = cp.Minimize(-4 * self.x[0] - 5 * self.x[1])
constraints = [2 * self.x[0] + self.x[1] <= 3,
self.x[0] + 2 * self.x[1] <= 3,
self.x[0] >= 0,
self.x[1] >= 0]
prob = Problem(objective, constraints)
prob.solve(verbose=False, solver=CBC)
prob = cp.Problem(objective, constraints)
prob.solve(verbose=False, solver=cp.CBC)
self.assertAlmostEqual(prob.value, -9)
self.assertItemsAlmostEqual(self.x.value, [1, 1])
else:
with self.assertRaises(Exception) as cm:
prob = Problem(Minimize(norm(self.x, 1)), [self.x == 0])
prob.solve(verbose=False, solver=CBC)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % CBC)
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)), [self.x == 0])
prob.solve(verbose=False, solver=cp.CBC)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % cp.CBC)

def test_mip(self):
"""Test a basic MILP with CBC. (from test_solver.py::test_cvxopt_glpk_mi)
"""Test a basic MILP with cp.CBC. (from test_solver.py::test_cvxopt_glpk_mi)
"""
# Either the problem is solved or CBC is not installed.
if CBC in installed_solvers():
bool_var = Variable(boolean=True)
int_var = Variable(integer=True)
prob = Problem(Minimize(norm(self.x, 1)),
[self.x == bool_var, bool_var == 0])
prob.solve(solver=CBC, verbose=False)
# Either the problem is solved or cp.CBC is not installed.
if cp.CBC in cp.installed_solvers():
bool_var = cp.Variable(boolean=True)
int_var = cp.Variable(integer=True)
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)),
[self.x == bool_var, bool_var == 0])
prob.solve(solver=cp.CBC, verbose=False)
self.assertAlmostEqual(prob.value, 0)
self.assertAlmostEqual(bool_var.value, 0)
self.assertItemsAlmostEqual(self.x.value, [0, 0])

# Example from http://cvxopt.org/userguide/coneprog.html?highlight=solvers.lp#cvxopt.solvers.lp
objective = Minimize(-4 * self.x[0] - 5 * self.x[1])
# Example from
# http://cvxopt.org/userguide/coneprog.html?highlight=solvers.lp#cvxopt.solvers.lp
objective = cp.Minimize(-4 * self.x[0] - 5 * self.x[1])
constraints = [2 * self.x[0] + self.x[1] <= int_var,
self.x[0] + 2 * self.x[1] <= 3*bool_var,
self.x[0] >= 0,
self.x[1] >= 0,
int_var == 3*bool_var,
int_var == 3]
prob = Problem(objective, constraints)
prob.solve(solver=CBC, verbose=False)
prob = cp.Problem(objective, constraints)
prob.solve(solver=cp.CBC, verbose=False)
self.assertAlmostEqual(prob.value, -9)
self.assertAlmostEqual(int_var.value, 3)
self.assertAlmostEqual(bool_var.value, 1)
self.assertItemsAlmostEqual(self.x.value, [1, 1])
else:
with self.assertRaises(Exception) as cm:
prob = Problem(Minimize(norm(self.x, 1)), [self.x == 0])
prob.solve(solver=CBC, verbose=False)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % CBC)
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)), [self.x == 0])
prob.solve(solver=cp.CBC, verbose=False)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % cp.CBC)

def test_hard_mip(self):
"""Test a hard knapsack problem with CBC.
"""Test a hard knapsack problem with cp.CBC.
"""
# Either the problem is solved or CBC is not installed.
if CBC in installed_solvers():
# Either the problem is solved or cp.CBC is not installed.
if cp.CBC in cp.installed_solvers():
# Instance "knapPI_1_50_1000_1" from "http://www.diku.dk/~pisinger/genhard.c"
n = 50
c = 995
Expand All @@ -135,33 +137,34 @@ def test_hard_mip(self):
[46, 303, 748, 0], [47, 764, 487, 0], [48, 536, 923, 0],
[49, 724, 29, 1], [50, 789, 674, 0]] # index, p / w / x

X = Variable(n, boolean=True)
prob = Problem(Maximize(sum(multiply([i[1] for i in coeffs], X))),
[sum(multiply([i[2] for i in coeffs], X)) <= c])
prob.solve(verbose=False, solver=CBC)
X = cp.Variable(n, boolean=True)
prob = cp.Problem(cp.Maximize(cp.sum(cp.multiply([i[1] for i in coeffs], X))),
[cp.sum(cp.multiply([i[2] for i in coeffs], X)) <= c])
prob.solve(verbose=False, solver=cp.CBC)
self.assertAlmostEqual(prob.value, z) # objective
else:
with self.assertRaises(Exception) as cm:
prob = Problem(Minimize(norm(self.x, 1)), [self.x == 0])
prob.solve(solver=CBC, verbose=False)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % CBC)
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)), [self.x == 0])
prob.solve(solver=cp.CBC, verbose=False)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % cp.CBC)

def test_options(self):
"""Test that all the CBC solver options work.
"""Test that all the cp.CBC solver options work.
"""
if CBC in installed_solvers():
prob = Problem(Minimize(norm(self.x, 1)), [self.x == Variable(2, boolean=True)])
if cp.CBC in cp.installed_solvers():
prob = cp.Problem(cp.Minimize(cp.norm(self.x, 1)),
[self.x == cp.Variable(2, boolean=True)])
for i in range(2):
# Some cut-generators seem to be buggy for now -> set to false
# prob.solve(solver=CBC, verbose=True, GomoryCuts=True, MIRCuts=True,
# prob.solve(solver=cp.CBC, verbose=True, GomoryCuts=True, MIRCuts=True,
# MIRCuts2=True, TwoMIRCuts=True, ResidualCapacityCuts=True,
# KnapsackCuts=True, FlowCoverCuts=True, CliqueCuts=True,
# LiftProjectCuts=True, AllDifferentCuts=False, OddHoleCuts=True,
# RedSplitCuts=False, LandPCuts=False, PreProcessCuts=False,
# ProbingCuts=True, SimpleRoundingCuts=True)
prob.solve(solver=CBC, verbose=True, maximumSeconds=100)
prob.solve(solver=cp.CBC, verbose=True, maximumSeconds=100)
self.assertItemsAlmostEqual(self.x.value, [0, 0])
else:
with self.assertRaises(Exception) as cm:
prob.solve(solver=CBC)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % CBC)
prob.solve(solver=cp.CBC)
self.assertEqual(str(cm.exception), "The solver %s is not installed." % cp.CBC)
Loading

0 comments on commit c6ed808

Please sign in to comment.