Advertisement
AlukardBF

Simplex

Dec 4th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 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 Simplex
  8. {
  9.     public class Simplex
  10.     {
  11.         //source - симплекс таблица без базисных переменных
  12.         double[,] table; //симплекс таблица
  13.  
  14.         int m, n;
  15.  
  16.         List<int> basis; //список базисных переменных
  17.  
  18.         public Simplex(double[,] source)
  19.         {
  20.             m = source.GetLength(0);
  21.             n = source.GetLength(1);
  22.             table = new double[m, n + m - 1];
  23.             basis = new List<int>();
  24.  
  25.             for (int i = 0; i < m; i++)
  26.             {
  27.                 for (int j = 0; j < table.GetLength(1); j++)
  28.                 {
  29.                     if (j < n)
  30.                         table[i, j] = source[i, j];
  31.                     else
  32.                         table[i, j] = 0;
  33.                 }
  34.                 //выставляем коэффициент 1 перед базисной переменной в строке
  35.                 if ((n + i) < table.GetLength(1))
  36.                 {
  37.                     table[i, n + i] = 1;
  38.                     basis.Add(n + i);
  39.                 }
  40.             }
  41.  
  42.             n = table.GetLength(1);
  43.         }
  44.  
  45.         //result - в этот массив будут записаны полученные значения X
  46.         public double[,] Calculate(double[] result)
  47.         {
  48.             int mainCol, mainRow; //ведущие столбец и строка
  49.  
  50.             while (!IsItEnd())
  51.             {
  52.                 mainCol = findMainCol();
  53.                 mainRow = findMainRow(mainCol);
  54.                 basis[mainRow] = mainCol;
  55.  
  56.                 double[,] new_table = new double[m, n];
  57.  
  58.                 for (int j = 0; j < n; j++)
  59.                     new_table[mainRow, j] = table[mainRow, j] / table[mainRow, mainCol];
  60.  
  61.                 for (int i = 0; i < m; i++)
  62.                 {
  63.                     if (i == mainRow)
  64.                         continue;
  65.  
  66.                     for (int j = 0; j < n; j++)
  67.                         new_table[i, j] = table[i, j] - table[i, mainCol] * new_table[mainRow, j];
  68.                 }
  69.                 table = new_table;
  70.             }
  71.  
  72.             //заносим в result найденные значения X
  73.             for (int i = 0; i < result.Length - 1; i++)
  74.             {
  75.                 int k = basis.IndexOf(i + 1);
  76.                 if (k != -1)
  77.                     result[i] = table[k, 0];
  78.                 else
  79.                     result[i] = 0;
  80.             }
  81.             //Значение функции
  82.             result[result.Length - 1] = table[m - 1, 0];
  83.             return table;
  84.         }
  85.  
  86.         private bool IsItEnd()
  87.         {
  88.             bool flag = true;
  89.  
  90.             for (int j = 1; j < n; j++)
  91.             {
  92.                 if (table[m - 1, j] < 0)
  93.                 {
  94.                     flag = false;
  95.                     break;
  96.                 }
  97.             }
  98.  
  99.             return flag;
  100.         }
  101.  
  102.         private int findMainCol()
  103.         {
  104.             int mainCol = 1;
  105.  
  106.             for (int j = 2; j < n; j++)
  107.                 if (table[m - 1, j] < table[m - 1, mainCol])
  108.                     mainCol = j;
  109.  
  110.             return mainCol;
  111.         }
  112.  
  113.         private int findMainRow(int mainCol)
  114.         {
  115.             int mainRow = 0;
  116.  
  117.             for (int i = 0; i < m - 1; i++)
  118.                 if (table[i, mainCol] > 0)
  119.                 {
  120.                     mainRow = i;
  121.                     break;
  122.                 }
  123.  
  124.             for (int i = mainRow + 1; i < m - 1; i++)
  125.                 if ((table[i, mainCol] > 0) && ((table[i, 0] / table[i, mainCol]) < (table[mainRow, 0] / table[mainRow, mainCol])))
  126.                     mainRow = i;
  127.  
  128.             return mainRow;
  129.         }
  130.     }
  131.  
  132.     class Program
  133.     {
  134.         static void Main(string[] args)
  135.         {
  136.             double[,] table = { {-12, 1, 1},
  137.                                 {8, 1, -4},
  138.                                 {0, 1, 5} };
  139.  
  140.             double[] result = new double[table.GetLength(1)];
  141.             double[,] table_result;
  142.             Simplex S = new Simplex(table);
  143.             table_result = S.Calculate(result);
  144.  
  145.             Console.WriteLine("Исходная таблица:");
  146.             for (int i = 0; i < table.GetLength(0); i++)
  147.             {
  148.                 for (int j = 0; j < table.GetLength(1); j++)
  149.                     Console.Write(table[i, j] + " ");
  150.                 Console.WriteLine();
  151.             }
  152.             Console.WriteLine();
  153.             Console.WriteLine("Cимплекс-таблица:");
  154.             for (int i = 0; i < table_result.GetLength(0); i++)
  155.             {
  156.                 for (int j = 0; j < table_result.GetLength(1); j++)
  157.                     Console.Write(table_result[i, j] + " ");
  158.                 Console.WriteLine();
  159.             }
  160.             Console.ReadLine();
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement