This commit is contained in:
Вячеслав Иванов 2024-12-04 12:21:26 +04:00
commit dfa1bb57f5
2 changed files with 233 additions and 0 deletions

220
index.html Normal file
View File

@ -0,0 +1,220 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Решение транспортной задачи</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { margin: 20px; }
.container { max-width: 1200px; }
.form-group { margin-bottom: 15px; }
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mb-4">Решение транспортной задачи методом северо-западного угла</h1>
<h2>Загрузка данных</h2>
<input type="file" id="fileInput" class="form-control mb-3" />
<button onclick="processFile()" class="btn btn-primary">Загрузить и отобразить данные</button>
<div id="inputData" class="mt-4"></div>
<h2 class="mt-4">Шаги решения</h2>
<div id="steps"></div>
<h2 class="mt-4">Итоговая матрица</h2>
<table class="table table-bordered" id="resultTable"></table>
<div class="errors" id="errors"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
let numSuppliers, numWarehouses;
let costMatrix = [], supply = [], demand = [];
let result = [];
function processFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (!file) {
alert("Пожалуйста, выберите файл.");
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const text = event.target.result;
parseCSV(text);
};
reader.readAsText(file);
}
function parseCSV(data) {
const rows = data.split('\n').map(row => row.trim()).filter(row => row !== '');
const firstRow = rows[0].split(',');
numSuppliers = parseInt(firstRow[0]);
numWarehouses = parseInt(firstRow[1]);
if (isNaN(numSuppliers) || isNaN(numWarehouses)) {
alert("Ошибка в количестве поставщиков и складов.");
return;
}
costMatrix = [];
supply = [];
demand = [];
for (let i = 1; i <= numSuppliers; i++) {
const row = rows[i].split(',').map(Number);
costMatrix.push(row);
}
const supplyRow = rows[numSuppliers + 1].split(',').map(Number);
supply = supplyRow;
const demandRow = rows[numSuppliers + 2].split(',').map(Number);
demand = demandRow;
displayInputData();
solve();
}
function displayInputData() {
let inputDiv = document.getElementById('inputData');
inputDiv.innerHTML = '';
let costTable = `<h3>Стоимость доставки</h3><table class="table table-bordered"><tr><th></th>`;
for (let j = 0; j < numWarehouses; j++) {
costTable += `<th>Склад ${j + 1}</th>`;
}
costTable += `</tr>`;
for (let i = 0; i < numSuppliers; i++) {
costTable += `<tr><th>Поставщик ${i + 1}</th>`;
for (let j = 0; j < numWarehouses; j++) {
costTable += `<td>${costMatrix[i][j]}</td>`;
}
costTable += `</tr>`;
}
costTable += `</table>`;
inputDiv.innerHTML += costTable;
let supplyTable = `<h3>Объемы поставок</h3><table class="table table-bordered"><tr><th>Поставщик</th>`;
for (let i = 0; i < numSuppliers; i++) {
supplyTable += `<th>Поставщик ${i + 1}</th>`;
}
supplyTable += `</tr><tr><th>Объем</th>`;
for (let i = 0; i < numSuppliers; i++) {
supplyTable += `<td>${supply[i]}</td>`;
}
supplyTable += `</tr></table>`;
inputDiv.innerHTML += supplyTable;
let demandTable = `<h3>Потребности складов</h3><table class="table table-bordered"><tr><th>Склад</th>`;
for (let j = 0; j < numWarehouses; j++) {
demandTable += `<th>Склад ${j + 1}</th>`;
}
demandTable += `</tr><tr><th>Потребность</th>`;
for (let j = 0; j < numWarehouses; j++) {
demandTable += `<td>${demand[j]}</td>`;
}
demandTable += `</tr></table>`;
inputDiv.innerHTML += demandTable;
}
function displayMatrix(matrix) {
let table = '<tr><th></th>';
for (let j = 0; j < numWarehouses; j++) {
table += `<th>Склад ${j + 1}</th>`;
}
table += `</tr>`;
for (let i = 0; i < numSuppliers; i++) {
table += `<tr><th>Поставщик ${i + 1}</th>`;
for (let j = 0; j < numWarehouses; j++) {
table += `<td>${matrix[i][j]}</td>`;
}
table += `</tr>`;
}
document.getElementById('steps').innerHTML += `<h4>Текущая матрица после шага:</h4><table class="table table-bordered">${table}</table>`;
}
function solve() {
result = [];
for (let i = 0; i < numSuppliers; i++) {
result[i] = [];
for (let j = 0; j < numWarehouses; j++) {
result[i][j] = 0;
}
}
let i = 0, j = 0;
let totalCost = 0;
let costDetails = []; // Для хранения всех вычислений в нужном формате
let stepsDiv = document.getElementById('steps');
stepsDiv.innerHTML = '<h3>Шаги решения:</h3>';
while (i < numSuppliers && j < numWarehouses) {
let transportAmount = Math.min(supply[i], demand[j]);
result[i][j] = transportAmount;
supply[i] -= transportAmount;
demand[j] -= transportAmount;
let stepCost = transportAmount * costMatrix[i][j];
totalCost += stepCost;
// Записываем умножения в формате "x * y"
costDetails.push(`${transportAmount} * ${costMatrix[i][j]}`);
stepsDiv.innerHTML += `<p>Поставщик ${i + 1} → Склад ${j + 1}: ${transportAmount} единиц. Стоимость: ${stepCost} ( ${transportAmount} * ${costMatrix[i][j]} )</p>`;
displayMatrix(result);
if (supply[i] === 0) {
i++;
} else if (demand[j] === 0) {
j++;
}
}
let resultTable = '<tr><th></th>';
for (let j = 0; j < numWarehouses; j++) {
resultTable += `<th>Склад ${j+1}</th>`;
}
resultTable += `</tr>`;
for (let i = 0; i < numSuppliers; i++) {
resultTable += `<tr><th>Поставщик ${i+1}</th>`;
for (let j = 0; j < numWarehouses; j++) {
resultTable += `<td>${result[i][j]}</td>`;
}
resultTable += `</tr>`;
}
document.getElementById('resultTable').innerHTML = resultTable;
// Выводим итоговую стоимость с промежуточными вычислениями
let totalCostCalculation = costDetails.join(' + ');
stepsDiv.innerHTML += `<h3>Итоговая стоимость доставки:</h3><h4><strong>Общая стоимость: ${totalCostCalculation} = ${totalCost}</strong></h4>`;
let errorDetails = `<h3>Погрешности:</h3>`;
let totalError = 0;
for (let i = 0; i < numSuppliers; i++) {
for (let j = 0; j < numWarehouses; j++) {
if (result[i][j] > 0) {
let error = result[i][j] * 0.05;
totalError += error;
errorDetails += `<p>Поставщик ${i+1} → Склад ${j+1}: Погрешность = ${result[i][j]} * 0.05 = ${error.toFixed(2)}</p>`;
}
}
}
document.getElementById('errors').innerHTML = errorDetails;
document.getElementById('errors').innerHTML += `<h4><strong>Общая погрешность: ${totalError.toFixed(2)}</strong></h4>`;
}
</script>
</body>
</html>

13
vm.csv Normal file
View File

@ -0,0 +1,13 @@
10,10
10,12,8,14,13,16,15,11,9,20
7,10,9,11,12,13,16,10,15,18
13,9,7,12,15,14,8,10,18,17
14,11,8,10,12,15,13,16,11,9
8,7,9,15,14,16,13,11,10,12
15,16,10,12,11,10,13,9,14,18
9,10,12,14,8,13,16,15,10,14
16,14,11,8,10,15,12,9,13,18
17,15,13,14,12,10,9,18,8,12
11,12,14,15,10,13,9,16,11,10
120,150,130,180,110,140,100,160,170,150
120,150,100,130,160,180,140,110,170,150
1 10,10
2 10,12,8,14,13,16,15,11,9,20
3 7,10,9,11,12,13,16,10,15,18
4 13,9,7,12,15,14,8,10,18,17
5 14,11,8,10,12,15,13,16,11,9
6 8,7,9,15,14,16,13,11,10,12
7 15,16,10,12,11,10,13,9,14,18
8 9,10,12,14,8,13,16,15,10,14
9 16,14,11,8,10,15,12,9,13,18
10 17,15,13,14,12,10,9,18,8,12
11 11,12,14,15,10,13,9,16,11,10
12 120,150,130,180,110,140,100,160,170,150
13 120,150,100,130,160,180,140,110,170,150