-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simples examples for multi-objective and constrained optimizations
- Loading branch information
1 parent
3411a75
commit cb7d636
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |