Skip to content

Commit

Permalink
Merge pull request #216 from nabenabe0928/add-simple-mo-and-const
Browse files Browse the repository at this point in the history
Add simples examples for multi-objective and constrained optimizations
  • Loading branch information
Alnusjaponica authored Oct 19, 2023
2 parents 42d84c8 + 7e994b7 commit 1486d10
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 @@ -26,6 +26,8 @@ The examples below provide codeblocks similar to the example above for various d
### Simple Black-box Optimization

* [Quadratic function](./quadratic_simple.py)
- [Quadratic multi-objective function](./multi_objective/quadratic_simple.py)
- [Quadratic function with constraints](./quadratic_simple_constraint.py)

### Examples with ML Libraries

Expand Down
30 changes: 30 additions & 0 deletions multi_objective/quadratic_simple.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)
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.TPESampler(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}")

0 comments on commit 1486d10

Please sign in to comment.