-
Notifications
You must be signed in to change notification settings - Fork 177
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
Alnusjaponica
merged 3 commits into
optuna:main
from
nabenabe0928:add-simple-mo-and-const
Oct 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
Alnusjaponica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This repository already contains examples of multi-objective optimization within the
multi_objective
directory. Would it be possible to move these examples to that location for consistency?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
A nit-picky, but
multi_objective/quadratic_simple_multi_objective.py
might sound verbose sincemulti_objective
is duplicated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed!