<commit_before>#include <hop_bits/optimisationAlgorithm/populationBasedAlgorithm/covarianceMatrixAdaptationEvolutionStrategy.hpp>

namespace hop {

  CovarianceMatrixAdaptationEvolutionStrategy::CovarianceMatrixAdaptationEvolutionStrategy(const std::shared_ptr<OptimisationProblem> optimisationProblem, const unsigned int& populationSize)
  : PopulationBasedAlgorithm(optimisationProblem, populationSize) {
    setStepsize(0.3);
  }

  void CovarianceMatrixAdaptationEvolutionStrategy::optimiseImplementation() {
    unsigned int numberOfDimensions = optimisationProblem_->getNumberOfDimensions();
    arma::Col<double> objectiveValues = arma::randu<arma::Col<double>>(numberOfDimensions);

    //init selection parameters
    //unsigned int lambda_ = 4 + std::floor(3 * std::log(numberOfDimensions)); //overwritten by constructor value
    double numberOfParents = populationSize_ / 2;
    arma::Col<double> weights = arma::Col<double>(numberOfParents);

    //starting the second log at 1 is important ;)
    for (std::size_t n = 0; n < weights.n_elem; ++n) {
      weights.at(n) = std::log(numberOfParents + 0.5) - std::log(n + 1);
    }

    numberOfParents = std::floor(numberOfParents);
    double weightSum = arma::sum(weights);
    for (std::size_t n = 0; n < weights.n_elem; ++n) {
      weights.at(n) = weights.at(n) / weightSum;
    }
    double varianceEffectiveness = std::pow(arma::sum(weights), 2) / arma::sum(arma::square(weights));
    //init adaptation parameters
    double cc = (4 + varianceEffectiveness / numberOfDimensions) / (numberOfDimensions + 4 + 2 * varianceEffectiveness / numberOfDimensions); //time constant for cumulation for C
    double cs = (varianceEffectiveness + 2) / (numberOfDimensions + varianceEffectiveness + 5); //t-const for cumulation for sigma control
    double c1 = 2 / (std::pow(numberOfDimensions + 1.3, 2) + varianceEffectiveness); //learning rate for rank-one update of C
    double cmu = std::min(1 - c1, 2 * (varianceEffectiveness - 2 + 1 / varianceEffectiveness) / (std::pow(numberOfDimensions + 2, 2) + varianceEffectiveness)); //and for rank-mu update
    double damps = 1 + 2 * std::max(0.0, std::sqrt((varianceEffectiveness - 1) / (numberOfDimensions + 1)) - 1) + cs; //damping for sigma 
    //init dynamic parameters and constants
    arma::Col<double> pc(numberOfDimensions, arma::fill::zeros);
    arma::Col<double> ps(numberOfDimensions, arma::fill::zeros);
    arma::Mat<double> B(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    arma::Col<double> D(numberOfDimensions, arma::fill::ones);
    arma::Mat<double> C(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    arma::Mat<double> invsqrtC(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    double chiN = std::sqrt(numberOfDimensions) * (1.0 - (1.0 / (4.0 * numberOfDimensions)) + (1.0 / (21.0 * std::pow(numberOfDimensions, 2))));

    //LOOP START
    while (!isFinished() && !isTerminated()) {
      //generate and evaluate lambda offspring
      arma::Col<double> arfitness = arma::Col<double>(populationSize_);
      arma::Mat<double> arx = arma::Mat<double>(numberOfDimensions, populationSize_);
      for (std::size_t n = 0; n < populationSize_; ++n) {
        arx.col(n) = objectiveValues + stepSize_ * B * (D % arma::randn<arma::Col<double>>(numberOfDimensions));
        arfitness(n) = optimisationProblem_->getObjectiveValue(arx.col(n));
        ++numberOfIterations_;

        //TODO: For performance, this could be done in another loop after the best have been selected. Not sure if really worth it
        if (arfitness(n) < bestObjectiveValue_) {<commit_msg>devel: Simplified calculating the weights<commit_after>#include <hop_bits/optimisationAlgorithm/populationBasedAlgorithm/covarianceMatrixAdaptationEvolutionStrategy.hpp>

namespace hop {

  CovarianceMatrixAdaptationEvolutionStrategy::CovarianceMatrixAdaptationEvolutionStrategy(const std::shared_ptr<OptimisationProblem> optimisationProblem, const unsigned int& populationSize)
  : PopulationBasedAlgorithm(optimisationProblem, populationSize) {
    setStepsize(0.3);
  }

  void CovarianceMatrixAdaptationEvolutionStrategy::optimiseImplementation() {
    unsigned int numberOfDimensions = optimisationProblem_->getNumberOfDimensions();
    arma::Col<double> objectiveValues = arma::randu<arma::Col<double>>(numberOfDimensions);

    //init selection parameters
    //unsigned int lambda_ = 4 + std::floor(3 * std::log(numberOfDimensions)); //overwritten by constructor value
    unsigned int numberOfParents = populationSize_ / 2;

    arma::Col<double> weights = arma::normalise(std::log((populationSize_ + 1) / 2) - arma::log(arma::linspace(1, std::floor(numberOfParents), std::floor(numberOfParents))), 1);

    double varianceEffectiveness = std::pow(arma::sum(weights), 2) / arma::sum(arma::square(weights));
    //init adaptation parameters
    double cc = (4 + varianceEffectiveness / numberOfDimensions) / (numberOfDimensions + 4 + 2 * varianceEffectiveness / numberOfDimensions); //time constant for cumulation for C
    double cs = (varianceEffectiveness + 2) / (numberOfDimensions + varianceEffectiveness + 5); //t-const for cumulation for sigma control
    double c1 = 2 / (std::pow(numberOfDimensions + 1.3, 2) + varianceEffectiveness); //learning rate for rank-one update of C
    double cmu = std::min(1 - c1, 2 * (varianceEffectiveness - 2 + 1 / varianceEffectiveness) / (std::pow(numberOfDimensions + 2, 2) + varianceEffectiveness)); //and for rank-mu update
    double damps = 1 + 2 * std::max(0.0, std::sqrt((varianceEffectiveness - 1) / (numberOfDimensions + 1)) - 1) + cs; //damping for sigma 
    //init dynamic parameters and constants
    arma::Col<double> pc(numberOfDimensions, arma::fill::zeros);
    arma::Col<double> ps(numberOfDimensions, arma::fill::zeros);
    arma::Mat<double> B(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    arma::Col<double> D(numberOfDimensions, arma::fill::ones);
    arma::Mat<double> C(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    arma::Mat<double> invsqrtC(numberOfDimensions, numberOfDimensions, arma::fill::eye);
    double chiN = std::sqrt(numberOfDimensions) * (1.0 - (1.0 / (4.0 * numberOfDimensions)) + (1.0 / (21.0 * std::pow(numberOfDimensions, 2))));

    //LOOP START
    while (!isFinished() && !isTerminated()) {
      //generate and evaluate lambda offspring
      arma::Col<double> arfitness = arma::Col<double>(populationSize_);
      arma::Mat<double> arx = arma::Mat<double>(numberOfDimensions, populationSize_);
      for (std::size_t n = 0; n < populationSize_; ++n) {
        arx.col(n) = objectiveValues + stepSize_ * B * (D % arma::randn<arma::Col<double>>(numberOfDimensions));
        arfitness(n) = optimisationProblem_->getObjectiveValue(arx.col(n));
        ++numberOfIterations_;

        //TODO: For performance, this could be done in another loop after the best have been selected. Not sure if really worth it
        if (arfitness(n) < bestObjectiveValue_) {
{'fraction_non_alphanumeric': 0.13229627515341802, 'fraction_numerical': 0.013700585129156558, 'mean_word_length': 4.933954276037257, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2567300', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2105, 'n_words': 486}