-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_interface.py
More file actions
89 lines (72 loc) · 2.61 KB
/
Copy pathtest_interface.py
File metadata and controls
89 lines (72 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# for python 3 testing compatibility
from __future__ import print_function
import platform
def import_error(msg):
print()
print("## IMPORT ERROR:", msg)
print()
try:
from nose.tools import assert_raises, assert_almost_equals
except ImportError:
import_error("Please install nose to run tests.")
raise
try:
import ecos
except ImportError:
import_error("You must install the ecos module before running tests.")
raise
try:
import numpy as np
except ImportError:
import_error("Please install numpy.")
raise
try:
import scipy.sparse as sp
except ImportError:
import_error("Please install scipy.")
raise
# global data structures for problem
c = np.array([-1.])
h = np.array([4., -0.])
G = (sp.csc_matrix([1., -1.]).T).tocsc()
A = sp.csc_matrix([1.])
b = np.array([3.])
dims = {'q': [], 'l': 2}
def check_solution(solution, expected):
assert_almost_equals(solution, expected, places=5)
def test_problems():
myopts = {'feastol': 2e-8, 'reltol': 2e-8, 'abstol': 2e-8, 'verbose':True};
sol = ecos.solve(c, G, h, dims, **myopts)
yield check_solution, sol['x'][0], 4
sol = ecos.solve(c, G, h, dims, A, b, **myopts)
yield check_solution, sol['x'][0], 3
new_dims = {'q':[2], 'l': 0}
sol = ecos.solve(c, G, h, new_dims, **myopts)
yield check_solution, sol['x'][0], 2
if platform.python_version_tuple() < ('3','0','0'):
def test_problems_with_longs():
new_dims = {'q': [], 'l': long(2)}
myopts = {'feastol': 2e-8, 'reltol': 2e-8, 'abstol': 2e-8};
sol = ecos.solve(c, G, h, new_dims, **myopts)
yield check_solution, sol['x'][0], 4
sol = ecos.solve(c, G, h, new_dims, A, b, **myopts)
yield check_solution, sol['x'][0], 3
new_dims = {'q':[long(2)], 'l': 0}
sol = ecos.solve(c, G, h, new_dims, **myopts)
yield check_solution, sol['x'][0], 2
def check_keyword(error_type, keyword, value):
assert_raises(error_type, ecos.solve, c,G,h,dims, **{keyword: value})
def test_failures():
yield assert_raises, TypeError, ecos.solve
yield assert_raises, TypeError, ecos.solve, c, G, h, dims, A
yield assert_raises, ValueError, ecos.solve, c, G, h, {'q':[], 'l':0}
yield assert_raises, TypeError, ecos.solve, c, G, h, {'q':[4], 'l':-2}
yield check_keyword, TypeError, 'verbose', 0
yield check_keyword, ValueError, 'feastol', 0
yield check_keyword, ValueError, 'abstol', 0
yield check_keyword, ValueError, 'reltol', 0
yield check_keyword, ValueError, 'feastol_inacc', 0
yield check_keyword, ValueError, 'abstol_inacc', 0
yield check_keyword, ValueError, 'reltol_inacc', 0
yield check_keyword, ValueError, 'max_iters', -1
yield check_keyword, TypeError, 'max_iters', 1.1