Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

linear programming
python
pulp
glpk

What is the GLPK solver in Python PuLP?

Educative Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The GLPK solver

The GNU Linear Programming Kit (GLPK) solver is an open-source solver designed to solve linear programming, mixed-integer, and other related problems. It can easily be accessed and used through Python PuLP with the aid of an API.

Set up the GLPK Solver in PuLP

PuLP uses an API solver from a list of available optimizers to solve a given linear programming problem. We can use the PuLP's listSolvers() method to view the list of solver APIs it can access:

print(listSolvers())
print(listSolvers(onlyAvailable = True))
Execute this code by clicking "Run"

Explanation

  • Line 1: We print a list of solvers accessible through PuLP. We can see that GLPK_CMD is on this list. However, this does not mean that GLPK is necessarily available for use.
  • Line 2: We print a list of solvers currently available to PuLP by passing the onlyAvailable = True argument to PuLP's listSolvers() method. This is the second list in our output. We can see that GLPK_CMD is on this list as well. This confirms that GLPK is available for use in our environment.

Note: If you are attempting to use GLPK with PuLP on your local machine or any other environment, and do not see GLPK_CMD in the list returned by the listSolvers(onlyAvailable = True) command, try running pip install glpk on your shell. You might have to install required dependencies as well.

Specify a solver in PuLP

To use a particular solver in PuLP, the user must specify the solver in the code. PuLP's solve() method allows users to specify their solver of choice. A solver instance can be loaded using the PuLP getSolver() method. The code below shows a way to use the GLPK solver using PuLP:

# using "solve()"
prob.solve(GLPK(msg = True))
print("******************************************")
Do not worry if the output is confusing you. This code is meant to demonstrate a way to access a particular solver.

Explanation

  • Line 2: This shows a way to specify our solver of choice at the time of solving prob—an instance of PuLP's LpProblem class—by passing it as an argument to the solve() method.

RELATED TAGS

linear programming
python
pulp
glpk
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring