Slavik9510

Untitled

Mar 14th, 2023
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.61 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.         private void calcBasis()
  22.         {
  23.             if (basis.Count == 0)
  24.             {
  25.                 for (int i = 0; i < array.GetLength(1); i++)
  26.                 {
  27.                     int zeroCount = 0;
  28.                     for (int j = 0; j < array.GetLength(0); j++)
  29.                     {
  30.                         if (array[j, i] == 0)
  31.                         {
  32.                             zeroCount++;
  33.                         }
  34.                     }
  35.                     if (zeroCount == array.GetLength(0) - 1)
  36.                     {
  37.                         basis.Add(i + 1);
  38.                     }
  39.                 }
  40.             }
  41.             else
  42.             {
  43.                 int insertIndex = -1, toInsert = -1;
  44.                 for (int i = 0; i < array.GetLength(1); i++)
  45.                 {
  46.                     int zeroCount = 0;
  47.                     for (int j = 0; j < array.GetLength(0); j++)
  48.                     {
  49.                         if (array[j, i] == 0)
  50.                         {
  51.                             zeroCount++;
  52.                         }
  53.                     }
  54.                     if (zeroCount != array.GetLength(0) - 1 && basis.Contains(i + 1))
  55.                     {
  56.                         insertIndex = basis.IndexOf(i + 1);
  57.                     }
  58.                     else if (zeroCount == array.GetLength(0) - 1 && !basis.Contains(i + 1))
  59.                     {
  60.                         toInsert = i + 1;
  61.                     }
  62.                 }
  63.                 basis.RemoveAt(insertIndex);
  64.                 basis.Insert(insertIndex, toInsert);
  65.             }
  66.         }
  67.         private void printTable()
  68.         {
  69.             Console.Write("Basis   ");
  70.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  71.             {
  72.                 Console.Write($"x{i + 1,-4}");
  73.             }
  74.             Console.Write("Bi   ");
  75.             Console.WriteLine("Bi/mainColumn");
  76.             for (int i = 0; i < array.GetLength(0); i++)
  77.             {
  78.                 if (i < basis.Count)
  79.                 {
  80.                     Console.Write($"X{basis[i],-7}");
  81.                 }
  82.                 else
  83.                 {
  84.                     Console.Write("{0,-8}", "Q");
  85.                 }
  86.                 for (int j = 0; j < array.GetLength(1); j++)
  87.                 {
  88.                     Console.Write($"{array[i, j],-5}");
  89.                 }
  90.                 if (i < assistCof.Length)
  91.                 {
  92.                     if (assistCof[i] < 0)
  93.                     {
  94.                         Console.WriteLine($"<0");
  95.                     }
  96.                     else
  97.                     {
  98.                         Console.WriteLine($"{assistCof[i]}");
  99.                     }
  100.                 }
  101.                 else
  102.                 {
  103.                     Console.WriteLine();
  104.                 }
  105.             }
  106.         }
  107.         public void Solve()
  108.         {
  109.             bool isSolved = true;
  110.             for (int i = 0; i < array.GetLength(1); i++)
  111.             {
  112.                 if (array[array.GetLength(0) - 1, i] < 0)
  113.                 {
  114.                     isSolved = false;
  115.                     break;
  116.                 }
  117.             }
  118.             int iter = 0;
  119.             while (!isSolved)
  120.             {
  121.                 //Знайдемо базисні елемент
  122.                 calcBasis();
  123.  
  124.                 //Знайдемо ведучий стовпець
  125.                 double max = -1;
  126.                 int mainColumn = -1;
  127.                 for (int j = 0; j < array.GetLength(1); j++)
  128.                 {
  129.                     if (array[array.GetLength(0) - 1, j] < 0 && Math.Abs(array[array.GetLength(0) - 1, j]) > max)
  130.                     {
  131.                         max = Math.Abs(array[array.GetLength(0) - 1, j]);
  132.                         mainColumn = j;
  133.                     }
  134.                 }
  135.  
  136.                 //Обрахуємо допоміжні коефіцієнти
  137.                 for (int i = 0; i < assistCof.Length; i++)
  138.                 {
  139.                     if (array[i, mainColumn] == 0)
  140.                     {
  141.                         assistCof[i] = -1;
  142.                     }
  143.                     else
  144.                     {
  145.                         assistCof[i] = array[i, array.GetLength(1) - 1] / array[i, mainColumn];
  146.                     }
  147.                 }
  148.  
  149.                 double min = -1;
  150.                 int mainRow = -1;
  151.                 for (int i = 0; i < assistCof.Length; i++)
  152.                 {
  153.                     if (assistCof[i] > 0)
  154.                     {
  155.                         if (min == -1)
  156.                         {
  157.                             min = assistCof[i];
  158.                             mainRow = i;
  159.                         }
  160.                         else
  161.                         {
  162.                             if (assistCof[i] < min)
  163.                             {
  164.                                 min = assistCof[i];
  165.                                 mainRow = i;
  166.                             }
  167.                         }
  168.                     }
  169.                 }
  170.  
  171.                 Console.WriteLine("******************");
  172.                 Console.WriteLine($"Table {iter + 1} -- mainRow {mainRow+1} -- mainColumn {mainColumn+1}");
  173.                 Console.WriteLine("******************");
  174.                 printTable();
  175.  
  176.                 oldArray = (double[,])array.Clone();
  177.                 double mainElement = array[mainRow, mainColumn];
  178.                 for (int i = 0; i < array.GetLength(0); i++)
  179.                 {
  180.                     if (i == mainRow) continue;
  181.                     for (int j = 0; j < array.GetLength(1); j++)
  182.                     {
  183.                         if (j == mainColumn)
  184.                         {
  185.                             array[i, j] = 0;
  186.                         }
  187.                         else
  188.                         {
  189.                             array[i, j] = oldArray[i, j] * mainElement - oldArray[mainRow, j] * oldArray[i, mainColumn];
  190.                             //  array[i, j] = oldArray[i, j] - oldArray[mainColumn, j] * oldArray[i, mainRow] / mainElement;
  191.                         }
  192.                     }
  193.                 }
  194.                 //перевіряємо на вирішеність
  195.                 isSolved = true;
  196.                 for (int i = 0; i < array.GetLength(1); i++)
  197.                 {
  198.                     if (array[array.GetLength(0) - 1, i] < 0)
  199.                     {
  200.                         isSolved = false;
  201.                         break;
  202.                     }
  203.                 }
  204.                 iter++;
  205.             }
  206.  
  207.             Console.WriteLine("******************");
  208.             Console.WriteLine($"Table {iter + 1}");
  209.             Console.WriteLine("******************");
  210.             calcBasis();
  211.             printTable();
  212.  
  213.             /*
  214.             for (int i = 0; i < array.GetLength(0); i++)
  215.             {
  216.                 for (int j = 0; j < array.GetLength(1); j++)
  217.                 {
  218.                     Console.Write($"{array[i, j]} ");
  219.                 }
  220.                 Console.WriteLine();
  221.             }
  222.             */
  223.         }
  224.     }
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment