123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Simple_method
- {
- internal class MethodClass
- {
- int kolOgr, kolPer;
- public MethodClass(int kolOgr, int kolPer)
- {
- this.kolOgr = kolOgr;
- this.kolPer = kolPer;
-
- }
- public void input(double[,] Table)
- {
- for (int i = 0; i < Table.GetLength(0); i++)
- {
- if (i == Table.GetLength(0) - 1)
- {
- Console.WriteLine("Введите коэффициенты целевой функции:");
- }
- else
- {
- Console.WriteLine("Введите коэффициенты {0} ограничения:", i+1);
- }
- string enter = Console.ReadLine();
- string[] mas = enter.Split(new char[] { ' ' });
- for (int j = 0; j < mas.Length; j++)
- {
- Table[i, j] = double.Parse(mas[j]);
- }
- }
- }
- public void output(double[,] Table)
- {
- for(int i = 0; i < Table.GetLength(0);i++)
- {
- for(int j = 0; j < Table.GetLength(1);j++)
- {
- Console.Write("{0} \t", Math.Round((double)Table[i,j],3));
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- public void max(double[,] Table)
- {
- int[] basis = new int[kolOgr];
- int b = kolOgr - 1;
- for (int i = 0; i < basis.Length; i++)
- {
- basis[i] = kolPer - b;
- b--;
- }
- while (true)
- {
- double min = 9999;
- int ind = 0;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- if (Table[kolOgr, i] == 0)
- continue;
- if (Table[kolOgr, i] < min)
- {
- min = Table[kolOgr, i];
- ind = i;
- }
- }
- double min1 = 999999, min2 = 99999;
- int indMin = 0;
- for (int i = 0; i < Table.GetLength(0) - 1; i++)
- {
- if (Table[i, kolPer] == 0)
- continue;
- min2 = Table[i, kolPer] / Table[i, ind];
- if (min2 <= 0)
- continue;
- if (min1 > min2)
- {
- min1 = min2;
- indMin = i;
- }
- }
- double delitel = Table[indMin, ind];
- basis[indMin] = ind;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- Table[indMin, i] = Table[indMin, i] / delitel;
- }
- for (int i = 0; i < Table.GetLength(0); i++)
- {
- double del2 = Table[i, ind];
- if (i == indMin)
- continue;
- for (int j = 0; j < Table.GetLength(1); j++)
- {
- Table[i, j] = Table[indMin, j] * (-del2) + Table[i, j];
- }
- }
- bool flag = true;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- if (Table[kolOgr, i] < 0)
- {
- flag = false;
- break;
- }
- }
- if(flag)
- {
- break;
- }
- }
- for (int i = 0; i < Table.GetLength(0) - 1; i++)
- {
- if (basis[i] > kolOgr)
- continue;
- else
- Console.WriteLine("x" + (basis[i] + 1) + " = " + Table[i, kolPer]);
- }
- Console.WriteLine("Значение целевой функции: " + Table[kolOgr, kolPer]);
- }
- public void min(double[,] Table)
- {
- int[] basis = new int[kolOgr];
- int b = kolOgr-1;
- for (int i = 0; i < basis.Length;i++)
- {
-
- basis[i] = kolPer - b;
- b--;
- }
- while (true)
- {
-
- double max = -9999;
- int ind = 0;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- if (Table[kolOgr, i] == 0)
- continue;
- if (Table[kolOgr, i] > max)
- {
- max = Table[kolOgr, i];
- ind = i;
- }
- }
- double min1 = 999999, min2 = 99999;
- int indMin = 0;
- for (int i = 0; i < Table.GetLength(0) - 1; i++)
- {
- if (Table[i, kolPer] == 0)
- continue;
- min2 = Table[i, kolPer] / Table[i, ind];
- if (min2 <= 0)
- continue;
- if (min1 > min2)
- {
- min1 = min2;
- indMin = i;
- }
- }
- double delitel = Table[indMin, ind];
- basis[indMin] = ind;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- Table[indMin, i] = Table[indMin, i] / delitel;
- }
- for (int i = 0; i < Table.GetLength(0); i++)
- {
- double del2 = Table[i, ind];
- if (i == indMin)
- continue;
- for (int j = 0; j < Table.GetLength(1); j++)
- {
- Table[i, j] = Table[indMin, j] * (-del2) + Table[i, j];
- }
- }
- bool flag = true;
- for (int i = 0; i < Table.GetLength(1); i++)
- {
- if (Table[kolOgr, i] > 0)
- {
- flag = false;
- break;
- }
- }
- if (flag)
- {
- break;
- }
- }
- for(int i = 0; i < Table.GetLength(0)-1; i++)
- {
- if (basis[i] > kolOgr)
- continue;
- else
- Console.WriteLine("x" + (basis[i] + 1) + " = " + Table[i, kolPer]);
- }
- Console.WriteLine("Значение целевой функции: "+ Table[kolOgr,kolPer]);
- }
- }
- }
|