This section includes source code for all of the Gurobi lp examples.The same source code can be found in theexamples directory of theGurobi distribution.
/* 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 file */#include<stdlib.h>#include<stdio.h>#include<math.h>#include"gurobi_c.h"intmain(intargc,char*argv[]){GRBenv*env=NULL;GRBmodel*model=NULL;interror=0;intoptimstatus;doubleobjval;if(argc<2){fprintf(stderr,"Usage: lp_c filename\n");exit(1);}/* Create environment */error=GRBloadenv(&env,"lp.log");if(error)gotoQUIT;/* Read model from file */error=GRBreadmodel(env,argv[1],&model);if(error)gotoQUIT;/* Solve model */error=GRBoptimize(model);if(error)gotoQUIT;/* Capture solution information */error=GRBgetintattr(model,GRB_INT_ATTR_STATUS,&optimstatus);if(error)gotoQUIT;/* If model is infeasible or unbounded, turn off presolve and resolve */if(optimstatus==GRB_INF_OR_UNBD){/* Change parameter on model environment. The model now has a copy of the original environment, so changing the original will no longer affect the model. */error=GRBsetintparam(GRBgetenv(model),"PRESOLVE",0);if(error)gotoQUIT;error=GRBoptimize(model);if(error)gotoQUIT;error=GRBgetintattr(model,GRB_INT_ATTR_STATUS,&optimstatus);if(error)gotoQUIT;}if(optimstatus==GRB_OPTIMAL){error=GRBgetdblattr(model,GRB_DBL_ATTR_OBJVAL,&objval);if(error)gotoQUIT;printf("Optimal objective: %.4e\n\n",objval);}elseif(optimstatus==GRB_INFEASIBLE){printf("Model is infeasible\n\n");error=GRBcomputeIIS(model);if(error)gotoQUIT;error=GRBwrite(model,"model.ilp");if(error)gotoQUIT;}elseif(optimstatus==GRB_UNBOUNDED){printf("Model is unbounded\n\n");}else{printf("Optimization was stopped with status = %d\n\n",optimstatus);}QUIT:/* Error reporting */if(error){printf("ERROR: %s\n",GRBgeterrormsg(env));exit(1);}/* Free model */GRBfreemodel(model);/* Free environment */GRBfreeenv(env);return0;}
/* 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 file */#include"gurobi_c++.h"usingnamespacestd;intmain(intargc,char*argv[]){if(argc<2){cout<<"Usage: lp_c++ filename"<<endl;return1;}try{GRBEnvenv=GRBEnv();GRBModelmodel=GRBModel(env,argv[1]);model.optimize();intoptimstatus=model.get(GRB_IntAttr_Status);if(optimstatus==GRB_INF_OR_UNBD){model.set(GRB_IntParam_Presolve,0);model.optimize();optimstatus=model.get(GRB_IntAttr_Status);}if(optimstatus==GRB_OPTIMAL){doubleobjval=model.get(GRB_DoubleAttr_ObjVal);cout<<"Optimal objective: "<<objval<<endl;}elseif(optimstatus==GRB_INFEASIBLE){cout<<"Model is infeasible"<<endl;// compute and write out IISmodel.computeIIS();model.write("model.ilp");}elseif(optimstatus==GRB_UNBOUNDED){cout<<"Model is unbounded"<<endl;}else{cout<<"Optimization was stopped with status = "<<optimstatus<<endl;}}catch(GRBExceptione){cout<<"Error code = "<<e.getErrorCode()<<endl;cout<<e.getMessage()<<endl;}catch(...){cout<<"Error during optimization"<<endl;}return0;}
/* 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 file. */usingSystem;usingGurobi;classlp_cs{staticvoidMain(string[]args){if(args.Length<1){Console.Out.WriteLine("Usage: lp_cs filename");return;}try{GRBEnvenv=newGRBEnv();GRBModelmodel=newGRBModel(env,args[0]);model.Optimize();intoptimstatus=model.Status;if(optimstatus==GRB.Status.INF_OR_UNBD){model.Parameters.Presolve=0;model.Optimize();optimstatus=model.Status;}if(optimstatus==GRB.Status.OPTIMAL){doubleobjval=model.ObjVal;Console.WriteLine("Optimal objective: "+objval);}elseif(optimstatus==GRB.Status.INFEASIBLE){Console.WriteLine("Model is infeasible");// compute and write out IISmodel.ComputeIIS();model.Write("model.ilp");}elseif(optimstatus==GRB.Status.UNBOUNDED){Console.WriteLine("Model is unbounded");}else{Console.WriteLine("Optimization was stopped with status = "+optimstatus);}// Dispose of model and envmodel.Dispose();env.Dispose();}catch(GRBExceptione){Console.WriteLine("Error code: "+e.ErrorCode+". "+e.Message);}}}
/* 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 file */importcom.gurobi.gurobi.*;publicclassLp{publicstaticvoidmain(String[]args){if(args.length<1){System.out.println("Usage: java Lp filename");System.exit(1);}try{GRBEnvenv=newGRBEnv();GRBModelmodel=newGRBModel(env,args[0]);model.optimize();intoptimstatus=model.get(GRB.IntAttr.Status);if(optimstatus==GRB.Status.INF_OR_UNBD){model.set(GRB.IntParam.Presolve,0);model.optimize();optimstatus=model.get(GRB.IntAttr.Status);}if(optimstatus==GRB.Status.OPTIMAL){doubleobjval=model.get(GRB.DoubleAttr.ObjVal);System.out.println("Optimal objective: "+objval);}elseif(optimstatus==GRB.Status.INFEASIBLE){System.out.println("Model is infeasible");// Compute and write out IISmodel.computeIIS();model.write("model.ilp");}elseif(optimstatus==GRB.Status.UNBOUNDED){System.out.println("Model is unbounded");}else{System.out.println("Optimization was stopped with status = "+optimstatus);}// Dispose of model and environmentmodel.dispose();env.dispose();}catch(GRBExceptione){System.out.println("Error code: "+e.getErrorCode()+". "+e.getMessage());}}}
functionlp()% Copyright 2025, Gurobi Optimization, LLC%% This example formulates and solves the following simple LP model:% maximize% x + 2 y + 3 z% subject to% x + y <= 1% y + z <= 1%model.A=sparse([110;011]);model.obj=[123];model.modelsense='Max';model.rhs=[11];model.sense=['<''<'];result=gurobi(model);disp(result.objval);disp(result.x);% Alterantive representation of A - as sparse triplet matrixi=[1;1;2;2];j=[1;2;2;3];x=[1;1;1;1];model.A=sparse(i,j,x,2,3);% Set some parametersparams.method=2;params.timelimit=100;result=gurobi(model,params);disp(result.objval);disp(result.x)end
#!/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'")
# Copyright 2025, Gurobi Optimization, LLC## This example formulates and solves the following simple LP model:# maximize# x + 2 y + 3 z# subject to# x + y <= 1# y + z <= 1library(Matrix)library(gurobi)model<-list()model$A<-matrix(c(1,1,0,0,1,1),nrow=2,byrow=T)model$obj<-c(1,2,3)model$modelsense<-'max'model$rhs<-c(1,1)model$sense<-c('<','<')result<-gurobi(model)print(result$objval)print(result$x)# Second option for A - as a sparseMatrix (using the Matrix package)...model$A<-spMatrix(2,3,c(1,1,2,2),c(1,2,2,3),c(1,1,1,1))params<-list(Method=2,TimeLimit=100)result<-gurobi(model,params)print(result$objval)print(result$x)# Third option for A - as a sparse triplet matrix (using the slam package)...model$A<-simple_triplet_matrix(c(1,1,2,2),c(1,2,2,3),c(1,1,1,1))params<-list(Method=2,TimeLimit=100)result<-gurobi(model,params)print(result$objval)print(result$x)# Clear spacerm(result,params,model)
' 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 file.ImportsSystemImportsGurobiClasslp_vbSharedSubMain(ByValargsAsString())Ifargs.Length<1ThenConsole.WriteLine("Usage: lp_vb filename")ReturnEndIfTryDimenvAsGRBEnv=NewGRBEnv("lp1.log")DimmodelAsGRBModel=NewGRBModel(env,args(0))model.Optimize()DimoptimstatusAsInteger=model.StatusIfoptimstatus=GRB.Status.INF_OR_UNBDThenmodel.Parameters.Presolve=0model.Optimize()optimstatus=model.StatusEndIfIfoptimstatus=GRB.Status.OPTIMALThenDimobjvalAsDouble=model.ObjValConsole.WriteLine("Optimal objective: "&objval)ElseIfoptimstatus=GRB.Status.INFEASIBLEThenConsole.WriteLine("Model is infeasible")model.ComputeIIS()model.Write("model.ilp")ElseIfoptimstatus=GRB.Status.UNBOUNDEDThenConsole.WriteLine("Model is unbounded")ElseConsole.WriteLine("Optimization was stopped with status = "&_optimstatus)EndIf' Dispose of model and envmodel.Dispose()env.Dispose()CatcheAsGRBExceptionConsole.WriteLine("Error code: "&e.ErrorCode&". "&e.Message)EndTryEndSubEndClass
Help and Feedback