10

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?

Community
  • 1
  • 1
Dylan Cross
  • 544
  • 2
  • 6
  • 14

9 Answers9

7

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

Guest
  • 86
  • 1
  • 3
  • 1
    Adding the path folder to the python path and restarting anaconda prompt / spyder worked for me. – cookesd Oct 31 '20 at 15:37
2

You can install glpk solver using this command -

brew install glpk

ManJan
  • 3,939
  • 3
  • 20
  • 22
1

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.

EJay
  • 159
  • 4
0

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

Richard
  • 56,349
  • 34
  • 180
  • 251
stonebig
  • 1,193
  • 1
  • 9
  • 13
0

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.

Dylan Cross
  • 544
  • 2
  • 6
  • 14
0

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

Paul G.
  • 632
  • 6
  • 16
0

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

BCR
  • 960
  • 11
  • 27
0

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

John Curry
  • 392
  • 3
  • 12
0

Here is the relevant part where pyomo 6.2 searches for the glpsol executable https://github.com/Pyomo/pyomo/blob/568c6595a56570c6ea69c3ae3198b73b9f473abd/pyomo/common/fileutils.py#L288

def _path():
    return (os.environ.get('PATH','') or os.defpath).split(os.pathsep)

There are two options to solve the PATH problem:

  1. Putting the executable in an available folder in PATH (recommended practice). The glpsol executable must be in one of the folders present in the PATH system environment variable. Use in your code print(os.environ['PATH']) to see the available folders and put it there.

  2. Adding the folder to PATH at runtime. You can add it to the system PATH statically or use code to add it dynamically (only while your script is running):

     GLPK_FOLDER_PATH = "path/to/glpk"
     os.environ["PATH"] += os.pathsep + str(GLPK_FOLDER_PATH)
    

In my case, my Python project has a virtual environment .venv, and I have an installation process that pastes the files essential to the glpsol executable when I install the project inside the .venv/Scripts folder. Because that folder is added automatically to the system PATH when Python is called from the virtual environment, libraries like Pyomo can find it. And I don't have to remember to add the folder to PATH at runtime whenever I want to use Pyomo.

Eligio Mariño
  • 318
  • 4
  • 13