fixed an error induced by zero entries in simplex solver

JIRA: MATH-288

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@807738 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-08-25 18:07:13 +00:00
parent dbdff0758b
commit 38983e8207
3 changed files with 23 additions and 4 deletions

View File

@ -77,9 +77,10 @@ public class SimplexSolver extends AbstractLinearOptimizer {
double minRatio = Double.MAX_VALUE; double minRatio = Double.MAX_VALUE;
Integer minRatioPos = null; Integer minRatioPos = null;
for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
double rhs = tableau.getEntry(i, tableau.getWidth() - 1); final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
if (MathUtils.compareTo(tableau.getEntry(i, col), 0, epsilon) >= 0) { final double entry = tableau.getEntry(i, col);
double ratio = rhs / tableau.getEntry(i, col); if (MathUtils.compareTo(entry, 0, epsilon) > 0) {
final double ratio = rhs / entry;
if (ratio < minRatio) { if (ratio < minRatio) {
minRatio = ratio; minRatio = ratio;
minRatioPos = i; minRatioPos = i;

View File

@ -39,6 +39,9 @@ The <action> type attribute can be add,update,fix,remove.
</properties> </properties>
<body> <body>
<release version="2.1" date="TBD" description="TBD"> <release version="2.1" date="TBD" description="TBD">
<action dev="luc" type="fix" issue="MATH-288" due-to="Benjamin McCann">
Fixed an error induced by entries set to 0
</action>
<action dev="luc" type="fix" issue="MATH-286" due-to="Benjamin McCann"> <action dev="luc" type="fix" issue="MATH-286" due-to="Benjamin McCann">
Fixed an error leading the simplex solver to compute the right solution Fixed an error leading the simplex solver to compute the right solution
but return another one but return another one

View File

@ -57,7 +57,22 @@ public class SimplexSolverTest {
RealPointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MAXIMIZE, true); RealPointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MAXIMIZE, true);
assertEquals(6.9, solution.getValue(), .0000001); assertEquals(6.9, solution.getValue(), .0000001);
} }
@Test
public void testMath288() throws OptimizationException {
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0 );
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
constraints.add(new LinearConstraint(new double[] { 3, 0, -5, 0 }, Relationship.LEQ, 0.0));
constraints.add(new LinearConstraint(new double[] { 2, 0, 0, -5 }, Relationship.LEQ, 0.0));
constraints.add(new LinearConstraint(new double[] { 0, 3, 0, -5 }, Relationship.LEQ, 0.0));
constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, 1.0));
constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, 1.0));
SimplexSolver solver = new SimplexSolver();
RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true);
assertEquals(10.0, solution.getValue(), .0000001);
}
@Test @Test
public void testSimplexSolver() throws OptimizationException { public void testSimplexSolver() throws OptimizationException {
LinearObjectiveFunction f = LinearObjectiveFunction f =