From cb7d636d1a979e7c82a86cf601ff4033187790c6 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Mon, 16 Oct 2023 03:52:46 +0200 Subject: [PATCH 1/3] Add simples examples for multi-objective and constrained optimizations --- README.md | 2 ++ quadratic_simple_constraint.py | 49 +++++++++++++++++++++++++++++ quadratic_simple_multi_objective.py | 30 ++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 quadratic_simple_constraint.py create mode 100644 quadratic_simple_multi_objective.py diff --git a/README.md b/README.md index 959831ad..1d5a3d2f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/quadratic_simple_constraint.py b/quadratic_simple_constraint.py new file mode 100644 index 00000000..d4881402 --- /dev/null +++ b/quadratic_simple_constraint.py @@ -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}") diff --git a/quadratic_simple_multi_objective.py b/quadratic_simple_multi_objective.py new file mode 100644 index 00000000..4a253e95 --- /dev/null +++ b/quadratic_simple_multi_objective.py @@ -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) From d6c3d8d330f9d4dfdd7d80554fc30ef9aab7e608 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Thu, 19 Oct 2023 04:48:46 +0200 Subject: [PATCH 2/3] Address the comments raised by Alnusjaponica and toshihikoyanase --- README.md | 2 +- .../quadratic_simple_multi_objective.py | 0 quadratic_simple_constraint.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename quadratic_simple_multi_objective.py => multi_objective/quadratic_simple_multi_objective.py (100%) diff --git a/README.md b/README.md index 1d5a3d2f..35bd4363 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 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 multi-objective function](./multi_objective/quadratic_simple_multi_objective.py) - [Quadratic function with constraints](./quadratic_simple_constraint.py) ### Examples with ML Libraries diff --git a/quadratic_simple_multi_objective.py b/multi_objective/quadratic_simple_multi_objective.py similarity index 100% rename from quadratic_simple_multi_objective.py rename to multi_objective/quadratic_simple_multi_objective.py diff --git a/quadratic_simple_constraint.py b/quadratic_simple_constraint.py index d4881402..60590925 100644 --- a/quadratic_simple_constraint.py +++ b/quadratic_simple_constraint.py @@ -29,7 +29,7 @@ def constraints(trial): if __name__ == "__main__": # We minimize obj1 and maximize obj2. - sampler = optuna.samplers.NSGAIISampler(constraints_func=constraints) + sampler = optuna.samplers.TPESampler(constraints_func=constraints) study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=500, timeout=1) From 7e994b7da016319a78ef860b6b08c47af2425e99 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Thu, 19 Oct 2023 06:25:29 +0200 Subject: [PATCH 3/3] Rename MO simple shorter --- README.md | 2 +- ...{quadratic_simple_multi_objective.py => quadratic_simple.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename multi_objective/{quadratic_simple_multi_objective.py => quadratic_simple.py} (100%) diff --git a/README.md b/README.md index 35bd4363..287f4b52 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ 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_multi_objective.py) +- [Quadratic multi-objective function](./multi_objective/quadratic_simple.py) - [Quadratic function with constraints](./quadratic_simple_constraint.py) ### Examples with ML Libraries diff --git a/multi_objective/quadratic_simple_multi_objective.py b/multi_objective/quadratic_simple.py similarity index 100% rename from multi_objective/quadratic_simple_multi_objective.py rename to multi_objective/quadratic_simple.py