Brief introduction
Modeling is a powerful way to solve complex problems. Based on the description of the book Modeling Languages in Mathematical optimization (see Resources), the model is used to:
Explain the phenomenon
To predict
Assess key factors
Identification limit
Analyze Compromise methods
In the industry, spreadsheet software such as Microsoft Excel is often the primary choice for modeling issues. Spreadsheets are now often very intuitive, but they have limitations in solving large problems. If you are a developer, you may find it more efficient to write scripts to solve modeling problems because you can easily integrate scripts into other systems. This article will introduce the basics of using the Pyomo library to achieve linear optimization in Python applications.
Entry
The first step is to install Pyomo. Pyomo is a central component of COOPR, and Coopr is a collection of Python software packages. You can download the Coopr_install script, which creates a Python virtual environment when you run it using the Python interpreter.
Create a relative directory named "COOPR":
noahs-macbook-air% python Coopr_install
Start Pyomo with the following command, which places the relative directory and its executable file in your path:
noahs-macbook-air% Source Coopr/bin/activate
Use the Pyomo--help command to get help using Pyomo:
(COOPR) noahs-macbook-air% Pyomo--help
You need a compiler to use the virtual Python Environment Builder (VIRTUALENV) and Pyomo. On OS X, use the XCode Developer Tools command line tool. On Linux, use the GNU Compiler Collection (GCC). After initializing this virtual environment, you can use Pyomo to solve the problem in one of the following two ways:
To use the Pyomo command line tool:
(COOPR) noahs-macbook-air% Pyomo my_problem.py--solver=glpk
Alternatively, embed the initialization code in your script and run it through the Python interpreter:
Listing 1. Call Pyomo in a script
#This is a optional code path that allows the script to be
run outside of
#pyomo command-line. For example: python wyndor.py
if __name__ = ' __main__ ':
#This replicates what the Pyomo command-line tools do Es from
coopr.opt import solverfactory
opt = solverfactory ("glpk")
instance = model.create ()
results = Opt.solve (instance)
#sends results to stdout
results.write ()
Pyomo assume that you have at least one solution (Solver) installed. The GNU Linear programming Kit (GLPK) is the default solution. See the installation instructions for the solution you want to use. Then make sure that the Pyomo can find the solution on its path.