Slavik9510

Untitled

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