CMAESOptimizer.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.  *      http://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. package org.apache.commons.math4.legacy.optim.nonlinear.scalar.noderiv;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;

  21. import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
  22. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  23. import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
  24. import org.apache.commons.math4.legacy.exception.OutOfRangeException;
  25. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
  26. import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
  27. import org.apache.commons.math4.legacy.linear.EigenDecomposition;
  28. import org.apache.commons.math4.legacy.linear.MatrixUtils;
  29. import org.apache.commons.math4.legacy.linear.RealMatrix;
  30. import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
  31. import org.apache.commons.math4.legacy.optim.OptimizationData;
  32. import org.apache.commons.math4.legacy.optim.PointValuePair;
  33. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;
  34. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.PopulationSize;
  35. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.Sigma;
  36. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.MultivariateOptimizer;
  37. import org.apache.commons.rng.UniformRandomProvider;
  38. import org.apache.commons.statistics.distribution.ContinuousDistribution;
  39. import org.apache.commons.statistics.distribution.NormalDistribution;
  40. import org.apache.commons.math4.core.jdkmath.JdkMath;

  41. /**
  42.  * An implementation of the active Covariance Matrix Adaptation Evolution Strategy (CMA-ES)
  43.  * for non-linear, non-convex, non-smooth, global function minimization.
  44.  * <p>
  45.  * The CMA-Evolution Strategy (CMA-ES) is a reliable stochastic optimization method
  46.  * which should be applied if derivative-based methods, e.g. quasi-Newton BFGS or
  47.  * conjugate gradient, fail due to a rugged search landscape (e.g. noise, local
  48.  * optima, outlier, etc.) of the objective function. Like a
  49.  * quasi-Newton method, the CMA-ES learns and applies a variable metric
  50.  * on the underlying search space. Unlike a quasi-Newton method, the
  51.  * CMA-ES neither estimates nor uses gradients, making it considerably more
  52.  * reliable in terms of finding a good, or even close to optimal, solution.
  53.  * <p>
  54.  * In general, on smooth objective functions the CMA-ES is roughly ten times
  55.  * slower than BFGS (counting objective function evaluations, no gradients provided).
  56.  * For up to <code>N=10</code> variables also the derivative-free simplex
  57.  * direct search method (Nelder and Mead) can be faster, but it is
  58.  * far less reliable than CMA-ES.
  59.  * <p>
  60.  * The CMA-ES is particularly well suited for non-separable
  61.  * and/or badly conditioned problems. To observe the advantage of CMA compared
  62.  * to a conventional evolution strategy, it will usually take about
  63.  * <code>30 N</code> function evaluations. On difficult problems the complete
  64.  * optimization (a single run) is expected to take <em>roughly</em> between
  65.  * <code>30 N</code> and <code>300 N<sup>2</sup></code>
  66.  * function evaluations.
  67.  * <p>
  68.  * This implementation is translated and adapted from the Matlab version
  69.  * of the CMA-ES algorithm as implemented in module {@code cmaes.m} version 3.51.
  70.  * <p>
  71.  * For more information, please refer to the following links:
  72.  * <ul>
  73.  *  <li><a href="http://www.lri.fr/~hansen/cmaes.m">Matlab code</a></li>
  74.  *  <li><a href="http://www.lri.fr/~hansen/cmaesintro.html">Introduction to CMA-ES</a></li>
  75.  *  <li><a href="http://en.wikipedia.org/wiki/CMA-ES">Wikipedia</a></li>
  76.  * </ul>
  77.  *
  78.  * @since 3.0
  79.  */
  80. public class CMAESOptimizer
  81.     extends MultivariateOptimizer {
  82.     // global search parameters
  83.     /**
  84.      * Population size, offspring number. The primary strategy parameter to play
  85.      * with, which can be increased from its default value. Increasing the
  86.      * population size improves global search properties in exchange to speed.
  87.      * Speed decreases, as a rule, at most linearly with increasing population
  88.      * size. It is advisable to begin with the default small population size.
  89.      */
  90.     private int lambda; // population size
  91.     /**
  92.      * Covariance update mechanism, default is active CMA. isActiveCMA = true
  93.      * turns on "active CMA" with a negative update of the covariance matrix and
  94.      * checks for positive definiteness. OPTS.CMA.active = 2 does not check for
  95.      * pos. def. and is numerically faster. Active CMA usually speeds up the
  96.      * adaptation.
  97.      */
  98.     private final boolean isActiveCMA;
  99.     /**
  100.      * Determines how often a new random offspring is generated in case it is
  101.      * not feasible / beyond the defined limits, default is 0.
  102.      */
  103.     private final int checkFeasableCount;
  104.     /**
  105.      * @see Sigma
  106.      */
  107.     private double[] inputSigma;
  108.     /** Number of objective variables/problem dimension. */
  109.     private int dimension;
  110.     /**
  111.      * Defines the number of initial iterations, where the covariance matrix
  112.      * remains diagonal and the algorithm has internally linear time complexity.
  113.      * diagonalOnly = 1 means keeping the covariance matrix always diagonal and
  114.      * this setting also exhibits linear space complexity. This can be
  115.      * particularly useful for dimension > 100.
  116.      * @see <a href="http://hal.archives-ouvertes.fr/inria-00287367/en">A Simple Modification in CMA-ES</a>
  117.      */
  118.     private int diagonalOnly;
  119.     /** Number of objective variables/problem dimension. */
  120.     private boolean isMinimize = true;
  121.     /** Indicates whether statistic data is collected. */
  122.     private final boolean generateStatistics;

  123.     // termination criteria
  124.     /** Maximal number of iterations allowed. */
  125.     private final int maxIterations;
  126.     /** Limit for fitness value. */
  127.     private final double stopFitness;
  128.     /** Stop if x-changes larger stopTolUpX. */
  129.     private double stopTolUpX;
  130.     /** Stop if x-change smaller stopTolX. */
  131.     private double stopTolX;
  132.     /** Stop if fun-changes smaller stopTolFun. */
  133.     private double stopTolFun;
  134.     /** Stop if back fun-changes smaller stopTolHistFun. */
  135.     private double stopTolHistFun;

  136.     // selection strategy parameters
  137.     /** Number of parents/points for recombination. */
  138.     private int mu; //
  139.     /** log(mu + 0.5), stored for efficiency. */
  140.     private double logMu2;
  141.     /** Array for weighted recombination. */
  142.     private RealMatrix weights;
  143.     /** Variance-effectiveness of sum w_i x_i. */
  144.     private double mueff; //

  145.     // dynamic strategy parameters and constants
  146.     /** Overall standard deviation - search volume. */
  147.     private double sigma;
  148.     /** Cumulation constant. */
  149.     private double cc;
  150.     /** Cumulation constant for step-size. */
  151.     private double cs;
  152.     /** Damping for step-size. */
  153.     private double damps;
  154.     /** Learning rate for rank-one update. */
  155.     private double ccov1;
  156.     /** Learning rate for rank-mu update'. */
  157.     private double ccovmu;
  158.     /** Expectation of ||N(0,I)|| == norm(randn(N,1)). */
  159.     private double chiN;
  160.     /** Learning rate for rank-one update - diagonalOnly. */
  161.     private double ccov1Sep;
  162.     /** Learning rate for rank-mu update - diagonalOnly. */
  163.     private double ccovmuSep;

  164.     // CMA internal values - updated each generation
  165.     /** Objective variables. */
  166.     private RealMatrix xmean;
  167.     /** Evolution path. */
  168.     private RealMatrix pc;
  169.     /** Evolution path for sigma. */
  170.     private RealMatrix ps;
  171.     /** Norm of ps, stored for efficiency. */
  172.     private double normps;
  173.     /** Coordinate system. */
  174.     private RealMatrix B;
  175.     /** Scaling. */
  176.     private RealMatrix D;
  177.     /** B*D, stored for efficiency. */
  178.     private RealMatrix BD;
  179.     /** Diagonal of sqrt(D), stored for efficiency. */
  180.     private RealMatrix diagD;
  181.     /** Covariance matrix. */
  182.     private RealMatrix C;
  183.     /** Diagonal of C, used for diagonalOnly. */
  184.     private RealMatrix diagC;
  185.     /** Number of iterations already performed. */
  186.     private int iterations;

  187.     /** History queue of best values. */
  188.     private double[] fitnessHistory;
  189.     /** Size of history queue of best values. */
  190.     private int historySize;

  191.     /** Gaussian sampler. */
  192.     private final ContinuousDistribution.Sampler random;

  193.     /** History of sigma values. */
  194.     private final List<Double> statisticsSigmaHistory = new ArrayList<>();
  195.     /** History of mean matrix. */
  196.     private final List<RealMatrix> statisticsMeanHistory = new ArrayList<>();
  197.     /** History of fitness values. */
  198.     private final List<Double> statisticsFitnessHistory = new ArrayList<>();
  199.     /** History of D matrix. */
  200.     private final List<RealMatrix> statisticsDHistory = new ArrayList<>();

  201.     /**
  202.      * @param maxIterations Maximal number of iterations.
  203.      * @param stopFitness Whether to stop if objective function value is smaller than
  204.      * {@code stopFitness}.
  205.      * @param isActiveCMA Chooses the covariance matrix update method.
  206.      * @param diagonalOnly Number of initial iterations, where the covariance matrix
  207.      * remains diagonal.
  208.      * @param checkFeasableCount Determines how often new random objective variables are
  209.      * generated in case they are out of bounds.
  210.      * @param rng Random generator.
  211.      * @param generateStatistics Whether statistic data is collected.
  212.      * @param checker Convergence checker.
  213.      *
  214.      * @since 3.1
  215.      */
  216.     public CMAESOptimizer(int maxIterations,
  217.                           double stopFitness,
  218.                           boolean isActiveCMA,
  219.                           int diagonalOnly,
  220.                           int checkFeasableCount,
  221.                           UniformRandomProvider rng,
  222.                           boolean generateStatistics,
  223.                           ConvergenceChecker<PointValuePair> checker) {
  224.         super(checker);
  225.         this.maxIterations = maxIterations;
  226.         this.stopFitness = stopFitness;
  227.         this.isActiveCMA = isActiveCMA;
  228.         this.diagonalOnly = diagonalOnly;
  229.         this.checkFeasableCount = Math.max(0, checkFeasableCount);
  230.         this.random = NormalDistribution.of(0, 1).createSampler(rng);
  231.         this.generateStatistics = generateStatistics;
  232.     }

  233.     /**
  234.      * @return History of sigma values.
  235.      */
  236.     public List<Double> getStatisticsSigmaHistory() {
  237.         return statisticsSigmaHistory;
  238.     }

  239.     /**
  240.      * @return History of mean matrix.
  241.      */
  242.     public List<RealMatrix> getStatisticsMeanHistory() {
  243.         return statisticsMeanHistory;
  244.     }

  245.     /**
  246.      * @return History of fitness values.
  247.      */
  248.     public List<Double> getStatisticsFitnessHistory() {
  249.         return statisticsFitnessHistory;
  250.     }

  251.     /**
  252.      * @return History of D matrix.
  253.      */
  254.     public List<RealMatrix> getStatisticsDHistory() {
  255.         return statisticsDHistory;
  256.     }

  257.     /**
  258.      * {@inheritDoc}
  259.      *
  260.      * @param optData Optimization data. In addition to those documented in
  261.      * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
  262.      * MultivariateOptimizer}, this method will register the following data:
  263.      * <ul>
  264.      *  <li>
  265.      *   {@link Sigma} values define the initial coordinate-wise standard
  266.      *   deviations for sampling new search points around the initial guess.
  267.      *   It is suggested to set them to the estimated distance from the
  268.      *   initial to the desired optimum.
  269.      *   Small values induce the search to be more local (and very small
  270.      *   values are more likely to find a local optimum close to the initial
  271.      *   guess).
  272.      *   Too small values might however lead to early termination.
  273.      *  </li>
  274.      *  <li>
  275.      *   {@link PopulationSize} is the number of offsprings and the primary
  276.      *   strategy parameter.
  277.      *   In the absence of better clues, a good default could be an integer
  278.      *   close to {@code 4 + 3 ln(n)}, where {@code n} is the number of
  279.      *   optimized parameters.
  280.      *   Increasing the population size improves global search properties at
  281.      *   the expense of speed (which in general decreases at most linearly
  282.      *   with increasing population size).
  283.      *  </li>
  284.      * </ul>
  285.      * @return {@inheritDoc}
  286.      * @throws TooManyEvaluationsException if the maximal number of evaluations
  287.      * is exceeded.
  288.      * @throws DimensionMismatchException if the initial guess, target, and
  289.      * weight arguments have inconsistent dimensions.
  290.      */
  291.     @Override
  292.     public PointValuePair optimize(OptimizationData... optData) {
  293.         // Set up base class and perform computation.
  294.         return super.optimize(optData);
  295.     }

  296.     /** {@inheritDoc} */
  297.     @Override
  298.     protected PointValuePair doOptimize() {
  299.          // -------------------- Initialization --------------------------------
  300.         isMinimize = getGoalType().equals(GoalType.MINIMIZE);
  301.         final FitnessFunction fitfun = new FitnessFunction();
  302.         final double[] guess = getStartPoint();
  303.         // number of objective variables/problem dimension
  304.         dimension = guess.length;
  305.         initializeCMA(guess);
  306.         iterations = 0;
  307.         ValuePenaltyPair valuePenalty = fitfun.value(guess);
  308.         double bestValue = valuePenalty.value+valuePenalty.penalty;
  309.         push(fitnessHistory, bestValue);
  310.         PointValuePair optimum
  311.             = new PointValuePair(getStartPoint(),
  312.                                  isMinimize ? bestValue : -bestValue);
  313.         PointValuePair lastResult = null;

  314.         // -------------------- Generation Loop --------------------------------
  315.         generationLoop:
  316.         for (iterations = 1; iterations <= maxIterations; iterations++) {
  317.             incrementIterationCount();

  318.             // Generate and evaluate lambda offspring
  319.             final RealMatrix arz = randn1(dimension, lambda);
  320.             final RealMatrix arx = zeros(dimension, lambda);
  321.             final double[] fitness = new double[lambda];
  322.             final ValuePenaltyPair[] valuePenaltyPairs = new ValuePenaltyPair[lambda];
  323.             // generate random offspring
  324.             for (int k = 0; k < lambda; k++) {
  325.                 RealMatrix arxk = null;
  326.                 for (int i = 0; i <= checkFeasableCount; i++) {
  327.                     if (diagonalOnly <= 0) {
  328.                         arxk = xmean.add(BD.multiply(arz.getColumnMatrix(k))
  329.                                          .scalarMultiply(sigma)); // m + sig * Normal(0,C)
  330.                     } else {
  331.                         arxk = xmean.add(times(diagD,arz.getColumnMatrix(k))
  332.                                          .scalarMultiply(sigma));
  333.                     }
  334.                     if (i >= checkFeasableCount ||
  335.                         fitfun.isFeasible(arxk.getColumn(0))) {
  336.                         break;
  337.                     }
  338.                     // regenerate random arguments for row
  339.                     arz.setColumn(k, randn(dimension));
  340.                 }
  341.                 copyColumn(arxk, 0, arx, k);
  342.                 try {
  343.                     valuePenaltyPairs[k] = fitfun.value(arx.getColumn(k)); // compute fitness
  344.                 } catch (TooManyEvaluationsException e) {
  345.                     break generationLoop;
  346.                 }
  347.             }
  348.             // Compute fitnesses by adding value and penalty after scaling by value range.
  349.             double valueRange = valueRange(valuePenaltyPairs);
  350.             for (int iValue=0;iValue<valuePenaltyPairs.length;iValue++) {
  351.                  fitness[iValue] = valuePenaltyPairs[iValue].value + valuePenaltyPairs[iValue].penalty*valueRange;
  352.             }
  353.             // Sort by fitness and compute weighted mean into xmean
  354.             final int[] arindex = sortedIndices(fitness);
  355.             // Calculate new xmean, this is selection and recombination
  356.             final RealMatrix xold = xmean; // for speed up of Eq. (2) and (3)
  357.             final RealMatrix bestArx = selectColumns(arx, Arrays.copyOf(arindex, mu));
  358.             xmean = bestArx.multiply(weights);
  359.             final RealMatrix bestArz = selectColumns(arz, Arrays.copyOf(arindex, mu));
  360.             final RealMatrix zmean = bestArz.multiply(weights);
  361.             final boolean hsig = updateEvolutionPaths(zmean, xold);
  362.             if (diagonalOnly <= 0) {
  363.                 updateCovariance(hsig, bestArx, arz, arindex, xold);
  364.             } else {
  365.                 updateCovarianceDiagonalOnly(hsig, bestArz);
  366.             }
  367.             // Adapt step size sigma - Eq. (5)
  368.             sigma *= JdkMath.exp(JdkMath.min(1, (normps/chiN - 1) * cs / damps));
  369.             final double bestFitness = fitness[arindex[0]];
  370.             final double worstFitness = fitness[arindex[arindex.length - 1]];
  371.             if (bestValue > bestFitness) {
  372.                 bestValue = bestFitness;
  373.                 lastResult = optimum;
  374.                 optimum = new PointValuePair(fitfun.repair(bestArx.getColumn(0)),
  375.                                              isMinimize ? bestFitness : -bestFitness);
  376.                 if (getConvergenceChecker() != null && lastResult != null &&
  377.                     getConvergenceChecker().converged(iterations, optimum, lastResult)) {
  378.                     break generationLoop;
  379.                 }
  380.             }
  381.             // handle termination criteria
  382.             // Break, if fitness is good enough
  383.             if (stopFitness != 0 && bestFitness < (isMinimize ? stopFitness : -stopFitness)) {
  384.                 break generationLoop;
  385.             }
  386.             final double[] sqrtDiagC = sqrt(diagC).getColumn(0);
  387.             final double[] pcCol = pc.getColumn(0);
  388.             for (int i = 0; i < dimension; i++) {
  389.                 if (sigma * JdkMath.max(JdkMath.abs(pcCol[i]), sqrtDiagC[i]) > stopTolX) {
  390.                     break;
  391.                 }
  392.                 if (i >= dimension - 1) {
  393.                     break generationLoop;
  394.                 }
  395.             }
  396.             for (int i = 0; i < dimension; i++) {
  397.                 if (sigma * sqrtDiagC[i] > stopTolUpX) {
  398.                     break generationLoop;
  399.                 }
  400.             }
  401.             final double historyBest = min(fitnessHistory);
  402.             final double historyWorst = max(fitnessHistory);
  403.             if (iterations > 2 &&
  404.                 JdkMath.max(historyWorst, worstFitness) -
  405.                 JdkMath.min(historyBest, bestFitness) < stopTolFun) {
  406.                 break generationLoop;
  407.             }
  408.             if (iterations > fitnessHistory.length &&
  409.                 historyWorst - historyBest < stopTolHistFun) {
  410.                 break generationLoop;
  411.             }
  412.             // condition number of the covariance matrix exceeds 1e14
  413.             if (max(diagD) / min(diagD) > 1e7) {
  414.                 break generationLoop;
  415.             }
  416.             // user-defined termination
  417.             if (getConvergenceChecker() != null) {
  418.                 final PointValuePair current
  419.                     = new PointValuePair(bestArx.getColumn(0),
  420.                                          isMinimize ? bestFitness : -bestFitness);
  421.                 if (lastResult != null &&
  422.                     getConvergenceChecker().converged(iterations, current, lastResult)) {
  423.                     break generationLoop;
  424.                     }
  425.                 lastResult = current;
  426.             }
  427.             // Adjust step size in case of equal function values (flat fitness)
  428.             if (bestValue == fitness[arindex[(int)(0.1+lambda/4.)]]) {
  429.                 sigma *= JdkMath.exp(0.2 + cs / damps);
  430.             }
  431.             if (iterations > 2 && JdkMath.max(historyWorst, bestFitness) -
  432.                 JdkMath.min(historyBest, bestFitness) == 0) {
  433.                 sigma *= JdkMath.exp(0.2 + cs / damps);
  434.             }
  435.             // store best in history
  436.             push(fitnessHistory,bestFitness);
  437.             if (generateStatistics) {
  438.                 statisticsSigmaHistory.add(sigma);
  439.                 statisticsFitnessHistory.add(bestFitness);
  440.                 statisticsMeanHistory.add(xmean.transpose());
  441.                 statisticsDHistory.add(diagD.transpose().scalarMultiply(1E5));
  442.             }
  443.         }
  444.         return optimum;
  445.     }

  446.     /**
  447.      * Scans the list of (required and optional) optimization data that
  448.      * characterize the problem.
  449.      *
  450.      * @param optData Optimization data. The following data will be looked for:
  451.      * <ul>
  452.      *  <li>{@link Sigma}</li>
  453.      *  <li>{@link PopulationSize}</li>
  454.      * </ul>
  455.      */
  456.     @Override
  457.     protected void parseOptimizationData(OptimizationData... optData) {
  458.         // Allow base class to register its own data.
  459.         super.parseOptimizationData(optData);

  460.         // The existing values (as set by the previous call) are reused if
  461.         // not provided in the argument list.
  462.         for (OptimizationData data : optData) {
  463.             if (data instanceof Sigma) {
  464.                 inputSigma = ((Sigma) data).getSigma();
  465.                 continue;
  466.             }
  467.             if (data instanceof PopulationSize) {
  468.                 lambda = ((PopulationSize) data).getPopulationSize();
  469.                 continue;
  470.             }
  471.         }

  472.         checkParameters();
  473.     }

  474.     /**
  475.      * Checks dimensions and values of boundaries and inputSigma if defined.
  476.      */
  477.     private void checkParameters() {
  478.         if (inputSigma != null) {
  479.             final double[] init = getStartPoint();

  480.             if (inputSigma.length != init.length) {
  481.                 throw new DimensionMismatchException(inputSigma.length, init.length);
  482.             }

  483.             final double[] lB = getLowerBound();
  484.             final double[] uB = getUpperBound();

  485.             for (int i = 0; i < init.length; i++) {
  486.                 if (inputSigma[i] > uB[i] - lB[i]) {
  487.                     throw new OutOfRangeException(inputSigma[i], 0, uB[i] - lB[i]);
  488.                 }
  489.             }
  490.         }
  491.     }

  492.     /**
  493.      * Initialization of the dynamic search parameters.
  494.      *
  495.      * @param guess Initial guess for the arguments of the fitness function.
  496.      */
  497.     private void initializeCMA(double[] guess) {
  498.         if (lambda <= 0) {
  499.             throw new NotStrictlyPositiveException(lambda);
  500.         }
  501.         // initialize sigma
  502.         final double[][] sigmaArray = new double[guess.length][1];
  503.         for (int i = 0; i < guess.length; i++) {
  504.             sigmaArray[i][0] = inputSigma[i];
  505.         }
  506.         final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
  507.         sigma = max(insigma); // overall standard deviation

  508.         // initialize termination criteria
  509.         stopTolUpX = 1e3 * max(insigma);
  510.         stopTolX = 1e-11 * max(insigma);
  511.         stopTolFun = 1e-12;
  512.         stopTolHistFun = 1e-13;

  513.         // initialize selection strategy parameters
  514.         mu = lambda / 2; // number of parents/points for recombination
  515.         logMu2 = JdkMath.log(mu + 0.5);
  516.         weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
  517.         double sumw = 0;
  518.         double sumwq = 0;
  519.         for (int i = 0; i < mu; i++) {
  520.             double w = weights.getEntry(i, 0);
  521.             sumw += w;
  522.             sumwq += w * w;
  523.         }
  524.         weights = weights.scalarMultiply(1 / sumw);
  525.         mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i

  526.         // initialize dynamic strategy parameters and constants
  527.         cc = (4 + mueff / dimension) /
  528.                 (dimension + 4 + 2 * mueff / dimension);
  529.         cs = (mueff + 2) / (dimension + mueff + 3.);
  530.         damps = (1 + 2 * JdkMath.max(0, JdkMath.sqrt((mueff - 1) /
  531.                                                        (dimension + 1)) - 1)) *
  532.             JdkMath.max(0.3,
  533.                          1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
  534.         ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
  535.         ccovmu = JdkMath.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
  536.                               ((dimension + 2) * (dimension + 2) + mueff));
  537.         ccov1Sep = JdkMath.min(1, ccov1 * (dimension + 1.5) / 3);
  538.         ccovmuSep = JdkMath.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
  539.         chiN = JdkMath.sqrt(dimension) *
  540.                 (1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
  541.         // initialize CMA internal values - updated each generation
  542.         xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
  543.         diagD = insigma.scalarMultiply(1 / sigma);
  544.         diagC = square(diagD);
  545.         pc = zeros(dimension, 1); // evolution paths for C and sigma
  546.         ps = zeros(dimension, 1); // B defines the coordinate system
  547.         normps = ps.getFrobeniusNorm();

  548.         B = eye(dimension, dimension);
  549.         D = ones(dimension, 1); // diagonal D defines the scaling
  550.         BD = times(B, repmat(diagD.transpose(), dimension, 1));
  551.         C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
  552.         historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
  553.         fitnessHistory = new double[historySize]; // history of fitness values
  554.         for (int i = 0; i < historySize; i++) {
  555.             fitnessHistory[i] = Double.MAX_VALUE;
  556.         }
  557.     }

  558.     /**
  559.      * Update of the evolution paths ps and pc.
  560.      *
  561.      * @param zmean Weighted row matrix of the gaussian random numbers generating
  562.      * the current offspring.
  563.      * @param xold xmean matrix of the previous generation.
  564.      * @return hsig flag indicating a small correction.
  565.      */
  566.     private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) {
  567.         ps = ps.scalarMultiply(1 - cs).add(
  568.                 B.multiply(zmean).scalarMultiply(
  569.                         JdkMath.sqrt(cs * (2 - cs) * mueff)));
  570.         normps = ps.getFrobeniusNorm();
  571.         final boolean hsig = normps /
  572.             JdkMath.sqrt(1 - JdkMath.pow(1 - cs, 2 * iterations)) /
  573.             chiN < 1.4 + 2 / ((double) dimension + 1);
  574.         pc = pc.scalarMultiply(1 - cc);
  575.         if (hsig) {
  576.             pc = pc.add(xmean.subtract(xold).scalarMultiply(JdkMath.sqrt(cc * (2 - cc) * mueff) / sigma));
  577.         }
  578.         return hsig;
  579.     }

  580.     /**
  581.      * Update of the covariance matrix C for diagonalOnly > 0.
  582.      *
  583.      * @param hsig Flag indicating a small correction.
  584.      * @param bestArz Fitness-sorted matrix of the gaussian random values of the
  585.      * current offspring.
  586.      */
  587.     private void updateCovarianceDiagonalOnly(boolean hsig,
  588.                                               final RealMatrix bestArz) {
  589.         // minor correction if hsig==false
  590.         double oldFac = hsig ? 0 : ccov1Sep * cc * (2 - cc);
  591.         oldFac += 1 - ccov1Sep - ccovmuSep;
  592.         diagC = diagC.scalarMultiply(oldFac) // regard old matrix
  593.             .add(square(pc).scalarMultiply(ccov1Sep)) // plus rank one update
  594.             .add(times(diagC, square(bestArz).multiply(weights)) // plus rank mu update
  595.                  .scalarMultiply(ccovmuSep));
  596.         diagD = sqrt(diagC); // replaces eig(C)
  597.         if (diagonalOnly > 1 &&
  598.             iterations > diagonalOnly) {
  599.             // full covariance matrix from now on
  600.             diagonalOnly = 0;
  601.             B = eye(dimension, dimension);
  602.             BD = diag(diagD);
  603.             C = diag(diagC);
  604.         }
  605.     }

  606.     /**
  607.      * Update of the covariance matrix C.
  608.      *
  609.      * @param hsig Flag indicating a small correction.
  610.      * @param bestArx Fitness-sorted matrix of the argument vectors producing the
  611.      * current offspring.
  612.      * @param arz Unsorted matrix containing the gaussian random values of the
  613.      * current offspring.
  614.      * @param arindex Indices indicating the fitness-order of the current offspring.
  615.      * @param xold xmean matrix of the previous generation.
  616.      */
  617.     private void updateCovariance(boolean hsig, final RealMatrix bestArx,
  618.                                   final RealMatrix arz, final int[] arindex,
  619.                                   final RealMatrix xold) {
  620.         double negccov = 0;
  621.         if (ccov1 + ccovmu > 0) {
  622.             final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu))
  623.                 .scalarMultiply(1 / sigma); // mu difference vectors
  624.             final RealMatrix roneu = pc.multiply(pc.transpose())
  625.                 .scalarMultiply(ccov1); // rank one update
  626.             // minor correction if hsig==false
  627.             double oldFac = hsig ? 0 : ccov1 * cc * (2 - cc);
  628.             oldFac += 1 - ccov1 - ccovmu;
  629.             if (isActiveCMA) {
  630.                 // Adapt covariance matrix C active CMA
  631.                 negccov = (1 - ccovmu) * 0.25 * mueff /
  632.                     (JdkMath.pow(dimension + 2.0, 1.5) + 2 * mueff);
  633.                 // keep at least 0.66 in all directions, small popsize are most
  634.                 // critical
  635.                 final double negminresidualvariance = 0.66;
  636.                 // where to make up for the variance loss
  637.                 final double negalphaold = 0.5;
  638.                 // prepare vectors, compute negative updating matrix Cneg
  639.                 final int[] arReverseIndex = reverse(arindex);
  640.                 RealMatrix arzneg = selectColumns(arz, Arrays.copyOf(arReverseIndex, mu));
  641.                 RealMatrix arnorms = sqrt(sumRows(square(arzneg)));
  642.                 final int[] idxnorms = sortedIndices(arnorms.getRow(0));
  643.                 final RealMatrix arnormsSorted = selectColumns(arnorms, idxnorms);
  644.                 final int[] idxReverse = reverse(idxnorms);
  645.                 final RealMatrix arnormsReverse = selectColumns(arnorms, idxReverse);
  646.                 arnorms = divide(arnormsReverse, arnormsSorted);
  647.                 final int[] idxInv = inverse(idxnorms);
  648.                 final RealMatrix arnormsInv = selectColumns(arnorms, idxInv);
  649.                 // check and set learning rate negccov
  650.                 final double negcovMax = (1 - negminresidualvariance) /
  651.                     square(arnormsInv).multiply(weights).getEntry(0, 0);
  652.                 if (negccov > negcovMax) {
  653.                     negccov = negcovMax;
  654.                 }
  655.                 arzneg = times(arzneg, repmat(arnormsInv, dimension, 1));
  656.                 final RealMatrix artmp = BD.multiply(arzneg);
  657.                 final RealMatrix cNeg = artmp.multiply(diag(weights)).multiply(artmp.transpose());
  658.                 oldFac += negalphaold * negccov;
  659.                 C = C.scalarMultiply(oldFac)
  660.                     .add(roneu) // regard old matrix
  661.                     .add(arpos.scalarMultiply( // plus rank one update
  662.                                               ccovmu + (1 - negalphaold) * negccov) // plus rank mu update
  663.                          .multiply(times(repmat(weights, 1, dimension),
  664.                                          arpos.transpose())))
  665.                     .subtract(cNeg.scalarMultiply(negccov));
  666.             } else {
  667.                 // Adapt covariance matrix C - nonactive
  668.                 C = C.scalarMultiply(oldFac) // regard old matrix
  669.                     .add(roneu) // plus rank one update
  670.                     .add(arpos.scalarMultiply(ccovmu) // plus rank mu update
  671.                          .multiply(times(repmat(weights, 1, dimension),
  672.                                          arpos.transpose())));
  673.             }
  674.         }
  675.         updateBD(negccov);
  676.     }

  677.     /**
  678.      * Update B and D from C.
  679.      *
  680.      * @param negccov Negative covariance factor.
  681.      */
  682.     private void updateBD(double negccov) {
  683.         if (ccov1 + ccovmu + negccov > 0 &&
  684.             (iterations % 1. / (ccov1 + ccovmu + negccov) / dimension / 10.) < 1) {
  685.             // to achieve O(N^2)
  686.             C = triu(C, 0).add(triu(C, 1).transpose());
  687.             // enforce symmetry to prevent complex numbers
  688.             final EigenDecomposition eig = new EigenDecomposition(C);
  689.             B = eig.getV(); // eigen decomposition, B==normalized eigenvectors
  690.             D = eig.getD();
  691.             diagD = diag(D);
  692.             if (min(diagD) <= 0) {
  693.                 for (int i = 0; i < dimension; i++) {
  694.                     if (diagD.getEntry(i, 0) < 0) {
  695.                         diagD.setEntry(i, 0, 0);
  696.                     }
  697.                 }
  698.                 final double tfac = max(diagD) / 1e14;
  699.                 C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
  700.                 diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
  701.             }
  702.             if (max(diagD) > 1e14 * min(diagD)) {
  703.                 final double tfac = max(diagD) / 1e14 - min(diagD);
  704.                 C = C.add(eye(dimension, dimension).scalarMultiply(tfac));
  705.                 diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac));
  706.             }
  707.             diagC = diag(C);
  708.             diagD = sqrt(diagD); // D contains standard deviations now
  709.             BD = times(B, repmat(diagD.transpose(), dimension, 1)); // O(n^2)
  710.         }
  711.     }

  712.     /**
  713.      * Pushes the current best fitness value in a history queue.
  714.      *
  715.      * @param vals History queue.
  716.      * @param val Current best fitness value.
  717.      */
  718.     private static void push(double[] vals, double val) {
  719.         if (vals.length - 1 > 0) {
  720.             System.arraycopy(vals, 0, vals, 1, vals.length - 1);
  721.         }
  722.         vals[0] = val;
  723.     }

  724.     /**
  725.      * Sorts fitness values.
  726.      *
  727.      * @param doubles Array of values to be sorted.
  728.      * @return a sorted array of indices pointing into doubles.
  729.      */
  730.     private int[] sortedIndices(final double[] doubles) {
  731.         final DoubleIndex[] dis = new DoubleIndex[doubles.length];
  732.         for (int i = 0; i < doubles.length; i++) {
  733.             dis[i] = new DoubleIndex(doubles[i], i);
  734.         }
  735.         Arrays.sort(dis);
  736.         final int[] indices = new int[doubles.length];
  737.         for (int i = 0; i < doubles.length; i++) {
  738.             indices[i] = dis[i].index;
  739.         }
  740.         return indices;
  741.     }
  742.    /**
  743.      * Get range of values.
  744.      *
  745.      * @param vpPairs Array of valuePenaltyPairs to get range from.
  746.      * @return a double equal to maximum value minus minimum value.
  747.      */
  748.     private double valueRange(final ValuePenaltyPair[] vpPairs) {
  749.         double max = Double.NEGATIVE_INFINITY;
  750.         double min = Double.MAX_VALUE;
  751.         for (ValuePenaltyPair vpPair:vpPairs) {
  752.             if (vpPair.value > max) {
  753.                 max = vpPair.value;
  754.             }
  755.             if (vpPair.value < min) {
  756.                 min = vpPair.value;
  757.             }
  758.         }
  759.         return max-min;
  760.     }

  761.     /**
  762.      * Used to sort fitness values. Sorting is always in lower value first
  763.      * order.
  764.      */
  765.     private static final class DoubleIndex implements Comparable<DoubleIndex> {
  766.         /** Value to compare. */
  767.         private final double value;
  768.         /** Index into sorted array. */
  769.         private final int index;

  770.         /**
  771.          * @param value Value to compare.
  772.          * @param index Index into sorted array.
  773.          */
  774.         DoubleIndex(double value, int index) {
  775.             this.value = value;
  776.             this.index = index;
  777.         }

  778.         /** {@inheritDoc} */
  779.         @Override
  780.         public int compareTo(DoubleIndex o) {
  781.             return Double.compare(value, o.value);
  782.         }

  783.         /** {@inheritDoc} */
  784.         @Override
  785.         public boolean equals(Object other) {

  786.             if (this == other) {
  787.                 return true;
  788.             }

  789.             if (other instanceof DoubleIndex) {
  790.                 return Double.compare(value, ((DoubleIndex) other).value) == 0;
  791.             }

  792.             return false;
  793.         }

  794.         /** {@inheritDoc} */
  795.         @Override
  796.         public int hashCode() {
  797.             long bits = Double.doubleToLongBits(value);
  798.             return (int) (1438542 ^ (bits >>> 32) ^ bits);
  799.         }
  800.     }
  801.     /**
  802.      * Stores the value and penalty (for repair of out of bounds point).
  803.      */
  804.     private static final class ValuePenaltyPair {
  805.         /** Objective function value. */
  806.         private double value;
  807.         /** Penalty value for repair of out out of bounds points. */
  808.         private double penalty;

  809.         /**
  810.          * @param value Function value.
  811.          * @param penalty Out-of-bounds penalty.
  812.         */
  813.         ValuePenaltyPair(final double value, final double penalty) {
  814.             this.value   = value;
  815.             this.penalty = penalty;
  816.         }
  817.     }


  818.     /**
  819.      * Normalizes fitness values to the range [0,1]. Adds a penalty to the
  820.      * fitness value if out of range.
  821.      */
  822.     private final class FitnessFunction {
  823.         /**
  824.          * Flag indicating whether the objective variables are forced into their
  825.          * bounds if defined.
  826.          */
  827.         private final boolean isRepairMode;

  828.         /** Simple constructor.
  829.          */
  830.         FitnessFunction() {
  831.             isRepairMode = true;
  832.         }

  833.         /**
  834.          * @param point Normalized objective variables.
  835.          * @return the objective value + penalty for violated bounds.
  836.          */
  837.         public ValuePenaltyPair value(final double[] point) {
  838.             final MultivariateFunction func = CMAESOptimizer.this.getObjectiveFunction();
  839.             double value;
  840.             double penalty = 0;
  841.             if (isRepairMode) {
  842.                 double[] repaired = repair(point);
  843.                 value = func.value(repaired);
  844.                 penalty = penalty(point, repaired);
  845.             } else {
  846.                 value = func.value(point);
  847.             }
  848.             value = isMinimize ? value : -value;
  849.             penalty = isMinimize ? penalty : -penalty;
  850.             return new ValuePenaltyPair(value,penalty);
  851.         }

  852.         /**
  853.          * @param x Normalized objective variables.
  854.          * @return {@code true} if in bounds.
  855.          */
  856.         public boolean isFeasible(final double[] x) {
  857.             final double[] lB = CMAESOptimizer.this.getLowerBound();
  858.             final double[] uB = CMAESOptimizer.this.getUpperBound();

  859.             for (int i = 0; i < x.length; i++) {
  860.                 if (x[i] < lB[i]) {
  861.                     return false;
  862.                 }
  863.                 if (x[i] > uB[i]) {
  864.                     return false;
  865.                 }
  866.             }
  867.             return true;
  868.         }

  869.         /**
  870.          * @param x Normalized objective variables.
  871.          * @return the repaired (i.e. all in bounds) objective variables.
  872.          */
  873.         private double[] repair(final double[] x) {
  874.             final double[] lB = CMAESOptimizer.this.getLowerBound();
  875.             final double[] uB = CMAESOptimizer.this.getUpperBound();

  876.             final double[] repaired = new double[x.length];
  877.             for (int i = 0; i < x.length; i++) {
  878.                 if (x[i] < lB[i]) {
  879.                     repaired[i] = lB[i];
  880.                 } else if (x[i] > uB[i]) {
  881.                     repaired[i] = uB[i];
  882.                 } else {
  883.                     repaired[i] = x[i];
  884.                 }
  885.             }
  886.             return repaired;
  887.         }

  888.         /**
  889.          * @param x Normalized objective variables.
  890.          * @param repaired Repaired objective variables.
  891.          * @return Penalty value according to the violation of the bounds.
  892.          */
  893.         private double penalty(final double[] x, final double[] repaired) {
  894.             double penalty = 0;
  895.             for (int i = 0; i < x.length; i++) {
  896.                 double diff = JdkMath.abs(x[i] - repaired[i]);
  897.                 penalty += diff;
  898.             }
  899.             return isMinimize ? penalty : -penalty;
  900.         }
  901.     }

  902.     // -----Matrix utility functions similar to the Matlab build in functions------

  903.     /**
  904.      * @param m Input matrix
  905.      * @return Matrix representing the element-wise logarithm of m.
  906.      */
  907.     private static RealMatrix log(final RealMatrix m) {
  908.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  909.         for (int r = 0; r < m.getRowDimension(); r++) {
  910.             for (int c = 0; c < m.getColumnDimension(); c++) {
  911.                 d[r][c] = JdkMath.log(m.getEntry(r, c));
  912.             }
  913.         }
  914.         return new Array2DRowRealMatrix(d, false);
  915.     }

  916.     /**
  917.      * @param m Input matrix.
  918.      * @return Matrix representing the element-wise square root of m.
  919.      */
  920.     private static RealMatrix sqrt(final RealMatrix m) {
  921.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  922.         for (int r = 0; r < m.getRowDimension(); r++) {
  923.             for (int c = 0; c < m.getColumnDimension(); c++) {
  924.                 d[r][c] = JdkMath.sqrt(m.getEntry(r, c));
  925.             }
  926.         }
  927.         return new Array2DRowRealMatrix(d, false);
  928.     }

  929.     /**
  930.      * @param m Input matrix.
  931.      * @return Matrix representing the element-wise square of m.
  932.      */
  933.     private static RealMatrix square(final RealMatrix m) {
  934.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  935.         for (int r = 0; r < m.getRowDimension(); r++) {
  936.             for (int c = 0; c < m.getColumnDimension(); c++) {
  937.                 double e = m.getEntry(r, c);
  938.                 d[r][c] = e * e;
  939.             }
  940.         }
  941.         return new Array2DRowRealMatrix(d, false);
  942.     }

  943.     /**
  944.      * @param m Input matrix 1.
  945.      * @param n Input matrix 2.
  946.      * @return the matrix where the elements of m and n are element-wise multiplied.
  947.      */
  948.     private static RealMatrix times(final RealMatrix m, final RealMatrix n) {
  949.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  950.         for (int r = 0; r < m.getRowDimension(); r++) {
  951.             for (int c = 0; c < m.getColumnDimension(); c++) {
  952.                 d[r][c] = m.getEntry(r, c) * n.getEntry(r, c);
  953.             }
  954.         }
  955.         return new Array2DRowRealMatrix(d, false);
  956.     }

  957.     /**
  958.      * @param m Input matrix 1.
  959.      * @param n Input matrix 2.
  960.      * @return Matrix where the elements of m and n are element-wise divided.
  961.      */
  962.     private static RealMatrix divide(final RealMatrix m, final RealMatrix n) {
  963.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  964.         for (int r = 0; r < m.getRowDimension(); r++) {
  965.             for (int c = 0; c < m.getColumnDimension(); c++) {
  966.                 d[r][c] = m.getEntry(r, c) / n.getEntry(r, c);
  967.             }
  968.         }
  969.         return new Array2DRowRealMatrix(d, false);
  970.     }

  971.     /**
  972.      * @param m Input matrix.
  973.      * @param cols Columns to select.
  974.      * @return Matrix representing the selected columns.
  975.      */
  976.     private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) {
  977.         final double[][] d = new double[m.getRowDimension()][cols.length];
  978.         for (int r = 0; r < m.getRowDimension(); r++) {
  979.             for (int c = 0; c < cols.length; c++) {
  980.                 d[r][c] = m.getEntry(r, cols[c]);
  981.             }
  982.         }
  983.         return new Array2DRowRealMatrix(d, false);
  984.     }

  985.     /**
  986.      * @param m Input matrix.
  987.      * @param k Diagonal position.
  988.      * @return Upper triangular part of matrix.
  989.      */
  990.     private static RealMatrix triu(final RealMatrix m, int k) {
  991.         final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()];
  992.         for (int r = 0; r < m.getRowDimension(); r++) {
  993.             for (int c = 0; c < m.getColumnDimension(); c++) {
  994.                 d[r][c] = r <= c - k ? m.getEntry(r, c) : 0;
  995.             }
  996.         }
  997.         return new Array2DRowRealMatrix(d, false);
  998.     }

  999.     /**
  1000.      * @param m Input matrix.
  1001.      * @return Row matrix representing the sums of the rows.
  1002.      */
  1003.     private static RealMatrix sumRows(final RealMatrix m) {
  1004.         final double[][] d = new double[1][m.getColumnDimension()];
  1005.         for (int c = 0; c < m.getColumnDimension(); c++) {
  1006.             double sum = 0;
  1007.             for (int r = 0; r < m.getRowDimension(); r++) {
  1008.                 sum += m.getEntry(r, c);
  1009.             }
  1010.             d[0][c] = sum;
  1011.         }
  1012.         return new Array2DRowRealMatrix(d, false);
  1013.     }

  1014.     /**
  1015.      * @param m Input matrix.
  1016.      * @return the diagonal n-by-n matrix if m is a column matrix or the column
  1017.      * matrix representing the diagonal if m is a n-by-n matrix.
  1018.      */
  1019.     private static RealMatrix diag(final RealMatrix m) {
  1020.         if (m.getColumnDimension() == 1) {
  1021.             final double[][] d = new double[m.getRowDimension()][m.getRowDimension()];
  1022.             for (int i = 0; i < m.getRowDimension(); i++) {
  1023.                 d[i][i] = m.getEntry(i, 0);
  1024.             }
  1025.             return new Array2DRowRealMatrix(d, false);
  1026.         } else {
  1027.             final double[][] d = new double[m.getRowDimension()][1];
  1028.             for (int i = 0; i < m.getColumnDimension(); i++) {
  1029.                 d[i][0] = m.getEntry(i, i);
  1030.             }
  1031.             return new Array2DRowRealMatrix(d, false);
  1032.         }
  1033.     }

  1034.     /**
  1035.      * Copies a column from m1 to m2.
  1036.      *
  1037.      * @param m1 Source matrix.
  1038.      * @param col1 Source column.
  1039.      * @param m2 Target matrix.
  1040.      * @param col2 Target column.
  1041.      */
  1042.     private static void copyColumn(final RealMatrix m1, int col1,
  1043.                                    RealMatrix m2, int col2) {
  1044.         for (int i = 0; i < m1.getRowDimension(); i++) {
  1045.             m2.setEntry(i, col2, m1.getEntry(i, col1));
  1046.         }
  1047.     }

  1048.     /**
  1049.      * @param n Number of rows.
  1050.      * @param m Number of columns.
  1051.      * @return n-by-m matrix filled with 1.
  1052.      */
  1053.     private static RealMatrix ones(int n, int m) {
  1054.         final double[][] d = new double[n][m];
  1055.         for (int r = 0; r < n; r++) {
  1056.             Arrays.fill(d[r], 1);
  1057.         }
  1058.         return new Array2DRowRealMatrix(d, false);
  1059.     }

  1060.     /**
  1061.      * @param n Number of rows.
  1062.      * @param m Number of columns.
  1063.      * @return n-by-m matrix of 0 values out of diagonal, and 1 values on
  1064.      * the diagonal.
  1065.      */
  1066.     private static RealMatrix eye(int n, int m) {
  1067.         final double[][] d = new double[n][m];
  1068.         for (int r = 0; r < n; r++) {
  1069.             if (r < m) {
  1070.                 d[r][r] = 1;
  1071.             }
  1072.         }
  1073.         return new Array2DRowRealMatrix(d, false);
  1074.     }

  1075.     /**
  1076.      * @param n Number of rows.
  1077.      * @param m Number of columns.
  1078.      * @return n-by-m matrix of zero values.
  1079.      */
  1080.     private static RealMatrix zeros(int n, int m) {
  1081.         return new Array2DRowRealMatrix(n, m);
  1082.     }

  1083.     /**
  1084.      * @param mat Input matrix.
  1085.      * @param n Number of row replicates.
  1086.      * @param m Number of column replicates.
  1087.      * @return a matrix which replicates the input matrix in both directions.
  1088.      */
  1089.     private static RealMatrix repmat(final RealMatrix mat, int n, int m) {
  1090.         final int rd = mat.getRowDimension();
  1091.         final int cd = mat.getColumnDimension();
  1092.         final double[][] d = new double[n * rd][m * cd];
  1093.         for (int r = 0; r < n * rd; r++) {
  1094.             for (int c = 0; c < m * cd; c++) {
  1095.                 d[r][c] = mat.getEntry(r % rd, c % cd);
  1096.             }
  1097.         }
  1098.         return new Array2DRowRealMatrix(d, false);
  1099.     }

  1100.     /**
  1101.      * @param start Start value.
  1102.      * @param end End value.
  1103.      * @param step Step size.
  1104.      * @return a sequence as column matrix.
  1105.      */
  1106.     private static RealMatrix sequence(double start, double end, double step) {
  1107.         final int size = (int) ((end - start) / step + 1);
  1108.         final double[][] d = new double[size][1];
  1109.         double value = start;
  1110.         for (int r = 0; r < size; r++) {
  1111.             d[r][0] = value;
  1112.             value += step;
  1113.         }
  1114.         return new Array2DRowRealMatrix(d, false);
  1115.     }

  1116.     /**
  1117.      * @param m Input matrix.
  1118.      * @return the maximum of the matrix element values.
  1119.      */
  1120.     private static double max(final RealMatrix m) {
  1121.         double max = -Double.MAX_VALUE;
  1122.         for (int r = 0; r < m.getRowDimension(); r++) {
  1123.             for (int c = 0; c < m.getColumnDimension(); c++) {
  1124.                 double e = m.getEntry(r, c);
  1125.                 if (max < e) {
  1126.                     max = e;
  1127.                 }
  1128.             }
  1129.         }
  1130.         return max;
  1131.     }

  1132.     /**
  1133.      * @param m Input matrix.
  1134.      * @return the minimum of the matrix element values.
  1135.      */
  1136.     private static double min(final RealMatrix m) {
  1137.         double min = Double.MAX_VALUE;
  1138.         for (int r = 0; r < m.getRowDimension(); r++) {
  1139.             for (int c = 0; c < m.getColumnDimension(); c++) {
  1140.                 double e = m.getEntry(r, c);
  1141.                 if (min > e) {
  1142.                     min = e;
  1143.                 }
  1144.             }
  1145.         }
  1146.         return min;
  1147.     }

  1148.     /**
  1149.      * @param m Input array.
  1150.      * @return the maximum of the array values.
  1151.      */
  1152.     private static double max(final double[] m) {
  1153.         double max = -Double.MAX_VALUE;
  1154.         for (int r = 0; r < m.length; r++) {
  1155.             if (max < m[r]) {
  1156.                 max = m[r];
  1157.             }
  1158.         }
  1159.         return max;
  1160.     }

  1161.     /**
  1162.      * @param m Input array.
  1163.      * @return the minimum of the array values.
  1164.      */
  1165.     private static double min(final double[] m) {
  1166.         double min = Double.MAX_VALUE;
  1167.         for (int r = 0; r < m.length; r++) {
  1168.             if (min > m[r]) {
  1169.                 min = m[r];
  1170.             }
  1171.         }
  1172.         return min;
  1173.     }

  1174.     /**
  1175.      * @param indices Input index array.
  1176.      * @return the inverse of the mapping defined by indices.
  1177.      */
  1178.     private static int[] inverse(final int[] indices) {
  1179.         final int[] inverse = new int[indices.length];
  1180.         for (int i = 0; i < indices.length; i++) {
  1181.             inverse[indices[i]] = i;
  1182.         }
  1183.         return inverse;
  1184.     }

  1185.     /**
  1186.      * @param indices Input index array.
  1187.      * @return the indices in inverse order (last is first).
  1188.      */
  1189.     private static int[] reverse(final int[] indices) {
  1190.         final int[] reverse = new int[indices.length];
  1191.         for (int i = 0; i < indices.length; i++) {
  1192.             reverse[i] = indices[indices.length - i - 1];
  1193.         }
  1194.         return reverse;
  1195.     }

  1196.     /**
  1197.      * @param size Length of random array.
  1198.      * @return an array of Gaussian random numbers.
  1199.      */
  1200.     private double[] randn(int size) {
  1201.         final double[] randn = new double[size];
  1202.         for (int i = 0; i < size; i++) {
  1203.             randn[i] = random.sample();
  1204.         }
  1205.         return randn;
  1206.     }

  1207.     /**
  1208.      * @param size Number of rows.
  1209.      * @param popSize Population size.
  1210.      * @return a 2-dimensional matrix of Gaussian random numbers.
  1211.      */
  1212.     private RealMatrix randn1(int size, int popSize) {
  1213.         final double[][] d = new double[size][popSize];
  1214.         for (int r = 0; r < size; r++) {
  1215.             for (int c = 0; c < popSize; c++) {
  1216.                 d[r][c] = random.sample();
  1217.             }
  1218.         }
  1219.         return new Array2DRowRealMatrix(d, false);
  1220.     }
  1221. }