-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplex.h
211 lines (184 loc) · 4.48 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
#pragma once
#include <cstdio>
#include <vector>
#include "Constants.h"
class Simplex {
private:
const double epsilon = 1e-7;
int m, n;
double z = 0.;
std::vector<std::vector<double>> a;
std::vector<double> b, c, sol;
std::vector<int> base;
bool equals(double a, double b) {
return abs(a - b) < epsilon;
}
// First negative in c
int firstNegInC() {
for (int col = 0; col < n; ++col) {
if (c[col] < 0)
return col;
}
return -1;
}
// Row with the smallest ratio in column col
int smallestRatio(int col) {
double minRatio = INF_D;
int minRow = -1;
for (int row = 0; row < m; ++row) {
if (a[row][col] > 0) {
double ratio = b[row] / a[row][col];
if (ratio < minRatio) {
minRatio = ratio;
minRow = row;
}
}
}
return minRow;
}
void multRowOfA(int row, double ratio) {
for (int col = 0; col < n; ++col)
a[row][col] *= ratio;
b[row] *= ratio;
}
void addRowToA(int fromRow, int toRow, double ratio) {
for (int col = 0; col < n; ++col)
a[toRow][col] += a[fromRow][col] * ratio;
b[toRow] += b[fromRow] * ratio;
}
void addRowToC(int fromRow, double ratio) {
for (int col = 0; col < n; ++col)
c[col] += a[fromRow][col] * ratio;
z += b[fromRow] * ratio;
}
// The only base column in the given row
int baseInRow(int row) {
for (int col = 0; col < n; ++col) {
if (base[col] && equals(a[row][col], 1.))
return col;
}
return -1;
}
void printTab() {
for (int i = 0; i < n; ++i)
printf("%8d ", base[i]);
printf("\n");
for (int i = 0; i < n; ++i)
printf("%8.2g ", c[i]);
printf(" | %8.2g\n", z);
for (int i = 0; i < n; ++i)
printf("---------");
printf("-+-");
printf("---------");
printf("\n");
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j)
printf("%8.2g ", a[i][j]);
printf(" | %8.2g\n", b[i]);
}
printf("\n");
}
void computeSolution() {
sol.assign(n, 0);
for (int row = 0; row < m; ++row)
sol[baseInRow(row)] = b[row];
}
void computeSimplex() {
//printTab();
int colIn;
while ((colIn = firstNegInC()) != -1) {
int pivot = smallestRatio(colIn);
if (pivot == -1) {
z = INF_D; // Unbounded
break;
}
int colOut = baseInRow(pivot);
multRowOfA(pivot, 1. / a[pivot][colIn]);
for (int row = 0; row < m; ++row) {
if (row != pivot)
addRowToA(pivot, row, -a[row][colIn]);
}
addRowToC(pivot, -c[colIn]);
base[colOut] = false;
base[colIn] = true;
//printTab();
}
computeSolution();
}
// Resolve the extended simplex problem with additional variables
Simplex(const std::vector<std::vector<double>>& a_, const std::vector<double>& b_)
: a(a_), b(b_) {
m = int(b.size());
n = int(a[0].size());
base.assign(n, false);
c.assign(n, 0.);
for (int col = 0; col < m; ++col) {
base.push_back(true);
c.push_back(1.);
for (int row = 0; row < m; ++row)
a[row].push_back(row == col ? 1. : 0.);
}
n += m;
for (int row = 0; row < m; ++row) {
addRowToC(row, -1.);
}
computeSimplex();
}
public:
// Standard form simplex
Simplex(const std::vector<std::vector<double>>& a_, const std::vector<double>& b_,
const std::vector<double>& c_)
: a(a_), b(b_), c(c_) {
m = int(b.size());
n = int(a[0].size());
for (int row = 0; row < m; ++row) {
if (b[row] < 0.)
multRowOfA(row, -1.);
}
Simplex extended{ a, b };
if (!equals(extended.z, 0.)) {
z = -INF_D; // No solution
return;
}
base = extended.base;
base.resize(n);
a = extended.a;
for (int row = 0; row < m; ++row)
a[row].resize(n);
b = extended.b;
computeSimplex();
}
// Geometric form simplex
Simplex(const std::vector<std::vector<double>>& a_, const std::vector<double>& b_,
const std::vector<double>& c_, bool geometric)
: a(a_), b(b_), c(c_) {
m = int(b.size()), n = int(a[0].size());
// Make the variables positive
// by adding another negative variable for each variable
for (int var = 0; var < n; ++var) {
c.push_back(-c[var]);
for (int row = 0; row < m; ++row) {
a[row].push_back(-a[row][var]);
}
}
// Make the inequalities equalities
// by substracting a positive variable in each inequality
for (int var = 0; var < m; ++var) {
c.push_back(0.);
for (int row = 0; row < m; ++row) {
a[row].push_back(row == var ? -1. : 0.);
}
}
Simplex standard{ a, b, c };
sol.resize(n);
for (int var = 0; var < n; ++var)
sol[var] = standard.sol[var] - standard.sol[var + n];
z = standard.z;
}
double objective() {
return -z;
}
std::vector<double> solution() {
return sol;
}
};