changed public fields to private and added accessors

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@613633 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-01-20 17:30:18 +00:00
parent c645488d32
commit 81e5bb6166
6 changed files with 47 additions and 33 deletions

View File

@ -543,8 +543,8 @@ public abstract class DirectSearchOptimizer {
// evaluate the cost at all non-evaluated simplex points // evaluate the cost at all non-evaluated simplex points
for (int i = 0; i < simplex.length; ++i) { for (int i = 0; i < simplex.length; ++i) {
PointCostPair pair = simplex[i]; PointCostPair pair = simplex[i];
if (Double.isNaN(pair.cost)) { if (Double.isNaN(pair.getCost())) {
simplex[i] = new PointCostPair(pair.point, evaluateCost(pair.point)); simplex[i] = new PointCostPair(pair.getPoint(), evaluateCost(pair.getPoint()));
} }
} }
@ -559,7 +559,7 @@ public abstract class DirectSearchOptimizer {
protected void replaceWorstPoint(PointCostPair pointCostPair) { protected void replaceWorstPoint(PointCostPair pointCostPair) {
int n = simplex.length - 1; int n = simplex.length - 1;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
if (simplex[i].cost > pointCostPair.cost) { if (simplex[i].getCost() > pointCostPair.getCost()) {
PointCostPair tmp = simplex[i]; PointCostPair tmp = simplex[i];
simplex[i] = pointCostPair; simplex[i] = pointCostPair;
pointCostPair = tmp; pointCostPair = tmp;
@ -576,8 +576,8 @@ public abstract class DirectSearchOptimizer {
} else if (o2 == null) { } else if (o2 == null) {
return -1; return -1;
} }
double cost1 = ((PointCostPair) o1).cost; double cost1 = ((PointCostPair) o1).getCost();
double cost2 = ((PointCostPair) o2).cost; double cost2 = ((PointCostPair) o2).getCost();
return (cost1 < cost2) ? -1 : ((o1 == o2) ? 0 : +1); return (cost1 < cost2) ? -1 : ((o1 == o2) ? 0 : +1);
} }
}; };

View File

@ -55,7 +55,7 @@ public class MultiDirectional
// save the original vertex // save the original vertex
PointCostPair[] original = simplex; PointCostPair[] original = simplex;
double originalCost = original[0].cost; double originalCost = original[0].getCost();
// perform a reflection step // perform a reflection step
double reflectedCost = evaluateNewSimplex(original, 1.0); double reflectedCost = evaluateNewSimplex(original, 1.0);
@ -94,14 +94,14 @@ public class MultiDirectional
private double evaluateNewSimplex(PointCostPair[] original, double coeff) private double evaluateNewSimplex(PointCostPair[] original, double coeff)
throws CostException { throws CostException {
double[] xSmallest = original[0].point; double[] xSmallest = original[0].getPoint();
int n = xSmallest.length; int n = xSmallest.length;
// create the linearly transformed simplex // create the linearly transformed simplex
simplex = new PointCostPair[n + 1]; simplex = new PointCostPair[n + 1];
simplex[0] = original[0]; simplex[0] = original[0];
for (int i = 1; i <= n; ++i) { for (int i = 1; i <= n; ++i) {
double[] xOriginal = original[i].point; double[] xOriginal = original[i].getPoint();
double[] xTransformed = new double[n]; double[] xTransformed = new double[n];
for (int j = 0; j < n; ++j) { for (int j = 0; j < n; ++j) {
xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
@ -111,7 +111,7 @@ public class MultiDirectional
// evaluate it // evaluate it
evaluateSimplex(); evaluateSimplex();
return simplex[0].cost; return simplex[0].getCost();
} }

View File

@ -62,16 +62,16 @@ public class NelderMead
int n = simplex.length - 1; int n = simplex.length - 1;
// interesting costs // interesting costs
double smallest = simplex[0].cost; double smallest = simplex[0].getCost();
double secondLargest = simplex[n-1].cost; double secondLargest = simplex[n-1].getCost();
double largest = simplex[n].cost; double largest = simplex[n].getCost();
double[] xLargest = simplex[n].point; double[] xLargest = simplex[n].getPoint();
// compute the centroid of the best vertices // compute the centroid of the best vertices
// (dismissing the worst point at index n) // (dismissing the worst point at index n)
double[] centroid = new double[n]; double[] centroid = new double[n];
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
double[] x = simplex[i].point; double[] x = simplex[i].getPoint();
for (int j = 0; j < n; ++j) { for (int j = 0; j < n; ++j) {
centroid[j] += x[j]; centroid[j] += x[j];
} }
@ -145,9 +145,9 @@ public class NelderMead
} }
// perform a shrink // perform a shrink
double[] xSmallest = simplex[0].point; double[] xSmallest = simplex[0].getPoint();
for (int i = 1; i < simplex.length; ++i) { 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) { for (int j = 0; j < n; ++j) {
x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]);
} }

View File

@ -33,10 +33,24 @@ public class PointCostPair {
this.cost = cost; 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. */ /** Point coordinates. */
public final double[] point; private final double[] point;
/** Cost associated to the point. */ /** Cost associated to the point. */
public final double cost; private final double cost;
} }

View File

@ -89,7 +89,7 @@ public class MultiDirectionalTest
}); });
assertTrue(count > 60); 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[] { 3.0, -1.0, 0.0, 1.0 },
new double[] { 4.0, 0.0, 1.0, 2.0 }); new double[] { 4.0, 0.0, 1.0, 2.0 });
assertTrue(count > 850); 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) { public boolean converged(PointCostPair[] simplex) {
PointCostPair smallest = simplex[0]; PointCostPair smallest = simplex[0];
PointCostPair largest = simplex[simplex.length - 1]; PointCostPair largest = simplex[simplex.length - 1];
return (largest.cost - smallest.cost) < threshold; return (largest.getCost() - smallest.getCost()) < threshold;
} }
private double threshold; private double threshold;

View File

@ -110,9 +110,9 @@ public class NelderMeadTest
assertTrue(count > 700); assertTrue(count > 700);
assertTrue(count < 800); assertTrue(count < 800);
assertEquals(0.0, optimum.cost, 5.0e-5); assertEquals(0.0, optimum.getCost(), 5.0e-5);
assertEquals(1.0, optimum.point[0], 0.01); assertEquals(1.0, optimum.getPoint()[0], 0.01);
assertEquals(1.0, optimum.point[1], 0.01); assertEquals(1.0, optimum.getPoint()[1], 0.01);
PointCostPair[] minima = nm.getMinima(); PointCostPair[] minima = nm.getMinima();
assertEquals(10, minima.length); assertEquals(10, minima.length);
@ -125,7 +125,7 @@ public class NelderMeadTest
} }
} else { } else {
if (i > 0) { 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)); new UniformRandomGenerator(rg));
optimum = optimum =
nm.minimizes(rosenbrock, 100, new ValueChecker(1.0e-3), rvg); 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 = optimum =
nm.minimizes(rosenbrock, 100, new ValueChecker(1.0e-3), rvg, 3); 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 }, new double[] { 4.0, 0.0, 1.0, 2.0 },
1, 1642738l); 1, 1642738l);
assertTrue(count < 150); assertTrue(count < 150);
assertEquals(0.0, optimum.cost, 6.0e-4); assertEquals(0.0, optimum.getCost(), 6.0e-4);
assertEquals(0.0, optimum.point[0], 0.07); assertEquals(0.0, optimum.getPoint()[0], 0.07);
assertEquals(0.0, optimum.point[1], 0.07); assertEquals(0.0, optimum.getPoint()[1], 0.07);
assertEquals(0.0, optimum.point[2], 0.07); assertEquals(0.0, optimum.getPoint()[2], 0.07);
assertEquals(0.0, optimum.point[3], 0.07); assertEquals(0.0, optimum.getPoint()[3], 0.07);
} }
@ -185,7 +185,7 @@ public class NelderMeadTest
public boolean converged(PointCostPair[] simplex) { public boolean converged(PointCostPair[] simplex) {
PointCostPair smallest = simplex[0]; PointCostPair smallest = simplex[0];
PointCostPair largest = simplex[simplex.length - 1]; PointCostPair largest = simplex[simplex.length - 1];
return (largest.cost - smallest.cost) < threshold; return (largest.getCost() - smallest.getCost()) < threshold;
} }
private double threshold; private double threshold;