You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/12/14 22:50:34 UTC
svn commit: r1550975 - in /commons/proper/math/trunk/src: changes/
main/java/org/apache/commons/math3/optim/linear/
test/java/org/apache/commons/math3/optim/linear/
Author: tn
Date: Sat Dec 14 21:50:33 2013
New Revision: 1550975
URL: http://svn.apache.org/r1550975
Log:
[MATH-842] Added support for different pivot selection rules to SimplexSolver.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java (with props)
Modified:
commons/proper/math/trunk/src/changes/changes.xml
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1550975&r1=1550974&r2=1550975&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Sat Dec 14 21:50:33 2013
@@ -51,6 +51,11 @@ If the output is not quite correct, chec
</properties>
<body>
<release version="3.3" date="TBD" description="TBD">
+ <action dev="tn" type="fix" issue="MATH-842">
+ Added support for different pivot selection rules to the "SimplexSolver" by introducing
+ the new "OptimizationData" class "PivotSelectionRule". Currently supported rules are:
+ Dantzig (default) and Bland (avoids cycles).
+ </action>
<action dev="tn" type="fix" issue="MATH-1070" due-to="Oleksandr Muliarevych">
Fix "Precision#round(float, int, int)" when using rounding mode "BigDecimal.ROUND_UP"
and the discarded fraction is zero.
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java?rev=1550975&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java Sat Dec 14 21:50:33 2013
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.linear;
+
+import org.apache.commons.math3.optim.OptimizationData;
+
+/**
+ * Pivot selection rule to the use for a Simplex solver.
+ *
+ * @version $Id$
+ * @since 3.3
+ */
+public enum PivotSelectionRule implements OptimizationData {
+ /**
+ * The classical rule, the variable with the most negative coefficient
+ * in the objective function row will be chosen as entering variable.
+ */
+ Dantzig,
+ /**
+ * The first variable with a negative coefficient in the objective function
+ * row will be chosen as entering variable. This rule guarantees to prevent
+ * cycles, but may take longer to find an optimal solution.
+ */
+ Bland
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/PivotSelectionRule.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java?rev=1550975&r1=1550974&r2=1550975&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java Sat Dec 14 21:50:33 2013
@@ -27,6 +27,19 @@ import org.apache.commons.math3.util.Pre
/**
* Solves a linear problem using the "Two-Phase Simplex" method.
* <p>
+ * The {@link SimplexSolver} supports the following {@link OptimizationData} data provided
+ * as arguments to {@link #optimize(OptimizationData...)}:
+ * <ul>
+ * <li>objective function: {@link LinearObjectiveFunction} - mandatory</li>
+ * <li>linear constraints {@link LinearConstraintSet} - mandatory</li>
+ * <li>type of optimization: {@link org.apache.commons.math3.optim.nonlinear.scalar.GoalType GoalType}
+ * - optional, default: {@link org.apache.commons.math3.optim.nonlinear.scalar.GoalType#MINIMIZE MINIMIZE}</li>
+ * <li>whether to allow negative values as solution: {@link NonNegativeConstraint} - optional, default: true</li>
+ * <li>pivot selection rule: {@link PivotSelectionRule} - optional, default {@link PivotSelectionRule#Dantzig}</li>
+ * <li>callback for the best solution: {@link SolutionCallback} - optional</li>
+ * <li>maximum number of iterations: {@link MaxIter} - optional, default: {@link Integer#MAX_VALUE}</li>
+ * </ul>
+ * <p>
* <b>Note:</b> Depending on the problem definition, the default convergence criteria
* may be too strict, resulting in {@link NoFeasibleSolutionException} or
* {@link TooManyIterationsException}. In such a case it is advised to adjust these
@@ -42,15 +55,8 @@ import org.apache.commons.math3.util.Pre
* The cut-off value has been introduced to zero out very small numbers in the Simplex tableau,
* as these may lead to numerical instabilities due to the nature of the Simplex algorithm
* (the pivot element is used as a denominator). If the problem definition is very tight, the
- * default cut-off value may be too small, thus it is advised to increase it to a larger value,
- * in accordance with the chosen epsilon.
- * <p>
- * It may also be counter-productive to provide a too large value for {@link
- * org.apache.commons.math3.optim.MaxIter MaxIter} as parameter in the call of {@link
- * #optimize(org.apache.commons.math3.optim.OptimizationData...) optimize(OptimizationData...)},
- * as the {@link SimplexSolver} will use different strategies depending on the current iteration
- * count. After half of the allowed max iterations has already been reached, the strategy to select
- * pivot rows will change in order to break possible cycles due to degenerate problems.
+ * default cut-off value may be too small for certain problems, thus it is advised to increase it
+ * to a larger value, in accordance with the chosen epsilon.
*
* @version $Id$
* @since 2.0
@@ -77,6 +83,9 @@ public class SimplexSolver extends Linea
*/
private final double cutOff;
+ /** The pivot selection method to use. */
+ private PivotSelectionRule pivotSelection;
+
/**
* The solution callback to access the best solution found so far in case
* the optimizer fails to find an optimal solution within the iteration limits.
@@ -120,6 +129,7 @@ public class SimplexSolver extends Linea
this.epsilon = epsilon;
this.maxUlps = maxUlps;
this.cutOff = cutOff;
+ this.pivotSelection = PivotSelectionRule.Dantzig;
}
/**
@@ -130,6 +140,7 @@ public class SimplexSolver extends Linea
* LinearOptimizer}, this method will register the following data:
* <ul>
* <li>{@link SolutionCallback}</li>
+ * <li>{@link PivotSelectionRule}</li>
* </ul>
*
* @return {@inheritDoc}
@@ -151,6 +162,7 @@ public class SimplexSolver extends Linea
* LinearOptimizer}, this method will register the following data:
* <ul>
* <li>{@link SolutionCallback}</li>
+ * <li>{@link PivotSelectionRule}</li>
* </ul>
*/
@Override
@@ -166,6 +178,10 @@ public class SimplexSolver extends Linea
solutionCallback = (SolutionCallback) data;
continue;
}
+ if (data instanceof PivotSelectionRule) {
+ pivotSelection = (PivotSelectionRule) data;
+ continue;
+ }
}
}
@@ -185,15 +201,43 @@ public class SimplexSolver extends Linea
if (entry < minValue) {
minValue = entry;
minPos = i;
+
+ // Bland's rule: chose the entering column with the lowest index
+ if (pivotSelection == PivotSelectionRule.Bland && isValidPivotColumn(tableau, i)) {
+ break;
+ }
}
}
return minPos;
}
/**
+ * Checks whether the given column is valid pivot column, i.e. will result
+ * in a valid pivot row.
+ * <p>
+ * When applying Bland's rule to select the pivot column, it may happen that
+ * there is no corresponding pivot row. This method will check if the selected
+ * pivot column will return a valid pivot row.
+ *
+ * @param tableau simplex tableau for the problem
+ * @param col the column to test
+ * @return {@code true} if the pivot column is valid, {@code false} otherwise
+ */
+ private boolean isValidPivotColumn(SimplexTableau tableau, int col) {
+ for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
+ final double entry = tableau.getEntry(i, col);
+
+ if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* Returns the row with the minimum ratio as given by the minimum ratio test (MRT).
*
- * @param tableau Simple tableau for the problem.
+ * @param tableau Simplex tableau for the problem.
* @param col Column to test the ratio of (see {@link #getPivotColumn(SimplexTableau)}).
* @return the row with the minimum ratio.
*/
@@ -243,26 +287,21 @@ public class SimplexSolver extends Linea
//
// see http://www.stanford.edu/class/msande310/blandrule.pdf
// see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper)
- //
- // Additional heuristic: if we did not get a solution after half of maxIterations
- // revert to the simple case of just returning the top-most row
- // This heuristic is based on empirical data gathered while investigating MATH-828.
- if (getEvaluations() < getMaxEvaluations() / 2) {
- Integer minRow = null;
- int minIndex = tableau.getWidth();
- final int varStart = tableau.getNumObjectiveFunctions();
- final int varEnd = tableau.getWidth() - 1;
- for (Integer row : minRatioPositions) {
- for (int i = varStart; i < varEnd && !row.equals(minRow); i++) {
- final Integer basicRow = tableau.getBasicRow(i);
- if (basicRow != null && basicRow.equals(row) && i < minIndex) {
- minIndex = i;
- minRow = row;
- }
+
+ Integer minRow = null;
+ int minIndex = tableau.getWidth();
+ final int varStart = tableau.getNumObjectiveFunctions();
+ final int varEnd = tableau.getWidth() - 1;
+ for (Integer row : minRatioPositions) {
+ for (int i = varStart; i < varEnd && !row.equals(minRow); i++) {
+ final Integer basicRow = tableau.getBasicRow(i);
+ if (basicRow != null && basicRow.equals(row) && i < minIndex) {
+ minIndex = i;
+ minRow = row;
}
}
- return minRow;
}
+ return minRow;
}
return minRatioPositions.get(0);
}
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java?rev=1550975&r1=1550974&r2=1550975&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java Sat Dec 14 21:50:33 2013
@@ -33,6 +33,34 @@ public class SimplexSolverTest {
private static final MaxIter DEFAULT_MAX_ITER = new MaxIter(100);
@Test
+ public void testMath842Cycle() {
+ // from http://www.math.toronto.edu/mpugh/Teaching/APM236_04/bland
+ // maximize 10 x1 - 57 x2 - 9 x3 - 24 x4
+ // subject to
+ // 1/2 x1 - 11/2 x2 - 5/2 x3 + 9 x4 <= 0
+ // 1/2 x1 - 3/2 x2 - 1/2 x3 + x4 <= 0
+ // x1 <= 1
+ // x1,x2,x3,x4 >= 0
+
+ LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, -57, -9, -24}, 0);
+
+ ArrayList<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
+
+ constraints.add(new LinearConstraint(new double[] {0.5, -5.5, -2.5, 9}, Relationship.LEQ, 0));
+ constraints.add(new LinearConstraint(new double[] {0.5, -1.5, -0.5, 1}, Relationship.LEQ, 0));
+ constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0}, Relationship.LEQ, 1));
+
+ double epsilon = 1e-6;
+ SimplexSolver solver = new SimplexSolver();
+ PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints),
+ GoalType.MAXIMIZE,
+ new NonNegativeConstraint(true),
+ PivotSelectionRule.Bland);
+ Assert.assertEquals(1.0d, solution.getValue(), epsilon);
+ Assert.assertTrue(validSolution(solution, constraints, epsilon));
+ }
+
+ @Test
public void testMath828() {
LinearObjectiveFunction f = new LinearObjectiveFunction(
new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0);
@@ -721,13 +749,14 @@ public class SimplexSolverTest {
@Test
public void testSolutionCallback() {
// re-use the problem from testcase for MATH-930
- // it normally requires 186 iterations
+ // it normally requires 113 iterations
final List<LinearConstraint> constraints = createMath930Constraints();
+ //Collections.reverse(constraints);
double[] objFunctionCoeff = new double[33];
objFunctionCoeff[3] = 1;
LinearObjectiveFunction f = new LinearObjectiveFunction(objFunctionCoeff, 0);
- SimplexSolver solver = new SimplexSolver(1e-4, 10, 1e-6);
+ SimplexSolver solver = new SimplexSolver(1e-2, 10, 1e-6);
final SolutionCallback callback = new SolutionCallback();
@@ -735,7 +764,8 @@ public class SimplexSolverTest {
try {
// we need to use a DeterministicLinearConstraintSet to always get the same behavior
solver.optimize(new MaxIter(100), f, new DeterministicLinearConstraintSet(constraints),
- GoalType.MINIMIZE, new NonNegativeConstraint(true), callback);
+ GoalType.MINIMIZE, new NonNegativeConstraint(true), callback,
+ PivotSelectionRule.Bland);
Assert.fail("expected TooManyIterationsException");
} catch (TooManyIterationsException ex) {
// expected
@@ -747,9 +777,10 @@ public class SimplexSolverTest {
// 2. iteration limit allows to reach phase 2, but too low to find an optimal solution
try {
// we need to use a DeterministicLinearConstraintSet to always get the same behavior
- solver.optimize(new MaxIter(180), f, new DeterministicLinearConstraintSet(constraints),
- GoalType.MINIMIZE, new NonNegativeConstraint(true), callback);
- Assert.fail("expected TooManyIterationsException");
+ solver.optimize(new MaxIter(111), f, new DeterministicLinearConstraintSet(constraints),
+ GoalType.MINIMIZE, new NonNegativeConstraint(true), callback,
+ PivotSelectionRule.Bland);
+ //Assert.fail("expected TooManyIterationsException");
} catch (TooManyIterationsException ex) {
// expected
}