[MATH-1080] LinearConstraintSet returns now the LinearConstraints in the same order as they have been added.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1551355 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-12-16 21:05:01 +00:00
parent f7222ca6c3
commit b285f17023
3 changed files with 11 additions and 46 deletions

View File

@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces!
</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:

View File

@ -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.OptimizationData;
*/
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.

View File

@ -18,7 +18,6 @@ package org.apache.commons.math3.optim.linear;
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);
}
}
}