|
| 1 | +/** |
| 2 | + * @file de.hpp |
| 3 | + * @author Rahul Ganesh Prabhu |
| 4 | + * |
| 5 | + * Differential Evolution is a method used for global optimization of arbitrary |
| 6 | + * functions that optimizes a problem by iteratively trying to improve a |
| 7 | + * candidate solution. |
| 8 | + * |
| 9 | + * ensmallen is free software; you may redistribute it and/or modify it under |
| 10 | + * the terms of the 3-clause BSD license. You should have received a copy of |
| 11 | + * the 3-clause BSD license along with ensmallen. If not, see |
| 12 | + * http://www.opensource.org/licenses/BSD-3-Clause for more information. |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef ENSMALLEN_DE_DE_HPP |
| 16 | +#define ENSMALLEN_DE_DE_HPP |
| 17 | + |
| 18 | +namespace ens { |
| 19 | + |
| 20 | +/** |
| 21 | + * Differential evolution is a stochastic evolutionary algorithm used for global |
| 22 | + * optimization. This class implements the best/1/bin strategy of differential |
| 23 | + * evolution to converge a given function to minima. |
| 24 | + * |
| 25 | + * The algorithm works by generating a fixed number of candidates from the |
| 26 | + * given starting point. At each pass through the population, the algorithm |
| 27 | + * mutates each candidate solution to create a trial solution. If the trial |
| 28 | + * solution is better than the candidate, it is replaced in the |
| 29 | + * population. |
| 30 | + * |
| 31 | + * The evolution takes place in two steps: |
| 32 | + * - Mutation |
| 33 | + * - Crossover |
| 34 | + * |
| 35 | + * Mutation is done by generating a new candidate solution from the best |
| 36 | + * candidate of the previous solution and two random other candidates. |
| 37 | + * |
| 38 | + * Crossover is done by mixing the parameters of the candidate solution and the |
| 39 | + * mutant solution. This is done only if a randomly generated number between 0 |
| 40 | + * and 1 is greater than the crossover rate. |
| 41 | + * |
| 42 | + * The final value and the parameters are returned by the Optimize() method. |
| 43 | + * |
| 44 | + * For more information, see the following: |
| 45 | + * |
| 46 | + * @techreport{storn1995, |
| 47 | + * title = {Differential Evolution—a simple and efficient adaptive scheme |
| 48 | + * for global optimization over continuous spaces}, |
| 49 | + * author = {Storn, Rainer and Price, Kenneth}, |
| 50 | + * year = 1995 |
| 51 | + * } |
| 52 | + * |
| 53 | + * DE can optimize arbitrary functions. For more details, see the |
| 54 | + * documentation on function types included with this distribution or on the |
| 55 | + * ensmallen website. |
| 56 | + */ |
| 57 | +class DE |
| 58 | +{ |
| 59 | + public: |
| 60 | + /** |
| 61 | + * Constructor for the DE optimizer |
| 62 | + * |
| 63 | + * The default values provided over here are not necessarily suitable for a |
| 64 | + * given function. Therefore it is highly recommended to adjust the |
| 65 | + * parameters according to the problem. |
| 66 | + * |
| 67 | + * @param populationSize The number of candidates in the population. |
| 68 | + * This should be at least 3 in size. |
| 69 | + * @param maxGenerations The maximum number of generations allowed for CNE. |
| 70 | + * @param crossoverRate The probability that a crossover will occur. |
| 71 | + * @param differentialWeight A parameter used in the mutation of candidate |
| 72 | + * solutions controls amplification factor of the differentiation. |
| 73 | + * @param tolerance The final value of the objective function for termination. |
| 74 | + */ |
| 75 | + DE(const size_t populationSize = 100, |
| 76 | + const size_t maxGenerations = 2000, |
| 77 | + const double crossoverRate = 0.6, |
| 78 | + const double differentialWeight = 0.8, |
| 79 | + const double tolerance = 1e-5); |
| 80 | + |
| 81 | + /** |
| 82 | + * Optimize the given function using DE. The given |
| 83 | + * starting point will be modified to store the finishing point of the |
| 84 | + * algorithm, and the final objective value is returned. |
| 85 | + * |
| 86 | + * @tparam DecomposableFunctionType Type of the function to be optimized. |
| 87 | + * @param function Function to optimize. |
| 88 | + * @param iterate Starting point (will be modified). |
| 89 | + * @return Objective value of the final point. |
| 90 | + */ |
| 91 | + template<typename DecomposableFunctionType> |
| 92 | + double Optimize(DecomposableFunctionType& function, arma::mat& iterate); |
| 93 | + |
| 94 | + //! Get the population size. |
| 95 | + size_t PopulationSize() const { return populationSize; } |
| 96 | + //! Modify the population size. |
| 97 | + size_t& PopulationSize() { return populationSize; } |
| 98 | + |
| 99 | + //! Get maximum number of generations. |
| 100 | + size_t MaxGenerations() const { return maxGenerations; } |
| 101 | + //! Modify maximum number of generations. |
| 102 | + size_t& MaxGenerations() { return maxGenerations; } |
| 103 | + |
| 104 | + //! Get crossover rate. |
| 105 | + double CrossoverRate() const { return crossoverRate; } |
| 106 | + //! Modify crossover rate. |
| 107 | + double& CrossoverRate() { return crossoverRate; } |
| 108 | + |
| 109 | + //! Get differential weight. |
| 110 | + double DifferentialWeight() const {return differentialWeight; } |
| 111 | + //! Modify differential weight. |
| 112 | + double& DifferentialWeight() { return differentialWeight; } |
| 113 | + |
| 114 | + //! Get the tolerance. |
| 115 | + double Tolerance() const { return tolerance; } |
| 116 | + //! Modify the tolerance. |
| 117 | + double& Tolerance() { return tolerance; } |
| 118 | + |
| 119 | + private: |
| 120 | + //! Population matrix. Each column is a candidate. |
| 121 | + arma::cube population; |
| 122 | + |
| 123 | + //! Vector of fitness values corresponding to each candidate. |
| 124 | + arma::vec fitnessValues; |
| 125 | + |
| 126 | + //! The number of candidates in the population. |
| 127 | + size_t populationSize; |
| 128 | + |
| 129 | + //! Maximum number of generations before termination criteria is met. |
| 130 | + size_t maxGenerations; |
| 131 | + |
| 132 | + //! Probability that crossover will occur. |
| 133 | + double crossoverRate; |
| 134 | + |
| 135 | + //! Amplification factor for differentiation. |
| 136 | + double differentialWeight; |
| 137 | + |
| 138 | + //! The tolerance for termination. |
| 139 | + double tolerance; |
| 140 | +}; |
| 141 | + |
| 142 | +} // namespace ens |
| 143 | + |
| 144 | +// Include implementation. |
| 145 | +#include "de_impl.hpp" |
| 146 | + |
| 147 | +#endif |
0 commit comments