Skip to main content

Fast multivariate grid search optimizer with C++ core

Project description

gridoptim

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.

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.1.1.tar.gz (13.9 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.1.1-cp313-cp313-win_amd64.whl (77.9 kB view details)

Uploaded CPython 3.13Windows x86-64

gridoptim-2.1.1-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.1.1-cp312-cp312-win_amd64.whl (77.9 kB view details)

Uploaded CPython 3.12Windows x86-64

gridoptim-2.1.1-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.1.1-cp311-cp311-win_amd64.whl (77.2 kB view details)

Uploaded CPython 3.11Windows x86-64

gridoptim-2.1.1-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.1.1-cp310-cp310-win_amd64.whl (76.4 kB view details)

Uploaded CPython 3.10Windows x86-64

gridoptim-2.1.1-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.1.1.tar.gz.

File metadata

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

File hashes

Hashes for gridoptim-2.1.1.tar.gz
Algorithm Hash digest
SHA256 fed00c8848852a95448c391ebe7f276dc359dc1c079edd69109bf93333e335dc
MD5 076205d653afef2115ffdffb8fbb072b
BLAKE2b-256 2ef9a2232e28043f08a20121af0f9a2e377d6b0e0fcc00e39ce0c73635bba124

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1.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.1.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.1.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 77.9 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.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 26759be09b75ca8aa19c35b19a25f4a35f58d65e8b1c3f166c50c538d1a0132a
MD5 e15229c2f12536dc6ab13afaf01de6e1
BLAKE2b-256 f97dca61d85b9cb8ed28569da92467508a984b8cb251f13a2c0793c209d08421

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a885b042cac982825b1c0f73d1ee1e98610649d3a14d48d27ed680a8daee1abd
MD5 68f04293b5162a2ca9a5b990aa5859e7
BLAKE2b-256 909cfeac1e14c706eb14caed37c47c9fdce01a95ebe267e11b5c5049e79337ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.1.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 77.9 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.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 15a8baafac538baf51600ece4784e79a823a333a622246a92f58f9df846bfd5b
MD5 a1fac4632582063362f72f11941a5d5e
BLAKE2b-256 b1ee530575237102d86c7213b1978ae77646875a541420b9ac6cd81d308753d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 db466e6cd848707926791700ed3a1e42e3e3f3de39d3fd9a27cb119d11d5ca00
MD5 044d5212c72842b8c7136a7131086512
BLAKE2b-256 c16741757e9921870bfd09d8b943427b10e5420d02b92abb9a7540b458ca92d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.1.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 77.2 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.1.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a007832f0ae2a89973cfbfb2fe6b62a158ff7a8076864799680f4a2607a503b9
MD5 c07bf95913662f8facd432e453474d5f
BLAKE2b-256 cb27ee05aacefd37ee294dac3ca53204f91fc85b9e34d0dcda9e5f16a3d0ff84

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dd6f514a945185945616c433942049d5565b8162a74e1aabd07bea22105157d4
MD5 82f2a3822f9fb0c7e34eab84d19aa27a
BLAKE2b-256 2f5ec2bfc7f3e3bbfa1077851e09feecc0914c1e899fa510f9ee3458ece5ca46

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: gridoptim-2.1.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 76.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.1.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 95b627cf1841e516a80326f24b44ef0e465a361e5d070a77a70ac38ccd8dcb51
MD5 0da251b5e4d80937fbf7d9461573f107
BLAKE2b-256 deea634e9548680a5d1f0cee64ba33c2f383b2147f5dff9616e43e7dafd4c0dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gridoptim-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 23efb7aa1ab8eb395a5de9f92204e727c30ba9b39542c290089b45d049226004
MD5 71c81f9410fb70e767d296ba77a8185b
BLAKE2b-256 ef5557f6b6238fc64db7dc5d1a72268b2ddb5443072b3b98b7b6fe02da46c6fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for gridoptim-2.1.1-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