-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (170 loc) · 6.25 KB
/
Copy pathmain.py
File metadata and controls
223 lines (170 loc) · 6.25 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
import typing
import numpy as np
class NoAnswerError(Exception):
def __init__(self):
super().__init__('No answer')
class Simplex:
def __init__(self):
self.goal: str = "min"
self.restrictions: typing.List[Restriction] = []
self.variables: typing.List[Var] = []
def add_non_original_variable(self):
self.variables.append(Var(0, False))
def add_original_variable(self, k: float):
self.variables.append(Var(k, True))
class Var:
def __init__(self, k: float, is_original: bool):
self.k = k
self.is_original = is_original
class Restriction:
def __init__(
self,
coefficients: typing.List[float],
answer: float,
additional_variable_number: int,
):
self.coefficients = coefficients
self.answer = answer
self.additional_variable_number = additional_variable_number
self.has_variable = additional_variable_number != -1
@staticmethod
def create_equal_restriction(
coefficients: typing.List[float],
answer: float,
):
return Restriction(coefficients, answer, -1)
@staticmethod
def create_greater_restriction(
coefficients: typing.List[float],
answer: float,
variable_number: int,
):
coefficients = [-i for i in coefficients] + [1]
answer = -answer
return Restriction(coefficients, answer, variable_number)
@staticmethod
def create_less_restriction(
coefficients: typing.List[float],
answer: float,
variable_number: int,
):
coefficients.append(1)
return Restriction(coefficients, answer, variable_number)
def update_for_variables(self, variables: typing.List[Var]):
for v in range(len(variables)):
if (
not variables[v].is_original and
self.has_variable and
self.additional_variable_number != v
):
self.coefficients.insert(v, 0)
class Table:
def __init__(self, simplex: Simplex):
self.si = simplex
self.coefficients = np.array([
r.coefficients + [r.answer]
for r in self.si.restrictions
], dtype=float)
self.basis = []
self.delta = []
def get_default_basis(self) -> typing.List[int]:
return [
-1 if not r.has_variable else r.additional_variable_number
for r in self.si.restrictions
]
def no_answer_condition(self, idx: int) -> bool:
return np.max(self.coefficients[:, idx]) <= 0
def check_optimality(self, idx_worst_argument: int) -> bool:
if self.si.goal == "min":
return self.delta[idx_worst_argument] <= 0
elif self.si.goal == "max":
return self.delta[idx_worst_argument] >= 0
def get_index_worst_argument(self) -> int:
if self.si.goal == "min":
return int(np.argmax(self.delta[:-1]))
elif self.si.goal == "max":
return int(np.argmin(self.delta[:-1]))
def create_answer(self):
answer = np.zeros(len(self.si.variables) - 1, dtype=float)
for bi in range(len(self.basis)):
answer[self.get_basic_element(bi)] = self.coefficients[bi, -1]
return [True, answer, self.delta[-1]]
def calc_delta(self):
self.delta = []
for v in range(len(self.si.variables)):
self.delta.append(-self.si.variables[v].k)
for r in range(len(self.si.restrictions)):
self.delta[v] += self.coefficients[r, v] * self.si.variables[self.basis[r]].k
def get_basic_element(self, idx: int):
if self.basis[idx] != -1:
return self.basis[idx]
self.basis[idx] += 1
while self.coefficients[idx, self.basis[idx]] == 0:
self.basis[idx] += 1
return self.basis[idx]
def make_new_basic(self, idx: int):
if self.no_answer_condition(idx):
raise NoAnswerError()
i = np.argmax(self.coefficients[:, idx])
self.basis[i] = idx
def solve(self):
self.basis = self.get_default_basis()
while True:
for bi in range(len(self.basis)):
be = self.get_basic_element(bi)
coef = self.coefficients[bi, be]
for ei in range(len(self.coefficients[bi, :])):
self.coefficients[bi, ei] /= coef
for bdi in range(len(self.basis)):
if bdi == bi:
continue
coef = self.coefficients[bdi, be]
self.coefficients[bdi, :] -= coef * self.coefficients[bi, :]
self.calc_delta()
wdi = self.get_index_worst_argument()
if self.check_optimality(wdi):
return self.create_answer()
try:
self.make_new_basic(wdi)
except NoAnswerError:
return [False, None, None]
def from_json(
path: str,
) -> typing.List[Simplex]:
with open(path, 'r') as f:
data = json.load(f)
return [
parse_test(test)
for test in data['tests']
]
def parse_test(
test: typing.Dict[str, typing.Any],
) -> Simplex:
si = Simplex()
si.goal = test["goal"]["case"]
for k in test["goal"]["coefs"]:
si.add_original_variable(k)
for r in test['restrictions']:
c, a, v = r['coefs'], r['answer'], len(si.variables)
if r['case'] == 'equal':
si.restrictions.append(Restriction.create_equal_restriction(c, a))
continue
if r['case'] == 'greater':
si.restrictions.append(Restriction.create_greater_restriction(c, a, v))
elif r['case'] == 'less':
si.restrictions.append(Restriction.create_less_restriction(c, a, v))
si.add_non_original_variable()
for r in range(len(si.restrictions)):
si.restrictions[r].update_for_variables(si.variables)
si.add_original_variable(0)
return si
def solve_from_json(
path: str,
) -> typing.List[typing.List[typing.Any]]:
sis = from_json(path)
return [Table(si).solve() for si in sis]
if __name__ == '__main__':
np.set_printoptions(precision=3, suppress=True)
for s in solve_from_json('test.json'):
print(s)