/** 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); } }
/** 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); } }
/** 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); } }
/** 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 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(); }
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; x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); simplex[i] = new PointCostPair(x, Double.NaN);