Advertisement
baadgeorge

Untitled

Dec 19th, 2021
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. //Симплекс метод
  8. void SimplexMethod(double basis[4]);
  9.  
  10. int main()
  11. {
  12.     setlocale(LC_ALL, "RUSSIAN");
  13.     double basis[4] = { 0, 0, 0, 0 }; //базисные переменные
  14.     SimplexMethod(basis);
  15.     system("pause");
  16.     return 0;
  17. }
  18. //Симплекс метод
  19. void SimplexMethod(double basis[4])
  20. {
  21.     //Заполнение массивов исходных функций
  22.     double function[4] = { -1, 3, -2, 1 };      // исходная функция
  23.     double FirstLimit[5] = { -1, 5, 0, 1, 2 };  // первое ограничение
  24.     double SecondLimit[5] = { 2, -1, 1, 0, 5 };     // второе ограничение
  25.     double SymplexDifference[5] = { 0, 0, 0, 0, 0 }; // симплекс разности
  26.     //Определение переменных метода
  27.     double x = -10000; //базисные переменные
  28.     double y = -10000;
  29.     int j = 0;
  30.     int base_index1 = 0; //номера базисных переменных
  31.     int base_index2 = 0;
  32.     int task = 0;   // номер выбранной задачи
  33.     int check = 1;  // проверка
  34.  
  35.     double MAXSymplexDiff = -1000; // макс. симплекс разности
  36.     double MINSymplexDiff = 1000; // мин. симплекс разности
  37.  
  38.     double lead_item = 0; //ведущий элемент
  39.     double lead_columnitem = 0; //элемент ведущего столбца
  40.  
  41.     int lead_columnindex = 0;   // номер ведущего столбца
  42.     int lead_rowindex = 0;  // номер ведущей строки
  43.  
  44.     int n = 4; // число переменных
  45.     int m = 2; // число ограничений
  46.     cout << "Число свободных переменных определяем как n - m:\n\t " << n - m << endl;
  47.     cout << "Число базисных переменных, которое равняется числу ограничений:\n\t " << m << endl << endl;
  48.     //Определяем базисные переменные
  49.     for (j = 0; j < 4; j++)
  50.     {
  51.         if (SecondLimit[j] == 0 && FirstLimit[j] != 0)
  52.         {
  53.             //поиск значения базвой переменнной
  54.             x = FirstLimit[4] / FirstLimit[j];
  55.             basis[j] = x;
  56.             base_index1 = j;
  57.             cout << "\tНайдена базисная переменная: x" << j + 1 << " = " << basis[j] << endl;
  58.         }
  59.         if (FirstLimit[j] == 0 && SecondLimit[j] != 0)
  60.         {
  61.             x = SecondLimit[4] / SecondLimit[j];
  62.             basis[j] = x;
  63.             base_index2 = j;
  64.             cout << "\tНайдена базисная переменная: x" << j + 1 << " = " << basis[j] << endl;
  65.         }
  66.     }
  67.     cout << endl;
  68.  
  69.     x = -10000;
  70.     cout << "Симплекс таблица\n" << endl;
  71.     cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
  72.     cout << setw(10) << function[base_index2] << setw(5) << "x" << base_index2 + 1 << setw(10) << basis[base_index2] << setw(10) << FirstLimit[0] << setw(10) << FirstLimit[1] << setw(10) << FirstLimit[2] << setw(10) << FirstLimit[3] << endl;
  73.     cout << setw(10) << function[base_index1] << setw(5) << "x" << base_index1 + 1 << setw(10) << basis[base_index1] << setw(10) << SecondLimit[0] << setw(10) << SecondLimit[1] << setw(10) << SecondLimit[2] << setw(10) << SecondLimit[3] << endl;
  74.     //подсчет симплекс разности
  75.     SymplexDifference[0] = 0 - basis[base_index1] * function[base_index1] - basis[base_index2] * function[base_index2];
  76.     cout << setw(22) << "Симплекс разности" << setw(4) << SymplexDifference[0];
  77.     for (j = 0; j < 4; j++)
  78.     {
  79.         SymplexDifference[j + 1] = function[j] - FirstLimit[j] * function[base_index1] - SecondLimit[j] * function[base_index2];
  80.         cout << setw(10) << SymplexDifference[j + 1];
  81.     }
  82.     cout << "\n\n!Введите нужный вариант поиска:\n1) Поиск максимума функции;\n2) Поиск минимума функции;" << endl;
  83.     cin >> task;
  84.  
  85.     if ((task != 1) && (task != 2))
  86.     {
  87.         cout << "Ошибка! Неверный номер задачи\n";
  88.         return;
  89.     }
  90.  
  91.     // поиск максимума или минимума функции
  92.     while (check > 0)
  93.     {
  94.         check = 0;
  95.         if (task == 1)
  96.         {
  97.             MAXSymplexDiff = 0;
  98.             //поиск max симлекс разности
  99.             for (j = 1; j < 5; j++)
  100.             {
  101.                 if (SymplexDifference[j] > MAXSymplexDiff)
  102.                 {
  103.                     MAXSymplexDiff = SymplexDifference[j];
  104.                     lead_columnindex = j;
  105.                 }
  106.             }
  107.         }
  108.         else
  109.         {
  110.             MINSymplexDiff = 1000;
  111.             //поиск min симлекс разности
  112.             for (j = 1; j < 5; j++)
  113.             {
  114.                 if (MINSymplexDiff > SymplexDifference[j])
  115.                 {
  116.                     MINSymplexDiff = SymplexDifference[j];
  117.                     lead_columnindex = j;
  118.                 }
  119.             }
  120.         }
  121.         //определение ведущей строки
  122.         if (basis[base_index2] / FirstLimit[lead_columnindex - 1] > 0 && FirstLimit[lead_columnindex - 1] != 0)
  123.             x = basis[base_index2] / FirstLimit[lead_columnindex - 1];
  124.         if (basis[base_index1] / SecondLimit[lead_columnindex - 1] > 0 && SecondLimit[lead_columnindex - 1] != 0)
  125.             y = basis[base_index1] / SecondLimit[lead_columnindex - 1];
  126.         if ((x > y && y > 0) || x < 0)
  127.         {
  128.             lead_rowindex = 2;
  129.             lead_item = SecondLimit[lead_columnindex - 1];
  130.         }
  131.         if ((x < y && x >0) || y < 0)
  132.         {
  133.             lead_rowindex = 1;
  134.             lead_item = FirstLimit[lead_columnindex - 1];
  135.         }
  136.         //определение ведущего элемента и пересчет коэффициентов ведущей строки
  137.         if (lead_rowindex == 1)
  138.         {
  139.             //добавление нового базового элемента
  140.             basis[lead_columnindex - 1] = basis[base_index1] / lead_item;
  141.             //обновление ведущей строки
  142.             for (j = 0; j < 4; j++)
  143.                 FirstLimit[j] = FirstLimit[j] / lead_item;
  144.             lead_columnitem = SecondLimit[lead_columnindex - 1];
  145.             //пересчёт второй строки симплекс таблицы
  146.             basis[base_index2] = basis[lead_columnindex - 1] * (-SecondLimit[lead_columnindex - 1]) + basis[base_index2];
  147.             for (j = 0; j < 4; j++)
  148.                 SecondLimit[j] = FirstLimit[j] * (-lead_columnitem) + SecondLimit[j];
  149.             //пересчёт строки симплекс разности и запоминание элемента ведущего столбца
  150.             lead_columnitem = SymplexDifference[lead_columnindex];
  151.             SymplexDifference[0] = basis[lead_columnindex - 1] * (-lead_columnitem) + SymplexDifference[0];
  152.             for (j = 0; j < 4; j++)
  153.                 SymplexDifference[j + 1] = FirstLimit[j] * (-lead_columnitem) + SymplexDifference[j + 1];
  154.             //удаление старого базисного элемента из массива
  155.             basis[base_index1] = 0;
  156.             base_index1 = lead_columnindex - 1;
  157.             cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
  158.             cout << setw(10) << function[base_index2] << setw(5) << "x" << base_index2 + 1 << setw(10) << basis[base_index2] << setw(10) << FirstLimit[0] << setw(10) << FirstLimit[1] << setw(10) << FirstLimit[2] << setw(10) << FirstLimit[3] << endl;
  159.             cout << setw(10) << function[base_index1] << setw(5) << "x" << base_index1 + 1 << setw(10) << basis[base_index1] << setw(10) << SecondLimit[0] << setw(10) << SecondLimit[1] << setw(10) << SecondLimit[2] << setw(10) << SecondLimit[3] << endl;
  160.             cout << setw(17) << "Симплекс разности" << setw(9);
  161.             //проверка на последующую итерацию
  162.             if (task == 1)
  163.             {
  164.                 for (j = 0; j < 5; j++)
  165.                 {
  166.                     cout << SymplexDifference[j] << setw(10);
  167.                     if (SymplexDifference[j] > 0)
  168.                         check++;
  169.                 }
  170.                 cout << "\n\n\n";
  171.             }
  172.             else
  173.             {
  174.                 for (j = 0; j < 5; j++)
  175.                 {
  176.                     cout << SymplexDifference[j] << setw(10);
  177.                     if (SymplexDifference[j] < 0)
  178.                         check++;
  179.                 }
  180.                 cout << "\n\n\n";
  181.             }
  182.         }
  183.         if (lead_rowindex == 2)
  184.         {
  185.             //добавление нового базового элемента
  186.             basis[lead_columnindex - 1] = basis[base_index2] / SecondLimit[lead_columnindex - 1];
  187.             //обновление ведущей строки
  188.             for (j = 0; j < 4; j++)
  189.                 SecondLimit[j] = SecondLimit[j] / lead_item;
  190.             lead_columnitem = FirstLimit[lead_columnindex - 1];
  191.             //пересчёт первой строки симплекс таблицы
  192.             basis[base_index1] = basis[lead_columnindex - 1] * (-lead_columnitem) + basis[base_index1];
  193.             for (j = 0; j < 4; j++)
  194.                 FirstLimit[j] = SecondLimit[j] * (-lead_columnitem) + FirstLimit[j];
  195.             //пересчёт строки симплекс разности и запоминание элемента ведущего столбца
  196.             lead_columnitem = SymplexDifference[lead_columnindex];
  197.             SymplexDifference[0] = basis[lead_columnindex - 1] * (-lead_columnitem) + SymplexDifference[0];
  198.             for (j = 0; j < 4; j++)
  199.                 SymplexDifference[j + 1] = SecondLimit[j] * (-lead_columnitem) + SymplexDifference[j + 1];
  200.             //удаление старого базисного элемента из массива
  201.             basis[base_index2] = 0;
  202.             base_index2 = lead_columnindex - 1;
  203.             cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
  204.             cout << setw(10) << function[base_index2] << setw(5) << "x" << base_index2 + 1 << setw(10) << basis[base_index2] << setw(10) << FirstLimit[0] << setw(10) << FirstLimit[1] << setw(10) << FirstLimit[2] << setw(10) << FirstLimit[3] << endl;
  205.             cout << setw(10) << function[base_index1] << setw(5) << "x" << base_index1 + 1 << setw(10) << basis[base_index1] << setw(10) << SecondLimit[0] << setw(10) << SecondLimit[1] << setw(10) << SecondLimit[2] << setw(10) << SecondLimit[3] << endl;
  206.             cout << setw(17) << "Симплекс разности" << setw(9);
  207.             for (j = 0; j < 5; j++)
  208.                 cout << SymplexDifference[j] << setw(10);
  209.             cout << "\n\n\n";
  210.             //проверка на последующую итерацию
  211.             if (task == 1)
  212.             {
  213.                 for (j = 1; j < 5; j++)
  214.                     if (SymplexDifference[j] > 0)
  215.  
  216.                         check++;
  217.                 cout << endl;
  218.             }
  219.             else
  220.             {
  221.                 for (j = 1; j < 5; j++)
  222.                     if (SymplexDifference[j] < 0)
  223.                         check++;
  224.                 cout << endl;
  225.  
  226.             }
  227.         }
  228.     }
  229.  
  230.     if (task == 1)
  231.     {
  232.         cout << "Максимум функции = " << -SymplexDifference[0];
  233.         cout << "\t в точке ( ";
  234.         for (j = 0; j < 4; j++)
  235.         {
  236.             if (j != 3)
  237.                 cout << basis[j] << ", ";
  238.             else
  239.                 cout << basis[j] << " ";
  240.         }
  241.         cout << ")" << endl;
  242.     }
  243.     if (task == 2)
  244.     {
  245.         cout << "Минимум функции = " << -SymplexDifference[0];
  246.         cout << "\t в точке ( ";
  247.         for (j = 0; j < 4; j++)
  248.         {
  249.             if (j != 3)
  250.                 cout << basis[j] << ", ";
  251.             else
  252.                 cout << basis[j] << " ";
  253.         }
  254.         cout << ")" << endl;
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement