The Gurobi Optimizer provides a 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 notebook, 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 8.1.1 in the recommended location,
you'll find the file in c:\gurobi811\win64\examples\data\coins.lp.
To modify this file, open it in a text editor (like WordPad).
Editing the LP file
Before you consider making any modifications to this file or creating your own, we should point out a few rules about LP format files.
1. Ordering of the sections
Our example contains an objective section (
Maximize...), a constraint section
Subject To...), a variable bound section (
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.
2. Separating tokens
Tokens must be separated by either a space or a newline. Thus, for example, the term:
+ .1 Dimesmust include a space or newline between
.1, and another between
3. Arranging variables
Variables must 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
4. Variable default bounds
Unless stated otherwise, a variable has a zero lower
bound and an infinite upper bound. Thus,
Cu <= 1000 really
0 <= Cu <= 1000. Similarly, any variable not mentioned in the
Bounds section may take any non-negative value.
Full details on the LP file format are provided in the file format section of the Gurobi Reference Manual.