View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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 }