Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- //Симплекс метод
- void SimplexMethod(double basis[4]);
- int main()
- {
- setlocale(LC_ALL, "RUSSIAN");
- double basis[4] = { 0, 0, 0, 0 }; //базисные переменные
- SimplexMethod(basis);
- system("pause");
- return 0;
- }
- //Симплекс метод
- void SimplexMethod(double basis[4])
- {
- //Заполнение массивов исходных функций
- double function[4] = { -1, 3, -2, 1 }; // исходная функция
- double FirstLimit[5] = { -1, 5, 0, 1, 2 }; // первое ограничение
- double SecondLimit[5] = { 2, -1, 1, 0, 5 }; // второе ограничение
- double SymplexDifference[5] = { 0, 0, 0, 0, 0 }; // симплекс разности
- //Определение переменных метода
- double x = -10000; //базисные переменные
- double y = -10000;
- int j = 0;
- int base_index1 = 0; //номера базисных переменных
- int base_index2 = 0;
- int task = 0; // номер выбранной задачи
- int check = 1; // проверка
- double MAXSymplexDiff = -1000; // макс. симплекс разности
- double MINSymplexDiff = 1000; // мин. симплекс разности
- double lead_item = 0; //ведущий элемент
- double lead_columnitem = 0; //элемент ведущего столбца
- int lead_columnindex = 0; // номер ведущего столбца
- int lead_rowindex = 0; // номер ведущей строки
- int n = 4; // число переменных
- int m = 2; // число ограничений
- cout << "Число свободных переменных определяем как n - m:\n\t " << n - m << endl;
- cout << "Число базисных переменных, которое равняется числу ограничений:\n\t " << m << endl << endl;
- //Определяем базисные переменные
- for (j = 0; j < 4; j++)
- {
- if (SecondLimit[j] == 0 && FirstLimit[j] != 0)
- {
- //поиск значения базвой переменнной
- x = FirstLimit[4] / FirstLimit[j];
- basis[j] = x;
- base_index1 = j;
- cout << "\tНайдена базисная переменная: x" << j + 1 << " = " << basis[j] << endl;
- }
- if (FirstLimit[j] == 0 && SecondLimit[j] != 0)
- {
- x = SecondLimit[4] / SecondLimit[j];
- basis[j] = x;
- base_index2 = j;
- cout << "\tНайдена базисная переменная: x" << j + 1 << " = " << basis[j] << endl;
- }
- }
- cout << endl;
- x = -10000;
- cout << "Симплекс таблица\n" << endl;
- cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
- 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;
- 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;
- //подсчет симплекс разности
- SymplexDifference[0] = 0 - basis[base_index1] * function[base_index1] - basis[base_index2] * function[base_index2];
- cout << setw(22) << "Симплекс разности" << setw(4) << SymplexDifference[0];
- for (j = 0; j < 4; j++)
- {
- SymplexDifference[j + 1] = function[j] - FirstLimit[j] * function[base_index1] - SecondLimit[j] * function[base_index2];
- cout << setw(10) << SymplexDifference[j + 1];
- }
- cout << "\n\n!Введите нужный вариант поиска:\n1) Поиск максимума функции;\n2) Поиск минимума функции;" << endl;
- cin >> task;
- if ((task != 1) && (task != 2))
- {
- cout << "Ошибка! Неверный номер задачи\n";
- return;
- }
- // поиск максимума или минимума функции
- while (check > 0)
- {
- check = 0;
- if (task == 1)
- {
- MAXSymplexDiff = 0;
- //поиск max симлекс разности
- for (j = 1; j < 5; j++)
- {
- if (SymplexDifference[j] > MAXSymplexDiff)
- {
- MAXSymplexDiff = SymplexDifference[j];
- lead_columnindex = j;
- }
- }
- }
- else
- {
- MINSymplexDiff = 1000;
- //поиск min симлекс разности
- for (j = 1; j < 5; j++)
- {
- if (MINSymplexDiff > SymplexDifference[j])
- {
- MINSymplexDiff = SymplexDifference[j];
- lead_columnindex = j;
- }
- }
- }
- //определение ведущей строки
- if (basis[base_index2] / FirstLimit[lead_columnindex - 1] > 0 && FirstLimit[lead_columnindex - 1] != 0)
- x = basis[base_index2] / FirstLimit[lead_columnindex - 1];
- if (basis[base_index1] / SecondLimit[lead_columnindex - 1] > 0 && SecondLimit[lead_columnindex - 1] != 0)
- y = basis[base_index1] / SecondLimit[lead_columnindex - 1];
- if ((x > y && y > 0) || x < 0)
- {
- lead_rowindex = 2;
- lead_item = SecondLimit[lead_columnindex - 1];
- }
- if ((x < y && x >0) || y < 0)
- {
- lead_rowindex = 1;
- lead_item = FirstLimit[lead_columnindex - 1];
- }
- //определение ведущего элемента и пересчет коэффициентов ведущей строки
- if (lead_rowindex == 1)
- {
- //добавление нового базового элемента
- basis[lead_columnindex - 1] = basis[base_index1] / lead_item;
- //обновление ведущей строки
- for (j = 0; j < 4; j++)
- FirstLimit[j] = FirstLimit[j] / lead_item;
- lead_columnitem = SecondLimit[lead_columnindex - 1];
- //пересчёт второй строки симплекс таблицы
- basis[base_index2] = basis[lead_columnindex - 1] * (-SecondLimit[lead_columnindex - 1]) + basis[base_index2];
- for (j = 0; j < 4; j++)
- SecondLimit[j] = FirstLimit[j] * (-lead_columnitem) + SecondLimit[j];
- //пересчёт строки симплекс разности и запоминание элемента ведущего столбца
- lead_columnitem = SymplexDifference[lead_columnindex];
- SymplexDifference[0] = basis[lead_columnindex - 1] * (-lead_columnitem) + SymplexDifference[0];
- for (j = 0; j < 4; j++)
- SymplexDifference[j + 1] = FirstLimit[j] * (-lead_columnitem) + SymplexDifference[j + 1];
- //удаление старого базисного элемента из массива
- basis[base_index1] = 0;
- base_index1 = lead_columnindex - 1;
- cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
- 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;
- 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;
- cout << setw(17) << "Симплекс разности" << setw(9);
- //проверка на последующую итерацию
- if (task == 1)
- {
- for (j = 0; j < 5; j++)
- {
- cout << SymplexDifference[j] << setw(10);
- if (SymplexDifference[j] > 0)
- check++;
- }
- cout << "\n\n\n";
- }
- else
- {
- for (j = 0; j < 5; j++)
- {
- cout << SymplexDifference[j] << setw(10);
- if (SymplexDifference[j] < 0)
- check++;
- }
- cout << "\n\n\n";
- }
- }
- if (lead_rowindex == 2)
- {
- //добавление нового базового элемента
- basis[lead_columnindex - 1] = basis[base_index2] / SecondLimit[lead_columnindex - 1];
- //обновление ведущей строки
- for (j = 0; j < 4; j++)
- SecondLimit[j] = SecondLimit[j] / lead_item;
- lead_columnitem = FirstLimit[lead_columnindex - 1];
- //пересчёт первой строки симплекс таблицы
- basis[base_index1] = basis[lead_columnindex - 1] * (-lead_columnitem) + basis[base_index1];
- for (j = 0; j < 4; j++)
- FirstLimit[j] = SecondLimit[j] * (-lead_columnitem) + FirstLimit[j];
- //пересчёт строки симплекс разности и запоминание элемента ведущего столбца
- lead_columnitem = SymplexDifference[lead_columnindex];
- SymplexDifference[0] = basis[lead_columnindex - 1] * (-lead_columnitem) + SymplexDifference[0];
- for (j = 0; j < 4; j++)
- SymplexDifference[j + 1] = SecondLimit[j] * (-lead_columnitem) + SymplexDifference[j + 1];
- //удаление старого базисного элемента из массива
- basis[base_index2] = 0;
- base_index2 = lead_columnindex - 1;
- cout << setw(26) << 0 << setw(10) << function[0] << setw(10) << function[1] << setw(10) << function[2] << setw(10) << function[3] << endl;
- 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;
- 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;
- cout << setw(17) << "Симплекс разности" << setw(9);
- for (j = 0; j < 5; j++)
- cout << SymplexDifference[j] << setw(10);
- cout << "\n\n\n";
- //проверка на последующую итерацию
- if (task == 1)
- {
- for (j = 1; j < 5; j++)
- if (SymplexDifference[j] > 0)
- check++;
- cout << endl;
- }
- else
- {
- for (j = 1; j < 5; j++)
- if (SymplexDifference[j] < 0)
- check++;
- cout << endl;
- }
- }
- }
- if (task == 1)
- {
- cout << "Максимум функции = " << -SymplexDifference[0];
- cout << "\t в точке ( ";
- for (j = 0; j < 4; j++)
- {
- if (j != 3)
- cout << basis[j] << ", ";
- else
- cout << basis[j] << " ";
- }
- cout << ")" << endl;
- }
- if (task == 2)
- {
- cout << "Минимум функции = " << -SymplexDifference[0];
- cout << "\t в точке ( ";
- for (j = 0; j < 4; j++)
- {
- if (j != 3)
- cout << basis[j] << ", ";
- else
- cout << basis[j] << " ";
- }
- cout << ")" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement