Muhammad Al-Xorazmiy nomidagi Toshkent axborot texnologiyalari universiteti Algoritmlash va Loyihalash




Download 4,32 Mb.
bet2/2
Sana20.05.2024
Hajmi4,32 Mb.
#246725
1   2
Bog'liq
G`ulomov Kamron

C++ code
#include
#include
#include
#include
using namespace std;
const double EPSILON = 1e-10;
// Simplex algoritmi
vector simplex(vector>& A, vector& b, vector& c) {
int m = b.size(), n = c.size();
vector> tableau(m + 1, vector(n + m + 1));
// Tableauga kiritish
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
tableau[i][j] = A[i][j];
}
tableau[i][n + i] = 1;
tableau[i][n + m] = b[i];
}
// Maqsad funktsiyasini qo'shish
for (int j = 0; j < n; ++j) {
tableau[m][j] = -c[j];
}
// Simplex algoritmini boshlash
while (true) {
// qatlamalardan chiqish
int pivot_col = min_element(tableau[m].begin(), tableau[m].end() - 1) - tableau[m].begin();
if (tableau[m][pivot_col] >= -EPSILON) {
break; // Optimallashtirilgan
}
// pivot ustunni tanlash
int pivot_row = -1;
double min_ratio = numeric_limits::infinity();
for (int i = 0; i < m; ++i) {
if (tableau[i][pivot_col] > EPSILON) {
double ratio = tableau[i][n + m] / tableau[i][pivot_col];
if (ratio < min_ratio) {
min_ratio = ratio;
pivot_row = i;
}
}
}
if (pivot_row == -1) {
throw logic_error("Infinitiyada resurslar mavjud");
}
// pivot elementni qayta ishlang
for (int j = 0; j <= n + m; ++j) {
if (j != pivot_col) {
tableau[pivot_row][j] /= tableau[pivot_row][pivot_col];
}
}
tableau[pivot_row][pivot_col] = 1;
// qatorlarni yangilash
for (int i = 0; i <= m; ++i) {
if (i != pivot_row) {
for (int j = 0; j <= n + m; ++j) {
if (j != pivot_col) {
tableau[i][j] -= tableau[i][pivot_col] * tableau[pivot_row][j];
}
}
tableau[i][pivot_col] = 0;
}
}
}
// Yechimlarni izlash
vector solution(n);
for (int i = 0; i < m; ++i) {
int one_index = find(tableau[i].begin(), tableau[i].end() - 1, 1) - tableau[i].begin();
if (one_index != n + m) {
solution[one_index] = tableau[i][n + m];
}
}
return solution;
}
int main() {
// Ko'rsatkichlar va shartlar
vector> A = {
{15, 10, 5},
{10, 4, 12},
{4, 15, 10}
};
vector b = {255,216,294};
vector c = {1800,2000,1500}; // Maqsad funktsiyasi
// Simplex algoritmini chaqirish
vector solution = simplex(A, b, c);
// Ekranga chiqarish
cout << "Optimal yechimlar:" << endl;
for (int i = 0; i < solution.size(); ++i) {
cout << "x(" << i+1 << "): " << solution[i] << endl;
}
// Umumiy daromadni hisoblash
double optimal_daromad = 0;
for (int i = 0; i < solution.size(); ++i) {
optimal_daromad += solution[i] * c[i];
}
cout << "Optimal daromad: " << optimal_daromad << endl;
return 0;
}

Download 4,32 Mb.
1   2




Download 4,32 Mb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Muhammad Al-Xorazmiy nomidagi Toshkent axborot texnologiyalari universiteti Algoritmlash va Loyihalash

Download 4,32 Mb.