Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DO_Lab1_Rect
- {
- internal class DualSimplexMethod
- {
- private double[,] array;
- private double[,] oldArray;
- private readonly List<int> basis;
- private double[] assistCof;
- public DualSimplexMethod(string path)
- {
- basis = new List<int>();
- string[] task = File.ReadAllLines(path);
- /* int variables = task.Length - 1;
- array = new double[task.Length + 1, task.Length + variables]; */
- int variables = task.Length - 1;
- int rows = task[0].Split(' ').Count() - 1;
- array = new double[rows + 1, rows + variables + 1];
- assistCof = new double[array.GetLength(1) - 1];
- //Обраховуємо коефіцієнти функції
- for (int j = 0; j < array.GetLength(1); j++)
- {
- if (j < variables)
- {
- array[array.GetLength(0) - 1, j] = double.Parse(task[j].Split(' ').Last());
- }
- else
- {
- array[array.GetLength(0) - 1, j] = 0;
- }
- }
- //обраховуємо коефіцієнти обмежень
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- if (j == variables + i)
- {
- array[i, j] = 1;
- }
- else if (j == array.GetLength(1) - 1)
- {
- array[i, j] = -double.Parse(task[task.Length - 1].Split(' ')[i]);
- }
- else if (j >= variables)
- {
- array[i, j] = 0;
- }
- else
- {
- array[i, j] = -double.Parse(task[j].Split(' ')[i]);
- }
- }
- }
- PrintCanonicalView();
- }
- private void printCurrentElement(int row, int column)
- {
- if (column < array.GetLength(1) - 2)
- {
- string sym = "";
- if (array[row, column + 1] >= 0) { sym = "+"; }
- else { sym = "-"; }
- if (column != 0)
- {
- Console.Write($"{Math.Abs(array[row, column])}*Y{column + 1} {sym} ");
- }
- else
- {
- Console.Write($"{array[row, column]}*Y{column + 1} {sym} ");
- }
- }
- else if (column == array.GetLength(1) - 2)
- {
- Console.Write($"{array[row, column]}*Y{column + 1} = ");
- }
- else
- {
- Console.Write($"{array[row, column]}");
- }
- }
- private void printMainFunc()
- {
- Console.Write($"F* = ");
- string sym = "";
- for (int j = 0; j < array.GetLength(1) - 1; j++)
- {
- if (-array[array.GetLength(0) - 1, j + 1] >= 0)
- {
- sym = "+";
- }
- else
- {
- sym = "-";
- }
- if (j == 0)
- {
- Console.Write($"{-array[array.GetLength(0) - 1, j]}*Y{j + 1} ");
- }
- else
- {
- Console.Write($"{Math.Abs(array[array.GetLength(0) - 1, j])}*Y{j + 1} ");
- }
- if (j != array.GetLength(1) - 2) Console.Write($"{sym} ");
- }
- Console.Write("-> Max");
- }
- public void PrintCanonicalView()
- {
- printMainFunc();
- for (int j = 0; j < array.GetLength(1); j++)
- {
- }
- Console.WriteLine("\n-----------------------------------");
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- printCurrentElement(i, j);
- }
- Console.WriteLine();
- }
- }
- private void calcBasis(int mainRow = -1, int mainColumn = -1)
- {
- if (basis.Count == 0)
- {
- for (int i = 0; i < array.GetLength(1); i++)
- {
- int zeroCount = 0;
- for (int j = 0; j < array.GetLength(0); j++)
- {
- if (array[j, i] == 0)
- {
- zeroCount++;
- }
- }
- if (zeroCount == array.GetLength(0) - 1)
- {
- basis.Add(i + 1);
- }
- }
- }
- else
- {
- //int toInsertIndex = basis.IndexOf(mainRow);
- int toInsertIndex = mainRow;
- basis.RemoveAt(toInsertIndex);
- basis.Insert(toInsertIndex, mainColumn + 1);
- }
- }
- private void printFinalTable()
- {
- Console.Write("Basis ");
- for (int i = 0; i < array.GetLength(1) - 1; i++)
- {
- Console.Write($"x{i + 1,-4}");
- }
- Console.WriteLine("Bi ");
- for (int i = 0; i < array.GetLength(0); i++)
- {
- if (i < basis.Count)
- {
- Console.Write($"X{basis[i],-7}");
- }
- else
- {
- Console.Write("{0,-8}", "Q");
- }
- for (int j = 0; j < array.GetLength(1); j++)
- {
- Console.Write($"{array[i, j],-7}");
- }
- Console.WriteLine();
- }
- double[] answer = new double[array.GetLength(1) - 1];
- for (int j = 0; j < answer.Length; j++)
- {
- if (basis.Contains(j + 1))
- {
- int index = basis.IndexOf(j + 1);
- answer[j] = array[index, array.GetLength(1) - 1];
- }
- else
- {
- answer[j] = 0;
- }
- }
- Console.WriteLine("Answer:");
- for (int i = 0; i < answer.Length; i++)
- {
- Console.Write($"Y{i + 1} = {answer[i]} ");
- }
- }
- private void printTable()
- {
- Console.Write("Basis ");
- for (int i = 0; i < array.GetLength(1) - 1; i++)
- {
- Console.Write($"x{i + 1,-4}");
- }
- Console.WriteLine("Bi ");
- for (int i = 0; i < array.GetLength(0); i++)
- {
- if (i < basis.Count)
- {
- Console.Write($"X{basis[i],-7}");
- }
- else
- {
- Console.Write("{0,-8}", "Q");
- }
- for (int j = 0; j < array.GetLength(1); j++)
- {
- Console.Write($"{array[i, j],-7}");
- }
- Console.WriteLine();
- }
- Console.Write("{0, -8}", "rel");
- for (int i = 0; i < assistCof.Length; i++)
- {
- if (!double.IsNaN(assistCof[i]))
- {
- Console.Write($"{assistCof[i],-4} ");
- }
- else
- {
- Console.Write("{0, -5}", "-");
- }
- }
- Console.WriteLine();
- }
- public void Solve()
- {
- bool isSolved = true;
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- if (array[i, array.GetLength(1) - 1] < 0)
- {
- isSolved = false;
- break;
- }
- }
- //Знайдемо базисні елемент
- calcBasis();
- int iter = 0;
- while (!isSolved)
- {
- double max = -1;
- int mainRow = -1;
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- double tst = array[i, array.GetLength(1) - 1];
- if (array[i, array.GetLength(1) - 1] < 0 && Math.Abs(array[i, array.GetLength(1) - 1]) > max)
- {
- max = Math.Abs(array[i, array.GetLength(1) - 1]);
- mainRow = i;
- }
- }
- //Обрахуємо допоміжні коефіцієнти
- for (int j = 0; j < assistCof.Length; j++)
- {
- if (array[mainRow, j] >= 0)
- {
- assistCof[j] = double.NaN;
- }
- else
- {
- assistCof[j] = -array[array.GetLength(0) - 1, j] / array[mainRow, j];
- }
- }
- double min = -1;
- int mainColumn = -1;
- for (int i = 0; i < assistCof.Length; i++)
- {
- if (!double.IsNaN(assistCof[i]))
- {
- if (Math.Abs(min + 1) < 1e-5)
- {
- min = assistCof[i];
- mainColumn = i;
- }
- else if (assistCof[i] < min)
- {
- min = assistCof[i];
- mainColumn = i;
- }
- }
- }
- Console.WriteLine("\n******************");
- Console.WriteLine($"Table {iter + 1} -- Main row {mainRow + 1} -- Main column {mainColumn + 1} -- Main element {array[mainRow, mainColumn]}");
- Console.WriteLine("******************");
- printTable();
- calcBasis(mainRow, mainColumn);
- oldArray = (double[,])array.Clone();
- double mainElement = array[mainRow, mainColumn];
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- if (i == mainRow)
- {
- array[i, j] = oldArray[i, j] / mainElement;
- }
- else
- {
- if (j == mainColumn)
- {
- array[i, j] = 0;
- }
- else
- {
- //array[i, j] = oldArray[i, j] * mainElement - oldArray[mainRow, j] * oldArray[i, mainColumn];
- array[i, j] = oldArray[i, j] - (oldArray[i, mainColumn] * oldArray[mainRow, j]) / mainElement;
- }
- }
- }
- }
- //перевіряємо на вирішеність
- isSolved = true;
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- double tst = array[i, array.GetLength(1) - 1];
- if (array[i, array.GetLength(1) - 1] < 0)
- {
- isSolved = false;
- break;
- }
- }
- iter++;
- if (isSolved)
- {
- calcBasis(mainRow, mainColumn);
- }
- }
- Console.WriteLine("******************");
- Console.WriteLine($"Table {iter + 1}");
- Console.WriteLine("******************");
- printFinalTable();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment