001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.optimization.linear;
019
020 import java.util.ArrayList;
021 import java.util.Collection;
022
023 import org.apache.commons.math.TestUtils;
024 import org.apache.commons.math.optimization.GoalType;
025
026 import junit.framework.TestCase;
027
028 public class SimplexTableauTest extends TestCase {
029
030 public void testInitialization() {
031 LinearObjectiveFunction f = createFunction();
032 Collection<LinearConstraint> constraints = createConstraints();
033 SimplexTableau tableau =
034 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
035 double[][] expectedInitialTableau = {
036 {-1, 0, -1, -1, 2, 0, 0, 0, -4},
037 { 0, 1, -15, -10, 25, 0, 0, 0, 0},
038 { 0, 0, 1, 0, -1, 1, 0, 0, 2},
039 { 0, 0, 0, 1, -1, 0, 1, 0, 3},
040 { 0, 0, 1, 1, -2, 0, 0, 1, 4}
041 };
042 assertMatrixEquals(expectedInitialTableau, tableau.getData());
043 }
044
045 public void testdiscardArtificialVariables() {
046 LinearObjectiveFunction f = createFunction();
047 Collection<LinearConstraint> constraints = createConstraints();
048 SimplexTableau tableau =
049 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
050 double[][] expectedTableau = {
051 { 1, -15, -10, 25, 0, 0, 0},
052 { 0, 1, 0, -1, 1, 0, 2},
053 { 0, 0, 1, -1, 0, 1, 3},
054 { 0, 1, 1, -2, 0, 0, 4}
055 };
056 tableau.discardArtificialVariables();
057 assertMatrixEquals(expectedTableau, tableau.getData());
058 }
059
060 public void testTableauWithNoArtificialVars() {
061 LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0);
062 Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
063 constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
064 constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
065 constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4));
066 SimplexTableau tableau =
067 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
068 double[][] initialTableau = {
069 {1, -15, -10, 25, 0, 0, 0, 0},
070 {0, 1, 0, -1, 1, 0, 0, 2},
071 {0, 0, 1, -1, 0, 1, 0, 3},
072 {0, 1, 1, -2, 0, 0, 1, 4}
073 };
074 assertMatrixEquals(initialTableau, tableau.getData());
075 }
076
077 public void testSerial() {
078 LinearObjectiveFunction f = createFunction();
079 Collection<LinearConstraint> constraints = createConstraints();
080 SimplexTableau tableau =
081 new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6);
082 assertEquals(tableau, TestUtils.serializeAndRecover(tableau));
083 }
084 private LinearObjectiveFunction createFunction() {
085 return new LinearObjectiveFunction(new double[] {15, 10}, 0);
086 }
087
088 private Collection<LinearConstraint> createConstraints() {
089 Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
090 constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2));
091 constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3));
092 constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.EQ, 4));
093 return constraints;
094 }
095
096 private void assertMatrixEquals(double[][] expected, double[][] result) {
097 assertEquals("Wrong number of rows.", expected.length, result.length);
098 for (int i = 0; i < expected.length; i++) {
099 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 }