An IPOpt connector for Python
Project description
IPyOpt
IPyOpt
is a Python 🐍 C++ extension that allows you to use
Ipopt in Python.
Installation
Note the pypi repo currently only provides 🐧 linux wheels.
pip install [--user] ipyopt
This will install a precompiled binary version from pypi. Please note, that the precompiled binary is linked against the unoptimized reference implementation of blas/lapack. If you want to take advantage of optimized versions of blas/lapack, compile from source:
pip install --no-binary ipyopt ipyopt
In this case, you also need Ipopt and Numpy. On a debian based system:
sudo apt-get install python3-numpy coinor-ipopt
If coinor-ipopt
does not link correctly, you might have to compile
ipopt
yourself.
See the section Build below or .ci/Dockerfile on
how this can be done.
Usage
You can use IPyOpt
like this:
import ipyopt
# define your call back functions
nlp = ipyopt.Problem(...)
nlp.solve(...)
For an example, see examples/hs071.py.
For maximal performance, there is also support for PyCapsules / scipy.LowLevelCallable. By using this approach, there will be no C++ <-> Python interactions during IPopt optimization. Here is an example test/c_capsules/ (C code) and test/test_ipyopt.py (Python code using the PyCapsules provided by the C code).
IPyOpt
as a module comes with docstring. You can poke around
it by using Python's help()
command.
Build
IPyOpt
depends on the following packages:
- A compiler and a linker, e.g. gcc, ld
- Ipopt
- Numpy
- Python.h (part of the python source code, you can download it from Python.org)
To build from source, first, get the latest source code using:
git clone https://gitlab.com/g-braeunlich/IPyOpt.git
Check whether a file ipopt.pc
was distributed with your Ipopt installation.
If this is the case and ipopt.pc
is in the search path of pkg-config
(on unix systems:
/usr/lib/pkgconfig
, /usr/share/pkgconfig
, /usr/local/lib/pkgconfig
,
/usr/local/share/pkgconfig
), nothing has to be modified.
In this case run
python setup.py build
sudo python setup.py install
If pkg-config
is not available for your system, you will need to
pass appropriate information to setup.py
by setting the environment
variable CFLAGS
. Example:
CFLAGS="-I/usr/include/coin/ -l/usr/lib64 -lipopt -lmumps_common -ldmumps -lzmumps -lsmumps -lcmumps -llapack -lblas -lblas -lblas -lm -ldl' ./setup.py build
sudo python setup.py install
If you have an ipopt.pc
which is not in the pkg-config
search path,
specify the path via the PKG_CONFIG_PATH
environment variable (see below).
If you cannot find an ipopt.pc
in your ipopt
installation, there is an
example pc file pkgconfig/ipopt.pc.
Copy it to a location (best of all directly in a subfolder named
pkgconfig
of your Ipopt installation) and edit it to reflect the
library and include paths of the dependencies.
Then do
PKG_CONFIG_PATH=<dir containing ipopt.pc> python setup.py build
sudo python setup.py install
Testing
Unit tests:
python -m unittest
Run examples:
Use the following command under the examples directory.
python hs071.py
The file examples/hs071.py contains a toy
optimization problem. If everything is OK, IPyOpt
will invoke
Ipopt
to solve it for you. This python file is self-documented and
can be used as a template for writing your own optimization problems.
Hessian Estimation: since Hessian estimation is usually tedious,
Ipopt can solve problems without Hessian estimation. IPyOpt
also
supports this feature. The file examples/hs071.py
demonstrates the idea. If you provide the ipyopt.Problem
constructor
with an eval_h
callback function, IPOpt
will delegate the Hessian matrix calculation to your
function (otherwise IPOpt
will approximate Hessian for you).
Contributing
- Fork it.
- Create a branch (
git checkout -b new_branch
) - Commit your changes (
git commit -am "your awesome message"
) - Push to the branch (
git push origin new_branch
) - Create a merge request
Credits
- Modifications on logger made by OpenMDAO at NASA Glenn Research Center, 2010 and 2011
- Added "eval_intermediate_callback" by OpenMDAO at NASA Glenn Research Center, 2010 and 2011
- Modifications on the SAFE_FREE macro made by Guillaume Jacquenot, 2012
- Changed logger from code contributed by alanfalloon
- Originally developed by Eric Xu when he was a PhD student at Washington University and issued under the BSD license. Original repository: xuy/pyipopt.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for ipyopt-0.12.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4191d493b19f9f17f5d740dc9aad56728912c2de51a07474a23d9298423bdc31 |
|
MD5 | a80eebd029f8728e4b102e4bba627e5c |
|
BLAKE2b-256 | 947475ccb4d8bb9ba4e3f9f694ac492fea391a592e0b45ab55d30c78ed60f6fa |
Hashes for ipyopt-0.12.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08d7bcfeeb31dfd07c6db536b30a876243bec32bee93d4ecb6944189f31e0be6 |
|
MD5 | 31ce2fc32ad85b7eb2c4e43c322ef166 |
|
BLAKE2b-256 | a0f44957d547d45ae81c406e354adf3b08d77e90f484f155bfda697b64ea03d2 |
Hashes for ipyopt-0.12.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdc874b604258aba6e047ef9a133520a7448dd0d11e3578704e3fff6db61184f |
|
MD5 | 8e380db822c6fb9e5932aef750efa1ff |
|
BLAKE2b-256 | 18fbbaac814e97c7528b79a8493e4e61799028514c4ba7cdb433d5f36ecb1b03 |
Hashes for ipyopt-0.12.0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3a0ee6784f1815aa73cbb13324f28d1b81faaf2049b4df55d64611bfeb861c5 |
|
MD5 | 6ed1be23fa0d249cc65edaa4f9f98250 |
|
BLAKE2b-256 | 7ed16cc82201f755225361d44365b29c522e8a4e971f5a948c5d12d046a28355 |
Hashes for ipyopt-0.12.0-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 780344dbf4d3f194d12fe2909f9460a898b3c20961d3888b21cf5129ca2a8164 |
|
MD5 | cc24013549f26c75d730ad15187f4d6a |
|
BLAKE2b-256 | e05c20ca097268820f99cfd66f0db251cbd1c19d3c86c18af096351f0dfa798c |