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.5.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: spinopt-24.5.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for spinopt-24.5.0.tar.gz
Algorithm Hash digest
SHA256 c59a27c30e27e7854f10348873dcacb40ccce8ef66b8241418a43c751f5a8770
MD5 4669c8f5148901722d03f11897a16b75
BLAKE2b-256 e2bd2d977dc9cdbc41425f019a45fe46826ab4ab673aa954ec42f84e9f6562a9

See more details on using hashes here.

File details

Details for the file spinopt-24.5.0-py3-none-any.whl.

File metadata

  • Download URL: spinopt-24.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for spinopt-24.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34c35fe8b13fe3b0660b304b0fd3aefd077809f0216d07b8fc8a91d8570bec2a
MD5 b11b4b66748c482cbd49e6e7362d991e
BLAKE2b-256 4db768a12a004e5f9149453e44176038386507f331242d89434aaf8988b1ff3e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page