diff --git a/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java b/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java index a6d7419cf..b0d114eea 100644 --- a/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java +++ b/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java @@ -272,12 +272,10 @@ class SimplexTableau implements Serializable { private Integer getBasicRow(final int col) { Integer row = null; for (int i = getNumObjectiveFunctions(); i < getHeight(); i++) { - if (!MathUtils.equals(getEntry(i, col), 0.0, epsilon)) { - if (row == null) { - row = i; - } else { - return null; - } + if (MathUtils.equals(getEntry(i, col), 1.0, epsilon) && (row == null)) { + row = i; + } else if (!MathUtils.equals(getEntry(i, col), 0.0, epsilon)) { + return null; } } return row; diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 4f53d0cb4..a1fe400a6 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -39,6 +39,9 @@ The type attribute can be add,update,fix,remove. + + Fixed a wrong check for basic variables + Fixed a problem when setting some variables (several variables were set instead of only one) diff --git a/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java b/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java index 806618577..954ad7ca0 100644 --- a/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java +++ b/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java @@ -64,6 +64,18 @@ public class SimplexSolverTest { assertEquals(57.0, solution.getValue(), 0.0); } + @Test + public void testSingleVariableAndConstraint() throws OptimizationException { + LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3 }, 0); + Collection constraints = new ArrayList(); + constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.LEQ, 10)); + + SimplexSolver solver = new SimplexSolver(); + RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); + assertEquals(10.0, solution.getPoint()[0], 0.0); + assertEquals(30.0, solution.getValue(), 0.0); + } + /** * With no artificial variables needed (no equals and no greater than * constraints) we can go straight to Phase 2.