Author: luc Date: Wed Aug 26 08:43:27 2009 New Revision: 807923 URL: http://svn.apache.org/viewvc?rev=807923&view=rev Log: fixed an error with negative constraints and unfeasible solution JIRA: MATH-290
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java commons/proper/math/trunk/src/site/xdoc/changes.xml commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java?rev=807923&r1=807922&r2=807923&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java Wed Aug 26 08:43:27 2009 @@ -69,7 +69,7 @@ private final LinearObjectiveFunction f; /** Linear constraints. */ - private final Collection<LinearConstraint> constraints; + private final List<LinearConstraint> constraints; /** Whether to restrict the variables to non-negative values. */ private final boolean restrictToNonNegative; @@ -103,7 +103,7 @@ final GoalType goalType, final boolean restrictToNonNegative, final double epsilon) { this.f = f; - this.constraints = constraints; + this.constraints = normalizeConstraints(constraints); this.restrictToNonNegative = restrictToNonNegative; this.epsilon = epsilon; this.numDecisionVariables = getNumVariables() + (restrictToNonNegative ? 0 : 1); @@ -123,7 +123,6 @@ protected double[][] createTableau(final boolean maximize) { // create a matrix of the correct size - List<LinearConstraint> constraints = getNormalizedConstraints(); int width = numDecisionVariables + numSlackVariables + numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS int height = constraints.size() + getNumObjectiveFunctions(); @@ -192,9 +191,10 @@ /** * Get new versions of the constraints which have positive right hand sides. + * @param constraints original (not normalized) constraints * @return new versions of the constraints */ - public List<LinearConstraint> getNormalizedConstraints() { + public List<LinearConstraint> normalizeConstraints(Collection<LinearConstraint> constraints) { List<LinearConstraint> normalized = new ArrayList<LinearConstraint>(); for (LinearConstraint constraint : constraints) { normalized.add(normalize(constraint)); Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=807923&r1=807922&r2=807923&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/changes.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/changes.xml Wed Aug 26 08:43:27 2009 @@ -39,6 +39,10 @@ </properties> <body> <release version="2.1" date="TBD" description="TBD"> + <action dev="luc" type="fix" issue="MATH-290" due-to="Benjamin McCann"> + Fixed a NullPointerException when no solution is possible and some constraints + are negative + </action> <action dev="luc" type="fix" issue="MATH-288" due-to="Benjamin McCann"> Fixed an error induced by entries set to 0 </action> Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java?rev=807923&r1=807922&r2=807923&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java Wed Aug 26 08:43:27 2009 @@ -18,6 +18,7 @@ package org.apache.commons.math.optimization.linear; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.util.ArrayList; import java.util.Collection; @@ -74,6 +75,16 @@ } @Test + public void testMath290() throws OptimizationException { + LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 5 }, 0 ); + Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>(); + constraints.add(new LinearConstraint(new double[] { 2, 0 }, Relationship.GEQ, -1.0)); + SimplexSolver solver = new SimplexSolver(); + RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); + assertNotNull(solution); + } + + @Test public void testSimplexSolver() throws OptimizationException { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 7);