Slavik9510

Untitled

Mar 21st, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 29.53 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.     enum Sign
  11.     {
  12.         LE = -1,
  13.         EQ = 0,
  14.         GE = 1
  15.     }
  16.     internal class DualSimplexMethod
  17.     {
  18.         private void printArr()
  19.         {
  20.             for (int i = 0; i < array.GetLength(0); i++)
  21.             {
  22.                 for (int j = 0; j < array.GetLength(1); j++)
  23.                 {
  24.                     Console.Write($"{array[i, j]} ");
  25.                 }
  26.                 Console.WriteLine();
  27.             }
  28.         }
  29.  
  30.         private double[,] array;
  31.         private double[,] oldArray;
  32.         private readonly List<int> basis;
  33.         private double[] assistCof;
  34.         public DualSimplexMethod(string path)
  35.         {
  36.             basis = new List<int>();
  37.  
  38.             List<string> task = File.ReadAllLines(path).ToList();
  39.  
  40.             //Визначаємо знак в і-тому рядку і забиаємо його зі стрічки
  41.             /*
  42.             List<Sign> sign = new List<Sign>();
  43.             for (int i = 0; i < task.Count - 1; i++)
  44.             {
  45.                 string current = task[i].Split(' ').Reverse().Skip(1).First();
  46.                 if (current == "<=")
  47.                 {
  48.                     sign.Add(Sign.LE);
  49.                 }
  50.                 else sign.Add(Sign.GE);
  51.                
  52.                 int indexToRemove = task[i].Split(' ').Length - 2; //Забираємо передостанній елемент
  53.                 List<string> tmp = task[i].Split(" ").ToList();
  54.                 tmp.Remove(current);
  55.                 task[i] = string.Join(" ", tmp);
  56.             }
  57.             */
  58.             //!!!!!!!!!!!!!!!!!!!!!!!!
  59.             /*
  60.             int variables = task.Count - 1;
  61.             int rows = task[0].Split(' ').Count() - 1;
  62.             array = new double[rows + 1, rows + variables + 1];
  63.             assistCof = new double[array.GetLength(1) - 1];
  64.  
  65.             //Обраховуємо коефіцієнти функції
  66.             for (int j = 0; j < array.GetLength(1); j++)
  67.             {
  68.                 if (j < variables)
  69.                 {
  70.                     array[array.GetLength(0) - 1, j] = double.Parse(task[j].Split(' ').Last());
  71.                 }
  72.                 else
  73.                 {
  74.                     array[array.GetLength(0) - 1, j] = 0;
  75.                 }
  76.             }
  77.  
  78.             //обраховуємо коефіцієнти обмежень
  79.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  80.             {
  81.                 for (int j = 0; j < array.GetLength(1); j++)
  82.                 {
  83.                     if (j == variables + i)
  84.                     {
  85.                         array[i, j] = 1;
  86.                     }
  87.                     else if (j == array.GetLength(1) - 1)
  88.                     {
  89.                         array[i, j] = -double.Parse(task[task.Count - 1].Split(' ')[i]);
  90.                     }
  91.                     else if (j >= variables)
  92.                     {
  93.                         array[i, j] = 0;
  94.                     }
  95.                     else
  96.                     {
  97.                         array[i, j] = -double.Parse(task[j].Split(' ')[i]);
  98.                     }
  99.                 }
  100.             }
  101.             */
  102.             getData(path);
  103.             convertToDual();
  104.             //PrintCanonicalView();
  105.             printArr();
  106.         }
  107.         private void convertToDual()
  108.         {
  109.             oldArray = (double[,])array.Clone();
  110.  
  111.             calcBasis();
  112.             int rows = array.GetLength(1) - basis.Count;
  113.             int variables = array.GetLength(0) - 1;
  114.             array = new double[rows, rows + variables];
  115.             basis.Clear();
  116.             //Обраховуємо коефіцієнти функції
  117.             for (int j = 0; j < array.GetLength(1); j++)
  118.             {
  119.                 if (j < variables)
  120.                 {
  121.                     array[array.GetLength(0) - 1, j] = oldArray[j, oldArray.GetLength(1) - 1];
  122.                 }
  123.                 else
  124.                 {
  125.                     array[array.GetLength(0) - 1, j] = 0;
  126.                 }
  127.             }
  128.  
  129.             //Транспонуємо
  130.             //обраховуємо коефіцієнти обмежень
  131.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  132.             {
  133.                 for (int j = 0; j < array.GetLength(1); j++)
  134.                 {
  135.                     if (j == variables + i)
  136.                     {
  137.                         array[i, j] = 1;
  138.                     }
  139.                     else if (j == array.GetLength(1) - 1)
  140.                     {
  141.                         array[i, j] = oldArray[oldArray.GetLength(0) - 1, i];
  142.                     }
  143.                     else if (j >= variables)
  144.                     {
  145.                         array[i, j] = 0;
  146.                     }
  147.                     else
  148.                     {
  149.                         if (oldArray[j, i] == 0)
  150.                         {
  151.                             array[i, j] = 0; //Щоб не виходило -0
  152.                         }
  153.                         else
  154.                         {
  155.                             array[i, j] = -oldArray[j, i];
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.         private void getData(string path)
  162.         {
  163.             List<string> task = File.ReadAllLines(path).ToList();
  164.  
  165.             List<Sign> signs = new List<Sign>();
  166.             for (int i = 0; i < task.Count - 1; i++)
  167.             {
  168.                 string currentSign = task[i].Split(' ').Reverse().Skip(1).First();
  169.                 if (currentSign == "<=")
  170.                 {
  171.                     signs.Add(Sign.LE);
  172.                 }
  173.                 else if (currentSign == ">=")
  174.                 {
  175.                     signs.Add(Sign.GE);
  176.                 }
  177.                 else signs.Add(Sign.EQ);
  178.  
  179.                 int indexToRemove = task[i].Split(' ').Length - 2; //Забираємо передостанній елемент
  180.                 List<string> tmp = task[i].Split(" ").ToList();
  181.                 tmp.Remove(currentSign);
  182.                 task[i] = string.Join(" ", tmp);
  183.             }
  184.  
  185.             string[] current = task.Last().Split(' ');
  186.             int variables = task.Last().Split(' ').Count();
  187.             array = new double[task.Count, task[0].Split(' ').Count()];
  188.             assistCof = new double[array.GetLength(0) - 1];
  189.  
  190.             for (int i = 0; i < array.GetLength(1); i++)
  191.             {
  192.                 if (i < current.Length)
  193.                 {
  194.                     array[array.GetLength(0) - 1, i] = double.Parse(current[i]);
  195.                 }
  196.                 else array[array.GetLength(0) - 1, i] = double.NaN;
  197.             }
  198.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  199.             {
  200.                 current = task[i].Split(' ');
  201.                 for (int j = 0; j < array.GetLength(1); j++)
  202.                 {
  203.                     array[i, j] = double.Parse(current[j]);
  204.                 }
  205.             }
  206.  
  207.             toCanonical(signs);
  208.         }
  209.         private void toCanonical(List<Sign> signs)
  210.         {
  211.             for (int i = 0; i < array.GetLength(0); i++)
  212.             {
  213.                 if (array[i, array.GetLength(1) - 1] < 0)
  214.                 {
  215.                     for (int j = 0; j < array.GetLength(1); j++)
  216.                     {
  217.                         array[i, j] *= -1;
  218.                     }
  219.                     signs[i] = (Sign)((int)signs[i] * -1);
  220.                 }
  221.             }
  222.             int additionalVariables = 0;
  223.             for (int i = 0; i < signs.Count; i++)
  224.             {
  225.                 if (signs[i] == Sign.LE)
  226.                 {
  227.                     //!!!!!!!!!!!!!!!!
  228.                     //LE_COUNT
  229.                     additionalVariables++;
  230.                 }
  231.                 else if (signs[i] == Sign.GE)
  232.                 {
  233.                     additionalVariables += 2;
  234.                 }
  235.             }
  236.             int variables = array.GetLength(1) - 1;
  237.             oldArray = (double[,])array.Clone();
  238.             array = new double[array.GetLength(0), array.GetLength(1) + additionalVariables];
  239.             List<int> toAddM = new List<int>();
  240.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  241.             {
  242.                 for (int j = 0; j < array.GetLength(1) - 1; j++)
  243.                 {
  244.                     if (j < oldArray.GetLength(1) - 1)
  245.                     {
  246.                         array[i, j] = oldArray[i, j];
  247.                     }
  248.                     else
  249.                     {
  250.                         // int tst = variables + additionalVariables;
  251.                         if (signs[i] == Sign.LE)
  252.                         {
  253.                             if (j == variables + i)
  254.                             {
  255.                                 array[i, j] = 1;
  256.                             }
  257.                             else array[i, j] = 0;
  258.                         }
  259.                         else if (signs[i] == Sign.GE)
  260.                         {
  261.                             if (j == variables + i)
  262.                             {
  263.                                 array[i, j] = -1;
  264.                             }
  265.                             else if (j == variables + i + array.GetLength(0) - 1)
  266.                             {
  267.                                 /*Рахуємо базисні змінні для рядків із знаком>=
  268.                                  * array.GetLength(0)-1 -- кількість рядків(базових змінних)
  269.                                  */
  270.                                 toAddM.Add(j);
  271.                                 array[i, j] = 1;
  272.                             }
  273.                             else array[i, j] = 0;
  274.                         }
  275.                     }
  276.                 }
  277.                 array[i, array.GetLength(1) - 1] = oldArray[i, oldArray.GetLength(1) - 1];
  278.             }
  279.  
  280.             //Обраховуємо останній рядок
  281.             for (int j = 0; j < array.GetLength(1); j++)
  282.             {
  283.                 if (j < oldArray.GetLength(1) - 1)
  284.                 {
  285.                     array[array.GetLength(0) - 1, j] = -oldArray[oldArray.GetLength(0) - 1, j];
  286.                 }
  287.                 else if (toAddM.Contains(j))
  288.                 {
  289.                     // array[array.GetLength(0) - 1, j] = double.NaN;
  290.                     array[array.GetLength(0) - 1, j] = 1;
  291.                 }
  292.             }
  293.             //printArr();
  294.         }
  295.         private void printCurrentElement(int row, int column)
  296.         {
  297.             if (column < array.GetLength(1) - 2)
  298.             {
  299.                 string sym = "";
  300.                 if (array[row, column + 1] >= 0) { sym = "+"; }
  301.                 else { sym = "-"; }
  302.                 if (column != 0)
  303.                 {
  304.                     Console.Write($"{Math.Abs(array[row, column])}*Y{column + 1} {sym} ");
  305.                 }
  306.                 else
  307.                 {
  308.                     Console.Write($"{array[row, column]}*Y{column + 1} {sym} ");
  309.                 }
  310.             }
  311.             else if (column == array.GetLength(1) - 2)
  312.             {
  313.                 Console.Write($"{array[row, column]}*Y{column + 1} = ");
  314.             }
  315.             else
  316.             {
  317.                 Console.Write($"{array[row, column]}");
  318.             }
  319.         }
  320.         private void printMainFunc()
  321.         {
  322.             Console.Write($"F* = ");
  323.             string sym = "";
  324.             for (int j = 0; j < array.GetLength(1) - 1; j++)
  325.             {
  326.                 if (-array[array.GetLength(0) - 1, j + 1] >= 0)
  327.                 {
  328.                     sym = "+";
  329.                 }
  330.                 else
  331.                 {
  332.                     sym = "-";
  333.                 }
  334.                 if (j == 0)
  335.                 {
  336.                     Console.Write($"{-array[array.GetLength(0) - 1, j]}*Y{j + 1} ");
  337.                 }
  338.                 else
  339.                 {
  340.                     Console.Write($"{Math.Abs(array[array.GetLength(0) - 1, j])}*Y{j + 1} ");
  341.                 }
  342.                 if (j != array.GetLength(1) - 2) Console.Write($"{sym} ");
  343.             }
  344.             Console.Write("-> Max");
  345.         }
  346.         public void PrintCanonicalView()
  347.         {
  348.             printMainFunc();
  349.  
  350.             for (int j = 0; j < array.GetLength(1); j++)
  351.             {
  352.  
  353.             }
  354.             Console.WriteLine("\n-----------------------------------");
  355.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  356.             {
  357.                 for (int j = 0; j < array.GetLength(1); j++)
  358.                 {
  359.                     printCurrentElement(i, j);
  360.                 }
  361.                 Console.WriteLine();
  362.             }
  363.         }
  364.         private void calcBasis(int mainRow = -1, int mainColumn = -1)
  365.         {
  366.             if (basis.Count == 0)
  367.             {
  368.                 for (int i = 0; i < array.GetLength(0); i++)
  369.                 {
  370.                     for (int j = array.GetLength(1) - 2; j >= 0; j--)
  371.                     {
  372.                         if (array[i, j] == 1)
  373.                         {
  374.                             basis.Add(j + 1);
  375.                             break;
  376.                         }
  377.                     }
  378.                 }
  379.             }
  380.             else
  381.             {
  382.                 //int toInsertIndex = basis.IndexOf(mainRow);
  383.                 int toInsertIndex = mainRow;
  384.                 basis.RemoveAt(toInsertIndex);
  385.                 basis.Insert(toInsertIndex, mainColumn + 1);
  386.             }
  387.         }
  388.         private void printFinalTable()
  389.         {
  390.             Console.Write("Basis   ");
  391.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  392.             {
  393.                 Console.Write($"x{i + 1,-4}");
  394.             }
  395.             Console.WriteLine("Bi   ");
  396.  
  397.             for (int i = 0; i < array.GetLength(0); i++)
  398.             {
  399.                 if (i < basis.Count)
  400.                 {
  401.                     Console.Write($"X{basis[i],-7}");
  402.                 }
  403.                 else
  404.                 {
  405.                     Console.Write("{0,-8}", "Q");
  406.                 }
  407.                 for (int j = 0; j < array.GetLength(1); j++)
  408.                 {
  409.                     Console.Write($"{array[i, j],-7}");
  410.                 }
  411.                 Console.WriteLine();
  412.             }
  413.  
  414.             double[] answer = new double[array.GetLength(1) - 1];
  415.             for (int j = 0; j < answer.Length; j++)
  416.             {
  417.                 if (basis.Contains(j + 1))
  418.                 {
  419.                     int index = basis.IndexOf(j + 1);
  420.                     answer[j] = array[index, array.GetLength(1) - 1];
  421.                 }
  422.                 else
  423.                 {
  424.                     answer[j] = 0;
  425.                 }
  426.             }
  427.             Console.WriteLine("Answer:");
  428.             for (int i = 0; i < answer.Length; i++)
  429.             {
  430.                 Console.Write($"Y{i + 1} = {answer[i]}    ");
  431.             }
  432.         }
  433.         private void printTable()
  434.         {
  435.             Console.Write("Basis   ");
  436.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  437.             {
  438.                 Console.Write($"x{i + 1,-4}");
  439.             }
  440.             Console.WriteLine("Bi   ");
  441.  
  442.             for (int i = 0; i < array.GetLength(0); i++)
  443.             {
  444.                 if (i < basis.Count)
  445.                 {
  446.                     Console.Write($"X{basis[i],-7}");
  447.                 }
  448.                 else
  449.                 {
  450.                     Console.Write("{0,-8}", "Q");
  451.                 }
  452.                 for (int j = 0; j < array.GetLength(1); j++)
  453.                 {
  454.                     Console.Write($"{array[i, j],-7}");
  455.                 }
  456.                 Console.WriteLine();
  457.             }
  458.             Console.Write("{0, -8}", "rel");
  459.             for (int i = 0; i < assistCof.Length; i++)
  460.             {
  461.                 if (!double.IsNaN(assistCof[i]))
  462.                 {
  463.                     Console.Write($"{assistCof[i],-4} ");
  464.                 }
  465.                 else
  466.                 {
  467.                     Console.Write("{0, -5}", "-");
  468.                 }
  469.             }
  470.             Console.WriteLine();
  471.         }
  472.         public void Solve()
  473.         {
  474.             bool isSolved = true;
  475.             for (int i = 0; i < array.GetLength(0) - 1; i++)
  476.             {
  477.                 if (array[i, array.GetLength(1) - 1] < 0)
  478.                 {
  479.                     isSolved = false;
  480.                     break;
  481.                 }
  482.             }
  483.  
  484.             //Знайдемо базисні елемент
  485.             calcBasis();
  486.             int iter = 0;
  487.             while (!isSolved)
  488.             {
  489.  
  490.                 double max = -1;
  491.                 int mainRow = -1;
  492.  
  493.                 for (int i = 0; i < array.GetLength(0) - 1; i++)
  494.                 {
  495.                     double tst = array[i, array.GetLength(1) - 1];
  496.                     if (array[i, array.GetLength(1) - 1] < 0 && Math.Abs(array[i, array.GetLength(1) - 1]) > max)
  497.                     {
  498.                         max = Math.Abs(array[i, array.GetLength(1) - 1]);
  499.                         mainRow = i;
  500.                     }
  501.                 }
  502.  
  503.                 //Обрахуємо допоміжні коефіцієнти
  504.                 for (int j = 0; j < assistCof.Length; j++)
  505.                 {
  506.                     if (array[mainRow, j] >= 0)
  507.                     {
  508.                         assistCof[j] = double.NaN;
  509.                     }
  510.                     else
  511.                     {
  512.                         assistCof[j] = -array[array.GetLength(0) - 1, j] / array[mainRow, j];
  513.                     }
  514.                 }
  515.  
  516.                 double min = -1;
  517.                 int mainColumn = -1;
  518.  
  519.                 for (int i = 0; i < assistCof.Length; i++)
  520.                 {
  521.                     if (!double.IsNaN(assistCof[i]))
  522.                     {
  523.                         if (Math.Abs(min + 1) < 1e-5)
  524.                         {
  525.                             min = assistCof[i];
  526.                             mainColumn = i;
  527.                         }
  528.                         else if (assistCof[i] < min)
  529.                         {
  530.                             min = assistCof[i];
  531.                             mainColumn = i;
  532.                         }
  533.                     }
  534.                 }
  535.                 Console.WriteLine("\n******************");
  536.                 Console.WriteLine($"Table {iter + 1} -- Main row {mainRow + 1} -- Main column {mainColumn + 1} -- Main element {array[mainRow, mainColumn]}");
  537.                 Console.WriteLine("******************");
  538.                 printTable();
  539.                 calcBasis(mainRow, mainColumn);
  540.  
  541.                 oldArray = (double[,])array.Clone();
  542.                 double mainElement = array[mainRow, mainColumn];
  543.                 for (int i = 0; i < array.GetLength(0); i++)
  544.                 {
  545.                     for (int j = 0; j < array.GetLength(1); j++)
  546.                     {
  547.                         if (i == mainRow)
  548.                         {
  549.                             array[i, j] = oldArray[i, j] / mainElement;
  550.                         }
  551.                         else
  552.                         {
  553.                             if (j == mainColumn)
  554.                             {
  555.                                 array[i, j] = 0;
  556.                             }
  557.                             else
  558.                             {
  559.                                 //array[i, j] = oldArray[i, j] * mainElement - oldArray[mainRow, j] * oldArray[i, mainColumn];
  560.                                 array[i, j] = oldArray[i, j] - (oldArray[i, mainColumn] * oldArray[mainRow, j]) / mainElement;
  561.                             }
  562.                         }
  563.                     }
  564.                 }
  565.                 //перевіряємо на вирішеність
  566.                 isSolved = true;
  567.                 for (int i = 0; i < array.GetLength(0) - 1; i++)
  568.                 {
  569.                     double tst = array[i, array.GetLength(1) - 1];
  570.                     if (array[i, array.GetLength(1) - 1] < 0)
  571.                     {
  572.                         isSolved = false;
  573.                         break;
  574.                     }
  575.                 }
  576.                 iter++;
  577.                 if (isSolved)
  578.                 {
  579.                     calcBasis(mainRow, mainColumn);
  580.                 }
  581.             }
  582.             Console.WriteLine("******************");
  583.             Console.WriteLine($"Table {iter + 1}");
  584.             Console.WriteLine("******************");
  585.             printFinalTable();
  586.         }
  587.         /*
  588.         public void Solve1()
  589.         {
  590.             bool isSolved = true;
  591.             for (int i = 0; i < array.GetLength(1); i++)
  592.             {
  593.                 if (array[array.GetLength(0) - 1, i] < 0)
  594.                 {
  595.                     isSolved = false;
  596.                     break;
  597.                 }
  598.             }
  599.             int iter = 0;
  600.             while (!isSolved)
  601.             {
  602.                 //Знайдемо базисні елемент
  603.                 calcBasis1();
  604.  
  605.                 //Знайдемо ведучий стовпець
  606.                 double max = -1;
  607.                 int mainColumn = -1;
  608.                 for (int j = 0; j < array.GetLength(1); j++)
  609.                 {
  610.                     if (array[array.GetLength(0) - 1, j] < 0 && Math.Abs(array[array.GetLength(0) - 1, j]) > max)
  611.                     {
  612.                         max = Math.Abs(array[array.GetLength(0) - 1, j]);
  613.                         mainColumn = j;
  614.                     }
  615.                 }
  616.  
  617.                 //Обрахуємо допоміжні коефіцієнти
  618.                 for (int i = 0; i < assistCof.Length; i++)
  619.                 {
  620.                     if (array[i, mainColumn] == 0)
  621.                     {
  622.                         assistCof[i] = -1;
  623.                     }
  624.                     else
  625.                     {
  626.                         assistCof[i] = array[i, array.GetLength(1) - 1] / array[i, mainColumn];
  627.                     }
  628.                 }
  629.  
  630.                 double min = -1;
  631.                 int mainRow = -1;
  632.                 for (int i = 0; i < assistCof.Length; i++)
  633.                 {
  634.                     if (assistCof[i] > 0)
  635.                     {
  636.                         if (min == -1)
  637.                         {
  638.                             min = assistCof[i];
  639.                             mainRow = i;
  640.                         }
  641.                         else
  642.                         {
  643.                             if (assistCof[i] < min)
  644.                             {
  645.                                 min = assistCof[i];
  646.                                 mainRow = i;
  647.                             }
  648.                         }
  649.                     }
  650.                 }
  651.  
  652.                 Console.WriteLine("******************");
  653.                 Console.WriteLine($"Table {iter + 1} -- Main row {mainRow + 1} -- Main column {mainColumn + 1} -- Main element {array[mainRow, mainColumn]}");
  654.                 Console.WriteLine("******************");
  655.                 printTable1();
  656.  
  657.                 oldArray = (double[,])array.Clone();
  658.                 double mainElement = array[mainRow, mainColumn];
  659.                 for (int i = 0; i < array.GetLength(0); i++)
  660.                 {
  661.                     for (int j = 0; j < array.GetLength(1); j++)
  662.                     {
  663.                         if (i == mainRow)
  664.                         {
  665.                             array[i, j] = oldArray[i, j] / mainElement;
  666.                         }
  667.                         else
  668.                         {
  669.                             if (j == mainColumn)
  670.                             {
  671.                                 array[i, j] = 0;
  672.                             }
  673.                             else
  674.                             {
  675.                                 //array[i, j] = oldArray[i, j] * mainElement - oldArray[mainRow, j] * oldArray[i, mainColumn];
  676.                                 array[i, j] = oldArray[i, j] - (oldArray[i, mainColumn] * oldArray[mainRow, j]) / mainElement;
  677.                             }
  678.                         }
  679.                     }
  680.                 }
  681.                 //перевіряємо на вирішеність
  682.                 isSolved = true;
  683.                 for (int i = 0; i < array.GetLength(1); i++)
  684.                 {
  685.                     if (array[array.GetLength(0) - 1, i] < 0)
  686.                     {
  687.                         isSolved = false;
  688.                         break;
  689.                     }
  690.                 }
  691.                 iter++;
  692.             }
  693.  
  694.             Console.WriteLine("******************");
  695.             Console.WriteLine($"Table {iter + 1}");
  696.             Console.WriteLine("******************");
  697.             calcBasis1();
  698.             printFinalTable1();
  699.         }
  700.         */
  701.         private void printTable1()
  702.         {
  703.             Console.Write("Basis   ");
  704.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  705.             {
  706.                 Console.Write($"x{i + 1,-4}");
  707.             }
  708.             Console.Write("Bi   ");
  709.             Console.WriteLine("Bi/mainColumn");
  710.             for (int i = 0; i < array.GetLength(0); i++)
  711.             {
  712.                 if (i < basis.Count)
  713.                 {
  714.                     Console.Write($"X{basis[i]}     ");
  715.                 }
  716.                 else
  717.                 {
  718.                     Console.Write("{0,-8}", "Q");
  719.                 }
  720.                 for (int j = 0; j < array.GetLength(1); j++)
  721.                 {
  722.                     Console.Write($"{array[i, j]:g2}    ");
  723.                 }
  724.                 if (i < assistCof.Length)
  725.                 {
  726.                     if (assistCof[i] < 0)
  727.                     {
  728.                         Console.WriteLine($"<0");
  729.                     }
  730.                     else
  731.                     {
  732.                         Console.WriteLine($"{assistCof[i]}");
  733.                     }
  734.                 }
  735.                 else
  736.                 {
  737.                     Console.WriteLine();
  738.                 }
  739.             }
  740.         }
  741.         private void printFinalTable1()
  742.         {
  743.             Console.Write("Basis   ");
  744.             for (int i = 0; i < array.GetLength(1) - 1; i++)
  745.             {
  746.                 Console.Write($"x{i + 1,-4}");
  747.             }
  748.             Console.WriteLine("Bi");
  749.             for (int i = 0; i < array.GetLength(0); i++)
  750.             {
  751.                 if (i < basis.Count)
  752.                 {
  753.                     Console.Write($"X{basis[i],-7}");
  754.                 }
  755.                 else
  756.                 {
  757.                     Console.Write("{0,-8}", "Q");
  758.                 }
  759.                 for (int j = 0; j < array.GetLength(1); j++)
  760.                 {
  761.                     Console.Write($"{array[i, j],-5}");
  762.                 }
  763.                 Console.WriteLine();
  764.             }
  765.             double[] answer = new double[array.GetLength(1) - 1];
  766.             for (int j = 0; j < answer.Length; j++)
  767.             {
  768.                 if (basis.Contains(j + 1))
  769.                 {
  770.                     int index = basis.IndexOf(j + 1);
  771.                     answer[j] = array[index, array.GetLength(1) - 1];
  772.                 }
  773.                 else
  774.                 {
  775.                     answer[j] = 0;
  776.                 }
  777.             }
  778.             Console.WriteLine("Answer:");
  779.             for (int i = 0; i < answer.Length; i++)
  780.             {
  781.                 Console.Write($"X{i + 1} = {answer[i]}    ");
  782.             }
  783.             Console.WriteLine($"\nQ = {array[array.GetLength(0) - 1, array.GetLength(1) - 1]}");
  784.         }
  785.         /*
  786.         private void calcBasis1()
  787.         {
  788.             if (basis.Count == 0)
  789.             {
  790.                 for (int i = 0; i < array.GetLength(1); i++)
  791.                 {
  792.                     int zeroCount = 0;
  793.                     double other = -1;
  794.                     for (int j = 0; j < array.GetLength(0)-1; j++)
  795.                     {
  796.                         double cur = array[j, i];
  797.                         if (i == 4)
  798.                         {
  799.                             ;
  800.                         }
  801.                         if (array[j, i] == 0)
  802.                         {
  803.                             zeroCount++;
  804.                         }
  805.                         else other = array[j, i];
  806.                     }
  807.                     if (zeroCount == array.GetLength(0) - 2 && Math.Abs(other - 1) < 1e-3)
  808.                     {
  809.                         basis.Add(i + 1);
  810.                     }
  811.                 }
  812.             }
  813.             else
  814.             {
  815.                 int insertIndex = -1, toInsert = -1;
  816.                 for (int i = 0; i < array.GetLength(1); i++)
  817.                 {
  818.                     int zeroCount = 0;
  819.                     for (int j = 0; j < array.GetLength(0); j++)
  820.                     {
  821.                         if (array[j, i] == 0)
  822.                         {
  823.                             zeroCount++;
  824.                         }
  825.                     }
  826.                     if (zeroCount != array.GetLength(0) - 1 && basis.Contains(i + 1))
  827.                     {
  828.                         insertIndex = basis.IndexOf(i + 1);
  829.                     }
  830.                     else if (zeroCount == array.GetLength(0) - 1 && !basis.Contains(i + 1))
  831.                     {
  832.                         toInsert = i + 1;
  833.                     }
  834.                 }
  835.                 basis.RemoveAt(insertIndex);
  836.                 basis.Insert(insertIndex, toInsert);
  837.             }
  838.         }
  839.         */
  840.     }
  841. }
Advertisement
Add Comment
Please, Sign In to add comment