SimonBlanke / Gradient-Free-Optimizers

Simple and reliable optimization with local, global, population-based and sequential techniques in numerical discrete search spaces.
https://simonblanke.github.io/gradient-free-optimizers-documentation
MIT License
1.21k stars 84 forks source link

Add support for constrained optimization #33

Closed SimonBlanke closed 1 year ago

SimonBlanke commented 2 years ago

I want to add the support for constrained optimization. The interface design could look like this:

import numpy as np
from gradient_free_optimizers import HillClimbingOptimizer

def parabola_function(para):
    loss = para["x1"] * para["x1"] + para["x2"] * para["x2"]
    return -loss

search_space = {
    "x1": np.arange(-10, 10, 0.1),
    "x2": np.arange(-10, 10, 0.1),
}

def constraint_1(para):
    return para["x1"] ** 2 + para["x2"] ** 2 <= 2

constraints_list = [constraint_1]

opt = HillClimbingOptimizer(search_space, constraints=constraints_list)
opt.search(parabola_function, n_iter=100000)

Implementing this in the optimization algorithms will need various solutions:

SimonBlanke commented 1 year ago

Here is an example search-path-gif with a constraint at the optimum around the [0, 0] position for the Hill-climbing algorithm:

HillClimbingOptimizer

SimonBlanke commented 1 year ago

A similar example for Bayesian Optimization. Very interesting to see:

BayesianOptimizer

SimonBlanke commented 1 year ago

A similar example for Grid Search. That way easier than expected:

GridSearchOptimizer

SimonBlanke commented 1 year ago

Multiple constraints (xx+yy > 5 && x > -3) are not a problem:

GridSearchOptimizer_constr

SimonBlanke commented 1 year ago

From my current progress I see the following optimization algorithms as non-trivial for the constraint optimization implementation:

SimonBlanke commented 1 year ago

I managed to add support for constrained optimization to all optimization algorithms and added some tests in dda3b6125f20ab79d93e43d5969f32c3ed4ba0d3.

Next I will add more tests, write the documentation and add examples. After that I will release this new feature in version 1.3.

About the implementation

There are three different ways this feature is implemented in the algorithms:

SimonBlanke commented 1 year ago

I added an example for constrained optimization to the repository and released v1.3 for the documentation for GFO. All tests look good.

I just released version 1.3 and will close this issue.

This is a big new feature, so feedback is appreciated and can be submitted in this issue.