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