Skip to main content

Python wrapper for the BAM derivative-free global optimization solver

Project description

bam-solver

Python wrapper for the BAM (Branch-And-Model) derivative-free global optimization solver.

Three layers of API ergonomics:

  1. bam.minimize: SciPy-style functional facade.
  2. Bam: full-control mirror of the BAM C API.
  3. bam.AskTell: ask/tell interface for off-process or asynchronous evaluators.

Requirements

  • Python >= 3.10
  • Linux (x86_64, aarch64), macOS (Intel, Apple Silicon), or Windows (x86_64)

No compiler and no separate BAM installation are needed: each wheel bundles libbam and its runtime libraries inside the package. The only runtime dependencies are NumPy and CFFI, installed automatically.

Installation

pip install bam-solver

That is all; there is nothing else to install or configure to start solving. BAM runs in free mode for small problems; larger problems need a license (see Licensing below).

Building from source (maintainers/contributors only) requires a local BAM build providing bam.h and libbam.*:

BAM_INCLUDE_DIR=/path/to/bam/include BAM_LIB_DIR=/path/to/bam/lib \
    uv build --wheel

See release/README.md for the full in-house build/test/publish process.

Quick examples

Functional (SciPy-style): bam.minimize

import bam

def shc(x):
    x1, x2 = x
    return ((4 - 2.1*x1**2 + x1**4/3) * x1**2
            + x1*x2 + (-4 + 4*x2**2) * x2**2)

res = bam.minimize(shc, x0=[0, 0],
                   bounds=[(-3, 3), (-1.5, 1.5)],
                   options={"max_evals": 80})
print(res.x, res.fun, res.nfev, res.message)

bam.minimize is also usable as the method= argument to scipy.optimize.minimize:

from scipy.optimize import minimize
res = minimize(shc, [0, 0], method=bam.minimize,
               bounds=[(-3, 3), (-1.5, 1.5)],
               options={"max_evals": 80})

Bounds are optional: omit them and BAM uses its default box, exactly as the full-control Bam class and the C API do. Common BAM options keys: max_evals, max_time, max_iter, max_no_gain, print_to_screen, outfname, tracefname, evalsfname, prevals; setting outfname/tracefname enables the listing/trace file, and evalsfname is required whenever prevals is nonzero. Any other BAM option is forwarded verbatim with a single warning naming it, or pass it through raw_options={"name": value} to forward without the warning. The wrapper also accepts x_evaluated/f_evaluated (pre-evaluated points) and history=True (attaches the full x_hist/f_hist evaluation trajectory to the result). Use integrality=[0, 1, ...] for mixed-integer problems (SciPy convention).

Ask/tell: bam.AskTell

For evaluators that BAM cannot call directly (a remote simulator, a long-running batch job, an HPC scheduler):

import bam

opt = bam.AskTell(nvars=2, bounds=[(-3, 3), (-1.5, 1.5)], max_evals=80)
while not opt.is_done():
    try:
        x = opt.ask()
    except StopIteration:
        break
    f = my_simulator(x)             # may take seconds, minutes, hours
    opt.tell(x, f)
res = opt.recommendation()
print(res.x, res.fun)

BAM runs in a background thread; ask() blocks until BAM requests a point, tell(x, f) feeds the result back. Bounds are optional here too (nvars gives the dimension). Use the context-manager form (with bam.AskTell(...) as opt: ...) so the worker thread is always cleaned up. One ask outstanding at a time; pickling / cross-process resume is not supported.

Full-control: Bam

import numpy as np
from bam import Bam

with Bam(nvars=2) as solver:
    solver.set_real_vector("xmin", [-3, -1.5])
    solver.set_real_vector("xmax", [ 3,  1.5])
    solver.set_int("maxevals", 80)
    solver.set_objective(shc)
    result = solver.solve(x0=[0.0, 0.0])

print(result.x, result.fun, result.nfev, result.message)

Licensing

BAM is free for problems with up to two variables. Larger problems require a license file, free for academic users at https://minlp.com/bam-licenses.

The wrapper defers to BAM for license-file discovery; BAM searches CWD, the executable's directory, and every entry of $PATH for a file whose name is configured via the licfname string option (default bamlice.txt). To override:

import bam
bam.set_license_file("/path/to/bam.lic")
# or set BAM_LICENSE_FILE in the environment before importing bam

Diagnostics: bam.license_status() returns a dict with state, path, and free_limit_nvars. If BAM is in free mode at solve time the wrapper emits a one-time BamLicenseWarning.

Tests

pytest                                   # runs the in-package suite
BAM_TEST_LICENSE=/usr/local/bin/bamlice.txt pytest    # also exercises licensed-mode paths

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

bam_solver-4.1.1.tar.gz (76.5 kB view details)

Uploaded Source

Built Distributions

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

bam_solver-4.1.1-cp310-abi3-win_amd64.whl (16.0 MB view details)

Uploaded CPython 3.10+Windows x86-64

bam_solver-4.1.1-cp310-abi3-manylinux_2_27_aarch64.whl (12.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.27+ ARM64

bam_solver-4.1.1-cp310-abi3-manylinux_2_24_x86_64.whl (15.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.24+ x86-64

bam_solver-4.1.1-cp310-abi3-macosx_13_0_arm64.whl (14.7 MB view details)

Uploaded CPython 3.10+macOS 13.0+ ARM64

bam_solver-4.1.1-cp310-abi3-macosx_10_12_x86_64.whl (20.6 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file bam_solver-4.1.1.tar.gz.

File metadata

  • Download URL: bam_solver-4.1.1.tar.gz
  • Upload date:
  • Size: 76.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for bam_solver-4.1.1.tar.gz
Algorithm Hash digest
SHA256 d3166bed5e5ebd6841380d6676d52ff28c53c714d89ff3f4412911a8f214c1bd
MD5 bcc8c0f3ee293f4a6df5ec54c98aadb8
BLAKE2b-256 90471091133ca0471f5f1d6ac269bc7a22199c8d5165361add531cc5f93fdfe7

See more details on using hashes here.

File details

Details for the file bam_solver-4.1.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: bam_solver-4.1.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 16.0 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for bam_solver-4.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 067eb318439697d614c3a444663d5a7e988bf36d32ce9148615452390eccc6be
MD5 8e8c14751e5beb2e57fd3e6b1dcc6fe5
BLAKE2b-256 15dd0257336c31c164d513d183a63b793514bc4a753b525a51ea50b19732853d

See more details on using hashes here.

File details

Details for the file bam_solver-4.1.1-cp310-abi3-manylinux_2_27_aarch64.whl.

File metadata

File hashes

Hashes for bam_solver-4.1.1-cp310-abi3-manylinux_2_27_aarch64.whl
Algorithm Hash digest
SHA256 1963abb76f6485525d64b3b34f529c6a6555cc55af0aba7ec1b15084d0d91ffa
MD5 85bb6828c912e97c42eab0c0641501e6
BLAKE2b-256 1335dce5b4857ca1ea3f7aab298893091e2a6cc668c3663836e79a9d35ea53aa

See more details on using hashes here.

File details

Details for the file bam_solver-4.1.1-cp310-abi3-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for bam_solver-4.1.1-cp310-abi3-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e033cf72b42e76a71ea4e21aeb64d917ff1415781ed4a7e56627a6b2c7192efe
MD5 5694cf9831f219a4cb0177d5b1aa491e
BLAKE2b-256 5b22f50ead5c496727a3d8e118394375911eb74c8535b72b3730f29691539eb6

See more details on using hashes here.

File details

Details for the file bam_solver-4.1.1-cp310-abi3-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for bam_solver-4.1.1-cp310-abi3-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 94844bfc9f8d5ebf803b60ece4e9b0fa2462dbf084767b2a7ab75f00fab50ea0
MD5 f57164514d471826f2d8e2a144592614
BLAKE2b-256 48a796775ca710d5d433decc9ce0b87b1700b02d83a80b8cf4ea6288c79cfd11

See more details on using hashes here.

File details

Details for the file bam_solver-4.1.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bam_solver-4.1.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 657a5a471b0a7f80b53e8d669d4b3e93cac41ffdaf38317f57115b192c4595d4
MD5 68875a1196b50feb4eec6935e6576dc4
BLAKE2b-256 0ef1e65cc5b204322207c02a04658b41b40e41ec9461192afc1f91174d6aa5b6

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