Skip to main content

A Python library to help implementing kurobako's solvers and problems

Project description


pypi GitHub license Actions Status

A Python library to help implement kurobako's solvers and problems.


$ pip install kurobako

Usage Examples

Define a solver based on random search

# filename:
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))
                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):

if __name__ == '__main__':
    runner = solver.SolverRunner(RandomSolverFactory())

Define a problem that represents a quadratic function x**2 + y

# filename:
from kurobako import problem

class QuadraticProblemFactory(problem.ProblemFactory):
    def specification(self):
        params = [
            problem.Var('x', problem.ContinuousRange(-10, 10)),
            problem.Var('y', problem.DiscreteRange(-3, 3))
        return problem.ProblemSpec(name='Quadratic Function',
                                   values=[problem.Var('x**2 + y')])

    def create_problem(self, seed):
        return QuadraticProblem()

class QuadraticProblem(problem.Problem):
    def create_evaluator(self, params):
        return QuadraticEvaluator(params)

class QuadraticEvaluator(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
        return [self._x**2 + self._y]

if __name__ == '__main__':
    runner = problem.ProblemRunner(QuadraticProblemFactory())

Run a benchmark that uses the above solver and problem

$ SOLVER=$(kurobako solver command python3
$ PROBLEM=$(kurobako problem command python3
$ kurobako studies --solvers $SOLVER --problems $PROBLEM | kurobako run > result.json

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for kurobako, version 0.1.9
Filename, size File type Python version Upload date Hashes
Filename, size kurobako-0.1.9-py3-none-any.whl (12.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size kurobako-0.1.9.tar.gz (10.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page