Author: tn Date: Mon Dec 16 21:05:01 2013 New Revision: 1551355 URL: http://svn.apache.org/r1551355 Log: [MATH-1080] LinearConstraintSet returns now the LinearConstraints in the same order as they have been added.
Modified: commons/proper/math/trunk/src/changes/changes.xml commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.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=1551355&r1=1551354&r2=1551355&view=diff ============================================================================== --- commons/proper/math/trunk/src/changes/changes.xml (original) +++ commons/proper/math/trunk/src/changes/changes.xml Mon Dec 16 21:05:01 2013 @@ -51,6 +51,10 @@ If the output is not quite correct, chec </properties> <body> <release version="3.3" date="TBD" description="TBD"> + <action dev="tn" type="update" issue="MATH-1080"> + The "LinearConstraintSet" will now return the enclosed collection of "LinearConstraint" + objects in the same order as they have been added. + </action> <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: Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java?rev=1551355&r1=1551354&r2=1551355&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java Mon Dec 16 21:05:01 2013 @@ -16,10 +16,11 @@ */ package org.apache.commons.math3.optim.linear; +import java.util.LinkedHashSet; import java.util.Set; -import java.util.HashSet; import java.util.Collection; import java.util.Collections; + import org.apache.commons.math3.optim.OptimizationData; /** @@ -30,8 +31,7 @@ import org.apache.commons.math3.optim.Op */ public class LinearConstraintSet implements OptimizationData { /** Set of constraints. */ - private final Set<LinearConstraint> linearConstraints - = new HashSet<LinearConstraint>(); + private final Set<LinearConstraint> linearConstraints = new LinkedHashSet<LinearConstraint>(); /** * Creates a set containing the given constraints. 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=1551355&r1=1551354&r2=1551355&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 Mon Dec 16 21:05:01 2013 @@ -18,7 +18,6 @@ package org.apache.commons.math3.optim.l import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; import org.apache.commons.math3.exception.TooManyIterationsException; @@ -99,7 +98,7 @@ public class SimplexSolverTest { double epsilon = 1e-6; PointValuePair solution = new SimplexSolver().optimize(DEFAULT_MAX_ITER, f, - new DeterministicLinearConstraintSet(constraints), + new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND); Assert.assertEquals(1.0d, solution.getValue(), epsilon); @@ -753,7 +752,6 @@ public class SimplexSolverTest { // re-use the problem from testcase for MATH-930 // it normally requires 113 iterations final List<LinearConstraint> constraints = createMath930Constraints(); - //Collections.reverse(constraints); double[] objFunctionCoeff = new double[33]; objFunctionCoeff[3] = 1; @@ -765,7 +763,7 @@ public class SimplexSolverTest { // 1. iteration limit is too low to reach phase 2 -> no feasible solution try { // we need to use a DeterministicLinearConstraintSet to always get the same behavior - solver.optimize(new MaxIter(100), f, new DeterministicLinearConstraintSet(constraints), + solver.optimize(new MaxIter(100), f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), callback, PivotSelectionRule.BLAND); Assert.fail("expected TooManyIterationsException"); @@ -779,10 +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(111), f, new DeterministicLinearConstraintSet(constraints), + solver.optimize(new MaxIter(112), f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), callback, PivotSelectionRule.BLAND); - //Assert.fail("expected TooManyIterationsException"); + Assert.fail("expected TooManyIterationsException"); } catch (TooManyIterationsException ex) { // expected } @@ -856,42 +854,5 @@ public class SimplexSolverTest { return true; } - - /** - * Needed for deterministic tests, as the original LinearConstraintSet uses as HashSet. - */ - public class DeterministicLinearConstraintSet extends LinearConstraintSet { - /** Set of constraints. */ - private final List<LinearConstraint> linearConstraints = new ArrayList<LinearConstraint>(); - - /** - * Creates a set containing the given constraints. - * - * @param constraints Constraints. - */ - public DeterministicLinearConstraintSet(LinearConstraint... constraints) { - for (LinearConstraint c : constraints) { - linearConstraints.add(c); - } - } - - /** - * Creates a set containing the given constraints. - * - * @param constraints Constraints. - */ - public DeterministicLinearConstraintSet(Collection<LinearConstraint> constraints) { - linearConstraints.addAll(constraints); - } - - /** - * Gets the set of linear constraints. - * - * @return the constraints. - */ - public Collection<LinearConstraint> getConstraints() { - return Collections.unmodifiableList(linearConstraints); - } - } }