Готово
This commit is contained in:
parent
b7c77c8b9c
commit
47a88e6f50
14
README.md
14
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
|
||||
|
36
main.py
Normal file
36
main.py
Normal file
@ -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)
|
98
simplexImp.py
Normal file
98
simplexImp.py
Normal file
@ -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
|
35
templates/index.html
Normal file
35
templates/index.html
Normal file
@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Симплекс метод (6 вариант)</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<h2>Начальная таблица:</h2>
|
||||
{{ start_table | safe }}
|
||||
|
||||
<h2 class="mt-3">Конечная таблица:</h2>
|
||||
{{ end_table | safe }}
|
||||
|
||||
<h2 class="mt-3">Описания столбцов:</h2>
|
||||
<table class="table table-dark table-striped table-hover">
|
||||
<thead class="table-success">
|
||||
<tr>
|
||||
<th>Название</th>
|
||||
<th>Значение</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="table-group-divider">
|
||||
{% for item in solution %}
|
||||
<tr>
|
||||
<td>{{ item.name }}</td>
|
||||
<td>{{ item.value }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user