public static void MainTest(string[] args) { Console.WriteLine("----- test 1 --------------------"); LinearProgramming.test1(); Console.WriteLine("----- test 2 --------------------"); LinearProgramming.test2(); Console.WriteLine("----- test 3 --------------------"); try { LinearProgramming.test3(); } catch (ArithmeticException e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } Console.WriteLine("----- test 4 --------------------"); LinearProgramming.test4(); Console.WriteLine("----- test random ---------------"); int M = 5, N = 5; if (args.Length == 3) { M = int.Parse(args[1]); N = int.Parse(args[2]); } 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); } } try { LinearProgramming lp = new LinearProgramming(A, b, c); Console.WriteLine(lp.Value); } catch (ArithmeticException) { Console.WriteLine("The randomlly generated linear program is unbounded. Try again."); } }
private double constant; // constant added to each entry in payoff matrix // (0 if all entries are strictly positive) /// <summary> /// Determines an optimal solution to the two-sum zero-sum game /// with the specified payoff matrix.</summary> /// <param name="payoff">the <c>M</c>-by-<c>N</c> payoff matrix</param> /// public TwoPersonZeroSumGame(double[,] payoff) { M = payoff.GetLength(0); N = payoff.GetLength(1); 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.PositiveInfinity; 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); Debug.Assert(certifySolution(payoff)); }
private static void test(double[,] A, double[] b, double[] c) { LinearProgramming lp = new LinearProgramming(A, b, c); Console.WriteLine("value = " + lp.Value); double[] x = lp.Primal(); for (int i = 0; i < x.Length; i++) { Console.WriteLine("x[{0}] = {1:F5}", i, x[i]); } double[] y = lp.Dual(); for (int j = 0; j < y.Length; j++) { Console.WriteLine("y[{0}] = {1:F5}", j, y[j]); } }