-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplex.h
414 lines (343 loc) · 7.24 KB
/
Simplex.h
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
Copyright(c) 2021 Mbadiwe Nnaemeka Ronald [email protected]
This software is provided 'as-is', without any express or implied
warranty.In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter itand redistribute it
freely, subject to the following restrictions :
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software.If you use this software
in a product, an acknowledgment in the product documentation must be
specified.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice must not be removed or altered from any source distribution.
*/
#pragma once
#include <vector>
#include <ostream>
#include "include/fraction.h"
using namespace std;
typedef vector<fraction> ROW;
class LINE
{
private:
string _key;
ROW _row;
public:
LINE(const string& key = "", const ROW& row = {})
: _key(key), _row(row)
{}
string Key() const {
return _key;
}
string& Key() {
return _key;
}
ROW Row() const
{
return _row;
}
ROW& Row()
{
return _row;
}
};
typedef vector<LINE> MATRIX;
class S_RESULT
{
private:
string _key;
fraction _value;
public:
S_RESULT(const string &key = "", const fraction &value = fraction(0))
: _key(key), _value(value)
{}
string Key() const {
return _key;
}
fraction Value() const
{
return _value;
}
bool IsNull() const
{
return _key.length() == 0;
}
};
class S_RESULTS
{
private:
vector<string> keys;
vector<S_RESULT> results;
public:
S_RESULTS(vector<S_RESULT> res = {})
{
for (int i = 0; i < (int)res.size(); i++)
{
S_RESULT val = res[i];
if (val.IsNull()) continue;
keys.push_back(val.Key());
results.push_back(val);
}
}
void Add(const S_RESULT& value)
{
if (value.IsNull()) return;
keys.push_back(value.Key());
results.push_back(value);
}
S_RESULT GetResult(const string& key) const
{
for (int i = 0; i < (int)keys.size(); i++)
{
if (key == keys[i]) return results[i];
}
throw runtime_error("Key does not exist");
}
fraction GetValue(const string& key) const
{
for (int i = 0; i < (int)keys.size(); i++)
{
if (key == keys[i]) return results[i].Value();
}
throw runtime_error("Key does not exist");
}
vector<S_RESULT> GetAll() const
{
return results;
}
int Length() const
{
return keys.size();
}
};
class Simplex
{
public:
static S_RESULTS SolveEq(const MATRIX& array, const int no_variables)
{
bool optimal;
int col;
bool artificial_var = HasArtificialVariable(array, no_variables);
MATRIX arr = SimplexEngine(array);
while (ShouldIterate(arr)) arr = SimplexEngine(arr);
if (artificial_var)
{
optimal = IsOptimal(arr, no_variables, col);
while (!optimal)
{
arr = SimplexEngine(arr, optimal, col);
optimal = IsOptimal(arr, no_variables, col);
}
}
S_RESULTS ans;
for (int i = 0; i < (int)arr.size(); i++)
{
LINE line = arr[i];
S_RESULT res = ResolveResult(line, no_variables);
ans.Add(res);
}
// clean result
for (int i = 0; i < no_variables; i++)
{
ostringstream s;
s << "x" << i + 1;
try
{
ans.GetValue(s.str());
}
catch (exception e)
{
ans.Add(S_RESULT(s.str(), fraction(0)));
}
}
for (int i = 0; i < (int)arr.size(); i++)
{
ostringstream s;
s << "w" << i + 1;
try
{
ans.GetValue(s.str());
}
catch (exception e)
{
ans.Add(S_RESULT(s.str(), fraction(0)));
}
}
return ans;
}
private:
static MATRIX SimplexEngine(const MATRIX& _arr, const bool optimal = true, const int optimal_col = 0)
{
MATRIX arr = _arr;
int key_col, key_row, pivot_row = 0, pivot_col = 0;
ROW row, main_row;
LINE line;
fraction divisor, val, smallest_postive_no;
// get the key row
for (int i = 0; i < (int)arr.size(); i++)
{
line = arr[i];
if (line.Key() == "P")
{
key_row = i;
break;
}
}
// get the key column
if (optimal)
{
fraction col, min_value;
row = arr[key_row].Row();
for (int i = 0; i < (int)row.size(); i++)
{
col = row[i];
if (col < min_value)
{
min_value = col;
key_col = i;
}
}
}
else
{
key_col = optimal_col;
}
// get the pivot
for (int i = 0; i < (int)arr.size(); i++)
{
line = arr[i];
row = line.Row();
divisor = row[key_col];
if (line.Key() != "P" && divisor != 0)
{
fraction temp = row[row.size() - 1] / divisor;
if (temp > 0 && smallest_postive_no == 0)
{
smallest_postive_no = temp;
pivot_row = i, pivot_col = key_col;
}
else if (temp > 0 && temp < smallest_postive_no)
{
smallest_postive_no = temp;
pivot_row = i, pivot_col = key_col;
}
}
}
// sanitize the key row
val = arr[pivot_row].Row()[pivot_col];
for (int i = 0; i < (int)arr[pivot_row].Row().size(); i++)
{
arr[pivot_row].Row()[i] /= val;
}
// sanitize the key col
main_row = arr[pivot_row].Row();
for (int i = 0; i < (int)arr.size(); i++)
{
line = arr[i];
row = line.Row();
divisor = row[key_col];
if (line.Key() != arr[pivot_row].Key() && divisor != 0)
{
for (int j = 0; j < (int)row.size(); j++)
{
arr[i].Row()[j] -= (main_row[j] * divisor);
}
}
}
return arr;
}
static inline bool IsOptimal(const MATRIX &arr, const int no_variables, int &col)
{
bool is_optimal = true;
col = 0;
for (int i = 0; i < (int)arr.size(); i++)
{
if (arr[i].Key() == "P")
{
ROW row = arr[i].Row();
for (i = 0; i < no_variables; i++)
{
if (row[i] > 0)
{
col = i;
is_optimal = false;
break;
}
}
break;
}
}
return is_optimal;
}
static inline bool ShouldIterate(const MATRIX &arr)
{
bool iterate = false;
for (int i = 0; i < (int)arr.size(); i++)
{
if (arr[i].Key() == "P")
{
ROW row = arr[i].Row();
for (i = 0; i < (int)row.size(); i++)
{
if (row[i] < 0)
{
iterate = true;
break;
}
}
break;
}
}
return iterate;
}
static inline bool HasArtificialVariable(const MATRIX& arr, const int no_variables)
{
LINE line;
ROW row;
for (int i = 0, j = no_variables; i < (int)arr.size(); i++, j++)
{
line = arr[i];
row = line.Row();
if (line.Key() != "P")
{
for (int j = no_variables; j < (int)row.size() - 1; j++) // check all slack variable for -1
{
if (row[j] < 0)
{
return true;
}
}
}
}
return false;
}
static inline S_RESULT ResolveResult(const LINE &line, const int no_variables)
{
ROW row = line.Row();
int pos = 0;
ostringstream output;
if (line.Key() == "P") return S_RESULT("Pmax", row[row.size() - 1]);
for (int i = 0; i < (int)row.size() - 1; i++) // without the constant `b`
{
if (row[i] == 1)
{
pos = i;
break;
}
}
if (pos < no_variables)
{
output << "x" << pos + 1;
return S_RESULT(output.str(), row[row.size() - 1]);
}
if (pos >= no_variables)
{
output << "w" << (pos - no_variables) + 1;
return S_RESULT(output.str(), row[row.size() - 1]);
}
return S_RESULT();
}
};