lab1 complete
This commit is contained in:
parent
d4fb67ce1c
commit
f2193d197a
14
README.md
14
README.md
@ -1,2 +1,14 @@
|
|||||||
# PIbd-31_Musoev.D.T._Computed_Math
|
# Лабораторная работа №1 по ВМ
|
||||||
|
|
||||||
|
___
|
||||||
|
|
||||||
|
## Состав команды (ПИбд-32):
|
||||||
|
|
||||||
|
- ### Мусоев Денис
|
||||||
|
- ### Батылкина Алина
|
||||||
|
- ### Ихонкина Екатерина
|
||||||
|
- ### Бытылкин Артем
|
||||||
|
|
||||||
|
## Задание:
|
||||||
|
|
||||||
|
![Картинка задания](image/task.jpg)
|
BIN
image/task.jpg
Normal file
BIN
image/task.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
76
src/main.py
Normal file
76
src/main.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
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):
|
||||||
|
self.table[i, :self.n] = source[i, :]
|
||||||
|
|
||||||
|
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_optimal_solution():
|
||||||
|
main_col = self.select_pivot_column()
|
||||||
|
main_row = self.select_pivot_row(main_col)
|
||||||
|
self.basis[main_row] = main_col
|
||||||
|
self.perform_pivot(main_row, main_col)
|
||||||
|
|
||||||
|
self.extract_solution(result)
|
||||||
|
return self.table
|
||||||
|
|
||||||
|
def is_optimal_solution(self):
|
||||||
|
return all(self.table[self.m - 1, 1:] >= 0)
|
||||||
|
|
||||||
|
def select_pivot_column(self):
|
||||||
|
return np.argmin(self.table[self.m - 1, 1:]) + 1
|
||||||
|
|
||||||
|
def select_pivot_row(self, main_col):
|
||||||
|
rows = [i for i in range(self.m - 1) if self.table[i, main_col] > 0]
|
||||||
|
return min(rows, key=lambda i: self.table[i, 0] / self.table[i, main_col])
|
||||||
|
|
||||||
|
def perform_pivot(self, main_row, main_col):
|
||||||
|
self.table[main_row, :] /= self.table[main_row, main_col]
|
||||||
|
for i in range(self.m):
|
||||||
|
if i != main_row:
|
||||||
|
self.table[i, :] -= self.table[i, main_col] * self.table[main_row, :]
|
||||||
|
|
||||||
|
def extract_solution(self, result):
|
||||||
|
for i in range(len(result)):
|
||||||
|
k = self.basis.index(i + 1) if i + 1 in self.basis else None
|
||||||
|
result[i] = self.table[k, 0] if k is not None else 0
|
||||||
|
|
||||||
|
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 = [["{0:.2f}".format(value) for value in row] for row in table_result]
|
||||||
|
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
ax.axis('tight')
|
||||||
|
ax.axis('off')
|
||||||
|
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()
|
Loading…
Reference in New Issue
Block a user