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.4.tar.gz (4.9 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.4-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: algobench-0.1.4.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.10 Linux/6.11.0-1014-azure

File hashes

Hashes for algobench-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cf10a3ab4795799a426638ec71fbea83bd051ad2b1c053efd8f36d840db28de1
MD5 07829ff97faeb49ee59d3977af62b09a
BLAKE2b-256 eee4866e53802bba7b240bb80450109d4d27d3dd7785b943c3e60dbcc56dc819

See more details on using hashes here.

File details

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

File metadata

  • Download URL: algobench-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.10 Linux/6.11.0-1014-azure

File hashes

Hashes for algobench-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e3bffc8c32addcc8b39f86077fd40f1f99a667a5f863580d9a7239d2f5af826d
MD5 76756440e9ec2a2dea8c815e32856895
BLAKE2b-256 771c6fc061031f0f821ab072788f0a4589e66b88b9b302e5880e919b9f59a70c

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