Skip to content

Commit 7c22f96

Browse files
committed
feat: OT Lab - 4 (initial simplex table generation)
1 parent f2f33f6 commit 7c22f96

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

OT Lab - 4 (16-08-24)/4.cpp

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include <bits/stdc++.h>
2+
#include <iomanip>
3+
using namespace std;
4+
5+
class soe{
6+
public:
7+
vector<double> coe;
8+
double b;
9+
string op;
10+
11+
void make_standard(vector<double> &a, double &bb, string &c){
12+
if(bb<0){
13+
if(c==">=") c = "<=";
14+
else if( c=="<=") c = ">=";
15+
bb = -bb;
16+
for(int i=0; i<a.size(); i++){
17+
a[i] = -a[i];
18+
}
19+
}
20+
}
21+
22+
soe(vector<double> a, double bb, string c){
23+
this->coe = a;
24+
this->b = bb;
25+
this->op = c;
26+
make_standard(coe, b, op);
27+
}
28+
};
29+
30+
double getMat(vector<double> &a, vector<vector<double>> &b, int j){
31+
double sum = 0;
32+
int m = a.size();
33+
for(int i=0; i<m; i++){
34+
sum += a[i]*b[i][j];
35+
}
36+
return sum;
37+
}
38+
39+
int main(){
40+
int n, m;
41+
cin >> n >> m;
42+
bool maximum;
43+
cin >> maximum;
44+
45+
vector<double> coeff(n+m, 0);
46+
for(int i=0; i<n; i++){
47+
cin >> coeff[i];
48+
if(!maximum) coeff[i]*=-1; }
49+
50+
vector<soe> mat;
51+
for(int i=0; i<m; i++){
52+
vector<double> rows(n);
53+
for(int j=0; j<n; j++)
54+
cin >> rows[j];
55+
string c; cin >> c;
56+
double b; cin >> b;
57+
soe eq(rows,b,c);
58+
mat.push_back(eq);
59+
}
60+
61+
vector<int> restricted(n);
62+
for(int i=0; i<n; i++)
63+
cin >> restricted[i];
64+
65+
cout << "Maximize: ";
66+
for(int i=1; i<=n; i++){
67+
cout << (coeff[i-1]<0 ? "- " : "+ ") << abs(coeff[i-1]) << "x" << i << " ";
68+
}
69+
cout << endl << endl;
70+
71+
// char t = 'a';
72+
vector<vector<double>> stf(m, vector<double> (m+n, 0));
73+
int cnt = n;
74+
for(int i=0; i<m; i++){
75+
soe m1 = mat[i];
76+
for(int j=1; j<=n+1; j++){
77+
if(j == n+1){
78+
stf[i][cnt] = 1;
79+
cnt++;
80+
} else {
81+
stf[i][j-1] = m1.coe[j-1];
82+
}
83+
}
84+
}
85+
86+
vector<double> del(m+n, 0);
87+
vector<double> b(m), XB(m), CB(m, 0);
88+
vector<string> B(m);
89+
for(int i=0; i<m; i++){
90+
string s = "x";
91+
s += to_string(n+i+1);
92+
B[i] = s;
93+
}
94+
95+
for(int i=0; i<m; i++){
96+
b[i] = mat[i].b;
97+
XB[i] = mat[i].b;
98+
}
99+
100+
for(int i=0; i<m+n; i++){
101+
double sum = getMat(CB, stf, i) - coeff[i];
102+
del[i] = sum;
103+
}
104+
105+
cout << " ";
106+
for(int i=0; i<m+n; i++){
107+
if(restricted[i] || i > m){
108+
if(coeff[i]>=0) cout << " ";
109+
cout << coeff[i] << " ";
110+
} else {
111+
if(coeff[i]>=0) cout << " ";
112+
cout << coeff[i] << " ";
113+
if(coeff[i]<0) cout << " ";
114+
cout << -coeff[i] << " ";
115+
}
116+
}
117+
cout << endl;
118+
119+
cout << "XB ";
120+
cout << "CB ";
121+
for(int i=1; i<=n+m; i++){
122+
if(restricted[i-1] || i >= n)
123+
cout << "x" << i << " ";
124+
else{
125+
cout << "x" << i << "' x" << i << "* ";
126+
}
127+
}
128+
cout << "b" << endl;
129+
130+
for(int i=0; i<m; i++){
131+
cout << "x" << n+i+1 << " ";
132+
cout << " " << 0 << " ";
133+
for(int j=0; j<n+m; j++){
134+
if(stf[i][j]>=0) cout << " ";
135+
if(restricted[j] || j > m){
136+
cout << stf[i][j] << " ";
137+
} else {
138+
cout << stf[i][j] << " ";
139+
if(stf[i][j]<0) cout << " ";
140+
cout << -stf[i][j] << " ";
141+
}
142+
}
143+
cout << b[i] << endl;
144+
}
145+
cout << " ";
146+
147+
for(int i=0; i<m+n; i++){
148+
if(restricted[i] || i > m){
149+
if(del[i]>=0) cout << " ";
150+
cout << del[i] << " ";
151+
} else {
152+
if(del[i]>=0) cout << " ";
153+
cout << del[i] << " ";
154+
if(del[i]<0) cout << " ";
155+
cout << -del[i] << " ";
156+
}
157+
}
158+
cout << " " << endl;
159+
160+
return 0;
161+
}
85.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)