Advertisement
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];
- }
- public SimplexMethod(string path)
- {
- basis = new List<int>();
- string[] task = File.ReadAllLines(path);
- string[] current = task[task.Length - 1].Split(' ');
- array = new double[task.Length, current.Length + task.Length];
- assistCof = new double[array.GetLength(0) - 1];
- int variables = current.Length;
- for (int i = 0; i < array.GetLength(1); i++)
- {
- if (i < current.Length)
- {
- array[array.GetLength(0) - 1, i] = -double.Parse(current[i]);
- }
- else array[array.GetLength(0) - 1, i] = 0;
- }
- for (int i = 0; i < array.GetLength(0) - 1; i++)
- {
- current = task[i].Split(' ');
- 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(current[current.Length - 1]);
- }
- else if (j >= variables)
- {
- array[i, j] = 0;
- }
- else
- {
- array[i, j] = double.Parse(current[j]);
- }
- }
- }
- }
- 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();
- }
- }
- }
- 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],-5}");
- }
- 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($"X{i + 1} = {answer[i]} ");
- }
- Console.WriteLine($"\nQ = {array[array.GetLength(0) - 1, array.GetLength(1) - 1]}");
- }
- 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} -- Main row {mainRow + 1} -- Main column {mainColumn + 1} -- Main element {array[mainRow, mainColumn]}");
- 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[i, mainColumn] * oldArray[mainRow, j]) / mainElement;
- // 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();
- printFinalTable();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement