-
Notifications
You must be signed in to change notification settings - Fork 522
/
Simplex.java
156 lines (146 loc) · 5.09 KB
/
Simplex.java
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
package optimization;
import java.util.Arrays;
import numbertheory.Rational;
public class Simplex {
// returns max c*x such that A*x <= b, x >= 0
public static Rational simplex(Rational[][] A, Rational[] b, Rational[] c, Rational[] x) {
int m = A.length;
int n = A[0].length + 1;
int[] index = new int[n + m];
for (int i = 0; i < n + m; i++) {
index[i] = i;
}
Rational[][] a = new Rational[m + 2][n + 1];
for (Rational[] a1 : a) {
Arrays.fill(a1, Rational.ZERO);
}
int L = m;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 1; j++) {
a[i][j] = A[i][j].negate();
}
a[i][n - 1] = Rational.ONE;
a[i][n] = b[i];
if (a[L][n].compareTo(a[i][n]) > 0) {
L = i;
}
}
for (int j = 0; j < n - 1; j++) {
a[m][j] = c[j];
}
a[m + 1][n - 1] = Rational.ONE.negate();
for (int E = n - 1;;) {
if (L < m) {
int t = index[E];
index[E] = index[L + n];
index[L + n] = t;
a[L][E] = a[L][E].inverse();
for (int j = 0; j <= n; j++) {
if (j != E) {
a[L][j] = a[L][j].mul(a[L][E].negate());
}
}
for (int i = 0; i <= m + 1; i++) {
if (i != L) {
for (int j = 0; j <= n; j++) {
if (j != E) {
a[i][j] = a[i][j].add(a[L][j].mul(a[i][E]));
}
}
a[i][E] = a[i][E].mul(a[L][E]);
}
}
}
E = -1;
for (int j = 0; j < n; j++) {
if (E < 0 || index[E] > index[j]) {
if (a[m + 1][j].signum() > 0 || a[m + 1][j].signum() == 0 && a[m][j].signum() > 0) {
E = j;
}
}
}
if (E < 0) {
break;
}
L = -1;
for (int i = 0; i < m; i++) {
if (a[i][E].signum() < 0) {
Rational d;
if (L < 0 || (d = a[L][n].div(a[L][E]).sub(a[i][n].div(a[i][E]))).signum() < 0
|| d.signum() == 0 && index[L + n] > index[i + n]) {
L = i;
}
}
}
if (L < 0) {
return Rational.POSITIVE_INFINITY;
}
}
if (a[m + 1][n].signum() < 0) {
return null;
}
if (x != null) {
Arrays.fill(x, Rational.ZERO);
for (int i = 0; i < m; i++)
if (index[n + i] < n - 1)
x[index[n + i]] = a[i][n];
}
return a[m][n];
}
// Usage example
public static void main(String[] args) {
long[][] a = {{4, -1}, {2, 1}, {-5, 2}};
long[] b = {8, 10, 2};
long[] c = {1, 1};
Rational[] x = new Rational[c.length];
Rational res = simplex(cnv(a), cnv(b), cnv(c), x);
System.out.println(new Rational(8).equals(res));
System.out.println(Arrays.toString(x));
a = new long[][] {{3, 4, -3}, {5, -4, -3}, {7, 4, 11}};
b = new long[] {23, 10, 30};
c = new long[] {-1, 1, 2};
x = new Rational[c.length];
res = simplex(cnv(a), cnv(b), cnv(c), x);
System.out.println(new Rational(57, 8).equals(res));
System.out.println(Arrays.toString(x));
// no feasible non-negative solutions
a = new long[][] {{4, -1}, {2, 1}, {-5, 2}};
b = new long[] {8, -10, 2};
c = new long[] {1, 1};
res = simplex(cnv(a), cnv(b), cnv(c), null);
System.out.println(null == res);
// unbounded problem
a = new long[][] {{-4, 1}, {-2, -1}, {-5, 2}};
b = new long[] {-8, -10, 2};
c = new long[] {1, 1};
res = simplex(cnv(a), cnv(b), cnv(c), null);
System.out.println(Rational.POSITIVE_INFINITY == res);
// no feasible solutions
a = new long[][] {{1}, {-1}};
b = new long[] {1, -2};
c = new long[] {0};
res = simplex(cnv(a), cnv(b), cnv(c), null);
System.out.println(null == res);
// infinite number of solutions, but only one is returned
a = new long[][] {{1, 1}};
b = new long[] {0};
c = new long[] {1, 1};
x = new Rational[c.length];
res = simplex(cnv(a), cnv(b), cnv(c), x);
System.out.println(Arrays.toString(x));
}
static Rational[] cnv(long[] a) {
Rational[] res = new Rational[a.length];
for (int i = 0; i < a.length; i++) {
res[i] = new Rational(a[i]);
}
return res;
}
static Rational[][] cnv(long[][] a) {
Rational[][] res = new Rational[a.length][];
for (int i = 0; i < a.length; i++) {
res[i] = cnv(a[i]);
}
return res;
}
}