Skip to main content

A drop-in for scipy optimize that allows keyword args (x0={'x': 1, 'y': 2}) and more

Project description

ez-optimize

Author: Quinn Marsh
GitHub: https://github.com/qthedoc/ez-optimize/
PyPI: https://pypi.org/project/ez-optimize/

ez-optimize makes optimization easy and intuitive. It is a lightweight wrapper for SciPy's optimize that offer a drop-in replacements for SciPy's minimize function with enhanced features like keyword-based parameter definitions and quick switching between minimization and maximization.

The Ironman suit for optimization.

Why ez-optimize?

1. Keyword-Based Optimization (e.g.: x0={'x': 1, 'y': 2})

By default, optimization uses arrays x0=[1, 2]. However sometimes it's more intuitive to use named parameters x0={'x': 1, 'y': 2}. ez-optimize allows you to define parameters as dictionaries. Then under the hood, ez-optimize automatically flattens parameters (and wraps your function) for SciPy while restoring the original structure in results. Keyword-based optimization is especially useful in complex systems like aerospace or energy models where parameters have meaningful names representing physical quantities.

2. Switch to Maximize with direction='max'

By default, optimization minimizes the objective function. To maximize, you typically need to write a negated version of your function. With ez-optimize, simply set direction='max' and the library will automatically negates your function under the hood.

Examples

Example 1: Minimizing the Rosenbrock Function with Array Mode

import numpy as np
from ez_optimize import minimize

def rosenbrock_2d(x, y, a, b):
    return (a - x)**2 + b * (y - x**2)**2

x0 = {'x': 1.3, 'y': 0.7}

result = minimize(rosenbrock_2d, x0, method='BFGS')

print(f"Optimal x: {result.x_original}")
print(f"Optimal value: {result.fun}")
Optimal x: {'x': 1.0, 'y': 1.0}
Optimal value: 0.0

Example 2: Using OptimizationProblem for Advanced Manual Control

For more control, use the OptimizationProblem class directly. This also serves as a look under the hood for how minimize works.:

from ez_optimize import OptimizationProblem
from scipy.optimize import minimize as scipy_minimize

def objective(a, b, c):
    return a**2 + b**2 + c**2

x0 = {'a': 1.0, 'b': 2.0, 'c': 3.0}
bounds = {'a': (0, 5), 'b': (0, 5), 'c': (0, 5)}

# Define the optimization problem
problem = OptimizationProblem(objective, x0, method='SLSQP', bounds=bounds)

# Run SciPy method directly, passing in the arguments prepared by the OptimizationProblem
scipy_result = scipy_minimize(**problem.scipy.get_minimize_args())

# Use the OptimizationProblem to interpret the result back into our structured format
result = problem.scipy.interpret_result(scipy_result)

print(f"Optimal parameters: {result.x_original}")
print(f"Optimal value: {result.fun}")
Optimal parameters: {'a': 0.0, 'b': 0.0, 'c': 0.0}
Optimal value: 0.0

Fundumentally Why?

Lets be honest, there is good reason optimization typically uses arrays and always minimizes... it makes the math simple and efficient. For example, optimizing in a vector space allows the hessian to be represented as a matrix. However, this level of optimization isn't always necessary like with black-box functions that have no gradient or hessian. In those cases, the convenience of defining keyword-based parameters and easy switching between min/max can outweigh the mathematical perfection of array-based optimization.

Acknowledgments

Inspired by better_optimize by Jesse Grabowski, licensed under MIT.

Contributing

Contributions Welcome! Report bugs, request features, or improve documentation via GitHub issues or pull requests.

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

ez_optimize-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

ez_optimize-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file ez_optimize-0.1.0.tar.gz.

File metadata

  • Download URL: ez_optimize-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for ez_optimize-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f8ef99be596bd677ebe75ce0e177f5afc80c4fd8fd3cc63812503968fa2b731c
MD5 830482c4cf891557ac044e5df64b961c
BLAKE2b-256 0a185f45720b81ee13e8231c4c9b38dfb1a0d0a019c2a4b7efd6a183f1a5b230

See more details on using hashes here.

File details

Details for the file ez_optimize-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ez_optimize-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for ez_optimize-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54d03218859aadfa4e85a069a12b4884b75e084eb4f6ef81a7fb55a838b47f64
MD5 67c7a7d514e3eaf48cda4c0f9813e931
BLAKE2b-256 5d75717a5b978bff2d9641c0c668c138e771e2496efe985af5a58937ab893bc3

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