An IPOpt connector for Python
Project description
IPyOpt
IPyOpt
is a python module that allows you to use
Ipopt in Python.
It was developed by Eric Xu when he was a PhD student at Washington
University and issued under the BSD license.
Original repository: xuy/pyipopt.
Installation
Note the pypi repo only provides linux builds for now.
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(...)
You can also check out examples/hs071.py to see
how to use IPyOpt
.
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
To see if you have IPyOpt
ready, 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.
IPyOpt
is a legitimate Python module, you can inspect it by using
standard Python commands like dir
or help
. All functions in
IPyOpt
are documented in details.
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 as well as the apply_new
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
Troubleshooting
Check Ipopt
IPyOpt
links to Ipopt
's C library. If that library is not
available IPyOpt
will fail during module initialization. To check
the availability of this library, you can go to
$IPOPT_DIR/Ipopt/examples/hs071_c/
and issue make
to ensure you can compile and run the toy example
supplied by Ipopt
.
Miscellaneous problems
-
Error:
import ipyopt
ImportError: can not find libipopt.so.0
-
Solution: find it and copy it to a folder that ld can access
-
Error:
import ipyopt
ImportError: /usr/lib/libipopt.so.0: undefined symbol: _gfortran_XXX
-
Solution: check if your
hs071_c
example work. It is very likely that your ipopt library is not correctly compiled. -
Error:
import ipyopt
ImportError: /usr/lib/libipopt.so.0: undefined symbol: SetIntermediateCallback
-
Solution: SetIntermediateCallback is a function added since Ipopt 3.9.1. (see https://projects.coin-or.org/Ipopt/changeset/1830 ) Make sure you have an Ipopt version >= 3.9.1
-
Error:
import ipyopt
ImportError: /usr/lib/libipopt.so.0: undefined symbol: ma19ad_
-
Solution: First, use
nm /usr/lib/libipopt.so.0 | grep ma19ad_
to see if it is marked with U. It should. This means that
libipopt.so.0
is not aware oflibcoinhsl.so.0
. You can fix this by adding-lcoinhsl
to theCFLAGS
variable (see section install). It seems to me that this happens in the recent versions ofipopt
. EventuallyIPyOpt
will have a better building mechanism, and I will fix this soon. -
Error:
import ipyopt
ImportError: /usr/lib/libipopt.so.0: undefined symbol: SomeKindOfSymbol
-
Solution: I can assure you that it is NOT a bug of
IPyOpt
. It is very likely that you did not link the right package when compilingIPyOpt
.First, use
nm /usr/lib/libipopt.so.0 | grep SomeKindOfSymbol
to see if this symbol is indeed missing. Do a Google search to find the library file, and add
-lWhateverLibrary
to theCFLAGS
variable (see section install).Ipopt is built using various third-party libraries. Different machines may have different set of libraries. You should try to locate these dependencies and indicate them when compiling
IPyOpt
. This is just a limitation of dynamic linking libraries and is not related toIPyOpt
. Please do not report a missing symbol error as a "bug" to me unless you are 100% sure it is the problem ofIPyOpt
.
Contact
Gerhard Bräunlich g.braeunlich@disroot.org
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
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.10.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abeedf0aa09260b95016a7853d48ed22f4b2d8806ce93c45616cf21077c3f1b5 |
|
MD5 | 40b80e034fa7965d29cea34d792465af |
|
BLAKE2b-256 | b5ea8bd86297ae0673909ec3f8c0e21d773d1994837275becc0412517756e626 |
Hashes for ipyopt-0.10.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 510185f0fd6b560ee220f924a1a829cb72eb65faefb3ecd68e0417af6ede2912 |
|
MD5 | 7d73e8efb2770491c8e8976316524a2e |
|
BLAKE2b-256 | de0b28d529b001dbf9bd75cc98afcd8fa6663409a281d8024355cb051a6f56f6 |
Hashes for ipyopt-0.10.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c4e12a220ccc6283683c9d1c3217fc5144cc4bd3eeed04923eea61030d7acb8 |
|
MD5 | ad334f5c8268870c96290e5fe50bc504 |
|
BLAKE2b-256 | 8d5c6cf7b95eadf3311efbb53f933be4204ea52b21a88c5fdac9d085e9481fec |
Hashes for ipyopt-0.10.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa91a9908a8370f91695fbf644a7fa881fd5a0cf785da0183711b69bc14b1c3d |
|
MD5 | 45f843358ef654e8ff8b808bb38cb694 |
|
BLAKE2b-256 | 1148b8fb2492b1cc6ed9e76a94aa1a21b5d8170bbbca64eaf6e2b5f7646ee337 |
Hashes for ipyopt-0.10.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71cc455f49cf6a36e8799ec84529433dc3af8e1b399b47c2b58b36e55da15831 |
|
MD5 | a35c1508b18156cf4d69220a3e8f1195 |
|
BLAKE2b-256 | 8aa0a7ce5213d80de03c2903c4859df8eb58c43123ff2f1b5827e5be57116e69 |