Skip to content

Instantly share code, notes, and snippets.

@hvy
Last active February 27, 2022 06:22
Show Gist options
  • Save hvy/6122adf87bb56247b2e2ef1d6804f238 to your computer and use it in GitHub Desktop.
Save hvy/6122adf87bb56247b2e2ef1d6804f238 to your computer and use it in GitHub Desktop.
Kurobako multi-objective (MO) sample
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()
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()
#!/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
@hvy
Copy link
Author

hvy commented Feb 27, 2022

kurobako plot curve --metric hypervolume

binh-and-korn-0cf93c9a4e0da649473aacd4e385329ec423d89478a4d10740c305359d43c599

kurobako plot pareto-front

binh-and-korn-random-search-dd1105cddc4d8eef3393e788089b33e98c2a31c1ef5b95964c8cb6ae329a69bd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment