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