Following the previous article on modeling and solving an optimization problem in Python using several “interfaces” (+), in this article, I try to provide a comprehensive review of open-source (OS), free, free & open-source (FOSS), and commercial “solvers,” which are usually used for specific types of problems and coded with low-level programming languages (such as C++, Java, etc.). Notably, this is still not a complete survey of what is available, but I try to complete it over time. Besides, the sequence of presentation says nothing about solvers’ quality. Finally, priority is given to solvers that have an easy-to-use interface for themselves.
Criteria on reviewing solvers for optimization in Python
All reviewed solvers must meet the following requirements:
- Authenticity: The solver should have an official website.
- Security: The website to download the solver should be reported secure by the browser.
- Updates: The solver should be under maintenance and not an EOL project.
- Application programming interface (API): Solvers that provide callable libraries for multiple programming languages, especially Python and C++.
- Popularity: Searching for the solver through the internet should show its name on the first page.
- Ability: It should solve mixed-integer linear programming (MILP) models.
1. CPLEX for optimization in Python
- Supported problems: LP, MIP, QCP, and MIQCP.
- Supporting interfaces: DOCPLEX, PULP, PYOMO, ORTOOLS, PICOS, and CVXPY.
- Supported scales: Small (free), Medium (license), and Large (license).
- Status: Commercial
Herein, I provide a coding example using “cplex” and “docplex” in Python:
# Installation (Uncomment the Line Below) #!pip install cplex #!pip install docplex # Import package import docplex as op from docplex.mp.model import Model # Define environment prob = Model("MyOptProblem") # Define decision variables x = prob.integer_var(lb=0,ub=None) y = prob.continuous_var(lb=0,ub=None) # Add objective function to the environment prob.set_objective("max", 2*x+5*y) # Add constraints to the environment prob.add_constraint(5*x+3*y<=10, ctname="const1") prob.add_constraint(2*x+7*y<=9, ctname="const2") # The status of the solution prob.print_information() prob.solve() # To display optimal decision variables prob.print_solution() # To display optimal value of objective function print("Optimal Value of Objective Is = ",prob.objective_value)
2. GUROBI for optimization in Python
- Supported problems: LP, MIP, QCP, and MIQCP.
- Supporting interfaces: GUROBIPY, MIP, PULP, PYOMO, ORTOOLS, PICOS, CVXPY, and DRAKE.
- Supported scales: Small (free), Medium (license), and Large (license).
- Status: Commercial
Herein, I provide a coding example using “gurobipy” in Python:
# Installation (Uncomment the Line Below) #!pip install gurobipy # Import package import gurobipy as op # Define environment prob = op.Model("MyOptProblem") # Define decision variables x = prob.addVar(vtype=op.GRB.INTEGER, lb=0 , name='x') y = prob.addVar(vtype=op.GRB.CONTINUOUS, lb=0, name='y') # Add objective function to the environment prob.setObjective(2*x+5*y, op.GRB.MAXIMIZE) # Add constraints to the environment prob.addConstr(5*x+3*y<=10, "const1") prob.addConstr(2*x+7*y<=9, "const2") # The status of the solution prob.optimize() # To display optimal decision variables for v in prob.getVars(): print('%s: %g' % (v.VarName, v.X)) # To display optimal value of objective function print('Optimal Value of Objective Is = %g' % prob.ObjVal)
3. LOCALSOLVER for optimization in Python
- Supported problems: LP, MIP, QCP, and MIQCP.
- Supporting interfaces: LOCALSOLVER.
- Supported scales: Small (license), Medium (license), and Large (license).
- Status: Commercial
Herein, I provide a coding example using “localsolver” in Python:
# Installation (Uncomment the Line Below) (Requires its software to be installed) # !pip install localsolver -i https://pip.localsolver.com # Import package import localsolver with localsolver.LocalSolver() as op: # Define environment prob = op.model # Define decision variables x = prob.int(0, None) y = prob.float(0, None) # Add constraints to the environment prob.constraint(5*x+3*y<=10) prob.constraint(2*x+7*y<=9) # Add objective function to the environment prob.maximize(2*x+5*y) # To display optimal decision variables prob.close() op.solve()
4. XPRESS for optimization in Python
- Supported problems: LP, MIP, NLP, CNS, DNLP, MINLP, QCP, and MIQCP.
- Supporting interfaces: XPRESS, PULP, and CVXPY.
- Supported scales: Small (free), Medium (license), and Large (license).
- Status: Commercial
Herein, I provide a coding example using “xpress” in Python:
# Installation (Uncomment the Line Below) #!pip install xpress # Import package import xpress as op # Define decision variables x = op.var(vartype=op.integer) y = op.var() z = 2 * x + 5 * y # Define environment prob = op.problem("MyOptProb") prob.addVariable(x) prob.addVariable(y) # Add constraints to the environment prob.addConstraint(5*x+3*y <= 10) prob.addConstraint(2*x+7*y <= 9) # Add objective function to the environment prob.setObjective(z, sense=op.maximize) # The status of the solution prob.solve() # To display optimal decision variables print("x: ", prob.getSolution(x)) print("y: ",prob.getSolution(y)) # To display optimal value of objective function print("Optimal Value of Objective Is = ", prob.getSolution(z))
5. OCTERACT for optimization in Python
- Supported problems: LP, MIP, NLP, QCP, MIQCP, MIQCQP, DNLP, and DMINLP.
- Supporting interfaces: OCTERACT, and PYOMO.
- Supported scales: Small (free license), Medium (free license), and Large (free license).
- Status: Free
Herein, I provide a coding example using “octeract” (a solver linker) in Python:
# Installation (Please refer to its website) (Requires its software to be installed) from octeract import * # Define environment prob = Model("MyOptProblem") # Define decision variables prob.add_variable("x", 0, None, INT) prob.add_variable("y", 0, None) # Add objective function to the environment prob.set_objective("2*x+5*y", MAXIMIZE) # Add constraints to the environment prob.add_constraint("5*x+3*y<=10") prob.add_constraint("2*x+7*y<=9") # To display optimal decision variables (using 4 threads) prob.global_solve(4)
6. SCIP for optimization in Python
- Supported problems: LP, MIP, NLP, MINLP, CNS, DNLP, and QCP.
- Supporting interfaces: PYSCIPOPT, PULP, ORTOOLS, and CVXPY.
- Supported scales: Small (free), Medium (free), and Large (free).
- Status: Free
Herein, I provide a coding example using “pyscipopt” in Python:
# Installation (Requires msvc>=14.0 for building binaries from its source code) (Uncomment the Line Below) # !pip install pyscipopt # Import package import pyscipopt as op # Define environment prob = op.Model("MyOptProblem") # Define decision variables x = prob.addVar("x", vtype="INTEGER") y = prob.addVar("y") # Add objective function to the environment prob.setObjective(2*x+5*y, sense='maximize') # Add constraints to the environment prob.addCons(5*x+3*y<=10) prob.addCons(2*x+7*y<=9) # The status of the solution prob.optimize() # To display optimal decision variables sol = prob.getBestSol() print("x: {}".format(sol[x])) print("y: {}".format(sol[y])) # To display optimal value of objective function print("Optimal Value of Objective Is = ", prob.getObjective())
If this article is going to be used in research or other publishing methods, you can cite it as Tafakkori (2022) (in text) and refer to it as follows: Tafakkori, K. (2022). Using solvers for optimization in Python. Supply Chain Data Analytics. url: https://www.supplychaindataanalytics.com/using-solvers-for-optimization-in-python/
Industrial engineer focusing on leveraging optimization methods and artificial intelligence technologies using multiple programming languages to empower a business to achieve its goals!
Leave a Reply