Dylan Cross
Dylan Cross

Reputation: 609

Pyomo can't locate GLPK solver

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?

Upvotes: 11

Views: 39629

Answers (10)

Pasu Boonkaew
Pasu Boonkaew

Reputation: 1

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.

Upvotes: 0

Eligio Mariño
Eligio Mariño

Reputation: 372

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.

Upvotes: 0

John Curry
John Curry

Reputation: 501

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

Upvotes: 2

stonebig
stonebig

Reputation: 1191

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

Upvotes: 0

EJay
EJay

Reputation: 161

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.

Upvotes: 2

Guest
Guest

Reputation: 106

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

Upvotes: 9

ManJan
ManJan

Reputation: 3991

You can install glpk solver using this command -

brew install glpk

Upvotes: 4

BCR
BCR

Reputation: 982

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

Upvotes: 1

Paul G.
Paul G.

Reputation: 632

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

Upvotes: 0

Dylan Cross
Dylan Cross

Reputation: 609

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.

Upvotes: 0

Related Questions