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    *      http://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  package org.apache.commons.math.optimization.linear;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  
23  import org.apache.commons.math.TestUtils;
24  import org.apache.commons.math.optimization.GoalType;
25  
26  import junit.framework.TestCase;
27  
28  public class SimplexTableauTest extends TestCase {
29  
30      public void testInitialization() {    
31          LinearObjectiveFunction f = createFunction();
32          Collection<LinearConstraint> constraints = createConstraints();
33          SimplexTableau tableau =
34              new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
35          double[][] expectedInitialTableau = {
36                                               {-1, 0,  -1,  -1,  2, 0, 0, 0, -4},
37                                               { 0, 1, -15, -10, 25, 0, 0, 0,  0},
38                                               { 0, 0,   1,   0, -1, 1, 0, 0,  2},
39                                               { 0, 0,   0,   1, -1, 0, 1, 0,  3},
40                                               { 0, 0,   1,   1, -2, 0, 0, 1,  4}
41          };
42          assertMatrixEquals(expectedInitialTableau, tableau.getData());
43      }
44  
45      public void testdiscardArtificialVariables() {    
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, -15, -10, 25, 0, 0, 0},
52                                        { 0,   1,   0, -1, 1, 0, 2},
53                                        { 0,   0,   1, -1, 0, 1, 3},
54                                        { 0,   1,   1, -2, 0, 0, 4}
55          };
56          tableau.discardArtificialVariables();
57          assertMatrixEquals(expectedTableau, tableau.getData());
58      }
59  
60      public void testTableauWithNoArtificialVars() {
61          LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0);
62          Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
63          constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
64          constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
65          constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4));    
66          SimplexTableau tableau =
67              new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
68          double[][] initialTableau = {
69                                       {1, -15, -10, 25, 0, 0, 0, 0},
70                                       {0,   1,   0, -1, 1, 0, 0, 2},
71                                       {0,   0,   1, -1, 0, 1, 0, 3},
72                                       {0,   1,   1, -2, 0, 0, 1, 4}
73          };
74          assertMatrixEquals(initialTableau, tableau.getData());
75      }
76  
77      public void testSerial() {
78          LinearObjectiveFunction f = createFunction();
79          Collection<LinearConstraint> constraints = createConstraints();
80          SimplexTableau tableau =
81              new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
82          assertEquals(tableau, TestUtils.serializeAndRecover(tableau));
83      }
84      private LinearObjectiveFunction createFunction() {
85          return new LinearObjectiveFunction(new double[] {15, 10}, 0);
86      }
87  
88      private Collection<LinearConstraint> createConstraints() {
89          Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
90          constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
91          constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
92          constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.EQ, 4));
93          return constraints;
94      }
95  
96      private void assertMatrixEquals(double[][] expected, double[][] result) {
97          assertEquals("Wrong number of rows.", expected.length, result.length);
98          for (int i = 0; i < expected.length; i++) {
99              assertEquals("Wrong number of columns.", expected[i].length, result[i].length);
100             for (int j = 0; j < expected[i].length; j++) {
101                 assertEquals("Wrong value at position [" + i + "," + j + "]", expected[i][j], result[i][j]);
102             }
103         }
104     }
105 
106 }