diff --git a/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java b/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java index b097e6e06..43a3e1fe7 100644 --- a/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java +++ b/src/java/org/apache/commons/math/optimization/DirectSearchOptimizer.java @@ -543,8 +543,8 @@ public abstract class DirectSearchOptimizer { // evaluate the cost at all non-evaluated simplex points for (int i = 0; i < simplex.length; ++i) { PointCostPair pair = simplex[i]; - if (Double.isNaN(pair.cost)) { - simplex[i] = new PointCostPair(pair.point, evaluateCost(pair.point)); + if (Double.isNaN(pair.getCost())) { + simplex[i] = new PointCostPair(pair.getPoint(), evaluateCost(pair.getPoint())); } } @@ -559,7 +559,7 @@ public abstract class DirectSearchOptimizer { protected void replaceWorstPoint(PointCostPair pointCostPair) { int n = simplex.length - 1; for (int i = 0; i < n; ++i) { - if (simplex[i].cost > pointCostPair.cost) { + if (simplex[i].getCost() > pointCostPair.getCost()) { PointCostPair tmp = simplex[i]; simplex[i] = pointCostPair; pointCostPair = tmp; @@ -576,8 +576,8 @@ public abstract class DirectSearchOptimizer { } else if (o2 == null) { return -1; } - double cost1 = ((PointCostPair) o1).cost; - double cost2 = ((PointCostPair) o2).cost; + double cost1 = ((PointCostPair) o1).getCost(); + double cost2 = ((PointCostPair) o2).getCost(); return (cost1 < cost2) ? -1 : ((o1 == o2) ? 0 : +1); } }; diff --git a/src/java/org/apache/commons/math/optimization/MultiDirectional.java b/src/java/org/apache/commons/math/optimization/MultiDirectional.java index 704ac0332..e80238cb4 100644 --- a/src/java/org/apache/commons/math/optimization/MultiDirectional.java +++ b/src/java/org/apache/commons/math/optimization/MultiDirectional.java @@ -55,7 +55,7 @@ public class MultiDirectional // save the original vertex PointCostPair[] original = simplex; - double originalCost = original[0].cost; + double originalCost = original[0].getCost(); // perform a reflection step double reflectedCost = evaluateNewSimplex(original, 1.0); @@ -94,14 +94,14 @@ public class MultiDirectional private double evaluateNewSimplex(PointCostPair[] original, double coeff) throws CostException { - double[] xSmallest = original[0].point; + double[] xSmallest = original[0].getPoint(); int n = xSmallest.length; // create the linearly transformed simplex simplex = new PointCostPair[n + 1]; simplex[0] = original[0]; for (int i = 1; i <= n; ++i) { - double[] xOriginal = original[i].point; + double[] xOriginal = original[i].getPoint(); double[] xTransformed = new double[n]; for (int j = 0; j < n; ++j) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); @@ -111,7 +111,7 @@ public class MultiDirectional // evaluate it evaluateSimplex(); - return simplex[0].cost; + return simplex[0].getCost(); } diff --git a/src/java/org/apache/commons/math/optimization/NelderMead.java b/src/java/org/apache/commons/math/optimization/NelderMead.java index 8f789a347..29d882689 100644 --- a/src/java/org/apache/commons/math/optimization/NelderMead.java +++ b/src/java/org/apache/commons/math/optimization/NelderMead.java @@ -62,16 +62,16 @@ public class NelderMead int n = simplex.length - 1; // interesting costs - double smallest = simplex[0].cost; - double secondLargest = simplex[n-1].cost; - double largest = simplex[n].cost; - double[] xLargest = simplex[n].point; + double smallest = simplex[0].getCost(); + double secondLargest = simplex[n-1].getCost(); + double largest = simplex[n].getCost(); + double[] xLargest = simplex[n].getPoint(); // compute the centroid of the best vertices // (dismissing the worst point at index n) double[] centroid = new double[n]; for (int i = 0; i < n; ++i) { - double[] x = simplex[i].point; + double[] x = simplex[i].getPoint(); for (int j = 0; j < n; ++j) { centroid[j] += x[j]; } @@ -145,9 +145,9 @@ public class NelderMead } // perform a shrink - double[] xSmallest = simplex[0].point; + double[] xSmallest = simplex[0].getPoint(); for (int i = 1; i < simplex.length; ++i) { - double[] x = simplex[i].point; + double[] x = simplex[i].getPoint(); for (int j = 0; j < n; ++j) { x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); } diff --git a/src/java/org/apache/commons/math/optimization/PointCostPair.java b/src/java/org/apache/commons/math/optimization/PointCostPair.java index 26da2eb45..6211fd11f 100644 --- a/src/java/org/apache/commons/math/optimization/PointCostPair.java +++ b/src/java/org/apache/commons/math/optimization/PointCostPair.java @@ -33,10 +33,24 @@ public class PointCostPair { this.cost = cost; } + /** Get the point. + * @return the stored point + */ + public double[] getPoint() { + return point; + } + + /** Get the cost. + * @return the stored cost + */ + public double getCost() { + return cost; + } + /** Point coordinates. */ - public final double[] point; + private final double[] point; /** Cost associated to the point. */ - public final double cost; + private final double cost; } diff --git a/src/test/org/apache/commons/math/optimization/MultiDirectionalTest.java b/src/test/org/apache/commons/math/optimization/MultiDirectionalTest.java index bdd86289f..cafcee578 100644 --- a/src/test/org/apache/commons/math/optimization/MultiDirectionalTest.java +++ b/src/test/org/apache/commons/math/optimization/MultiDirectionalTest.java @@ -89,7 +89,7 @@ public class MultiDirectionalTest }); assertTrue(count > 60); - assertTrue(optimum.cost > 0.01); + assertTrue(optimum.getCost() > 0.01); } @@ -114,7 +114,7 @@ public class MultiDirectionalTest new double[] { 3.0, -1.0, 0.0, 1.0 }, new double[] { 4.0, 0.0, 1.0, 2.0 }); assertTrue(count > 850); - assertTrue(optimum.cost > 0.015); + assertTrue(optimum.getCost() > 0.015); } @@ -127,7 +127,7 @@ public class MultiDirectionalTest public boolean converged(PointCostPair[] simplex) { PointCostPair smallest = simplex[0]; PointCostPair largest = simplex[simplex.length - 1]; - return (largest.cost - smallest.cost) < threshold; + return (largest.getCost() - smallest.getCost()) < threshold; } private double threshold; diff --git a/src/test/org/apache/commons/math/optimization/NelderMeadTest.java b/src/test/org/apache/commons/math/optimization/NelderMeadTest.java index 2c6a13076..5b7ce1119 100644 --- a/src/test/org/apache/commons/math/optimization/NelderMeadTest.java +++ b/src/test/org/apache/commons/math/optimization/NelderMeadTest.java @@ -110,9 +110,9 @@ public class NelderMeadTest assertTrue(count > 700); assertTrue(count < 800); - assertEquals(0.0, optimum.cost, 5.0e-5); - assertEquals(1.0, optimum.point[0], 0.01); - assertEquals(1.0, optimum.point[1], 0.01); + assertEquals(0.0, optimum.getCost(), 5.0e-5); + assertEquals(1.0, optimum.getPoint()[0], 0.01); + assertEquals(1.0, optimum.getPoint()[1], 0.01); PointCostPair[] minima = nm.getMinima(); assertEquals(10, minima.length); @@ -125,7 +125,7 @@ public class NelderMeadTest } } else { if (i > 0) { - assertTrue(minima[i-1].cost <= minima[i].cost); + assertTrue(minima[i-1].getCost() <= minima[i].getCost()); } } } @@ -138,10 +138,10 @@ public class NelderMeadTest new UniformRandomGenerator(rg)); optimum = nm.minimizes(rosenbrock, 100, new ValueChecker(1.0e-3), rvg); - assertEquals(0.0, optimum.cost, 2.0e-4); + assertEquals(0.0, optimum.getCost(), 2.0e-4); optimum = nm.minimizes(rosenbrock, 100, new ValueChecker(1.0e-3), rvg, 3); - assertEquals(0.0, optimum.cost, 3.0e-5); + assertEquals(0.0, optimum.getCost(), 3.0e-5); } @@ -168,11 +168,11 @@ public class NelderMeadTest new double[] { 4.0, 0.0, 1.0, 2.0 }, 1, 1642738l); assertTrue(count < 150); - assertEquals(0.0, optimum.cost, 6.0e-4); - assertEquals(0.0, optimum.point[0], 0.07); - assertEquals(0.0, optimum.point[1], 0.07); - assertEquals(0.0, optimum.point[2], 0.07); - assertEquals(0.0, optimum.point[3], 0.07); + assertEquals(0.0, optimum.getCost(), 6.0e-4); + assertEquals(0.0, optimum.getPoint()[0], 0.07); + assertEquals(0.0, optimum.getPoint()[1], 0.07); + assertEquals(0.0, optimum.getPoint()[2], 0.07); + assertEquals(0.0, optimum.getPoint()[3], 0.07); } @@ -185,7 +185,7 @@ public class NelderMeadTest public boolean converged(PointCostPair[] simplex) { PointCostPair smallest = simplex[0]; PointCostPair largest = simplex[simplex.length - 1]; - return (largest.cost - smallest.cost) < threshold; + return (largest.getCost() - smallest.getCost()) < threshold; } private double threshold;