Pyomo can't locate GLPK solver

允我心安 提交于 2021-02-07 03:20:27

问题


I'm trying to use the GLPK solver with Pyomo. I have a working model that's been tested, but keep getting an error saying GLPK can't be found.

WARNING: Could not locate the 'glpsol' executable, which is required for solver 'glpk'

I've installed glpk sucessfully. I also added the directory to my path variable so the executed can be called globally. I tested this with glpsol --help from my command line, and see the help info printed.

The below thread says it should be working, but alas, it is not.

How do you install glpk-solver along with pyomo in Winpython

Any ideas?


回答1:


This answer is late but I want to share the solution that worked for me.

solvername='glpk'

solverpath_folder='C:\\glpk\\w64' #does not need to be directly on c drive

solverpath_exe='C:\\glpk\\w64\\glpsol' #does not need to be directly on c drive

I used to do this:

sys.path.append(solverpath_folder)

solver=SolverFactory(solvername)

This works for the cbc solver in coin-or but it does not work for glpk. Then I tried something different:

solver=SolverFactory(solvername,executable=solverpath_exe)

This worked for both cbc and glpk. No idea why this works (I really didn't do anything else).

Version: Python 2.7 or Python 3.7 (tested both), glpk 4.65




回答2:


You can install glpk solver using this command -

brew install glpk




回答3:


Installing the glpk package worked for me. As I use Anaconda:

conda install -c conda-forge glpk

This was after already including the 'glpsol' exectuable's folder path in my PATH variables.




回答4:


So it looks like the set path variable is not handled by your Python installation.

A normal Python installation is set up for a seperated "PYTHONPATH" environment variable to look up additional modules. There is also the option to make an entry in the windows registry or (like you already mentioned) move the files to the Python home directory, which is recognized relative to your installation directory if "PYTHONHOME" is not set.

More information in the Python Documentary under 3.3.3. https://docs.python.org/2/using/windows.html#finding-modules




回答5:


I was having the same issue. I don't know if this is a solution but it definitely got the solver working.

After downloading the Windows installation. I copied all the files in the w64 folder and pasted them directly into my Python working directory.

After that my python code could locate the solver.

NOTE: this was for Python 3.4.3.4, Windows 8.1 64 bit




回答6:


Reading the source code here suggests you try:

from pyutilib.services import register_executable, registered_executable
register_executable(name='glpsol')

maybe will it give a clue




回答7:


I had the same issue on Windows 10 and it was down to glpk being installed in a different conda environment. Full steps for installing pyomo and glpk below.

Test the installation by running 'Repeated Solves' example from https://pyomo.readthedocs.io/en/latest/working_models.html

Instructions (run at an anaconda prompt)

conda create --name myenv

conda activate myenv

conda install -c conda-forge pyomo

conda install -c conda-forge pyomo.extras

conda install -c conda-forge glpk

Run spyder from myenv so that if finds everything

spyder activate myenv




回答8:


For anyone that has the same issue, I found a workaround (not a solution!). I copied all the glpk files into my C:/Python27 directory, and (Surprise!) Python can now find them.

I'll hold out for a real solution before accepting this one.




回答9:


Thanks to Stonebig for this suggestion: mays suggest you try:

   from pyutilib.services import register_executable, registered_executable
   register_executable( name='glpsol')

maybe will it give a clue

I had a situation under Linux, where I could not add to the PATH environment value prior to starting Python, so I had to augment the os.environ['PATH'] with the path for my solver ('glpsol'). This was insufficient by itself, but by making the call suggested by Stonebig after modifying PATH, it worked. Pyomo did find the solver, and was able to solve my model.



来源:https://stackoverflow.com/questions/32316069/pyomo-cant-locate-glpk-solver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!