Last active
February 27, 2022 06:22
-
-
Save hvy/6122adf87bb56247b2e2ef1d6804f238 to your computer and use it in GitHub Desktop.
Kurobako multi-objective (MO) sample
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
from kurobako import problem | |
class BinhAndKornProblemFactory(problem.ProblemFactory): | |
def specification(self): | |
params = [ | |
problem.Var("x", problem.ContinuousRange(0, 5)), | |
problem.Var("y", problem.ContinuousRange(0, 3)), | |
] | |
return problem.ProblemSpec( | |
name="Binh and Korn", | |
params=params, | |
values=[ | |
problem.Var("4 * x ** 2 + 4 * y ** 2"), | |
problem.Var("(x - 5) ** 2 + (y - 5) ** 2"), | |
], | |
reference_point=[140, 50], # Used at `kurobako plot curve --metric hypervolume`. | |
) | |
def create_problem(self, seed): | |
return BinhAndKornProblem() | |
class BinhAndKornProblem(problem.Problem): | |
def create_evaluator(self, params): | |
return BinhAndKornEvaluator(params) | |
class BinhAndKornEvaluator(problem.Evaluator): | |
def __init__(self, params): | |
self._x, self._y = params | |
self._current_step = 0 | |
def current_step(self): | |
return self._current_step | |
def evaluate(self, next_step): | |
self._current_step = 1 | |
x, y = self._x, self._y | |
v0 = 4 * x ** 2 + 4 * y ** 2 | |
v1 = (x - 5) ** 2 + (y - 5) ** 2 | |
return [v0, v1] | |
if __name__ == "__main__": | |
runner = problem.ProblemRunner(BinhAndKornProblemFactory()) | |
runner.run() |
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
import numpy as np | |
from kurobako import problem | |
from kurobako import solver | |
class RandomSolverFactory(solver.SolverFactory): | |
def specification(self): | |
return solver.SolverSpec(name="Random Search") | |
def create_solver(self, seed, problem): | |
return RandomSolver(seed, problem) | |
class RandomSolver(solver.Solver): | |
def __init__(self, seed, problem): | |
self._rng = np.random.RandomState(seed) | |
self._problem = problem | |
def ask(self, idg): | |
params = [] | |
for p in self._problem.params: | |
if p.distribution == problem.Distribution.UNIFORM: | |
params.append(self._rng.uniform(p.range.low, p.range.high)) | |
else: | |
low = np.log(p.range.low) | |
high = np.log(p.range.high) | |
params.append(float(np.exp(self._rng.uniform(low, high)))) | |
trial_id = idg.generate() | |
next_step = self._problem.last_step | |
return solver.NextTrial(trial_id, params, next_step) | |
def tell(self, trial): | |
pass | |
if __name__ == "__main__": | |
runner = solver.SolverRunner(RandomSolverFactory()) | |
runner.run() |
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
#!/bin/bash | |
SOLVER=$(kurobako solver command python3 random_solver.py) | |
PROBLEM=$(kurobako problem command python3 binh_and_korn_problem.py) | |
# Run the benchmark and save its output in a JSON file. | |
kurobako studies --solvers $SOLVER --problems $PROBLEM | kurobako run > result.json | |
# From the output, generate a hypervolume curve plot under `images/curve/`. | |
cat result.json | kurobako plot curve --metric hypervolume | |
# From the output, generate a Pareto front plot under `images/pareto_front/`. | |
cat result.json | kurobako plot pareto-front |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kurobako plot curve --metric hypervolume
kurobako plot pareto-front