-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplex-method.cpp
206 lines (188 loc) · 6.46 KB
/
simplex-method.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using namespace std;
void printsolution(string* varnames, double* solutions, int nsolutions, stringstream &buffer) {
buffer << endl;
for (int i = 0; i < nsolutions; i++) {
buffer << varnames[i] << " = " << solutions[i] << endl;
}
}
double* read_simplex(double** tableau, int width, int height) {
double* solutions = new double[width - 1];
int test = 0;
for(int i = 0; i < width - 1; i++) {
bool foundnonzeroval = false;
bool nonbasic = false;
int basicvalindex;
for (int j = 0; j < height; j++) {
if (tableau[i][j] != 0) {
if (tableau[i][j] == 1 && foundnonzeroval == false) {
foundnonzeroval = true;
basicvalindex = j;
}
else {
solutions[i] = 0;
nonbasic = true;
break;
}
}
}
if (nonbasic == false)
{
solutions[i] = tableau[width - 1][basicvalindex];
}
}
return solutions;
}
void printtableau(double** tableau, int width, int height) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << tableau[j][i] << " ";
}
cout << endl;
}
cout << endl << endl << endl;
}
int get_pivot_column(double** tableau, int width, bool maximise) {
int val = 0;
int valindex = -1;
for (int i = 1; i < width - 1; i++) {
if (tableau[i][0] < val && maximise == true) {
val = tableau[i][0];
valindex = i;
}
if (tableau[i][0] > val && maximise == false) {
val = tableau[i][0];
valindex = i;
}
}
return valindex;
}
int get_pivot_row(double** tableau, int pivotcolumnindex, int height, int rhscolumnindex, int startingrowindex) {
//second row down must be constraints
//only first row is objectiev function, therefore strip second stage when applicable
int minratioindex;
double minratio;
bool minratiodefined = false;
for (int i = startingrowindex; i < height; i++) {
if (tableau[pivotcolumnindex][i] == 0) {
continue;
}
double ratio = tableau[rhscolumnindex][i] / tableau[pivotcolumnindex][i];
if (ratio < 0) {
continue;
}
if (minratiodefined == false) {
minratio = ratio;
minratioindex = i;
minratiodefined = true;
}
if (ratio < minratio) {
minratio = ratio;
minratioindex = i;
}
}
return minratioindex; //minratioindex == minpivotrowindex is true
}
double** divide_pivot_row(double** tableau, int pivotcolumnindex, int pivotrowindex, int width) {
double multiplier = tableau[pivotcolumnindex][pivotrowindex];
for (int i = 0; i < width; i++) {
tableau[i][pivotrowindex] /= multiplier;
}
return tableau;
}
double** perform_iteration(double** tableau, int pivotcolumnindex, int pivotrowindex, int width, int height) {
// i represents the i th row and j represents the j th column
tableau = divide_pivot_row(tableau, pivotcolumnindex, pivotrowindex, width);
for (int i = 0; i < height; i++) {
if (i == pivotrowindex) {
continue;
}
double multiplier = -1 * tableau[pivotcolumnindex][i] / tableau[pivotcolumnindex][pivotrowindex];
for (int j = 0; j < width; j++) {
tableau[j][i] += multiplier * tableau[j][pivotrowindex];
}
}
return tableau;
}
double** maximise_objective_function(double** tableau, int width, int height, int startingrowindex) {
cout << "maximising:" << endl;
bool maximised = false;
while(maximised == false) {
printtableau(tableau, width, height);
int pivotcolumnindex = get_pivot_column(tableau, width, true);
cout << "pivot column index: " << pivotcolumnindex << endl;
if (pivotcolumnindex == -1) {
maximised = true;
continue;
//maximised
}
int pivotrowindex = get_pivot_row(tableau, pivotcolumnindex, height, width - 1, startingrowindex);
cout << "pivot row index: " << pivotrowindex << endl;
tableau = perform_iteration(tableau, pivotcolumnindex, pivotrowindex, width, height);
}
return tableau;
}
double** minimise_objective_function(double** tableau, int width, int height, int startingrowindex) {
cout << "minimising:" << endl;
bool minimised = false;
while(minimised == false) {
printtableau(tableau, width, height);
int pivotcolumnindex = get_pivot_column(tableau, width, false);
cout << "pivot column index: " << pivotcolumnindex << endl;
if (pivotcolumnindex == -1) {
minimised = true;
continue;
//maximised
}
int pivotrowindex = get_pivot_row(tableau, pivotcolumnindex, height, width - 1, startingrowindex);
cout << "pivot row index: " << pivotrowindex << endl;
tableau = perform_iteration(tableau, pivotcolumnindex, pivotrowindex, width, height);
}
return tableau;
}
double** convert_to_standard_form(double** tableau,int &width, int &height, int nartificialvar) {
cout << "width: " << width << endl;
for (int i = 0; i < width; i++) {
//delete &tableau[i][0];
//cout << "works" << endl;
for (int j = 0; j < height - 1; j++) {
//cout << j << endl;
//cout << tableau[i][j] << " " << tableau[i][j + 1] << endl;
tableau[i][j] = tableau[i][j + 1];
}
}
//cout << "works" << endl;
height -= 1;
for (int i = 0; i < height; i++) { // move RHS column to first art variable then change width index to effectively remove all art var
tableau[width - 1 - nartificialvar][i] = tableau[width - 1][i];
}
width -= nartificialvar;
for (int j = 0; j < height; j++) // remove art var "A" at beginning
{
for (int i = 0; i < width; i++)
{
tableau[i][j] = tableau[i+1][j]; //need to shift all var back by 1 along row (horizontally)
}
}
width -= 1; // move index
return tableau;
}
double** apply_simplex(double** tableau, int nvar, int nconstraints, int nslackvar, int nartificialvar, int startingrowindex, string* varnames, stringstream &buffer) {
int height;
int width = nvar + nslackvar + nartificialvar + 2;
if (nartificialvar != 0) {
width++;
height = nconstraints + 2;
tableau = minimise_objective_function(tableau, width, height, startingrowindex);
//delete first row and artificial variable columns
tableau = convert_to_standard_form(tableau, width, height, nartificialvar);
tableau = maximise_objective_function(tableau, width, height, 1);
}
else {
height = nconstraints + 1;
tableau = maximise_objective_function(tableau, width, height, startingrowindex);
}
double* solutions = read_simplex(tableau, width, height);
cout << endl << "solutions[2]: " << solutions[2] << endl;
printsolution(varnames, solutions, width-1, buffer);
return tableau;
}