Reading and optimizing a model

To access the Gurobi Interactive Shell, you can simply type from the command prompt. If you've installed a Python IDE, the shell will also be available from that environment.

Once the optimizer has started, you are ready to load and optimize a model. We'll consider model coins.lp from <installdir>/examples/data...


Using license file /opt/gurobi/gurobi.lic
Set parameter LogFile to value gurobi.log

Gurobi Interactive Shell, Version 9.1.2
Copyright (c) 2021, Gurobi Optimization, LLC
Type "help()" for help

gurobi> m = read('/opt/gurobi912/linux64/examples/data/coins.lp')
Read LP format model from file /opt/gurobi912/linux64/examples/data/coins.lp

Reading time = 0.01 seconds
: 4 rows, 9 columns, 16 nonzeros

gurobi> m.optimize()
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (linux64)
Optimize a model with 4 rows, 9 columns and 16 nonzeros
Model fingerprint: 0x5ce1d538
Variable types: 4 continuous, 5 integer (0 binary)
Coefficient statistics:
  Matrix range     [6e-02, 7e+00]
  Objective range  [1e-02, 1e+00]
  Bounds range     [5e+01, 1e+03]
  RHS range        [0e+00, 0e+00]
Found heuristic solution: objective -0.0000000
Presolve removed 1 rows and 5 columns
Presolve time: 0.00s
Presolved: 3 rows, 4 columns, 9 nonzeros
Variable types: 0 continuous, 4 integer (0 binary)

Root relaxation: objective 1.134615e+02, 2 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0  113.46154    0    1   -0.00000  113.46154      -     -    0s
H    0     0                     113.4500000  113.46154  0.01%     -    0s
     0     0  113.46154    0    1  113.45000  113.46154  0.01%     -    0s

Explored 1 nodes (2 simplex iterations) in 0.01 seconds
Thread count was 8 (of 8 available processors)

Solution count 2: 113.45 -0

Optimal solution found (tolerance 1.00e-04)
Best objective 1.134500000000e+02, best bound 1.134500000000e+02, gap 0.0000%

The read() command reads a model from a file and returns a Model object. In the example, that object is placed into variable m. There is no need to declare variables in the Python language; you simply assign a value to a variable.

Note that read() accepts wildcard characters, so you could also have said:

gurobi> m = read('/opt/gurobi912/linux64/*/*/coin*')

Note also that Gurobi commands that read or write files will also function correctly with compressed files. If gzip, bzip2, or 7zip are installed on your machine and available in your default path, then you simply need to add the appropriate suffix (.gz, .bz2, .zip, or .7z) to the file name to read or write compressed versions.

The next statement in the example, m.optimize(), invokes the optimize method on the Model object (you can obtain a list of all methods on Model objects by typing help(Model) or help(m)). The Gurobi optimization engine finds an optimal solution with objective 113.45.

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.