Skip to content

Commit

Permalink
Add simples examples for multi-objective and constrained optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
nabenabe0928 committed Oct 16, 2023
1 parent 3411a75 commit cb7d636
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
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](./quadratic_simple_multi_objective.py)
- [Quadratic function with constraints](./quadratic_simple_constraint.py)

### Examples with ML Libraries

Expand Down
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.NSGAIISampler(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}")
30 changes: 30 additions & 0 deletions quadratic_simple_multi_objective.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)

0 comments on commit cb7d636

Please sign in to comment.