-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsimplex_template.py
More file actions
111 lines (90 loc) · 3.33 KB
/
simplex_template.py
File metadata and controls
111 lines (90 loc) · 3.33 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
import numpy as np
import sys
import argparse
# global vars
eps = 0.00001
def PrimalSimplex(c, A, b, basis=None, nbasis=None):
# max c^T x
# Ax = b
# x >= 0
# n - число переменных
# m - число ограничений
# Да, действительно, считаем что n > m (с учетом слаков)
# Предполагаем что проблема точно feasible но возможно unbounded
m, n = A.shape
n -= m
if basis is None or nbasis is None:
basis = list(range(n, n + m))
nbasis = list(range(0, n))
while True:
# Подсказка: np.linalg.solve(M, v) решает систему Mx = v
# TODO: Посчитать reduced cost's
reduced_cost = ...
# TODO: Находим кандидата для входа в базис
entering_index = ...
# TODO: Вычисляем направление, не забывая детектировать unbounded
d = ...
# TODO: Найти кандидата для выхода из базиса
leaving_index = ...
# TODO: Обновляем basis и nbasis
basis = ...
nbasis = ...
# TODO: Восстановить исходную систему, восстановить x и вернуть результат
return "optimal", ..., ...
def Phase1(c, A, b):
# TODO: Создаем вспомогательную задачу
new_c = ...
new_A = ...
basis = ...
nbasis = ...
status, x, obj = PrimalSimplex(new_c, new_A, b, basis, nbasis)
if status != "optimal" or obj > eps:
return "infeasible", None, None
# TODO: Нужно восстановить исходную задачу
c = ...
A = ...
basis = ...
nbasis = ...
return PrimalSimplex(c, A, b, basis, nbasis)
def Solve(c, A, b):
if np.all(b >= 0):
# TODO: Добавляем слаки в систему
return PrimalSimplex(c, A, b)
# Иначе запускаем фазу 1
return Phase1(c, A, b)
def proc_cmd():
parser = argparse.ArgumentParser(description="Solve a linear program using the Primal Simplex method.")
parser.add_argument("filename", type=str, help="Input file containing the LP problem.")
return parser.parse_args()
def main():
# boilerplate for reading input data
args = proc_cmd()
with open(args.filename, 'r', encoding='utf-8') as f:
n, m = map(int, f.readline().split())
c = np.array(list(map(float, f.readline().split())))
A = []
b = []
for _ in range(m):
*row, bi = map(float, f.readline().split())
A.append(row)
b.append(bi)
A = np.array(A)
b = np.array(b)
print("n =", n)
print("m =", m)
print("c =", c)
print("A =", A)
print("b =", b)
print("Solving the linear program using the Primal Simplex method...\n")
status, solution, objective = Solve(c, A, b)
print("\nResult:")
print("Status:", status)
if status == "optimal":
print("Optimal solution x* =", solution)
print("Optimal value =", objective)
elif status == "unbounded":
print("The problem is unbounded.")
else:
print("No solution found.")
if __name__ == '__main__':
main()