From b285f17023da248bdcb3093283e4b9e930bdd09c Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 16 Dec 2013 21:05:01 +0000 Subject: [PATCH] [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 --- src/changes/changes.xml | 4 ++ .../optim/linear/LinearConstraintSet.java | 6 +-- .../math3/optim/linear/SimplexSolverTest.java | 47 ++----------------- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bef0cbfaf..42cd1cdf8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces! + + The "LinearConstraintSet" will now return the enclosed collection of "LinearConstraint" + objects in the same order as they have been added. + Added support for different pivot selection rules to the "SimplexSolver" by introducing the new "OptimizationData" class "PivotSelectionRule". Currently supported rules are: diff --git a/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java b/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java index cf5279a34..b2a120996 100644 --- a/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java +++ b/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java @@ -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 linearConstraints - = new HashSet(); + private final Set linearConstraints = new LinkedHashSet(); /** * Creates a set containing the given constraints. diff --git a/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java b/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java index 9b348ce44..1d1090948 100644 --- a/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java +++ b/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java @@ -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 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 linearConstraints = new ArrayList(); - - /** - * 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 constraints) { - linearConstraints.addAll(constraints); - } - - /** - * Gets the set of linear constraints. - * - * @return the constraints. - */ - public Collection getConstraints() { - return Collections.unmodifiableList(linearConstraints); - } - } }