Feature: Completed lab

This commit is contained in:
Emelyanov535 2023-12-13 15:36:58 +04:00
parent 2ba1c23808
commit 67c0bc343d
2 changed files with 92 additions and 11 deletions

73
Simplex.py Normal file
View File

@ -0,0 +1,73 @@
import numpy as np
class Simplex:
def __init__(self, source):
m, n = source.shape
self.table = np.zeros((m, n + m - 1))
self.basis = []
for i in range(m):
for j in range(self.table.shape[1]):
if j < n:
self.table[i, j] = source[i, j]
else:
self.table[i, j] = 0
if (n + i) < self.table.shape[1]:
self.table[i, n + i] = 1
self.basis.append(n + i)
self.m = m
self.n = self.table.shape[1]
def calculate(self, result):
while not self.is_it_end():
main_col = self.find_main_col()
main_row = self.find_main_row(main_col)
self.basis[main_row] = main_col
new_table = np.zeros((self.m, self.n))
for j in range(self.n):
new_table[main_row, j] = self.table[main_row, j] / self.table[main_row, main_col]
for i in range(self.m):
if i == main_row:
continue
for j in range(self.n):
new_table[i, j] = self.table[i, j] - self.table[i, main_col] * new_table[main_row, j]
self.table = new_table
for i in range(len(result)):
k = self.basis.index(i + 1) if i + 1 in self.basis else -1
result[i] = self.table[k, 0] if k != -1 else 0
return self.table
def is_it_end(self):
return np.all(self.table[self.m - 1, 1:] >= 0)
def find_main_col(self):
main_col = 1
for j in range(2, self.n):
if self.table[self.m - 1, j] < self.table[self.m - 1, main_col]:
main_col = j
return main_col
def find_main_row(self, main_col):
main_row = 0
for i in range(self.m - 1):
if self.table[i, main_col] > 0:
main_row = i
break
for i in range(main_row + 1, self.m - 1):
if self.table[i, main_col] > 0 and (self.table[i, 0] / self.table[i, main_col]) < (self.table[main_row, 0] / self.table[main_row, main_col]):
main_row = i
return main_row

30
main.py
View File

@ -1,16 +1,24 @@
# This is a sample Python script.
import numpy as np
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from Simplex import Simplex
table = np.array([[25, -3, 5],
[30, -2, 5],
[10, 1, 0],
[6, 3, -8],
[0, -6, -5]])
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
result = np.zeros(2)
S = Simplex(table)
table_result = S.calculate(result)
print("Решенная симплекс-таблица:")
for i in range(table_result.shape[0]):
for j in range(table_result.shape[1]):
print(table_result[i, j], end=" ")
print()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
print()
print("Решение:")
print("X[1] =", result[0])
print("X[2] =", result[1])