Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace DO_Lab1_Rect
- {
- internal class SimplexMethod
- {
- private double[,] array;
- private double[,] oldArray;
- private List<int> basis;
- private double[] assistCof;
- public SimplexMethod(double[,] array)
- {
- this.array = array;
- basis = new List<int>();
- assistCof = new double[array.GetLength(0) - 1];
- }
- private void calcBasis()
- {
- 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 insertIndex = -1, toInsert = -1;
- 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.Contains(i + 1))
- {
- insertIndex = basis.IndexOf(i + 1);
- }
- else if (zeroCount == array.GetLength(0) - 1 && !basis.Contains(i + 1))
- {
- toInsert = i + 1;
- }
- }
- basis.RemoveAt(insertIndex);
- basis.Insert(insertIndex, toInsert);
- }
- }
- private void printTable()
- {
- Console.Write("Basis ");
- for (int i = 0; i < array.GetLength(1) - 1; i++)
- {
- Console.Write($"x{i + 1,-4}");
- }
- Console.Write("Bi ");
- Console.WriteLine("Bi/mainColumn");
- 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],-5}");
- }
- if (i < assistCof.Length)
- {
- if (assistCof[i] < 0)
- {
- Console.WriteLine($"<0");
- }
- else
- {
- Console.WriteLine($"{assistCof[i]}");
- }
- }
- else
- {
- Console.WriteLine();
- }
- }
- }
- public void Solve()
- {
- bool isSolved = true;
- for (int i = 0; i < array.GetLength(1); i++)
- {
- if (array[array.GetLength(0) - 1, i] < 0)
- {
- isSolved = false;
- break;
- }
- }
- int iter = 0;
- while (!isSolved)
- {
- //Знайдемо базисні елемент
- calcBasis();
- //Знайдемо ведучий стовпець
- double max = -1;
- int mainColumn = -1;
- for (int j = 0; j < array.GetLength(1); j++)
- {
- if (array[array.GetLength(0) - 1, j] < 0 && Math.Abs(array[array.GetLength(0) - 1, j]) > max)
- {
- max = Math.Abs(array[array.GetLength(0) - 1, j]);
- mainColumn = j;
- }
- }
- //Обрахуємо допоміжні коефіцієнти
- for (int i = 0; i < assistCof.Length; i++)
- {
- if (array[i, mainColumn] == 0)
- {
- assistCof[i] = -1;
- }
- else
- {
- assistCof[i] = array[i, array.GetLength(1) - 1] / array[i, mainColumn];
- }
- }
- double min = -1;
- int mainRow = -1;
- for (int i = 0; i < assistCof.Length; i++)
- {
- if (assistCof[i] > 0)
- {
- if (min == -1)
- {
- min = assistCof[i];
- mainRow = i;
- }
- else
- {
- if (assistCof[i] < min)
- {
- min = assistCof[i];
- mainRow = i;
- }
- }
- }
- }
- Console.WriteLine("******************");
- Console.WriteLine($"Table {iter + 1} -- mainRow {mainRow+1} -- mainColumn {mainColumn+1}");
- Console.WriteLine("******************");
- printTable();
- oldArray = (double[,])array.Clone();
- double mainElement = array[mainRow, mainColumn];
- for (int i = 0; i < array.GetLength(0); i++)
- {
- if (i == mainRow) continue;
- for (int j = 0; j < array.GetLength(1); j++)
- {
- 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[mainColumn, j] * oldArray[i, mainRow] / mainElement;
- }
- }
- }
- //перевіряємо на вирішеність
- isSolved = true;
- for (int i = 0; i < array.GetLength(1); i++)
- {
- if (array[array.GetLength(0) - 1, i] < 0)
- {
- isSolved = false;
- break;
- }
- }
- iter++;
- }
- Console.WriteLine("******************");
- Console.WriteLine($"Table {iter + 1}");
- Console.WriteLine("******************");
- calcBasis();
- printTable();
- /*
- for (int i = 0; i < array.GetLength(0); i++)
- {
- for (int j = 0; j < array.GetLength(1); j++)
- {
- Console.Write($"{array[i, j]} ");
- }
- Console.WriteLine();
- }
- */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment