1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.optim.linear;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21
22 import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26 public class SimplexTableauTest {
27
28 @Test
29 public void testInitialization() {
30 LinearObjectiveFunction f = createFunction();
31 Collection<LinearConstraint> constraints = createConstraints();
32 SimplexTableau tableau =
33 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
34 double[][] expectedInitialTableau = {
35 {-1, 0, -1, -1, 2, 0, 0, 0, -4},
36 { 0, 1, -1.875, -1.25, 3.125, 0, 0, 0, 0},
37 { 0, 0, 1, 0, -1, 1, 0, 0, 2},
38 { 0, 0, 0, 1, -1, 0, 1, 0, 3},
39 { 0, 0, 1, 1, -2, 0, 0, 1, 4}
40 };
41 assertMatrixEquals(expectedInitialTableau, tableau.getData());
42 }
43
44 @Test
45 public void testDropPhase1Objective() {
46 LinearObjectiveFunction f = createFunction();
47 Collection<LinearConstraint> constraints = createConstraints();
48 SimplexTableau tableau =
49 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
50 double[][] expectedTableau = {
51 { 1, -1.875, -1.25, 0, 0, 0, 0},
52 { 0, 1, 0, 1, 0, 0, 2},
53 { 0, 0, 1, 0, 1, 0, 3},
54 { 0, 1, 1, 0, 0, 1, 4}
55 };
56 tableau.dropPhase1Objective();
57 assertMatrixEquals(expectedTableau, tableau.getData());
58 }
59
60 @Test
61 public void testTableauWithNoArtificialVars() {
62 LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0);
63 Collection<LinearConstraint> constraints = new ArrayList<>();
64 constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
65 constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
66 constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4));
67 SimplexTableau tableau =
68 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
69 double[][] initialTableau = {
70 {1, -1.875, -1.25, 3.125, 0, 0, 0, 0},
71 {0, 1, 0, -1, 1, 0, 0, 2},
72 {0, 0, 1, -1, 0, 1, 0, 3},
73 {0, 1, 1, -2, 0, 0, 1, 4}
74 };
75 assertMatrixEquals(initialTableau, tableau.getData());
76 }
77
78 private LinearObjectiveFunction createFunction() {
79 return new LinearObjectiveFunction(new double[] {15, 10}, 0);
80 }
81
82 private Collection<LinearConstraint> createConstraints() {
83 Collection<LinearConstraint> constraints = new ArrayList<>();
84 constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
85 constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
86 constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.EQ, 4));
87 return constraints;
88 }
89
90 private void assertMatrixEquals(double[][] expected, double[][] result) {
91 Assert.assertEquals("Wrong number of rows.", expected.length, result.length);
92 for (int i = 0; i < expected.length; i++) {
93 Assert.assertEquals("Wrong number of columns.", expected[i].length, result[i].length);
94 for (int j = 0; j < expected[i].length; j++) {
95 Assert.assertEquals("Wrong value at position [" + i + "," + j + "]", expected[i][j], result[i][j], 1.0e-15);
96 }
97 }
98 }
99 }