Skip to main content

Supercharge your algorithm development

Project description

algobench SDK

Improve solutions for your optimization problem by adding a single decorator to your code.

  • Your optimization problem will be submitted to algobench to generate improved solution algorithms.
  • While your code solves an instance, algobench delivers improved solutions on-the-fly.
  • Visit algobench.io to check your optimization problems.

Installation

pip install algobench

Prerequisites

Generally, we consider any problem of the form

def solve(instance: Instance) -> Solution:
    # do some complicated computation here

A computed solution should be feasible, i.e. pass some checks defined by

def feasible(instance: Instance, solution: Solution) -> bool:
    # check feasibility of solution

and for a feasible solution a score is determined by

def score(instance: Instance, solution: Solution) -> float:
    # compute score of the solution

These three functions, together with definitions of Instance and Solution make up the optimization problem.

Once you wrote your optimization problem, apply algobench's decorator to the algorithm function.

@algorithm(
    name="Optimization Problem Name",
    feasibility_function=feasible,
    scoring_function=score,
    api_key="API_KEY",
    is_minimization=False,
    additional_wait_seconds=2)
def solve(instance: Instance) -> Solution:
    # do some complicated computation here
  • Obtain your API key from algobench.io
  • Specify whether you want to maximize or minimize the scoring function via is_minimization
  • With additional_wait_seconds you can specify how many more seconds you want to wait for algobench after your local algorithm has computed its solution.

Usage example (Knapsack Problem)

from pydantic import BaseModel
from algobench import algorithm


class Item(BaseModel):
    id: int
    weight: float
    value: float


class Instance(BaseModel):
    items: dict[int, Item]
    capacity: float


class Solution(BaseModel):
    chosen_items: set[int]


def check(instance: Instance, solution: Solution) -> bool:
    if not set(instance.items.keys()).issuperset(solution.chosen_items):
        return False
    return (
        sum(instance.items[i].weight for i in solution.chosen_items)
        <= instance.capacity
    )


def score(instance: Instance, solution: Solution) -> float:
    return sum(instance.items[i].value for i in solution.chosen_items)


@algorithm(
    name="Knapsack-new",
    feasibility_function=check,
    scoring_function=score,
    api_key=API_KEY,
    is_minimization=False,
    additional_wait_seconds=2,
)
def solve(instance: Instance) -> Solution:
    remaining_capacity = instance.capacity
    chosen_ids = set()
    for item in sorted(
        instance.items.values(), key=lambda item: -item.value / item.weight
    ):
        if item.weight <= remaining_capacity:
            chosen_ids.add(item.id)
            remaining_capacity -= item.weight
    return Solution(chosen_items=chosen_ids)


def main():
    items = {
        1: Item(id=1, weight=1, value=1.5),
        2: Item(id=2, weight=2, value=2),
        3: Item(id=3, weight=3, value=3),
    }
    instance_1 = Instance(items=items, capacity=5)

    result = solve(instance_1)
    print(check(instance_1, result), score(instance_1, result))


if __name__ == "__main__":
    main()

Initially we use a greedy algorithm to solve the knapsack problem, which results in item 1 and 2 being chosen. What you can see from the second run is that in the background, algobench evolved a better algorithm using an automated evolutionary process and computed the optimal solution, which is to choose items 2 and 3.

Requirements and (current) limitations

  • The whole optimization problem needs to be contained in a single python file.
  • All classes need to be convertible to and from 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

algobench-0.1.5.tar.gz (50.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

algobench-0.1.5-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file algobench-0.1.5.tar.gz.

File metadata

  • Download URL: algobench-0.1.5.tar.gz
  • Upload date:
  • Size: 50.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for algobench-0.1.5.tar.gz
Algorithm Hash digest
SHA256 6aa2bd216495ddc94982f839bb76aa91fd4da6d774caa9862e5340b7405bd5b0
MD5 538691e7f551e5cb29f1ba2aa380df0a
BLAKE2b-256 e804c159f5eaaf46551414cec9f70bc4f8604c107185569cf9c4152f43bb41c3

See more details on using hashes here.

File details

Details for the file algobench-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: algobench-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for algobench-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2277671d28c6d44af8f5dbff3f04189b066f90c200a2bad288e54a0b4509a02f
MD5 fa9751bd0ace72a7b121a4ff38db97b4
BLAKE2b-256 7d9a6b18ef7c6e2cc392abb1f928c9de18c4608c5c40b409e56780b9bfa64c64

See more details on using hashes here.

Supported by

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