diff --git a/README.md b/README.md index 5d32c9a..37390cc 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,19 @@ ## Участники 1. Бондаренко Максим ПИбд-32 +2. Исмаилов Ровшан ПИбд-32 +3. Алейкин Артём ПИбд-32 +4. Давыдов Юрий ПИбд-32 +5. Карамушко Максим ПИбд-32 ## Симплекс комбо (Вариант 6) + +### F= 2*x1 + 3*x2 -> max + +### Cистема: +1. x1 + 3*x2 <= 18 +2. 2*x1 + x2 <= 16 +3. x2 <= 5 +4. 3*x1 <= 21 + +### Найди x1 и x2, а также максимальную прибыль F diff --git a/main.py b/main.py new file mode 100644 index 0000000..7c68407 --- /dev/null +++ b/main.py @@ -0,0 +1,36 @@ +import numpy as np +import pandas as pd +from simplexImp import SimplexMethod +from flask import Flask, render_template, request, redirect, send_file + + +app = Flask(__name__) + + +@app.route('/') +def index(): + table = np.array([ + [1, 3, 18], + [2, 1, 16], + [0, 1, 5], + [3, 0, 21], + [-2, -3, 0] + ]) + + simplex = SimplexMethod(table) + start_table = simplex.return_table() + simplex.solve() + end_table = simplex.return_table() + + solution = simplex.return_result(table.shape[1] - 1) + + return render_template("index.html", + start_table=pd.DataFrame(start_table).to_html( + classes='table table-dark table-bordered table-hover'), + end_table=pd.DataFrame(end_table).to_html( + classes='table table-dark table-bordered table-hover'), + solution=solution) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/simplexImp.py b/simplexImp.py new file mode 100644 index 0000000..8905b4d --- /dev/null +++ b/simplexImp.py @@ -0,0 +1,98 @@ +import numpy as np + + +def return_status(table, n): + for i in range(n - 1): + if table[-1, i] < 0: + return True + return False + + +def find_min_col(table): + if np.ndim(table) != 2: + return + m, n = table.shape + main_col = 1 + for j in range(n - 2): + if table[m - 1, j] < table[m - 1, main_col]: + main_col = j + return main_col + + +def return_row(table, column): + if np.ndim(table) != 2: + return + m, n = table.shape + main_row = 0 + for i in range(m - 1): + if table[i, column] > 0: + main_row = i + break + for i in range(main_row + 1, m - 1): + if table[i, column] > 0 and (table[i, 0] / table[i, column]) < ( + table[main_row, 0] / table[main_row, column]): + main_row = i + return main_row + + +class SimplexMethod: + def __init__(self, data): + m, n = data.shape + + self.table = np.zeros((m, n + m)) + self.table[:, :n - 1] = data[:, :n - 1] # Копируем значения из data в первые n столбцов table + self.table[:, -2] = data[:, -1] # Копируем последний столбец (вектор правой части) + + for i in range(m - 1): + self.table[i, n - 1 + i] = 1 + + self.m, self.n = self.table.shape + + def return_table(self): + return self.table + + def solve(self): + while return_status(self.table, self.n): + column_with_min_value = find_min_col(self.table) + row_with_min_value = return_row(self.table, column_with_min_value) + + new_table = np.copy(self.table) + + new_table[row_with_min_value, :] /= self.table[row_with_min_value, column_with_min_value] + + for i in range(self.m): + if i == row_with_min_value: + continue + + ratio = self.table[i, column_with_min_value] + + new_table[i, :] -= ratio * new_table[row_with_min_value, :] + + self.table = new_table + + def return_result(self, out_count): + result = [] + + for j in range(self.n - 2): + if len(result) >= out_count: + break + + index = 0 + count = 0 + + for i in range(self.m): + if self.table[i, j] != 0: + count += 1 + index = i + + if count == 1: + result.append({ + "name": "x" + str(index), + "value": self.table[index, self.n - 2] / self.table[index, j] + }) + + result.append({ + "name": "F", + "value": self.table[self.m - 1, self.n - 2] + }) + return result diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..b8a3185 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,35 @@ + + + + Симплекс метод (6 вариант) + + + +
+

Начальная таблица:

+ {{ start_table | safe }} + +

Конечная таблица:

+ {{ end_table | safe }} + +

Описания столбцов:

+ + + + + + + + + {% for item in solution %} + + + + + {% endfor %} + +
НазваниеЗначение
{{ item.name }}{{ item.value }}
+
+ + + \ No newline at end of file