commit 95486ec62a945fd6333c18c5cd94720ca3eb54c1 Author: PIbd-21_Spasskyi_Artem Date: Mon Dec 18 18:08:31 2023 +0400 Lab diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Symp.iml b/.idea/Symp.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/Symp.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..cff4016 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ee82c36 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..f1b8384 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ea27f2e --- /dev/null +++ b/main.py @@ -0,0 +1,74 @@ +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): + 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(): + main_col = self.find_main_col() + main_row = self.find_main_row(main_col) + self.basis[main_row] = main_col + + self.table[main_row, :] /= self.table[main_row, main_col] + + for i in range(self.m): + if i == main_row: + continue + self.table[i, :] -= self.table[i, main_col] * self.table[main_row, :] + + 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 + + return self.table + + def is_optimal(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): + positive_ratios = [i for i in range(self.m - 1) if self.table[i, main_col] > 0] + main_row = positive_ratios[0] if positive_ratios else None + + for i in positive_ratios[1:]: + if (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) + simplex_solver = Simplex(table) + table_result = simplex_solver.calculate(result) + + if simplex_solver.is_optimal(): + np.set_printoptions(precision=2, suppress=True) + print("Решенная симплекс-таблица:") + print(table_result) + + print("\nРешение:") + 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])}") + else: + print("Задача не имеет оптимального решения.") +