-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_simplex.py
More file actions
186 lines (148 loc) · 4.21 KB
/
test_simplex.py
File metadata and controls
186 lines (148 loc) · 4.21 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import unittest
from simplex import solve_simplex
class TestSimplex(unittest.TestCase):
def test_maximization_1(self):
objective = [10, 12]
constraints = [
[1, 1, 100, '<='],
[1, 3, 270, '<='],
]
variables = [15, 85]
solution = 1170
problem = ["Max", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_2(self):
objective = [6, 5, 4]
constraints = [
[2, 1, 1, 180, '<='],
[1, 3, 2, 300, '<='],
[2, 1, 2, 240, '<=']
]
variables = [48, 84, 60]
solution = 708
problem = ["Max", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_3(self):
objective = [2, 3, 4]
constraints = [
[1, 1, 1, 100, '<='],
[2, 1, 0, 210, '<='],
[1, 0, 0, 80, '<='],
]
variables = [0, 0, 100]
solution = 400
problem = ["Max" , objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_4(self):
objective = [2, 0, 4]
constraints = [
[1, 2, 1, 8000, '<='],
[2, 0, 0, 6000, '<='],
[0, 1, 1, 620, '<='],
]
variables = [3000, 0, 620]
solution = 8480
problem = ["Max" , objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_5(self):
objective = [6000, 10000]
constraints = [
[4, 2, 32, '<='],
[2, 4, 22, '<='],
[2, 6, 30, '<='],
]
variables = [7, 2]
solution = 62000
problem = ["Max" , objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_6(self):
objective = [7, 8, 10]
constraints = [
[2, 3, 2, 1000, '<='],
[1, 1, 2, 800, '<='],
]
variables = [200, 0, 300]
solution = 4400
problem = ["Max", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_7(self):
objective = [8, 10, 7]
constraints = [
[1, 3, 2, 10, '<='],
[1, 5, 1, 8, '<='],
]
variables = [8, 0, 0]
solution = 64
problem = ["Max", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_maximization_8(self):
objective = [50, 80]
constraints = [
[1, 2, 120, '<='],
[1, 1, 90, '<='],
]
variables = [60, 30]
solution = 5400
problem = ["Max", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_minimization_1(self):
objective = [14, 20]
constraints = [
[1, 2, 4, '>='],
[7, 6, 20, '>='],
]
variables = [2, 1]
solution = 48
problem = ["Min", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_minimization_2(self):
objective = [0.12, 0.15]
constraints = [
[60, 60, 300, '>='],
[12, 6, 36, '>='],
[10, 30, 90, '>=']
]
variables = [3, 2]
solution = 0.66
problem = ["Min", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_minimization_3(self):
objective = [3, 2]
constraints = [
[2, 1, 6, '>='],
[1, 1, 4, '>=']
]
variables = [2, 2]
solution = 10
problem = ["Min", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_minimization_4(self):
objective = [2, 10, 8]
constraints = [
[1, 1, 1, 6, '>='],
[0, 1, 2, 8, '>='],
[-1, 2, 2, 4, '>=']
]
variables = [2, 0, 4]
solution = 36
problem = ["Min", objective, constraints]
self.assertProblem(problem, variables, solution)
def test_minimization_5(self):
objective = [-5, -4]
constraints = [
[2, 2, 14, '<='],
[6, 3, 36, '<='],
[5, 10, 60, '<=']
]
variables = [5, 2]
solution = -33
problem = ["Min", objective, constraints]
self.assertProblem(problem, variables, solution)
def assertProblem(self, problem, variables, value):
solution = solve_simplex(*problem)
round_vars = [round(x) for x in solution["variables"]]
round_value = round(solution["value"])
self.assertEqual(round_vars, [round(x) for x in variables])
self.assertEqual(round_value, round(value))
if __name__ == '__main__':
unittest.main()