Advertisement
Slavik9510

Untitled

Mar 14th, 2023
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DO_Lab1_Rect
  8. {
  9.     internal class SimplexMethod
  10.     {
  11.         private double[,] array;
  12.         private double[,] oldArray;
  13.         private List<int> basis;
  14.         private double[] assistCof;
  15.         public SimplexMethod(double[,] array)
  16.         {
  17.             this.array = array;
  18.             basis = new List<int>();
  19.             assistCof = new double[array.GetLength(0) - 1];
  20.         }
  21.         public SimplexMethod(string path)
  22.         {
  23.             basis = new List<int>();
  24.  
  25.             string[] task = File.ReadAllLines(path);
  26.             string[] current = task[task.Length - 1].Split(' ');
  27.             array = new double[task.Length, current.Length + task.Length];
  28.             assistCof = new double[array.GetLength(0) - 1];
  29.             int variables = current.Length;
  30.             for (int i = 0; i < array.GetLength(1); i++)
  31.             {
  32.                 if (i < current.Length)
  33.                 {
  34.                     array[array.GetLength(0) - 1, i] = -double.Parse(current[i]);
  35.                 }
  36.                 else array[array.GetLength(0) - 1, i] = 0;
  37.             }
  38.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  39.             {
  40.                 current = task[i].Split(' ');
  41.                 for (int j = 0; j < array.GetLength(1); j++)
  42.                 {
  43.                     if (j == variables + i)
  44.                     {
  45.                         array[i, j] = 1;
  46.                     }
  47.                     else if (j == array.GetLength(1) - 1)
  48.                     {
  49.                         array[i, j] = double.Parse(current[current.Length - 1]);
  50.                     }
  51.                     else if (j >= variables)
  52.                     {
  53.                         array[i, j] = 0;
  54.                     }
  55.                     else
  56.                     {
  57.                         array[i, j] = double.Parse(current[j]);
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.         private void calcBasis()
  63.         {
  64.             if (basis.Count == 0)
  65.             {
  66.                 for (int i = 0; i < array.GetLength(1); i++)
  67.                 {
  68.                     int zeroCount = 0;
  69.                     for (int j = 0; j < array.GetLength(0); j++)
  70.                     {
  71.                         if (array[j, i] == 0)
  72.                         {
  73.                             zeroCount++;
  74.                         }
  75.                     }
  76.                     if (zeroCount == array.GetLength(0) - 1)
  77.                     {
  78.                         basis.Add(i + 1);
  79.                     }
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 int insertIndex = -1, toInsert = -1;
  85.                 for (int i = 0; i < array.GetLength(1); i++)
  86.                 {
  87.                     int zeroCount = 0;
  88.                     for (int j = 0; j < array.GetLength(0); j++)
  89.                     {
  90.                         if (array[j, i] == 0)
  91.                         {
  92.                             zeroCount++;
  93.                         }
  94.                     }
  95.                     if (zeroCount != array.GetLength(0) - 1 && basis.Contains(i + 1))
  96.                     {
  97.                         insertIndex = basis.IndexOf(i + 1);
  98.                     }
  99.                     else if (zeroCount == array.GetLength(0) - 1 && !basis.Contains(i + 1))
  100.                     {
  101.                         toInsert = i + 1;
  102.                     }
  103.                 }
  104.                 basis.RemoveAt(insertIndex);
  105.                 basis.Insert(insertIndex, toInsert);
  106.             }
  107.         }
  108.         private void printTable()
  109.         {
  110.             Console.Write("Basis   ");
  111.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  112.             {
  113.                 Console.Write($"x{i + 1,-4}");
  114.             }
  115.             Console.Write("Bi   ");
  116.             Console.WriteLine("Bi/mainColumn");
  117.             for (int i = 0; i < array.GetLength(0); i++)
  118.             {
  119.                 if (i < basis.Count)
  120.                 {
  121.                     Console.Write($"X{basis[i],-7}");
  122.                 }
  123.                 else
  124.                 {
  125.                     Console.Write("{0,-8}", "Q");
  126.                 }
  127.                 for (int j = 0; j < array.GetLength(1); j++)
  128.                 {
  129.                     Console.Write($"{array[i, j],-5}");
  130.                 }
  131.                 if (i < assistCof.Length)
  132.                 {
  133.                     if (assistCof[i] < 0)
  134.                     {
  135.                         Console.WriteLine($"<0");
  136.                     }
  137.                     else
  138.                     {
  139.                         Console.WriteLine($"{assistCof[i]}");
  140.                     }
  141.                 }
  142.                 else
  143.                 {
  144.                     Console.WriteLine();
  145.                 }
  146.             }
  147.         }
  148.         private void printFinalTable()
  149.         {
  150.             Console.Write("Basis   ");
  151.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  152.             {
  153.                 Console.Write($"x{i + 1,-4}");
  154.             }
  155.             Console.WriteLine("Bi");
  156.             for (int i = 0; i < array.GetLength(0); i++)
  157.             {
  158.                 if (i < basis.Count)
  159.                 {
  160.                     Console.Write($"X{basis[i],-7}");
  161.                 }
  162.                 else
  163.                 {
  164.                     Console.Write("{0,-8}", "Q");
  165.                 }
  166.                 for (int j = 0; j < array.GetLength(1); j++)
  167.                 {
  168.                     Console.Write($"{array[i, j],-5}");
  169.                 }
  170.                 Console.WriteLine();
  171.             }
  172.             double[] answer = new double[array.GetLength(1) - 1];
  173.             for (int j = 0; j < answer.Length; j++)
  174.             {
  175.                 if (basis.Contains(j + 1))
  176.                 {
  177.                     int index = basis.IndexOf(j + 1);
  178.                     answer[j] = array[index, array.GetLength(1) - 1];
  179.                 }
  180.                 else
  181.                 {
  182.                     answer[j] = 0;
  183.                 }
  184.             }
  185.             Console.WriteLine("Answer:");
  186.             for (int i = 0; i < answer.Length; i++)
  187.             {
  188.                 Console.Write($"X{i + 1} = {answer[i]}    ");
  189.             }
  190.             Console.WriteLine($"\nQ = {array[array.GetLength(0) - 1, array.GetLength(1) - 1]}");
  191.         }
  192.         public void Solve()
  193.         {
  194.             bool isSolved = true;
  195.             for (int i = 0; i < array.GetLength(1); i++)
  196.             {
  197.                 if (array[array.GetLength(0) - 1, i] < 0)
  198.                 {
  199.                     isSolved = false;
  200.                     break;
  201.                 }
  202.             }
  203.             int iter = 0;
  204.             while (!isSolved)
  205.             {
  206.                 //Знайдемо базисні елемент
  207.                 calcBasis();
  208.  
  209.                 //Знайдемо ведучий стовпець
  210.                 double max = -1;
  211.                 int mainColumn = -1;
  212.                 for (int j = 0; j < array.GetLength(1); j++)
  213.                 {
  214.                     if (array[array.GetLength(0) - 1, j] < 0 && Math.Abs(array[array.GetLength(0) - 1, j]) > max)
  215.                     {
  216.                         max = Math.Abs(array[array.GetLength(0) - 1, j]);
  217.                         mainColumn = j;
  218.                     }
  219.                 }
  220.  
  221.                 //Обрахуємо допоміжні коефіцієнти
  222.                 for (int i = 0; i < assistCof.Length; i++)
  223.                 {
  224.                     if (array[i, mainColumn] == 0)
  225.                     {
  226.                         assistCof[i] = -1;
  227.                     }
  228.                     else
  229.                     {
  230.                         assistCof[i] = array[i, array.GetLength(1) - 1] / array[i, mainColumn];
  231.                     }
  232.                 }
  233.  
  234.                 double min = -1;
  235.                 int mainRow = -1;
  236.                 for (int i = 0; i < assistCof.Length; i++)
  237.                 {
  238.                     if (assistCof[i] > 0)
  239.                     {
  240.                         if (min == -1)
  241.                         {
  242.                             min = assistCof[i];
  243.                             mainRow = i;
  244.                         }
  245.                         else
  246.                         {
  247.                             if (assistCof[i] < min)
  248.                             {
  249.                                 min = assistCof[i];
  250.                                 mainRow = i;
  251.                             }
  252.                         }
  253.                     }
  254.                 }
  255.  
  256.                 Console.WriteLine("******************");
  257.                 Console.WriteLine($"Table {iter + 1} -- Main row {mainRow + 1} -- Main column {mainColumn + 1} -- Main element {array[mainRow, mainColumn]}");
  258.                 Console.WriteLine("******************");
  259.                 printTable();
  260.  
  261.                 oldArray = (double[,])array.Clone();
  262.                 double mainElement = array[mainRow, mainColumn];
  263.                 for (int i = 0; i < array.GetLength(0); i++)
  264.                 {
  265.                     if (i == mainRow) continue;
  266.                     for (int j = 0; j < array.GetLength(1); j++)
  267.                     {
  268.                         if (j == mainColumn)
  269.                         {
  270.                             array[i, j] = 0;
  271.                         }
  272.                         else
  273.                         {
  274.                             //array[i, j] = oldArray[i, j] * mainElement - oldArray[mainRow, j] * oldArray[i, mainColumn];
  275.                             array[i, j] = oldArray[i, j] - (oldArray[i, mainColumn] * oldArray[mainRow, j]) / mainElement;
  276.                             //  array[i, j] = oldArray[i, j] - oldArray[mainColumn, j] * oldArray[i, mainRow] / mainElement;
  277.                         }
  278.                     }
  279.                 }
  280.                 //перевіряємо на вирішеність
  281.                 isSolved = true;
  282.                 for (int i = 0; i < array.GetLength(1); i++)
  283.                 {
  284.                     if (array[array.GetLength(0) - 1, i] < 0)
  285.                     {
  286.                         isSolved = false;
  287.                         break;
  288.                     }
  289.                 }
  290.                 iter++;
  291.             }
  292.  
  293.             Console.WriteLine("******************");
  294.             Console.WriteLine($"Table {iter + 1}");
  295.             Console.WriteLine("******************");
  296.             calcBasis();
  297.             printFinalTable();
  298.         }
  299.     }
  300. }
  301.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement