AbstractSimplex.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.optim.nonlinear.scalar.noderiv;

  22. import java.util.Arrays;
  23. import java.util.Comparator;

  24. import org.hipparchus.analysis.MultivariateFunction;
  25. import org.hipparchus.exception.LocalizedCoreFormats;
  26. import org.hipparchus.exception.MathIllegalArgumentException;
  27. import org.hipparchus.exception.NullArgumentException;
  28. import org.hipparchus.optim.LocalizedOptimFormats;
  29. import org.hipparchus.optim.OptimizationData;
  30. import org.hipparchus.optim.PointValuePair;
  31. import org.hipparchus.util.MathUtils;

  32. /**
  33.  * This class implements the simplex concept.
  34.  * It is intended to be used in conjunction with {@link SimplexOptimizer}.
  35.  * <br>
  36.  * The initial configuration of the simplex is set by the constructors
  37.  * {@link #AbstractSimplex(double[])} or {@link #AbstractSimplex(double[][])}.
  38.  * The other {@link #AbstractSimplex(int) constructor} will set all steps
  39.  * to 1, thus building a default configuration from a unit hypercube.
  40.  * <br>
  41.  * Users <em>must</em> call the {@link #build(double[]) build} method in order
  42.  * to create the data structure that will be acted on by the other methods of
  43.  * this class.
  44.  *
  45.  * @see SimplexOptimizer
  46.  */
  47. public abstract class AbstractSimplex implements OptimizationData {
  48.     /** Simplex. */
  49.     private PointValuePair[] simplex;
  50.     /** Start simplex configuration. */
  51.     private double[][] startConfiguration;
  52.     /** Simplex dimension (must be equal to {@code simplex.length - 1}). */
  53.     private final int dimension;

  54.     /**
  55.      * Build a unit hypercube simplex.
  56.      *
  57.      * @param n Dimension of the simplex.
  58.      */
  59.     protected AbstractSimplex(int n) {
  60.         this(n, 1d);
  61.     }

  62.     /**
  63.      * Build a hypercube simplex with the given side length.
  64.      *
  65.      * @param n Dimension of the simplex.
  66.      * @param sideLength Length of the sides of the hypercube.
  67.      */
  68.     protected AbstractSimplex(int n,
  69.                               double sideLength) {
  70.         this(createHypercubeSteps(n, sideLength));
  71.     }

  72.     /**
  73.      * The start configuration for simplex is built from a box parallel to
  74.      * the canonical axes of the space. The simplex is the subset of vertices
  75.      * of a box parallel to the canonical axes. It is built as the path followed
  76.      * while traveling from one vertex of the box to the diagonally opposite
  77.      * vertex moving only along the box edges. The first vertex of the box will
  78.      * be located at the start point of the optimization.
  79.      * As an example, in dimension 3 a simplex has 4 vertices. Setting the
  80.      * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the
  81.      * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }.
  82.      * The first vertex would be set to the start point at (1, 1, 1) and the
  83.      * last vertex would be set to the diagonally opposite vertex at (2, 11, 3).
  84.      *
  85.      * @param steps Steps along the canonical axes representing box edges. They
  86.      * may be negative but not zero.
  87.      * @throws NullArgumentException if {@code steps} is {@code null}.
  88.      * @throws MathIllegalArgumentException if one of the steps is zero.
  89.      */
  90.     protected AbstractSimplex(final double[] steps) {
  91.         if (steps == null) {
  92.             throw new NullArgumentException();
  93.         }
  94.         if (steps.length == 0) {
  95.             throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NOT_ALLOWED);
  96.         }
  97.         dimension = steps.length;

  98.         // Only the relative position of the n final vertices with respect
  99.         // to the first one are stored.
  100.         startConfiguration = new double[dimension][dimension];
  101.         for (int i = 0; i < dimension; i++) {
  102.             final double[] vertexI = startConfiguration[i];
  103.             for (int j = 0; j < i + 1; j++) {
  104.                 if (steps[j] == 0) {
  105.                     throw new MathIllegalArgumentException(LocalizedOptimFormats.EQUAL_VERTICES_IN_SIMPLEX);
  106.                 }
  107.                 System.arraycopy(steps, 0, vertexI, 0, j + 1);
  108.             }
  109.         }
  110.     }

  111.     /**
  112.      * The real initial simplex will be set up by moving the reference
  113.      * simplex such that its first point is located at the start point of the
  114.      * optimization.
  115.      *
  116.      * @param referenceSimplex Reference simplex.
  117.      * @throws MathIllegalArgumentException if the reference simplex does not
  118.      * contain at least one point.
  119.      * @throws MathIllegalArgumentException if there is a dimension mismatch
  120.      * in the reference simplex.
  121.      * @throws IllegalArgumentException if one of its vertices is duplicated.
  122.      */
  123.     protected AbstractSimplex(final double[][] referenceSimplex) {
  124.         if (referenceSimplex.length <= 0) {
  125.             throw new MathIllegalArgumentException(LocalizedOptimFormats.SIMPLEX_NEED_ONE_POINT,
  126.                                                    referenceSimplex.length);
  127.         }
  128.         dimension = referenceSimplex.length - 1;

  129.         // Only the relative position of the n final vertices with respect
  130.         // to the first one are stored.
  131.         startConfiguration = new double[dimension][dimension];
  132.         final double[] ref0 = referenceSimplex[0];

  133.         // Loop over vertices.
  134.         for (int i = 0; i < referenceSimplex.length; i++) {
  135.             final double[] refI = referenceSimplex[i];

  136.             // Safety checks.
  137.             if (refI.length != dimension) {
  138.                 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  139.                                                        refI.length, dimension);
  140.             }
  141.             for (int j = 0; j < i; j++) {
  142.                 final double[] refJ = referenceSimplex[j];
  143.                 boolean allEquals = true;
  144.                 for (int k = 0; k < dimension; k++) {
  145.                     if (refI[k] != refJ[k]) {
  146.                         allEquals = false;
  147.                         break;
  148.                     }
  149.                 }
  150.                 if (allEquals) {
  151.                     throw new MathIllegalArgumentException(LocalizedOptimFormats.EQUAL_VERTICES_IN_SIMPLEX,
  152.                                                            i, j);
  153.                 }
  154.             }

  155.             // Store vertex i position relative to vertex 0 position.
  156.             if (i > 0) {
  157.                 final double[] confI = startConfiguration[i - 1];
  158.                 for (int k = 0; k < dimension; k++) {
  159.                     confI[k] = refI[k] - ref0[k];
  160.                 }
  161.             }
  162.         }
  163.     }

  164.     /**
  165.      * Get simplex dimension.
  166.      *
  167.      * @return the dimension of the simplex.
  168.      */
  169.     public int getDimension() {
  170.         return dimension;
  171.     }

  172.     /**
  173.      * Get simplex size.
  174.      * After calling the {@link #build(double[]) build} method, this method will
  175.      * will be equivalent to {@code getDimension() + 1}.
  176.      *
  177.      * @return the size of the simplex.
  178.      */
  179.     public int getSize() {
  180.         return simplex.length;
  181.     }

  182.     /**
  183.      * Compute the next simplex of the algorithm.
  184.      *
  185.      * @param evaluationFunction Evaluation function.
  186.      * @param comparator Comparator to use to sort simplex vertices from best
  187.      * to worst.
  188.      * @throws org.hipparchus.exception.MathIllegalStateException
  189.      * if the algorithm fails to converge.
  190.      */
  191.     public abstract void iterate(MultivariateFunction evaluationFunction,
  192.                                  Comparator<PointValuePair> comparator);

  193.     /**
  194.      * Build an initial simplex.
  195.      *
  196.      * @param startPoint First point of the simplex.
  197.      * @throws MathIllegalArgumentException if the start point does not match
  198.      * simplex dimension.
  199.      */
  200.     public void build(final double[] startPoint) {
  201.         if (dimension != startPoint.length) {
  202.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  203.                                                    dimension, startPoint.length);
  204.         }

  205.         // Set first vertex.
  206.         simplex = new PointValuePair[dimension + 1];
  207.         simplex[0] = new PointValuePair(startPoint, Double.NaN);

  208.         // Set remaining vertices.
  209.         for (int i = 0; i < dimension; i++) {
  210.             final double[] confI = startConfiguration[i];
  211.             final double[] vertexI = new double[dimension];
  212.             for (int k = 0; k < dimension; k++) {
  213.                 vertexI[k] = startPoint[k] + confI[k];
  214.             }
  215.             simplex[i + 1] = new PointValuePair(vertexI, Double.NaN);
  216.         }
  217.     }

  218.     /**
  219.      * Evaluate all the non-evaluated points of the simplex.
  220.      *
  221.      * @param evaluationFunction Evaluation function.
  222.      * @param comparator Comparator to use to sort simplex vertices from best to worst.
  223.      * @throws org.hipparchus.exception.MathIllegalStateException
  224.      * if the maximal number of evaluations is exceeded.
  225.      */
  226.     public void evaluate(final MultivariateFunction evaluationFunction,
  227.                          final Comparator<PointValuePair> comparator) {
  228.         // Evaluate the objective function at all non-evaluated simplex points.
  229.         for (int i = 0; i < simplex.length; i++) {
  230.             final PointValuePair vertex = simplex[i];
  231.             final double[] point = vertex.getPointRef();
  232.             if (Double.isNaN(vertex.getValue())) {
  233.                 simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
  234.             }
  235.         }

  236.         // Sort the simplex from best to worst.
  237.         Arrays.sort(simplex, comparator);
  238.     }

  239.     /**
  240.      * Replace the worst point of the simplex by a new point.
  241.      *
  242.      * @param pointValuePair Point to insert.
  243.      * @param comparator Comparator to use for sorting the simplex vertices
  244.      * from best to worst.
  245.      */
  246.     protected void replaceWorstPoint(PointValuePair pointValuePair,
  247.                                      final Comparator<PointValuePair> comparator) {
  248.         for (int i = 0; i < dimension; i++) {
  249.             if (comparator.compare(simplex[i], pointValuePair) > 0) {
  250.                 PointValuePair tmp = simplex[i];
  251.                 simplex[i] = pointValuePair;
  252.                 pointValuePair = tmp;
  253.             }
  254.         }
  255.         simplex[dimension] = pointValuePair;
  256.     }

  257.     /**
  258.      * Get the points of the simplex.
  259.      *
  260.      * @return all the simplex points.
  261.      */
  262.     public PointValuePair[] getPoints() {
  263.         final PointValuePair[] copy = new PointValuePair[simplex.length];
  264.         System.arraycopy(simplex, 0, copy, 0, simplex.length);
  265.         return copy;
  266.     }

  267.     /**
  268.      * Get the simplex point stored at the requested {@code index}.
  269.      *
  270.      * @param index Location.
  271.      * @return the point at location {@code index}.
  272.      */
  273.     public PointValuePair getPoint(int index) {
  274.         MathUtils.checkRangeInclusive(index, 0, simplex.length - 1);
  275.         return simplex[index];
  276.     }

  277.     /**
  278.      * Store a new point at location {@code index}.
  279.      * Note that no deep-copy of {@code point} is performed.
  280.      *
  281.      * @param index Location.
  282.      * @param point New value.
  283.      */
  284.     protected void setPoint(int index, PointValuePair point) {
  285.         MathUtils.checkRangeInclusive(index, 0, simplex.length - 1);
  286.         simplex[index] = point;
  287.     }

  288.     /**
  289.      * Replace all points.
  290.      * Note that no deep-copy of {@code points} is performed.
  291.      *
  292.      * @param points New Points.
  293.      */
  294.     protected void setPoints(PointValuePair[] points) {
  295.         if (points.length != simplex.length) {
  296.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
  297.                                                    points.length, simplex.length);
  298.         }
  299.         simplex = points.clone();
  300.     }

  301.     /**
  302.      * Create steps for a unit hypercube.
  303.      *
  304.      * @param n Dimension of the hypercube.
  305.      * @param sideLength Length of the sides of the hypercube.
  306.      * @return the steps.
  307.      */
  308.     private static double[] createHypercubeSteps(int n,
  309.                                                  double sideLength) {
  310.         final double[] steps = new double[n];
  311.         for (int i = 0; i < n; i++) {
  312.             steps[i] = sideLength;
  313.         }
  314.         return steps;
  315.     }
  316. }