You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/03/27 08:56:05 UTC

svn commit: r759045 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/optimization/linear/SimplexTableau.java test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java

Author: luc
Date: Fri Mar 27 07:56:04 2009
New Revision: 759045

URL: http://svn.apache.org/viewvc?rev=759045&view=rev
Log:
fixed an error in Simplex algorithm when several ambiguous solutions exist

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java?rev=759045&r1=759044&r2=759045&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java Fri Mar 27 07:56:04 2009
@@ -327,9 +327,20 @@
      * @return The value of the given decision variable.
      */
     protected double getDecisionVariableValue(final int decisionVariable) {
-        Integer basicRow = getBasicRow(getNumObjectiveFunctions() + decisionVariable);
-        return basicRow == null ? 0 : getEntry(basicRow, getRhsOffset()); 
-    }
+      int col = getNumObjectiveFunctions() + decisionVariable;  
+      Integer basicRow = getBasicRow(col);
+      if (basicRow == null) {
+          return 0;
+      }
+      // if there are multiple variables that can take the value on the RHS
+      // then we'll give the first variable that value
+      for (int i = getNumObjectiveFunctions(); i < col; i++) {
+          if (tableau.getEntry(basicRow, i) == 1) {
+              return 0;
+          }
+      }
+      return getEntry(basicRow, getRhsOffset()); 
+  }
 
     /**
      * Subtracts a multiple of one row from another.

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java?rev=759045&r1=759044&r2=759045&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/optimization/linear/SimplexSolverTest.java Fri Mar 27 07:56:04 2009
@@ -143,7 +143,7 @@
         assertEquals(1438556.7491409, solution.getValue(), .0000001);
     }
 
-    public void testSomething() throws OptimizationException {
+    public void testTrivialModel() throws OptimizationException {
         LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 1 }, 0);
         Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
         constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ,  0));
@@ -276,8 +276,7 @@
 
         SimplexSolver solver = new SimplexSolver();
         RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
-        assertEquals(13366.0, solution.getValue(), .0000001);
-        //assertEquals(7518.0, solution.getValue(), .0000001);
+        assertEquals(7518.0, solution.getValue(), .0000001);
     }
     
     /**