Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMA stop conditions #401

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions doc/optimizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ matrix within an iterative procedure using the covariance matrix.
* `CMAES<`_`SelectionPolicyType, TransformationPolicyType`_`>(`_`lambda, transformationPolicy, batchSize`_`)`
* `CMAES<`_`SelectionPolicyType, TransformationPolicyType`_`>(`_`lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy`_`)`
* `CMAES<`_`SelectionPolicyType, TransformationPolicyType`_`>(`_`lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize`_`)`
* `CMAES<`_`SelectionPolicyType, TransformationPolicyType`_`>(`_`lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations`_`)`
* `CMAES<SelectionPolicyType, TransformationPolicyType>(lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations, minObjective)`
* `CMAES<SelectionPolicyType, TransformationPolicyType>(lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations, minObjective, toleranceConditionCov)`
* `CMAES<SelectionPolicyType, TransformationPolicyType>(lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations, minObjective, toleranceConditionCov, toleranceNoEffectAxis)`
* `CMAES<SelectionPolicyType, TransformationPolicyType>(lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations, minObjective, toleranceConditionCov, toleranceNoEffectAxis, toleranceNoEffectCoord)`
* `CMAES<SelectionPolicyType, TransformationPolicyType>(lambda, transformationPolicy, batchSize, maxIterations, tolerance, selectionPolicy, stepSize, maxFunctionEvaluations, minObjective, toleranceConditionCov, toleranceNoEffectAxis, toleranceNoEffectCoord, toleranceRange, toleranceRangePatience)`

The _`SelectionPolicyType`_ template parameter refers to the strategy used to
compute the (approximate) objective function. The `FullSelection` and
Expand Down Expand Up @@ -896,6 +902,13 @@ For convenience the following types can be used:
| `double` | **`tolerance`** | Maximum absolute tolerance to terminate algorithm. | `1e-5` |
| `SelectionPolicyType` | **`selectionPolicy`** | Instantiated selection policy used to calculate the objective. | `SelectionPolicyType()` |
| `size_t` | **`stepSize`** | Initial step size | `0` |
| `int` | **`maxFunctionEvaluations`** | Stop if the number of function evaluations reaches this limit. | `std::numeric_limits<int>::max()` |
| `double` | **`minObjective`** | Stop if the best objective value is less than or equal to this target value. | `std::numeric_limits<double>::lowest()` |
| `size_t` | **`toleranceConditionCov`** | Tolerance for stopping if the condition number of the covariance matrix exceeds this threshold. | `1e14` |
| `double` | **`toleranceNoEffectAxis`** | Tolerance for stopping if adding a 0.1-standard deviation vector in a principal axis direction of mean vector. | `1e-12` |
| `double` | **`toleranceNoEffectCoord`** | Tolerance for stopping if adding a 0.2-standard deviation in each coordinate does not change mean vector. | `1e-12` |
| `double` | **`toleranceRange`** | Tolerance for stopping if the range of the fitness values is less than this value. | `1e-12` |
| `int` | **`toleranceRangePatience`** | Patience for stopping if the range of the fitness values remains less than `toleranceRange` for a defined number of generations. | `1` |

Attributes of the optimizer may also be changed via the member methods
`Lambda()`, `TransformationPolicy()`, `BatchSize()`, `MaxIterations()`,
Expand Down Expand Up @@ -925,12 +938,12 @@ RosenbrockFunction f;
arma::mat coordinates = f.GetInitialPoint();

// CMAES with the FullSelection and BoundaryBoxConstraint policies.
BoundaryBoxConstraint b(-1, 1);
BoundaryBoxConstraint<> b(-1, 1);
CMAES optimizer(0, b, 32, 200, 1e-4);
optimizer.Optimize(f, coordinates);

// CMAES with the RandomSelection and BoundaryBoxConstraint policies.
ApproxCMAES<BoundaryBoxConstraint<>> cmaes(0, b, 32, 200, 1e-4);
ApproxCMAES<BoundaryBoxConstraint<>> approxOptimizer(0, b, 32, 200, 1e-4);
approxOptimizer.Optimize(f, coordinates);
```

Expand Down
105 changes: 103 additions & 2 deletions include/ensmallen_bits/cmaes/cmaes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class CMAES
* @param selectionPolicy Instantiated selection policy used to calculate the
* objective.
* @param stepSize Starting sigma/step size (will be modified).
* @param maxFunctionEvaluations Maximum number of function evaluations allowed.
* @param minObjective Minimum objective value to terminate the optimization.
* @param toleranceConditionCov Tolerance condition for covariance matrix.
* @param toleranceNoEffectCoord Tolerance for stopping if there is no change when adding 0.2 std in each coordinate.
* @param toleranceNoEffectAxis Tolerance for stopping if there is no change when adding 0.1 std along each axis.
* @param toleranceRange Tolerance for stopping if range of the fitness values is less than this value.
* @param toleranceRangePatience Patience for stopping if range of the fitness values is less than toleranceRange.
*/
CMAES(const size_t lambda = 0,
const TransformationPolicyType&
Expand All @@ -83,7 +90,15 @@ class CMAES
const size_t maxIterations = 1000,
const double tolerance = 1e-5,
const SelectionPolicyType& selectionPolicy = SelectionPolicyType(),
double stepSize = 0);
double stepSize = 0,
const int maxFunctionEvaluations = std::numeric_limits<int>::max(),
const double minObjective = std::numeric_limits<double>::lowest(),
const size_t toleranceConditionCov = 1e14,
const double toleranceNoEffectCoord = 1e-12,
const double toleranceNoEffectAxis = 1e-12,
const double toleranceRange = 1e-12,
const size_t toleranceRangePatience = 1
);

/**
* Construct the CMA-ES optimizer with the given function and parameters
Expand All @@ -103,6 +118,13 @@ class CMAES
* @param selectionPolicy Instantiated selection policy used to calculate the
* objective.
* @param stepSize Starting sigma/step size (will be modified).
* @param maxFunctionEvaluations Maximum number of function evaluations allowed.
* @param minObjective Minimum objective value to terminate the optimization.
* @param toleranceConditionCov Tolerance condition for covariance matrix.
* @param toleranceNoEffectCoord Tolerance for stopping if there is no change when adding 0.2 std in each coordinate.
* @param toleranceNoEffectAxis Tolerance for stopping if there is no change when adding 0.1 std along each axis.
* @param toleranceRange Tolerance for stopping if range of the fitness values is less than this value.
* @param toleranceRangePatience Patience for stopping if range of the fitness values is less than toleranceRange.
*/
CMAES(const size_t lambda = 0,
const double lowerBound = -10,
Expand All @@ -111,7 +133,15 @@ class CMAES
const size_t maxIterations = 1000,
const double tolerance = 1e-5,
const SelectionPolicyType& selectionPolicy = SelectionPolicyType(),
double stepSize = 0);
double stepSize = 0,
const int maxFunctionEvaluations = std::numeric_limits<int>::max(),
const double minObjective = std::numeric_limits<double>::lowest(),
const size_t toleranceConditionCov = 1e14,
const double toleranceNoEffectCoord = 1e-12,
const double toleranceNoEffectAxis = 1e-12,
const double toleranceRange = 1e-12,
const size_t toleranceRangePatience = 1
);

/**
* Optimize the given function using CMA-ES. The given starting point will be
Expand Down Expand Up @@ -153,6 +183,21 @@ class CMAES
//! Modify the tolerance for termination.
double& Tolerance() { return tolerance; }

//! Get the maximum number of function evaluations.
size_t MaxFunctionEvaluations() const { return maxFunctionEvaluations; }
//! Modify the maximum number of function evaluations.
size_t& MaxFunctionEvaluations() { return maxFunctionEvaluations; }

//! Get the minimum objective value to terminate the optimization.
double MinObjective() const { return minObjective; }
//! Modify the minimum objective value to terminate the optimization.
double& MinObjective() { return minObjective; }

//! Get the tolerance condition for covariance matrix.
size_t ToleranceConditionCov() const { return toleranceConditionCov; }
//! Modify the tolerance condition for covariance matrix.
size_t& ToleranceConditionCov() { return toleranceConditionCov; }

//! Get the selection policy.
const SelectionPolicyType& SelectionPolicy() const { return selectionPolicy; }
//! Modify the selection policy.
Expand All @@ -172,6 +217,38 @@ class CMAES
double& StepSize()
{ return stepSize; }

//! Get the total number of function evaluations.
size_t FunctionEvaluations() const
{ return functionEvaluations; }

//! Get the tolerance for stopping if there is no change when adding 0.2 std in each coordinate.
double ToleranceNoEffectCoord() const
{ return toleranceNoEffectCoord; }
//! Modify the tolerance for stopping if there is no change when adding 0.2 std in each coordinate.
double& ToleranceNoEffectCoord()
{ return toleranceNoEffectCoord; }

//! Get the tolerance for stopping if there is no change when adding 0.1 std along each axis.
double ToleranceNoEffectAxis() const
{ return toleranceNoEffectAxis; }
//! Modify the tolerance for stopping if there is no change when adding 0.1 std along each axis.
double& ToleranceNoEffectAxis()
{ return toleranceNoEffectAxis; }

//! Get the tolerance for stopping if range of the fitness values is less than this value.
double ToleranceRange() const
{ return toleranceRange; }
//! Modify the tolerance for stopping if range of the fitness values is less than this value.
double& ToleranceRange()
{ return toleranceRange; }

//! Get the patience for stopping if range of the fitness values is less than toleranceRange.
size_t ToleranceRangePatience() const
{ return toleranceRangePatience; }
//! Modify the patience for stopping if range of the fitness values is less than toleranceRange.
size_t& ToleranceRangePatience()
{ return toleranceRangePatience; }

private:
//! Population size.
size_t lambda;
Expand All @@ -185,6 +262,21 @@ class CMAES
//! The tolerance for termination.
double tolerance;

//! The tolerance for stopping if there is no change when adding 0.2 std in each coordinate.
double toleranceNoEffectCoord;

//! The tolerance for stopping if there is no change when adding 0.1 std along each axis.
double toleranceNoEffectAxis;

//! The maximum number of function evaluations.
size_t maxFunctionEvaluations;

//! The minimum objective value to terminate the optimization.
double minObjective;

//! The tolerance condition for covariance matrix.
size_t toleranceConditionCov;

//! The selection policy used to calculate the objective.
SelectionPolicyType selectionPolicy;

Expand All @@ -195,6 +287,15 @@ class CMAES

//! The step size.
double stepSize;

//! Counter for the number of function evaluations.
size_t functionEvaluations = 0;

//! The tolerance for stopping if range of the fitness values is less than this value.
double toleranceRange;

//! The patience for stopping if range of the fitness values is less than toleranceRange.
size_t toleranceRangePatience;
};

/**
Expand Down
Loading