-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_qsoptex.py
executable file
·153 lines (118 loc) · 5.39 KB
/
test_qsoptex.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
import unittest
import numbers
import fractions
import qsoptex
from six import b, u
class TestVariableType(unittest.TestCase):
def test_variable_str(self):
"""Test that a variable of bytes type works"""
self._problem = qsoptex.ExactProblem()
self._problem.add_variable(name=b('x'))
def test_variable_unicode(self):
"""Test that a variable of unicode type works"""
self._problem = qsoptex.ExactProblem()
self._problem.add_variable(name=u('x'))
class TestSmallExactProblem(unittest.TestCase):
def setUp(self):
self._problem = qsoptex.ExactProblem()
self._problem.add_variable(
name='x', objective=2, lower=3.5, upper=17.5)
self._problem.add_variable(
name='y', objective=-1, lower=None, upper=2)
self._problem.add_linear_constraint(
qsoptex.ConstraintSense.EQUAL, {'x': 1, 'y': 1}, rhs=0, name='c1')
self._problem.set_objective_sense(qsoptex.ObjectiveSense.MAXIMIZE)
def test_add_infinity_variable_bound(self):
self._problem.add_variable(
name='z1', lower=float('-inf'), upper=10)
self._problem.add_variable(
name='z2', lower=0, upper=float('inf'))
def test_reaches_status_optimal(self):
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.OPTIMAL)
def test_reaches_status_infeasible(self):
self._problem.add_variable(name='z', lower=200, upper=None)
self._problem.add_linear_constraint(
qsoptex.ConstraintSense.GREATER, {'y': 1, 'z': -1}, rhs=0)
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.INFEASIBLE)
def test_reaches_status_unbounded(self):
self._problem.add_variable(
name='z', lower=200, upper=None, objective=1)
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.UNBOUNDED)
def test_objective_value_is_correct(self):
self._problem.solve()
obj = self._problem.get_objective_value()
self.assertEqual(obj, fractions.Fraction('105/2'))
def test_objective_value_is_rational(self):
self._problem.solve()
obj = self._problem.get_objective_value()
self.assertIsInstance(obj, numbers.Rational)
def test_solution_values_by_name_are_rational(self):
self._problem.solve()
x = self._problem.get_value('x')
self.assertIsInstance(x, numbers.Rational)
y = self._problem.get_value('y')
self.assertIsInstance(y, numbers.Rational)
def test_solution_values_by_index_are_same(self):
self._problem.solve()
x = self._problem.get_value('x')
self.assertEqual(x, self._problem.get_value(0))
y = self._problem.get_value('y')
self.assertEqual(y, self._problem.get_value(1))
def test_solution_get_all_values_are_correct(self):
self._problem.solve()
values = self._problem.get_values()
self.assertEqual(values, [fractions.Fraction('35/2'),
fractions.Fraction('-35/2')])
def test_solution_value_index_negative(self):
self._problem.solve()
with self.assertRaises(IndexError):
self._problem.get_value(-1)
def test_solution_value_index_too_high(self):
self._problem.solve()
with self.assertRaises(IndexError):
self._problem.get_value(10)
def test_get_status_method(self):
s = self._problem.solve()
self.assertEqual(s, self._problem.get_status())
def test_get_constraint_count(self):
self.assertEqual(self._problem.get_constraint_count(), 1)
def test_get_variable_count(self):
self.assertEqual(self._problem.get_variable_count(), 2)
def test_rerun_solve(self):
self._problem.solve()
# Modified problem
self._problem.add_variable(name='z', objective=1, lower=0, upper=200)
self._problem.add_linear_constraint(
qsoptex.ConstraintSense.LESS, {'z': 1, 'x': -10}, rhs=-50)
self._problem.solve()
self.assertEqual(
self._problem.get_objective_value(), fractions.Fraction('355/2'))
def test_delete_variable(self):
self._problem.add_variable(name='z', objective=1, lower=0, upper=1)
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.OPTIMAL)
obj = self._problem.get_objective_value()
self.assertEqual(obj, fractions.Fraction('107/2'))
self._problem.delete_variable('z')
# Solve problem again without z
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.OPTIMAL)
obj = self._problem.get_objective_value()
self.assertEqual(obj, fractions.Fraction('105/2'))
def test_delete_constraint(self):
self._problem.add_linear_constraint(
qsoptex.ConstraintSense.LESS, {'x': 1}, rhs=15, name='constr2')
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.OPTIMAL)
self.assertEqual(self._problem.get_value('x'), 15)
self._problem.delete_linear_constraint('constr2')
status = self._problem.solve()
self.assertEqual(status, qsoptex.SolutionStatus.OPTIMAL)
self.assertEqual(
self._problem.get_value('x'), fractions.Fraction('35/2'))
if __name__ == '__main__':
unittest.main()