<commit_before>#include <optimisationProblem/optimisationProblem.hpp>

#include <string>
using std::to_string;

#include <stdexcept>
using std::runtime_error;
using std::logic_error;

#include <iostream>
using std::cout;
using std::endl;

#include <armadillo>
using arma::zeros;
using arma::ones;
using arma::eye;

namespace hop {
  OptimisationProblem::OptimisationProblem(const unsigned int& numberOfDimensions) :
    _numberOfDimensions(numberOfDimensions),
    _numberOfEvaluations(0)
  {
    setLowerBounds(zeros<Col<double>>(numberOfDimensions) - numeric_limits<double>::max());
    setUpperBounds(zeros<Col<double>>(numberOfDimensions) + numeric_limits<double>::max());
    setParameterTranslation(zeros<Col<double>>(numberOfDimensions));
    setParameterRotation(eye<Mat<double>>(numberOfDimensions, numberOfDimensions));
    setParameterScale(ones<Col<double>>(numberOfDimensions));
    setObjectiveValueTranslation(0.0);
    setObjectiveValueScale(1.0);
    setMaximalNumberOfEvaluations(1000);
    setAcceptableObjectiveValue(numeric_limits<double>::lowest());
  }

  Col<uword> OptimisationProblem::isSatisfyingLowerBounds(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingLowerBounds.find(parameter);
    if(cachePosition == _cachedIsSatisfyingLowerBounds.end()) {
      Col<uword> result = (getScaledCongruentParameter(parameter) >= _lowerBounds);

      _cachedIsSatisfyingLowerBounds.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  Col<uword> OptimisationProblem::isSatisfyingUpperBounds(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingUpperBounds.find(parameter);
    if(cachePosition == _cachedIsSatisfyingUpperBounds.end()) {
      Col<uword> result = (getScaledCongruentParameter(parameter) <= _upperBounds);

      _cachedIsSatisfyingUpperBounds.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  bool OptimisationProblem::isSatisfyingSoftConstraints(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingSoftConstraints.find(parameter);
    if(cachePosition == _cachedIsSatisfyingSoftConstraints.end()) {
      bool result = (getSoftConstraintsValue(parameter) == 0);

      _cachedIsSatisfyingSoftConstraints.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  bool OptimisationProblem::isSatisfyingConstraints(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingConstraints.find(parameter);
    if(cachePosition == _cachedIsSatisfyingConstraints.end()) {
      bool result = (all(isSatisfyingLowerBounds(parameter)) && all(isSatisfyingUpperBounds(parameter)) && isSatisfyingSoftConstraints(parameter));

      _cachedIsSatisfyingConstraints.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  double OptimisationProblem::getSoftConstraintsValue(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedSoftConstraintsValues.find(parameter);
    if(cachePosition == _cachedSoftConstraintsValues.end()) {
      double result = getSoftConstraintsValueImplementation(getScaledCongruentParameter(parameter));

      _cachedSoftConstraintsValues.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  double OptimisationProblem::getObjectiveValue(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    if(_numberOfEvaluations >= _maximalNumberOfEvaluations) {
      throw runtime_error("Reached maximal number of evaluations (" + to_string(_maximalNumberOfEvaluations) + ").");
    }

    _numberOfEvaluations++;

    auto cachePosition = _cachedObjectivValues.find(parameter);
    if(cachePosition == _cachedObjectivValues.end()) {<commit_msg>devel: Removed limitations on the maximal number of evaluations<commit_after>#include <optimisationProblem/optimisationProblem.hpp>

#include <string>
using std::to_string;

#include <stdexcept>
using std::runtime_error;
using std::logic_error;

#include <iostream>
using std::cout;
using std::endl;

#include <armadillo>
using arma::zeros;
using arma::ones;
using arma::eye;

namespace hop {
  OptimisationProblem::OptimisationProblem(const unsigned int& numberOfDimensions) :
    _numberOfDimensions(numberOfDimensions),
    _numberOfEvaluations(0)
  {
    setLowerBounds(zeros<Col<double>>(numberOfDimensions) - numeric_limits<double>::max());
    setUpperBounds(zeros<Col<double>>(numberOfDimensions) + numeric_limits<double>::max());
    setParameterTranslation(zeros<Col<double>>(numberOfDimensions));
    setParameterRotation(eye<Mat<double>>(numberOfDimensions, numberOfDimensions));
    setParameterScale(ones<Col<double>>(numberOfDimensions));
    setObjectiveValueTranslation(0.0);
    setObjectiveValueScale(1.0);
    setMaximalNumberOfEvaluations(1000);
    setAcceptableObjectiveValue(numeric_limits<double>::lowest());
  }

  Col<uword> OptimisationProblem::isSatisfyingLowerBounds(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingLowerBounds.find(parameter);
    if(cachePosition == _cachedIsSatisfyingLowerBounds.end()) {
      Col<uword> result = (getScaledCongruentParameter(parameter) >= _lowerBounds);

      _cachedIsSatisfyingLowerBounds.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  Col<uword> OptimisationProblem::isSatisfyingUpperBounds(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingUpperBounds.find(parameter);
    if(cachePosition == _cachedIsSatisfyingUpperBounds.end()) {
      Col<uword> result = (getScaledCongruentParameter(parameter) <= _upperBounds);

      _cachedIsSatisfyingUpperBounds.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  bool OptimisationProblem::isSatisfyingSoftConstraints(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingSoftConstraints.find(parameter);
    if(cachePosition == _cachedIsSatisfyingSoftConstraints.end()) {
      bool result = (getSoftConstraintsValue(parameter) == 0);

      _cachedIsSatisfyingSoftConstraints.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  bool OptimisationProblem::isSatisfyingConstraints(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedIsSatisfyingConstraints.find(parameter);
    if(cachePosition == _cachedIsSatisfyingConstraints.end()) {
      bool result = (all(isSatisfyingLowerBounds(parameter)) && all(isSatisfyingUpperBounds(parameter)) && isSatisfyingSoftConstraints(parameter));

      _cachedIsSatisfyingConstraints.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  double OptimisationProblem::getSoftConstraintsValue(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    auto cachePosition = _cachedSoftConstraintsValues.find(parameter);
    if(cachePosition == _cachedSoftConstraintsValues.end()) {
      double result = getSoftConstraintsValueImplementation(getScaledCongruentParameter(parameter));

      _cachedSoftConstraintsValues.insert({parameter, result});

      return result;
    } else {
      return cachePosition->second;
    }
  }

  double OptimisationProblem::getObjectiveValue(const Col<double>& parameter) {
    if(parameter.n_elem != _numberOfDimensions) {
      throw logic_error("The dimension of the parameter (" + to_string(parameter.n_elem) + ") must match the dimension of the optimisation problem (" + to_string(_numberOfDimensions) + ").");
    }

    _numberOfEvaluations++;

    auto cachePosition = _cachedObjectivValues.find(parameter);
    if(cachePosition == _cachedObjectivValues.end()) {
{'fraction_non_alphanumeric': 0.11140558447704685, 'fraction_numerical': 0.001893043066729768, 'mean_word_length': 5.211640211640212, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 72, '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': '667883', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2934, 'n_words': 545}