0% found this document useful (0 votes)
2 views3 pages

Simplex

The document contains a revised C++ code implementing the Simplex algorithm for linear programming. It includes functions for data input, pivoting, formula calculation, optimization, and displaying results. The code utilizes the 'using namespace std;' directive to simplify standard library usage.

Uploaded by

nagarajuabhyaas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Simplex

The document contains a revised C++ code implementing the Simplex algorithm for linear programming. It includes functions for data input, pivoting, formula calculation, optimization, and displaying results. The code utilizes the 'using namespace std;' directive to simplify standard library usage.

Uploaded by

nagarajuabhyaas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

simplex

04 August 2024 10:07 PM

Sure, here's the revised C++ code with the `using namespace std;` directive:

```cpp
#include <iostream>
#include <cmath>
#include <vector>

#define CMAX 10 // max. number of variables in economic function


#define VMAX 10 // max. number of constraints

using namespace std;

int NC, NV, NOPTIMAL, P1, P2, XERR;


vector<vector<double>> TS(CMAX, vector<double>(VMAX));

void Data() {
double R1, R2;
char R;
int I, J;
cout << "\n LINEAR PROGRAMMING\n\n";
cout << " MAXIMIZE (Y/N) ? ";
cin >> R;
cout << "\n NUMBER OF VARIABLES OF ECONOMIC FUNCTION ? ";
cin >> NV;
cout << "\n NUMBER OF CONSTRAINTS ? ";
cin >> NC;

R1 = (R == 'Y' || R == 'y') ? 1.0 : -1.0;

cout << "\n INPUT COEFFICIENTS OF ECONOMIC FUNCTION:\n";


for (J = 1; J <= NV; J++) {
cout << " #" << J << " ? ";
cin >> R2;
TS[1][J + 1] = R2 * R1;
}
cout << " Right hand side ? ";
cin >> R2;
TS[1][1] = R2 * R1;

for (I = 1; I <= NC; I++) {


cout << "\n CONSTRAINT #" << I << ":\n";
for (J = 1; J <= NV; J++) {
cout << " #" << J << " ? ";
cin >> R2;
TS[I + 1][J + 1] = -R2;
}
cout << " Right hand side ? ";
cin >> TS[I + 1][1];
}

New Section 1 Page 1


}

cout << "\n\n RESULTS:\n\n";


for (J = 1; J <= NV; J++) TS[0][J + 1] = J;
for (I = NV + 1; I <= NV + NC; I++) TS[I - NV + 1][0] = I;
}

void Pivot();
void Formula();
void Optimize();

void Simplex() {
do {
Pivot();
Formula();
Optimize();
} while (NOPTIMAL == 1);
}

void Pivot() {
double RAP, V, XMAX = 0.0;
for (int J = 2; J <= NV + 1; J++) {
if (TS[1][J] > 0.0 && TS[1][J] > XMAX) {
XMAX = TS[1][J];
P2 = J;
}
}

RAP = 999999.0;
for (int I = 2; I <= NC + 1; I++) {
if (TS[I][P2] >= 0.0) continue;
V = fabs(TS[I][1] / TS[I][P2]);
if (V < RAP) {
RAP = V;
P1 = I;
}
}

V = TS[0][P2];
TS[0][P2] = TS[P1][0];
TS[P1][0] = V;
}

void Formula() {
for (int I = 1; I <= NC + 1; I++) {
if (I == P1) continue;
for (int J = 1; J <= NV + 1; J++) {
if (J == P2) continue;
TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2];
}
}

TS[P1][P2] = 1.0 / TS[P1][P2];


for (int J = 1; J <= NV + 1; J++) {

New Section 1 Page 2


for (int J = 1; J <= NV + 1; J++) {
if (J == P2) continue;
TS[P1][J] *= fabs(TS[P1][P2]);
}
for (int I = 1; I <= NC + 1; I++) {
if (I == P1) continue;
TS[I][P2] *= TS[P1][P2];
}
}

void Optimize() {
XERR = 0;
for (int I = 2; I <= NC + 1; I++) {
if (TS[I][1] < 0.0) {
XERR = 1;
return;
}
}

NOPTIMAL = 0;
for (int J = 2; J <= NV + 1; J++) {
if (TS[1][J] > 0.0) {
NOPTIMAL = 1;
break;
}
}
}

void Results() {
if (XERR != 0) {
cout << " NO SOLUTION.\n";
return;
}

for (int I = 1; I <= NV; I++) {


for (int J = 2; J <= NC + 1; J++) {
if (TS[J][0] != 1.0 * I) continue;
cout << " VARIABLE #" << I << ": " << TS[J][1] << "\n";
}
}
cout << "\n ECONOMIC FUNCTION: " << TS[1][1] << "\n";
}

int main() {
Data();
Simplex();
Results();
return 0;
}
```

This code uses `using namespace std;` to avoid having to prefix the standard library components (like
`cout`, `cin`, `vector`, etc.) with `std::`.

New Section 1 Page 3

You might also like