Pyomo can't locate GLPK solver
Asked Answered
P

10

11

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?

Pascoe answered 31/8, 2015 at 16:46 Comment(0)
I
9

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

Iaverne answered 2/10, 2018 at 10:57 Comment(1)
Adding the path folder to the python path and restarting anaconda prompt / spyder worked for me.Headpiece
M
4

You can install glpk solver using this command -

brew install glpk

Millburn answered 1/6, 2017 at 21:18 Comment(0)
S
2

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.

Sammiesammons answered 23/5, 2019 at 9:45 Comment(0)
A
1

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

Apostil answered 16/9, 2015 at 1:0 Comment(1)
That's the same thing I'm doing now.Pascoe
P
1

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

Proclivity answered 24/5, 2020 at 13:40 Comment(0)
V
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

Veiled answered 31/8, 2015 at 17:48 Comment(1)
I tried throwing the source code you indicated with my import statements, but no luck. Same error.Pascoe
P
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.

Pascoe answered 1/9, 2015 at 18:28 Comment(0)
Q
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

Quar answered 4/9, 2015 at 7:9 Comment(0)
R
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.

Route answered 14/12, 2021 at 16:43 Comment(0)
S
0

This is how I make it work:

  1. Open Anaconda Prompt.
  2. Type conda activate [your environment].
  3. Type conda install -c conda-forge pyomo.
  4. Type conda install -c conda-forge glpk.
  5. Type where glpsol.
  6. Copy the path that keep the file glpsol.exe.

Now you have to set the path in your computer's environment by:

  1. Click Start and search for "Edit the system environment variables".
  2. Click "Environment Variables...".
  3. Double-click "Path" in user variables.
  4. Paste the path you got from step 4 (Only the path that keeps the file glpsol.exe).
  5. Click OK and restart your Anaconda.

Hope it helps, and sorry for any confusion that might have occurred.

Schoonover answered 5/3 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.