Загрузил(а) файлы в 'src'

This commit is contained in:
the 2023-12-14 08:49:25 +04:00
parent 0ea4036771
commit 44809033e5

107
src/main.py Normal file
View File

@ -0,0 +1,107 @@
import numpy as np
import math
class Simplex:
def __init__(self, source):
self.m, self.n = source.shape
self.table = np.zeros((self.m, self.n + self.m - 1))
self.basis = []
for i in range(self.m):
for j in range(self.n):
self.table[i, j] = source[i, j]
if (self.n + i) < self.table.shape[1]:
self.table[i, self.n + i] = 1
self.basis.append(self.n + i)
self.n = self.table.shape[1]
def calculate(self, result):
# while there are negatives in last row
while not self.is_it_end():
main_col = self.find_main_col()
main_row = self.find_main_row(main_col)
# set basis for main row to be main col
self.basis[main_row] = main_col
new_table = np.zeros((self.m, self.n))
# divide main row by [main row, main col] element and set it as the new main row
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
print("Main col: " + str(main_col))
print("Main row: " + str(main_row))
print("Basis: ")
for i in self.basis:
print(self.basis.index(i), i)
for i in range(self.table.shape[0]):
for j in range(self.table.shape[1]):
print("%5.2f " % self.table[i][j], end='')
print()
print()
for i in range(len(result)):
k = self.basis.index(i + 1) if i + 1 in self.basis else None
if k != None:
result[i] = self.table[k, 0]
else:
result[i] = 0
return self.table
# check if any element in last row is negative, return true if none found
def is_it_end(self):
return all(self.table[self.m - 1, 1:] >= 0)
# find the min element in the last row and mark col of said element
def find_main_col(self):
return np.argmin(self.table[self.m - 1, 1:]) + 1
def find_main_row(self, main_col):
main_row = 0
# scan the maincol (except the bottom element), mark the row of first positive element
for i in range(self.m - 1):
if self.table[i, main_col] > 0:
main_row = i
break
# scan further down the column, if (element > 0 and b / element < b / prev_element), mark it
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
if __name__ == "__main__":
table = np.array([[45, 5, 3],
[-8, -1, 0],
[-10, 0, -1],
[0, -40, -36]])
result = np.zeros(2)
table_result = Simplex(table).calculate(result)
print("Решенная симплекс-таблица:")
for i in range(table_result.shape[0]):
for j in range(table_result.shape[1]):
print("%5.2f " % table_result[i][j], end='')
print()
print()
print("Решение:")
print(f"X[1] = {result[0]}")
print(f"X[2] = {math.ceil(result[1])}")
print(f"F(x1, x2) = {-table[-1][-2] * result[0] + -table[-1][-1] * math.ceil(result[1])}")