Skip to content

Commit

Permalink
[refactor] Modify MO simple based on the Optuna code convention
Browse files Browse the repository at this point in the history
  • Loading branch information
nabenabe0928 committed Oct 19, 2023
1 parent 1486d10 commit 50b3b22
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions multi_objective/quadratic_simple.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
"""
Optuna example that optimizes simple quadratic functions.
In this example, we optimize simple quadratic functions.
In this example, we optimize two objective values.
Unlike a single-objective optimization, an optimization gives the trade-off between two objectives.
As a result, we get best trade-offs between two objectives, a.k.a Pareto solutions.
"""

import optuna


# Define simple 2-dimensional objective functions.
# Define two objective functions.
# We would like to minimize obj1 and maximize obj2.
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]
return obj1, obj2


if __name__ == "__main__":
# We minimize obj1 and maximize obj2.
# We minimize the first objective value and maximize the second objective value.
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]
print("Number of finished trials: ", len(study.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)
for i, best_trial in enumerate(study.best_trials):
print(f"The {i}-th Pareto solution was found at Trial#{best_trial.number}.")
print(f" Params: {best_trial.params}")
print(f" Values: {best_trial.values}")

0 comments on commit 50b3b22

Please sign in to comment.