/** * Unit tests the {@code LinearProgramming} data type. * * @param args the command-line arguments */ public static void main(String[] args) { StdOut.println("----- test 1 --------------------"); test1(); StdOut.println(); StdOut.println("----- test 2 --------------------"); test2(); StdOut.println(); StdOut.println("----- test 3 --------------------"); test3(); StdOut.println(); StdOut.println("----- test 4 --------------------"); test4(); StdOut.println(); StdOut.println("----- test random ---------------"); int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); double[] c = new double[n]; double[] b = new double[m]; double[][] A = new double[m][n]; for (int j = 0; j < n; j++) c[j] = StdRandom.uniform(1000); for (int i = 0; i < m; i++) b[i] = StdRandom.uniform(1000); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) A[i][j] = StdRandom.uniform(100); LinearProgramming lp = new LinearProgramming(A, b, c); test(A, b, c); }
private double constant; // constant added to each entry in payoff matrix // (0 if all entries are strictly positive) /** * Determines an optimal solution to the two-sum zero-sum game * with the specified payoff matrix. * * @param payoff the <em>m</em>-by-<em>n</em> payoff matrix */ public TwoPersonZeroSumGame(double[][] payoff) { m = payoff.length; n = payoff[0].length; double[] c = new double[n]; double[] b = new double[m]; double[][] A = new double[m][n]; for (int i = 0; i < m; i++) b[i] = 1.0; for (int j = 0; j < n; j++) c[j] = 1.0; // find smallest entry constant = Double.POSITIVE_INFINITY; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (payoff[i][j] < constant) constant = payoff[i][j]; // add constant to every entry to make strictly positive if (constant <= 0) constant = -constant + 1; else constant = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) A[i][j] = payoff[i][j] + constant; lp = new LinearProgramming(A, b, c); assert certifySolution(payoff); }
private static void test(double[][] A, double[] b, double[] c) { LinearProgramming lp; try { lp = new LinearProgramming(A, b, c); } catch (ArithmeticException e) { System.out.println(e); return; } StdOut.println("value = " + lp.value()); double[] x = lp.primal(); for (int i = 0; i < x.length; i++) StdOut.println("x[" + i + "] = " + x[i]); double[] y = lp.dual(); for (int j = 0; j < y.length; j++) StdOut.println("y[" + j + "] = " + y[j]); }