Try our new documentation site (beta).
The model file
The Gurobi Optimizer provides a wide variety of options for expressing an optimization model. Typically, you would build the model using an interface to a programming languages (C, C++, C#, Java, etc.) or using a higher-level application environment (a spreadsheet, a modeling system, MATLAB, R, etc.). However, to keep our example as simple as possible, we're going to read the model from an LP format file. The LP format was designed to be human readable, and as such it is well suited for our needs.
The LP format is mostly self-explanatory. Here is our model:
Maximize .01 Pennies + .05 Nickels + .1 Dimes + .25 Quarters + 1 Dollars Subject To Copper: .06 Pennies + 3.8 Nickels + 2.1 Dimes + 5.2 Quarters + 7.2 Dollars - Cu = 0 Nickel: 1.2 Nickels + .2 Dimes + .5 Quarters + .2 Dollars - Ni = 0 Zinc: 2.4 Pennies + .5 Dollars - Zi = 0 Manganese: .3 Dollars - Mn = 0 Bounds Cu <= 1000 Ni <= 50 Zi <= 50 Mn <= 50 Integers Pennies Nickels Dimes Quarters Dollars End
You'll find this model in file coins.lp
in the
<installdir>/examples/data
directory of your Gurobi
distribution. Specifically, assuming you've installed
Gurobi 5.6.0 in the recommended location, you'll find the file here:
- Windows (64-bit):
c:\gurobi560\win64\examples\data\coins.lp
- Linux:
/opt/gurobi560/linux64/examples/data/coins.lp
- Mac OS:
/Library/gurobi560/mac64/examples/data/coins.lp
Feel free to open the file in a text editor. However, before you
consider making any modifications to this file or
creating your own, we should point out a
few rules about LP format files. One
relates to the ordering of the various sections. Our example contains
an objective section (Maximize...
), a constraint section
(Subject To...
), a variable bound section (Bounds...
),
and an integrality section (Integers...
). The sections must come
in that order. The complete list of section types, and the associated
ordering rules, can be found in the file format section of the
Gurobi Reference Manual.
The second rule is that tokens must be separated by either a space or a newline. Thus, for example, the term:
+ .1 Dimesmust include a space or newline between
+
and .1
, and
another between .1
and Dimes
.
The third important rule is that variables always appear on the left-hand side of a constraint. The right-hand side is always a constant. Thus, our constraint:
Cu = .06 Pennies + 3.8 Nickels + 2.1 Dimes + 5.2 Quarters + 7.2 Dollars...becomes...
.06 Pennies + 3.8 Nickels + 2.1 Dimes + 5.2 Quarters + 7.2 Dollars - Cu = 0
Another important property of LP files is that variables have
default bounds. Unless stated otherwise, a variable has a zero lower
bound and an infinite upper bound. Thus, Cu <= 1000
really
means 0 <= Cu <= 1000
. Similarly, any variable not mentioned in the
Bounds
section may take any non-negative value.
As we mentioned earlier, full details on the LP file format are provided in the file format section of the Gurobi Reference Manual.