/** Replace the worst point of the simplex by a new point. * @param pointCostPair point to insert */ protected void replaceWorstPoint(PointCostPair pointCostPair) { int n = simplex.length - 1; for (int i = 0; i < n; ++i) { if (simplex[i].getCost() > pointCostPair.getCost()) { PointCostPair tmp = simplex[i]; simplex[i] = pointCostPair; pointCostPair = tmp; } } simplex[n] = pointCostPair; }
/** Evaluate all the non-evaluated points of the simplex. * @exception CostException if no cost can be computed for the parameters */ protected void evaluateSimplex() throws CostException { // 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.getCost())) { simplex[i] = new PointCostPair(pair.getPoint(), evaluateCost(pair.getPoint())); } } // sort the simplex from lowest cost to highest cost Arrays.sort(simplex, pointCostPairComparator); }
/** Compute the next simplex of the algorithm. * @exception CostException if the function cannot be evaluated at * some point */ protected void iterateSimplex() throws CostException { while (true) { // save the original vertex PointCostPair[] original = simplex; double originalCost = original[0].getCost(); // perform a reflection step double reflectedCost = evaluateNewSimplex(original, 1.0); if (reflectedCost < originalCost) { // compute the expanded simplex PointCostPair[] reflected = simplex; double expandedCost = evaluateNewSimplex(original, khi); if (reflectedCost <= expandedCost) { // accept the reflected simplex simplex = reflected; } return; } // compute the contracted simplex double contractedCost = evaluateNewSimplex(original, gamma); if (contractedCost < originalCost) { // accept the contracted simplex return; } } }
/** Compute and evaluate a new simplex. * @param original original simplex (to be preserved) * @param coeff linear coefficient * @return smallest cost in the transformed simplex * @exception CostException if the function cannot be evaluated at * some point */ private double evaluateNewSimplex(PointCostPair[] original, double coeff) throws CostException { 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].getPoint(); double[] xTransformed = new double[n]; for (int j = 0; j < n; ++j) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); } simplex[i] = new PointCostPair(xTransformed, Double.NaN); } // evaluate it evaluateSimplex(); return simplex[0].getCost(); }