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

Add tutorial for constrained problems #212

Merged
merged 8 commits into from
Dec 18, 2024
Merged
Changes from 2 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
31 changes: 31 additions & 0 deletions recipes/007_benchmarks_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import optuna
from optunahub.benchmarks import BaseProblem
from optunahub.benchmarks import ConstrainedMixin


###################################################################################################
Expand Down Expand Up @@ -63,6 +64,36 @@ def evaluate(self, params: dict[str, float]) -> float:
study = optuna.create_study(directions=dynamic_problem.directions)
study.optimize(dynamic_problem, n_trials=20)


###################################################################################################
# Implementing a problem with constraints
# -------------------------------------------------
# Here, let's implement a problem with constraints.
# To implement a problem with constraints, you need to inherit ``ConstrainedMixin`` class in addition to ``BaseProblem`` and implement the ``evaluate_constraints`` method.
# The ``evaluate_constraints`` method evaluates the constraint functions given a dictionary of input parameters and returns a list of constraint values.
# Then, ``ConstrainedMixin`` internally defines the ``constraints_func`` method properly for Optuna samplers to handle constraints.
y0z marked this conversation as resolved.
Show resolved Hide resolved
class ConstrainedProblem(ConstrainedMixin, DynamicProblem):
def evaluate_constraints(self, params: dict[str, float]) -> list[float]:
y0z marked this conversation as resolved.
Show resolved Hide resolved
x = params["x"]
c0 = x - 2
if "y" not in params:
return [c0]
y0z marked this conversation as resolved.
Show resolved Hide resolved
else:
y = params["y"]
c1 = x + y - 3
return [c0, c1]
y0z marked this conversation as resolved.
Show resolved Hide resolved


###################################################################################################
# Then, you can optimize the problem with Optuna as usual.
# Don't forget to set the `constraints_func` argument of the sampler to the `constraints_func` of the problem.
y0z marked this conversation as resolved.
Show resolved Hide resolved
problem = ConstrainedProblem()
sampler = optuna.samplers.TPESampler(
constraints_func=problem.constraints_func
) # Pass the constraints_func to the sampler.
study = optuna.create_study(sampler=sampler, directions=problem.directions)
study.optimize(problem, n_trials=20)

###################################################################################################
# After implementing your own benchmark problem, you can register it with OptunaHub.
# See :doc:`002_registration` for how to register your benchmark problem with OptunaHub.
Loading