Skip to content

Commit 9842566

Browse files
committedAug 28, 2024
feat: OT Lab - 5 (Implement Simplex algorithm for linear programming problem)
1 parent 7c22f96 commit 9842566

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
 

‎OT Lab - 5 (23-08-24)/5.cpp

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class Simplex {
5+
private:
6+
int n, m;
7+
vector<vector<double>> A;
8+
vector<double> c;
9+
vector<double> b;
10+
vector<double> CB;
11+
vector<double> B;
12+
vector<double> delta;
13+
14+
public:
15+
Simplex(int variables, int constraints) : n(variables), m(constraints), A(constraints, vector<double> (variables + constraints)), c(variables + constraints), b(constraints), CB(constraints), B(constraints), delta(variables + constraints) {}
16+
17+
void readInput() {
18+
// cout << "Enter the coefficients of the objective function: ";
19+
for (int i = 0; i < n; i++) {
20+
cin >> c[i];
21+
}
22+
23+
// cout << "Enter the coefficients of the constraints:" << endl;
24+
for (int i = 0; i < m; i++) {
25+
for (int j = 0; j < n; j++) {
26+
cin >> A[i][j];
27+
}
28+
}
29+
30+
// cout << "Enter the right-hand side of the constraints: ";
31+
for (int i = 0; i < m; i++) {
32+
cin >> b[i];
33+
}
34+
35+
for (int i = 0; i < m; i++) {
36+
A[i][n + i] = 1.0;
37+
c[n + i] = 0.0;
38+
B[i] = n + i;
39+
CB[i] = 0.0;
40+
}
41+
}
42+
43+
void computeDelta() {
44+
for (int j = 0; j < n + m; j++) {
45+
double sum = 0.0;
46+
for (int i = 0; i < m; i++) {
47+
sum += CB[i] * A[i][j];
48+
}
49+
delta[j] = sum - c[j];
50+
}
51+
}
52+
53+
int findEnteringVariable() {
54+
int entering = -1;
55+
for (int j = 0; j < n + m; j++) {
56+
if (delta[j] < 0) {
57+
if (entering == -1 || delta[j] < delta[entering]) {
58+
entering = j;
59+
}
60+
}
61+
}
62+
return entering;
63+
}
64+
65+
int findLeavingVariable(int entering) {
66+
int leaving = -1;
67+
double minRatio = 1e18;
68+
for (int i = 0; i < m; i++) {
69+
if (A[i][entering] > 0) {
70+
double ratio = b[i] / A[i][entering];
71+
if (ratio < minRatio) {
72+
minRatio = ratio;
73+
leaving = i;
74+
}
75+
}
76+
}
77+
return leaving;
78+
}
79+
80+
void pivot(int entering, int leaving) {
81+
double pivotElement = A[leaving][entering];
82+
83+
for (int j = 0; j < n + m; j++) {
84+
A[leaving][j] /= pivotElement;
85+
}
86+
b[leaving] /= pivotElement;
87+
88+
for (int i = 0; i < m; i++) {
89+
if (i != leaving) {
90+
double factor = A[i][entering];
91+
for (int j = 0; j < n + m; j++) {
92+
A[i][j] -= factor * A[leaving][j];
93+
}
94+
b[i] -= factor * b[leaving];
95+
}
96+
}
97+
98+
B[leaving] = entering;
99+
CB[leaving] = c[entering];
100+
}
101+
102+
void printTable() {
103+
cout << "Simplex Table:" << endl;
104+
cout << "\tXB" << "\tCB";
105+
for (int j = 0; j < n + m; j++) {
106+
cout << "\tX" << j + 1;
107+
}
108+
cout << "\tb" << endl;
109+
110+
for (int i = 0; i < m; i++) {
111+
cout << "\tX" << B[i] + 1 << "\t" << CB[i];
112+
for (int j = 0; j < n + m; j++) {
113+
cout << "\t" << A[i][j];
114+
}
115+
cout << "\t" << b[i] << endl;
116+
}
117+
118+
cout << "Δj:\t\t";
119+
for (int j = 0; j < n + m; j++) {
120+
cout << "\t" << delta[j];
121+
}
122+
cout << endl << endl;
123+
}
124+
125+
void solve() {
126+
while (true) {
127+
computeDelta();
128+
printTable();
129+
130+
int entering = findEnteringVariable();
131+
if (entering == -1) {
132+
cout << "Optimal solution found." << endl;
133+
break;
134+
}
135+
136+
int leaving = findLeavingVariable(entering);
137+
if (leaving == -1) {
138+
cout << "Problem is unbounded." << endl;
139+
break;
140+
}
141+
142+
pivot(entering, leaving);
143+
}
144+
145+
cout << "Final Solution:" << endl;
146+
for (int i = 0; i < m; i++) {
147+
cout << "X" << B[i] + 1 << " = " << b[i] << endl;
148+
}
149+
double optimalValue = 0;
150+
for (int i = 0; i < m; i++) {
151+
optimalValue += CB[i] * b[i];
152+
}
153+
cout << "Optimal value of the objective function: " << optimalValue << endl;
154+
}
155+
};
156+
157+
int main() {
158+
int n,m;
159+
// cout << "Enter the number of variables: ";
160+
cin >> n;
161+
// cout << "Enter the number of constraints: ";
162+
cin >> m;
163+
164+
Simplex simplex(n, m);
165+
simplex.readInput();
166+
simplex.solve();
167+
168+
return 0;
169+
}
Binary file not shown.

0 commit comments

Comments
 (0)