Try our new documentation site (beta).


# Copyright 2016, Gurobi Optimization, Inc.

# This example reads a MIP model from a file, solves it and prints
# the objective values from all feasible solutions generated while
# solving the MIP. Then it creates the associated fixed model and
# solves that model.

import sys
from gurobipy import *

if len(sys.argv) < 2:
    print('Usage: filename')

# Read and solve model

model = read(sys.argv[1])

if model.isMIP == 0:
    print('Model is not a MIP')


if model.status == GRB.Status.OPTIMAL:
    print('Optimal objective: %g' % model.objVal)
elif model.status == GRB.Status.INF_OR_UNBD:
    print('Model is infeasible or unbounded')
elif model.status == GRB.Status.INFEASIBLE:
    print('Model is infeasible')
elif model.status == GRB.Status.UNBOUNDED:
    print('Model is unbounded')
    print('Optimization ended with status %d' % model.status)

# Iterate over the solutions and compute the objectives
model.params.outputFlag = 0
for k in range(model.solCount):
    model.params.solutionNumber = k
    objn = 0
    for v in model.getVars():
        objn += v.obj * v.xn
    print('Solution %d has objective %g' % (k, objn))
model.params.outputFlag = 1

fixed = model.fixed()
fixed.params.presolve = 0

if fixed.status != GRB.Status.OPTIMAL:
    print("Error: fixed model isn't optimal")

diff = model.objVal - fixed.objVal

if abs(diff) > 1e-6 * (1.0 + abs(model.objVal)):
    print('Error: objective values are different')

# Print values of nonzero variables
for v in fixed.getVars():
    if v.x != 0:
        print('%s %g' % (v.varName, v.x))

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.