SimplexMethod

This commit is contained in:
Danil Malin 2023-12-17 20:12:03 +04:00
commit 29ba3132fc
5 changed files with 104 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

7
README.md Normal file
View File

@ -0,0 +1,7 @@
#Участники команды
+ Малин Данил - выполнивший задание
+ Сорокин Павел - помощник
+ Пятаков Кирилл - помощник
+ Распаев Николай -помощник
#Задание
![Задание](/assets/image.png)

BIN
assets/example.xlsx Normal file

Binary file not shown.

BIN
assets/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

96
main.py Normal file
View File

@ -0,0 +1,96 @@
import math
import numpy as np
c = [0, 0, 0, 1, -2]
c_num = 3
A = [
[1, 0, 0, -1, 1],
[0, 1, 0, 2, 3],
[0, 0, 1, 1, -2]
]
b = [2, 7, 1]
# c = [1, 1, 0, 0, 0]
# c_num = 2
# A = [
# [-1, 1, 1, 0, 0],
# [ 1, 0, 0, 1, 0],
# [ 0, 1, 0, 0, 1]
# ]
# b = [2, 4, 4]
def simplex(c, A, b):
tableau = to_tableau(c, A, b)
while can_be_improved(tableau):
pivot_position = get_pivot_position(tableau)
tableau = pivot_step(tableau, pivot_position)
return get_solution(tableau)
def to_tableau(c, A, b):
xb = [eq + [x] for eq, x in zip(A, b)]
z = c + [0]
return xb + [z]
def can_be_improved(tableau):
z = tableau[-1]
return any(x < 0 for x in z[:-1])
def get_pivot_position(tableau):
z = tableau[-1]
column = next(i for i, x in enumerate(z[:-1]) if x < 0)
restrictions = []
for eq in tableau[:-1]:
el = eq[column]
restrictions.append(math.inf if el <= 0 else eq[-1] / el)
row = restrictions.index(min(restrictions))
return row, column
def pivot_step(tableau, pivot_position):
new_tableau = [[] for eq in tableau]
i, j = pivot_position
pivot_value = tableau[i][j]
new_tableau[i] = np.array(tableau[i]) / pivot_value
for eq_i, eq in enumerate(tableau):
if eq_i != i:
multiplier = np.array(new_tableau[i]) * tableau[eq_i][j]
new_tableau[eq_i] = np.array(tableau[eq_i]) - multiplier
return new_tableau
def is_basic(column):
return sum(column) == 1 and len([c for c in column if c == 0]) == len(column) - 1
def get_solution(tableau):
columns = np.array(tableau).T
solutions = []
for column in columns[:-1]:
solution = 0
if is_basic(column):
one_index = column.tolist().index(1)
solution = columns[-1][one_index]
solutions.append(solution)
return solutions
solution = simplex(c, A, b)
print('solution: ', solution)
result = 0
for i in range(len(solution)):
result += c[i] * solution[i]
result += c_num
print(result)