/** 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); }
/** Build a simplex from all its points. * @param vertices array containing all vertices of the simplex */ private void buildSimplex(double[][] vertices) { int n = vertices.length - 1; simplex = new PointCostPair[n + 1]; for (int i = 0; i <= n; ++i) { simplex[i] = new PointCostPair(vertices[i], Double.NaN); } }
/** 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; }
/** Build a simplex from two extreme vertices. * <p>The two vertices are considered to represent two opposite * vertices of a box parallel to the canonical axes of the * space. The simplex is the subset of vertices encountered while * going from vertexA to vertexB traveling along the box edges * only. This can be seen as a scaled regular simplex using the * projected separation between the given points as the scaling * factor along each coordinate axis.</p> * @param vertexA first vertex * @param vertexB last vertex */ private void buildSimplex(double[] vertexA, double[] vertexB) { int n = vertexA.length; simplex = new PointCostPair[n + 1]; // set up the simplex traveling around the box for (int i = 0; i <= n; ++i) { double[] vertex = new double[n]; if (i > 0) { System.arraycopy(vertexB, 0, vertex, 0, i); } if (i < n) { System.arraycopy(vertexA, i, vertex, i, n - i); } simplex[i] = new PointCostPair(vertex, Double.NaN); } }
/** 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(); }
/** 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; } } }
/** Build a simplex randomly. * @param generator random vector generator */ private void buildSimplex(RandomVectorGenerator generator) { // use first vector size to compute the number of points double[] vertex = generator.nextVector(); int n = vertex.length; simplex = new PointCostPair[n + 1]; simplex[0] = new PointCostPair(vertex, Double.NaN); // fill up the vertex for (int i = 1; i <= n; ++i) { simplex[i] = new PointCostPair(generator.nextVector(), Double.NaN); } }
double smallest = simplex[0].getCost(); double secondLargest = simplex[n-1].getCost(); double largest = simplex[n].getCost(); double[] xLargest = simplex[n].getPoint(); double[] x = simplex[i].getPoint(); for (int j = 0; j < n; ++j) { centroid[j] += x[j]; replaceWorstPoint(new PointCostPair(xR, costR)); replaceWorstPoint(new PointCostPair(xE, costE)); } else { replaceWorstPoint(new PointCostPair(xR, costR)); replaceWorstPoint(new PointCostPair(xC, costC)); return; replaceWorstPoint(new PointCostPair(xC, costC)); return; double[] xSmallest = simplex[0].getPoint(); for (int i = 1; i < simplex.length; ++i) { double[] x = simplex[i].getPoint(); for (int j = 0; j < n; ++j) { x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); simplex[i] = new PointCostPair(x, Double.NaN);