-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Aligh package's readme to the template one
- Loading branch information
1 parent
07f0e8a
commit 2ae95fb
Showing
8 changed files
with
138 additions
and
197 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,17 @@ | ||
import optuna | ||
import optunahub | ||
|
||
|
||
def objective(trial: optuna.Trial) -> float: | ||
x = trial.suggest_float("x", 0, 1) | ||
|
||
return x | ||
|
||
|
||
if __name__ == "__main__": | ||
module = optunahub.load_module("samplers/demo") | ||
sampler = module.DemoSampler(seed=42) | ||
study = optuna.create_study(sampler=sampler) | ||
study.optimize(objective, n_trials=5) | ||
|
||
print(study.best_trial) |
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,46 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
import optuna | ||
from optuna import Study | ||
from optuna.distributions import BaseDistribution | ||
from optuna.distributions import FloatDistribution | ||
from optuna.distributions import IntDistribution | ||
from optuna.trial import FrozenTrial | ||
import optunahub | ||
|
||
|
||
class UserDefinedSampler(optunahub.load_module("samplers/simple").SimpleSampler): # type: ignore | ||
def __init__(self, search_space: dict[str, BaseDistribution]) -> None: | ||
super().__init__(search_space) | ||
self._rng = np.random.RandomState() | ||
|
||
def sample_relative( | ||
self, | ||
study: Study, | ||
trial: FrozenTrial, | ||
search_space: dict[str, BaseDistribution], | ||
) -> dict[str, Any]: | ||
params = {} | ||
for n, d in search_space.items(): | ||
if isinstance(d, FloatDistribution): | ||
params[n] = self._rng.uniform(d.low, d.high) | ||
elif isinstance(d, IntDistribution): | ||
params[n] = self._rng.randint(d.low, d.high) | ||
else: | ||
raise ValueError("Unsupported distribution") | ||
return params | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
def objective(trial: optuna.Trial) -> float: | ||
x = trial.suggest_float("x", 0, 1) | ||
|
||
return x | ||
|
||
sampler = UserDefinedSampler({"x": FloatDistribution(0, 1)}) | ||
study = optuna.create_study(sampler=sampler) | ||
study.optimize(objective, n_trials=20) | ||
|
||
print(study.best_trial.value, study.best_trial.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
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,18 @@ | ||
import optuna | ||
import optunahub | ||
|
||
|
||
def objective(trial: optuna.Trial) -> float: | ||
x = trial.suggest_float("x", 0, 1) | ||
|
||
return x | ||
|
||
|
||
if __name__ == "__main__": | ||
mod = optunahub.load_module("samplers/simulated_annealing") | ||
|
||
sampler = mod.SimulatedAnnealingSampler() | ||
study = optuna.create_study(sampler=sampler) | ||
study.optimize(objective, n_trials=20) | ||
|
||
print(study.best_trial.value, study.best_trial.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
22 changes: 22 additions & 0 deletions
22
package/visualization/plot_hypervolume_history_with_rp/example.py
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,22 @@ | ||
import optuna | ||
import optunahub | ||
|
||
|
||
def objective(trial: optuna.trial.Trial) -> tuple[float, float]: | ||
x = trial.suggest_float("x", 0, 5) | ||
y = trial.suggest_float("y", 0, 3) | ||
|
||
v0 = 4 * x**2 + 4 * y**2 | ||
v1 = (x - 5) ** 2 + (y - 5) ** 2 | ||
return v0, v1 | ||
|
||
|
||
if __name__ == "__main__": | ||
mod = optunahub.load_module("visualization/plot_hypervolume_history_with_rp") | ||
|
||
study = optuna.create_study(directions=["minimize", "minimize"]) | ||
study.optimize(objective, n_trials=50) | ||
|
||
reference_point = [100.0, 50.0] | ||
fig = mod.plot_hypervolume_history(study, reference_point) | ||
fig.show() |