Skip to main content

Fast multivariate grid search optimizer with C++ core

Project description

gridoptim

PyPI Downloads

gridoptim is a deterministic multivariate grid-search optimizer for Python with a compiled C++ backend.

It evaluates a mathematical expression across every point in a user-defined parameter grid and returns the best result for either minimization or maximization.

Features

  • Deterministic brute-force optimization
  • Fast native C++ core exposed through Python
  • Simple string-based expression interface
  • Support for multi-variable parameter sweeps
  • Reproducible results across runs
  • Small public API with minimal setup

Installation

Install from PyPI:

pip install gridoptim

Requirements:

  • Python 3.10+
  • A supported build environment if installing from source

Quick Start

from gridoptim import GridSearchOptimiser

opt = GridSearchOptimiser()

value, params = (
    opt
    .function("x^2 + y^2")
    .set_range("x", -10, 10, 0.5)
    .set_range("y", -10, 10, 0.5)
    .optimise("min")
)

print("Best value:", value)
print("Best parameters:", params)

Expected output:

Best value: 0.0
Best parameters: {'x': 0.0, 'y': 0.0}

How It Works

gridoptim performs an exhaustive search over all combinations of values produced by the ranges you define.

For example, if you search:

  • x from -10 to 10 with step 1
  • y from -10 to 10 with step 1

the optimizer evaluates 21 x 21 = 441 points.

This guarantees the best result within the grid you specified, unlike probabilistic or heuristic optimizers that may trade certainty for speed.

Basic Usage

Optimization follows three steps:

  1. Create an optimizer.
  2. Define the expression to evaluate.
  3. Define a range for each variable, then run optimise("min") or optimise("max").
from gridoptim import GridSearchOptimiser

opt = GridSearchOptimiser()
opt.function("sin(x) + cos(y) + x^2")
opt.set_range("x", -3.14, 3.14, 0.1)
opt.set_range("y", -3.14, 3.14, 0.1)

value, params = opt.optimise("min")

API

GridSearchOptimiser

Main optimization class.

GridSearchOptimiser(expr: str | None = None)

Creates a new optimizer. You may optionally provide the expression at construction time.

function(expr: str) -> GridSearchOptimiser

Sets the mathematical expression to optimize.

opt.function("x^2 + y^2")

set_range(var: str, min_val: float, max_val: float, step: float) -> GridSearchOptimiser

Defines the search range for a variable.

opt.set_range("x", -10, 10, 0.1)

Arguments:

  • var: variable name used in the expression
  • min_val: lower bound
  • max_val: upper bound
  • step: step size, must be greater than 0

optimise(mode: str = "min") -> tuple[float, dict[str, float]]

Runs the grid search and returns:

  • best_value
  • best_parameter_dict
value, params = opt.optimise("max")

Valid modes:

  • "min" for minimization
  • "max" for maximization

Expression Syntax

Expressions are passed as strings and evaluated by the native backend using a lightweight mathematical expression parser.

Supported operators include:

  • +
  • -
  • *
  • /
  • ^

Common mathematical functions such as sin, cos, tan, log, sqrt, exp, and abs are supported.

Example:

opt.function("sin(x) * cos(y) + x^2")

Examples

Minimize a quadratic

from gridoptim import GridSearchOptimiser

opt = GridSearchOptimiser()

value, params = (
    opt
    .function("x^2 + y^2")
    .set_range("x", -5, 5, 0.1)
    .set_range("y", -5, 5, 0.1)
    .optimise("min")
)

print(value)
print(params)

Maximize a trigonometric expression

from gridoptim import GridSearchOptimiser

opt = GridSearchOptimiser()

value, params = (
    opt
    .function("sin(x) * cos(y)")
    .set_range("x", -3.14, 3.14, 0.01)
    .set_range("y", -3.14, 3.14, 0.01)
    .optimise("max")
)

print(value)
print(params)

Search across three variables

from gridoptim import GridSearchOptimiser

opt = GridSearchOptimiser()

value, params = (
    opt
    .function("x^2 + y^2 + z^2")
    .set_range("x", -10, 10, 1)
    .set_range("y", -10, 10, 1)
    .set_range("z", -10, 10, 1)
    .optimise("min")
)

print(value)
print(params)

Performance Notes

Grid search grows exponentially with the number of variables and the number of steps per variable.

For example:

  • 3 variables
  • 100 steps per variable

results in 100 x 100 x 100 = 1,000,000 evaluations.

Because the heavy computation is performed in C++, gridoptim can handle much larger search spaces than a pure Python implementation, but exhaustive search still becomes expensive as the grid grows.

Benchmark

Benchmark problem: 4D grid search optimization.

Optimizer Time
gridoptim 0.821 s
scipy.brute 89.167 s

Result:

  • Same optimum value
  • Same optimum point
  • ~108× speedup

Hardware:

  • CPU: Intel i5-12500H
  • Python 3.14

License

See the LICENSE file included in this repository for licensing terms.

Author

Akhil Shimna Kumar

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

gridoptim-2.2.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distributions

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

gridoptim-2.2.0-cp313-cp313-win_amd64.whl (81.8 kB view details)

Uploaded CPython 3.13Windows x86-64

gridoptim-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

gridoptim-2.2.0-cp312-cp312-win_amd64.whl (81.7 kB view details)

Uploaded CPython 3.12Windows x86-64

gridoptim-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

gridoptim-2.2.0-cp311-cp311-win_amd64.whl (81.1 kB view details)

Uploaded CPython 3.11Windows x86-64

gridoptim-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

gridoptim-2.2.0-cp310-cp310-win_amd64.whl (80.4 kB view details)

Uploaded CPython 3.10Windows x86-64

gridoptim-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

File details

Details for the file gridoptim-2.2.0.tar.gz.

File metadata

  • Download URL: gridoptim-2.2.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gridoptim-2.2.0.tar.gz
Algorithm Hash digest
SHA256 b16d54ac1325abe923dfbde2823bb227520238ecd9f602bd2f8f422edc6d107f
MD5 62ad055de9a052c8fae8c93575e699b9
BLAKE2b-256 b7e15f2d4c6dd4a868f2cc286f4691b2bb028e7da55a9c991326760efd97f107

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0.tar.gz:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 81.8 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gridoptim-2.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 aad465970a931ba85db689753860ba56e6c4bc6d9ede097ad2a1f9da7c9e3d97
MD5 7fe6ec00bd05b815d06f44bbcd0449c3
BLAKE2b-256 b4a6c635af247e9beab6cb9517a41892df4fdbcdb4869eae2f1e0cc7ea569ac5

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cad12b33a6c3ae978798c1f806f83808aa5a2d16058a32eb7bd4154cbdba5df9
MD5 896653ce98a21ac6593e56c8307b9a4d
BLAKE2b-256 5d96cb5a1bdfe98921b1bea246cff91de07deac86de09af1c33b3ae9e1c81445

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 81.7 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gridoptim-2.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a754e026fc9e2cc7c0555349d3886ca90b8f2cdbbaa676841c11ddd44a5b474b
MD5 ffa617f7d8f37950c29a6cf1abbe6324
BLAKE2b-256 dd2ca9aeb1827917ea3c5101add3ead60b1f8fd821b97cd2bb90843bb333a68c

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0eacef4f8fb2adb4d26d60cdd79dea6fd33dfc4c82784693adf9624a85a197d7
MD5 698e54940df55a02059ee82d1b34577d
BLAKE2b-256 c3b858337eb0b2eb2f41a5fef57f4155d868ba6f135665d37dac71a247009762

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 81.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gridoptim-2.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 599cf1275ca94619d10b47121e2be076e4111858cf60b437e47dadc51b7eba2b
MD5 800e75cdf9917a20a89dc35de482e315
BLAKE2b-256 9e5f9b50fd6148d3ff632b6037d6bb38b2f09c2a99fbebf98d9a06c215741546

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48ad68b98894487a2b73e441bee6ddec172d74bdbcaa06d71d97bbc701e92498
MD5 a7b1e5c54e56b29dd441ab085551ada7
BLAKE2b-256 53f81ec10933c6b4b018f4f8567fb014afe9ecd9495871b20fbfa294662f78b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 80.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gridoptim-2.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7e96fc309c549fa602f22a10338e702ef47c19a6942e60ad398be85238b9b77b
MD5 abc4057816b5b6bbe03fb740842bf553
BLAKE2b-256 26802c215a9dfbd646f37809c3d5b01c30bccdb57d86b8c1466920335cb75b11

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gridoptim-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8e3f365b2b7e244b3f228a421c509cd98f68e382c3267c8711efcae0189acc15
MD5 addc6099cc6e8d672eacadc1c3a8493c
BLAKE2b-256 6620e3c5e646abcf46ee7291732e1b4fb1ded29c94825546738e124ec23abbf5

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on Halfblood-Prince/gridoptim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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