Skip to main content

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

Project description

kurobako-py

pypi GitHub license Actions Status

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

Installation

$ pip install kurobako

Usage Examples

Define a solver based on random search

# filename: random_solver.py
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()

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

# filename: quadratic_problem.py
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',
                                   params=params,
                                   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())
    runner.run()

Run a benchmark that uses the above solver and problem

$ SOLVER=$(kurobako solver command python3 random_solver.py)
$ PROBLEM=$(kurobako problem command python3 quadratic_problem.py)
$ 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.

Source Distribution

kurobako-0.1.9.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

kurobako-0.1.9-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file kurobako-0.1.9.tar.gz.

File metadata

  • Download URL: kurobako-0.1.9.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kurobako-0.1.9.tar.gz
Algorithm Hash digest
SHA256 5a104f2148fc9a8af5e20cb330683dde2120010ab7465a96da7b81a52cd5ba29
MD5 4007f28b262148b08af686e0ae52abb7
BLAKE2b-256 cebb0b0af41b73416e6d8d9319a589ed4559e12715965e9a795e8b4f31c2d460

See more details on using hashes here.

File details

Details for the file kurobako-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: kurobako-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for kurobako-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 122a83d010a9398300cd2a90a81b8f60e4365e0f1a588a1a30962791015b96d2
MD5 bc2cf8505d451df1f048370a61350099
BLAKE2b-256 68127753a0f4dd7adb3b6fb0d1196e8e0c6841a6799ef1b7d4289a31cd269c03

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page