Skip to main content

Scipy interface to NLOPT

Project description

SpiNopt: a Scipy optimization interface to NLOPT

For optimization, everyone starts out with the Scipy optimization library, but, at some point, you might want to try something else. NLOPT is a great library, but can be quite a hassle rewrite your code to use it.

This package provides a Scipy interface to the NLOPT optimization library. It's aim is not to provide a complete ecosystem which different solvers are available, but merely a way to quickly connect the NLOPT solvers, once you already have something set up with Scipy, or are familiar with how to set something up with Scipy.

Basic example

import numpy as np
from spinopt import NLOptimizer

dim = 3

# Define objective in this way
def my_easy_func(x, grad):
    if grad.size > 0:
        grad[:] = 2 * (x - np.arange(len(x)))
    x = x - np.arange(len(x))
    return x.dot(x)

# Define constraints Scipy style
A = np.ones((1, dim))
b = np.ones((1, 1))
constraints = [{"type": "eq", "jac": lambda w: A, "fun": lambda w: A.dot(w) - b.squeeze()}]

# Initialize optimizer
x0=np.zeros(dim)
opt = NLOptimizer(my_easy_func, x0, constraints=constraints)

# Optimize
res = opt.minimize()
assert res.success
assert np.allclose(res.x, np.arange(dim), atol=1e-5)

Installation

To install from PyPI:

pip install spinopt

To install the latest development version from github:

pip install git+https://github.com/mvds314/spinopt.git

Development

For development purposes, clone the repo:

git clone https://github.com/mvds314/spinopt.git

Then navigate to the folder containing setup.py and run

pip install -e .

to install the package in edit mode.

Run unittests with pytest.

Related software

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

spinopt-24.7.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

spinopt-24.7.1-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file spinopt-24.7.1.tar.gz.

File metadata

  • Download URL: spinopt-24.7.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for spinopt-24.7.1.tar.gz
Algorithm Hash digest
SHA256 fc5e932176c017b0d660f2cec9dbf34b71d6d6a2c8e95d3d012a103c55f709e1
MD5 19f0150638cab0d4c91f1a57f67f8f64
BLAKE2b-256 dd4954cbcb060c48fe49aeb01c694cf42a3b91ef41640ea5652a85d0c0da80c5

See more details on using hashes here.

File details

Details for the file spinopt-24.7.1-py2.py3-none-any.whl.

File metadata

  • Download URL: spinopt-24.7.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for spinopt-24.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b5e10db487c7ca25c8d2abf4cf6ee8ba3a1b41523945f2108a03bb9b6698b88
MD5 32241d9a49be82310b44a3d82ed9e5ac
BLAKE2b-256 b27eaf9943d5fd88e83c090ebe75ca96dc7e469dc7ddb47a67dee81380c9d153

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