How to install the optimization pack pyOpt in Pyth

2019-05-31 02:08发布

问题:

I have Anaconda pack for Python installed on my windows 7 laptop.

I followed all the installation steps as explained at: http://www.pyopt.org/install.html#installation

After some trial and error, I could install pyOpt using the following command:

python setup.py install --compiler=mingw32

But still, I can't run even the simplest tutorial example at .. http://www.pyopt.org/tutorial.html

.. maybe because the optimizer SLSQP is missing in the installation. I get following error message when I run the tutorial example:

slsqp = pyOpt.SLSQP()
AttributeError: 'module' object has no attribute 'SLSQP'

How can I install pyOpt completely with optimizers etc. on windows?

回答1:

I'm going to preface this with: I know very little about what I am doing...

I've been stuck on this for a long time too. I have a feeling it has something to do with how the modules are built during installation. I think the pyOpt modules all point to fortran and c libraries which are "translated" to python when you run

python setup.py install --compiler=mingw32

I have been playing around using different compilers and command environments (administrator privledges, powershell, cygwin, etc.) to no avail. The msvc compiler fails at a certain point. Cygwin, MinGW32, IntelEM, all compile but produce the same error when going to run the actual examples.

If you look in the C:\PathToPython\Lib\site-packages\pyOpt\ folder you'll see the folders with all the modules. When you call import, you're importing these modules... so by entering the following (for example... I think):

from pyOpt import pySNOPT

you are more or less instructing the imports to navigate through the folders to find pySNOPT and import it ... I think. You'll notice that in \pyOpt\pySNOPT\ there is a file pySNOPT.py. When importing, using from pyOpt import SNOPT it looks in pyOpt for SNOPT.py... but you'll find (as python has) that there is NO such file to import. Instead, there is only a pySNOPT.py. Knowing this we would expect the following to fix the problem:

from pyOpt.pySNOPT import pySNOPT

But this, too, fails. This time producing an import error stating "SNOPT shared library failed to import" (this is a pretty big clue, I suspect).

If you open the pySNOPT.py (this could be replaced with the acronyms for any of the other solvers) file you'll see the familiar format of a python module staring back at you. The first lines of all of these files goes something like:

try:
    import snopt
except ImportError:
    raise ImportError('SNOPT shared library failed to import')

This is where the breakdown occurs. The "import snopt" line is supposed to load the snopt.dll (snopt.so, snopt.a for Linux, Unix). When running setup.py, if I use a cygwin environment it creates DLLs in each of the C:\PathToCygwinDrive\lib\python2.7\site-packages\pyOpt\pyACRONYM folders. These DLLs do NOT appear to be placed in the location corresponding to the version of Python I am telling PyCharm to use (c:\PathToPython\Lib\site-packages\pyOpt\pyACRONYM). I used all of the compilers (including cygwin) in other environments (powershell, Visual C++ 2008 64-bit command prompt, any other command prompt for that matter). Simply copying and pasting the DLLs from the Cygwin pyOpt folder to the regular Python pyOpt folder did not work. I don't know if there's a special way that DLLs have to be imported or something. Reading up on building C extensions and linking them to python projects got over my head and tedious way too fast.

I thought this was local to my computer (since a group member managed to get his installation working) but when going through setup on another group member's computer I ran into the EXACT same error.

One important thing to note... I am doing everything in x64 architecture (as was the group member who I tried to help but also got stuck here). This might be the real secret to fixing this. Perhaps there are too many hoops to jump through to get pyOpt working on 64-bit Python. Which brings me to my last point... there some workarounds I can think of:

  1. install 32-bit Python, restart the process.
  2. install Spyder IDE (through anaconda or something), then install pyOpt using:

    conda install --channel https://conda.anaconda.org/melund pyopt
    

    or

    conda install --channel https://conda.anaconda.org/mutirri pyopt
    

    (both for x64 windows) DO NOT use the default conda pyOpt (conda install pyopt) as this is version 0.84 and will not work.

  3. Switch to Linux (since programming with linux is like using lube with anal... you CAN make it work without it... but everything will be a lot more painful)

These are the workarounds I've found so far. I hope someone posts an actual solution SOON!



回答2:

I succeeded to install pyOpt on Win64 on Anaconda. Here is how:

One can use the command from the comment above to install pyOpt 1.2 from mutirri channel:

conda install --channel https://conda.anaconda.org/mutirri pyopt

Installation goes almost smoothly. With the only exception that it asks to install Microsoft MPI package in order for pyOpt to work properly (that is easy: just go to https://www.microsoft.com/en-us/download/details.aspx?id=49926 and download the latest installer).

However, when we try to run some test script for pyOpt, an attempt to use the SLSQP solver fails. Surprise! So, some research is needed.

If we load Lib\site-packages\pyOpt\pySLSQP\slsqp.pyd file into Dependency Walker we can see that LIBGFORTRAN-3.DLL file is missing.

Quik google search shows that LIBGFORTRAN-3.DLL file comes bundled with MinGW64. So, we need to download and install MinGW64 using installer from here:

https://sourceforge.net/projects/mingw-w64/files/mingw-w64/

Then locate LIBGFORTRAN-3.DLL in the mingw64\bin folder and copy all DLLs from this folder somewhere into the PATH (I just copied all mingw64 DLLs to C:\Anaconda2\Library\bin).

Now SLSQP solver works!