The model file

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 is a text format that 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:

  .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
  Cu <= 1000
  Ni <= 50
  Zi <= 50
  Mn <= 50
  Pennies Nickels Dimes Quarters Dollars

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 10.0.3 in the recommended location, you'll find the file in /opt/gurobi1003/linux64/examples/data/coins.lp.

To modify this file, open it in a text editor (like vim).

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 (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.

2. Separating tokens

Tokens must be separated by either a space or a newline. Thus, for example, the term:

+ .1 Dimes
must include a space or newline between + and .1, and another between .1 and Dimes.

3. Arranging variables

In LP files and the C API, 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

  .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 means 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.

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.