#!/usr/bin/env python3# Copyright 2025, Gurobi Optimization, LLC# This example reads an LP model from a file and solves it.# If the model is infeasible or unbounded, the example turns off# presolve and solves the model again. If the model is infeasible,# the example computes an Irreducible Inconsistent Subsystem (IIS),# and writes it to a fileimportsysimportgurobipyasgpfromgurobipyimportGRBiflen(sys.argv)<2:print("Usage: lp.py filename")sys.exit(0)# Read and solve modelmodel=gp.read(sys.argv[1])model.optimize()ifmodel.Status==GRB.INF_OR_UNBD:# Turn presolve off to determine whether model is infeasible# or unboundedmodel.setParam(GRB.Param.Presolve,0)model.optimize()ifmodel.Status==GRB.OPTIMAL:print(f"Optimal objective:{model.ObjVal:g}")model.write("model.sol")sys.exit(0)elifmodel.Status!=GRB.INFEASIBLE:print(f"Optimization was stopped with status{model.Status}")sys.exit(0)# Model is infeasible - compute an Irreducible Inconsistent Subsystem (IIS)print("")print("Model is infeasible")model.computeIIS()model.write("model.ilp")print("IIS written to file 'model.ilp'")
Help and Feedback