LabWork_1

This commit is contained in:
Максим 2023-12-14 11:35:55 +03:00
parent a3d37a43d6
commit df482e3936
3 changed files with 124 additions and 1 deletions

View File

@ -1,2 +1,15 @@
# PIbd-32_Kashin_M.I_Computed_Math # Лабораторная работа №1 по ВМ
___
## Состав команды (ПИбд-32):
- ### Кашин Максим
- ### Клюшенкова Ксения
- ### Жимолостнова Анна
- ### Базунов Андрей
- ### Цуканова Ирина
## Задание:
![Картинка задания](image/task.jpg)

BIN
image/task.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

110
src/main.py Normal file
View File

@ -0,0 +1,110 @@
import numpy as np
import math
import matplotlib.pyplot as plt
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 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 None
if k is not None:
result[i] = self.table[k, 0]
else:
result[i] = 0
return self.table
def is_it_end(self):
return all(self.table[self.m - 1, 1:] >= 0)
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
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
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)
temp = -table[-1][-2] * result[0] + -table[-1][-1] * math.ceil(result[1])
table_data = []
for i in range(table_result.shape[0]):
row_data = []
for j in range(table_result.shape[1]):
row_data.append("{0:.2f}".format(table_result[i][j]))
table_data.append(row_data)
fig, ax = plt.subplots()
ax.axis('tight')
ax.axis('off')
table = ax.table(cellText=table_data, cellLoc='center', loc='center')
plt.text (-0.06, 0.055, "Решение:")
plt.text (-0.05, 0.045, f"X[1] = {result[0]}")
plt.text (-0.05, 0.035, f"X[2] = {math.ceil(result[1])}")
plt.text (-0.05, 0.025, f"F(x1, x2) = {temp}")
plt.text (-0.06, 0.015, "Решенная симплекс-таблица:")
plt.show()