prestige

Simplex-method

Apr 3rd, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Simplex {
  4.  
  5. // returns x | c*x -> max, A*x <= b
  6. public static Rational simplex(Rational[][] A, Rational[] b, Rational[] c, Rational[] x) {
  7. int m = A.length;
  8. int n = A[0].length + 1;
  9. int[] index = new int[n + m];
  10. for (int i = 0; i < n + m; i++) {
  11. index[i] = i;
  12. }
  13. Rational[][] a = new Rational[m + 2][n + 1];
  14. for (Rational[] a1 : a) {
  15. Arrays.fill(a1, Rational.ZERO);
  16. }
  17. int L = m;
  18. for (int i = 0; i < m; i++) {
  19. for (int j = 0; j < n - 1; j++) {
  20. a[i][j] = A[i][j].negate();
  21. }
  22. a[i][n - 1] = Rational.ONE;
  23. a[i][n] = b[i];
  24. if (a[L][n].compareTo(a[i][n]) > 0) {
  25. L = i;
  26. }
  27. }
  28. for (int j = 0; j < n - 1; j++) {
  29. a[m][j] = c[j];
  30. }
  31. a[m + 1][n - 1] = Rational.ONE.negate();
  32. for (int E = n - 1;;) {
  33. if (L < m) {
  34. int t = index[E];
  35. index[E] = index[L + n];
  36. index[L + n] = t;
  37. a[L][E] = a[L][E].inverse();
  38. for (int j = 0; j <= n; j++) {
  39. if (j != E) {
  40. a[L][j] = a[L][j].mul(a[L][E].negate());
  41. }
  42. }
  43. for (int i = 0; i <= m + 1; i++) {
  44. if (i != L) {
  45. for (int j = 0; j <= n; j++) {
  46. if (j != E) {
  47. a[i][j] = a[i][j].add(a[L][j].mul(a[i][E]));
  48. }
  49. }
  50. a[i][E] = a[i][E].mul(a[L][E]);
  51. }
  52. }
  53. }
  54. E = -1;
  55. for (int j = 0; j < n; j++) {
  56. if (E < 0 || index[E] > index[j]) {
  57. if (a[m + 1][j].signum() > 0 || a[m + 1][j].signum() == 0 && a[m][j].signum() > 0) {
  58. E = j;
  59. }
  60. }
  61. }
  62. if (E < 0) {
  63. break;
  64. }
  65. L = -1;
  66. for (int i = 0; i < m; i++) {
  67. if (a[i][E].signum() < 0) {
  68. Rational d;
  69. if (L < 0 || (d = a[L][n].div(a[L][E]).sub(a[i][n].div(a[i][E]))).signum() < 0 || d.signum() == 0
  70. && index[L + n] > index[i + n]) {
  71. L = i;
  72. }
  73. }
  74. }
  75. if (L < 0) {
  76. return Rational.POSITIVE_INFINITY;
  77. }
  78. }
  79. if (a[m + 1][n].signum() < 0) {
  80. return null;
  81. }
  82. if (x != null) {
  83. Arrays.fill(x, Rational.ZERO);
  84. for (int i = 0; i < m; i++)
  85. if (index[n + i] < n - 1)
  86. x[index[n + i]] = a[i][n];
  87. }
  88. return a[m][n];
  89. }
  90.  
  91. // Usage example
  92. public static void main(String[] args) {
  93. long[][] a = { { 4, -1 }, { 2, 1 }, { -5, 2 } };
  94. long[] b = { 8, 10, 2 };
  95. long[] c = { 1, 1 };
  96. Rational[] x = new Rational[c.length];
  97. Rational res = simplex(cnv(a), cnv(b), cnv(c), x);
  98. System.out.println(new Rational(8).equals(res));
  99. System.out.println(Arrays.toString(x));
  100.  
  101. a = new long[][] { { 3, 4, -3 }, { 5, -4, -3 }, { 7, 4, 11 } };
  102. b = new long[] { 23, 10, 30 };
  103. c = new long[] { -1, 1, 2 };
  104. x = new Rational[c.length];
  105. res = simplex(cnv(a), cnv(b), cnv(c), x);
  106. System.out.println(new Rational(57, 8).equals(res));
  107. System.out.println(Arrays.toString(x));
  108.  
  109. a = new long[][] { { 4, -1 }, { 2, 1 }, { -5, 2 } };
  110. b = new long[] { 8, -10, 2 };
  111. c = new long[] { 1, 1 };
  112. res = simplex(cnv(a), cnv(b), cnv(c), null);
  113. System.out.println(null == res);
  114.  
  115. a = new long[][] { { -4, 1 }, { -2, -1 }, { -5, 2 } };
  116. b = new long[] { -8, -10, 2 };
  117. c = new long[] { 1, 1 };
  118. res = simplex(cnv(a), cnv(b), cnv(c), null);
  119. System.out.println(Rational.POSITIVE_INFINITY == res);
  120.  
  121. a = new long[][] { { 1 }, { -1 } };
  122. b = new long[] { 1, -2 };
  123. c = new long[] { 0, 0 };
  124. res = simplex(cnv(a), cnv(b), cnv(c), null);
  125. System.out.println(null == res);
  126. }
  127.  
  128. static Rational[] cnv(long[] a) {
  129. Rational[] res = new Rational[a.length];
  130. for (int i = 0; i < a.length; i++) {
  131. res[i] = new Rational(a[i]);
  132. }
  133. return res;
  134. }
  135.  
  136. static Rational[][] cnv(long[][] a) {
  137. Rational[][] res = new Rational[a.length][];
  138. for (int i = 0; i < a.length; i++) {
  139. res[i] = cnv(a[i]);
  140. }
  141. return res;
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment