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 simples examples for multi-objective and constrained optimizations #216

Merged
merged 3 commits into from
Oct 19, 2023
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This page contains a list of example codes written with Optuna.
### Simple Black-box Optimization

* [Quadratic function](./quadratic_simple.py)
- [Quadratic multi-objective function](./multi_objective/quadratic_simple.py)
- [Quadratic function with constraints](./quadratic_simple_constraint.py)
Comment on lines +9 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like * should be used for listing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! if necessary, I'm happy to include this too in #217 as a followup.

Copy link
Collaborator

@Alnusjaponica Alnusjaponica Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment! I appreciate your follow-up.

Copy link
Member

@nzw0301 nzw0301 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your response; I've updated the PR!


### Examples with ML Libraries

Expand Down
30 changes: 30 additions & 0 deletions multi_objective/quadratic_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Optuna example that optimizes simple quadratic functions.

In this example, we optimize simple quadratic functions.

"""

import optuna


# Define simple 2-dimensional objective functions.
def objective(trial):
x = trial.suggest_float("x", -100, 100)
y = trial.suggest_categorical("y", [-1, 0, 1])
obj1 = x**2 + y
obj2 = -((x - 2) ** 2 + y)
return [obj1, obj2]


if __name__ == "__main__":
# We minimize obj1 and maximize obj2.
study = optuna.create_study(directions=["minimize", "maximize"])
study.optimize(objective, n_trials=500, timeout=1)

pareto_front = [t.values for t in study.best_trials]
pareto_sols = [t.params for t in study.best_trials]

for i, (params, values) in enumerate(zip(pareto_sols, pareto_front)):
print(f"The {i}-th Pareto solution and its objective values")
print("\t", params, values)
49 changes: 49 additions & 0 deletions quadratic_simple_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Optuna example that optimizes a simple quadratic function with constraints.

In this example, we optimize a simple quadratic function with constraints.

Please check https://optuna.readthedocs.io/en/stable/faq.html#id16 as well.

"""

import optuna


# Define a simple 2-dimensional objective function with constraints.
def objective(trial):
x = trial.suggest_float("x", -100, 100)
y = trial.suggest_categorical("y", [-1, 0, 1])
return x**2 + y


# Define a function that returns constraints.
def constraints(trial):
params = trial.params
x, y = params["x"], params["y"]
# c1 <= 0 and c2 <= 0 must be satisfied.
c1 = 3 * x * y + 10
c2 = x * y + 30
return [c1, c2]


if __name__ == "__main__":
# We minimize obj1 and maximize obj2.
sampler = optuna.samplers.TPESampler(constraints_func=constraints)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=500, timeout=1)

best_trial_id, best_value, best_params = None, float("inf"), None
for t in study.trials:
infeasible = any(c > 0.0 for c in t.system_attrs["constraints"])
if infeasible:
continue
if best_value > t.value:
best_value = t.value
best_params = t.params.copy()
best_trial_id = t._trial_id

if best_trial_id is None:
print("All trials violated the constraints.")
else:
print(f"Best value is {best_value} with params {best_params}")