This section includes source code for all of the Gurobi lpmethod examples.The same source code can be found in theexamples directory of theGurobi distribution.
/* Copyright 2025, Gurobi Optimization, LLC *//* Solve a model with different values of the Method parameter; show which value gives the shortest solve time. */#include<assert.h>#include<stdlib.h>#include<stdio.h>#include"gurobi_c.h"intmain(intargc,char*argv[]){GRBenv*env=NULL,*menv;GRBmodel*m=NULL;interror=0;inti;intoptimstatus;intbestMethod=-1;doublebestTime;if(argc<2){fprintf(stderr,"Usage: lpmethod_c filename\n");exit(1);}error=GRBloadenv(&env,"lpmethod.log");if(error)gotoQUIT;/* Read model */error=GRBreadmodel(env,argv[1],&m);if(error)gotoQUIT;menv=GRBgetenv(m);assert(menv!=NULL);error=GRBgetdblparam(menv,"TimeLimit",&bestTime);if(error)gotoQUIT;/* Solve the model with different values of Method */for(i=0;i<=2;++i){error=GRBreset(m,0);if(error)gotoQUIT;error=GRBsetintparam(menv,"Method",i);if(error)gotoQUIT;error=GRBoptimize(m);if(error)gotoQUIT;error=GRBgetintattr(m,"Status",&optimstatus);if(error)gotoQUIT;if(optimstatus==GRB_OPTIMAL){error=GRBgetdblattr(m,"Runtime",&bestTime);if(error)gotoQUIT;bestMethod=i;/* Reduce the TimeLimit parameter to save time with other methods */error=GRBsetdblparam(menv,"TimeLimit",bestTime);if(error)gotoQUIT;}}/* Report which method was fastest */if(bestMethod==-1){printf("Unable to solve this model\n");}else{printf("Solved in %f seconds with Method: %i\n",bestTime,bestMethod);}QUIT:/* Error reporting */if(error){printf("ERROR: %s\n",GRBgeterrormsg(env));exit(1);}/* Free model */GRBfreemodel(m);/* Free environment */GRBfreeenv(env);return0;}
/* Copyright 2025, Gurobi Optimization, LLC *//* Solve a model with different values of the Method parameter; show which value gives the shortest solve time. */#include"gurobi_c++.h"usingnamespacestd;intmain(intargc,char*argv[]){if(argc<2){cout<<"Usage: lpmethod_c++ filename"<<endl;return1;}try{// Read modelGRBEnvenv=GRBEnv();GRBModelm=GRBModel(env,argv[1]);// Solve the model with different values of MethodintbestMethod=-1;doublebestTime=m.get(GRB_DoubleParam_TimeLimit);for(inti=0;i<=2;++i){m.reset();m.set(GRB_IntParam_Method,i);m.optimize();if(m.get(GRB_IntAttr_Status)==GRB_OPTIMAL){bestTime=m.get(GRB_DoubleAttr_Runtime);bestMethod=i;// Reduce the TimeLimit parameter to save time// with other methodsm.set(GRB_DoubleParam_TimeLimit,bestTime);}}// Report which method was fastestif(bestMethod==-1){cout<<"Unable to solve this model"<<endl;}else{cout<<"Solved in "<<bestTime<<" seconds with Method: "<<bestMethod<<endl;}}catch(GRBExceptione){cout<<"Error code = "<<e.getErrorCode()<<endl;cout<<e.getMessage()<<endl;}catch(...){cout<<"Exception during optimization"<<endl;}return0;}
/* Copyright 2025, Gurobi Optimization, LLC *//* Solve a model with different values of the Method parameter; show which value gives the shortest solve time. */usingSystem;usingGurobi;classlpmethod_cs{staticvoidMain(string[]args){if(args.Length<1){Console.Out.WriteLine("Usage: lpmethod_cs filename");return;}try{// Read modelGRBEnvenv=newGRBEnv();GRBModelmodel=newGRBModel(env,args[0]);// Solve the model with different values of MethodintbestMethod=-1;doublebestTime=model.Parameters.TimeLimit;for(inti=0;i<=2;++i){model.Reset();model.Parameters.Method=i;model.Optimize();if(model.Status==GRB.Status.OPTIMAL){bestTime=model.Runtime;bestMethod=i;// Reduce the TimeLimit parameter to save time// with other methodsmodel.Parameters.TimeLimit=bestTime;}}// Report which method was fastestif(bestMethod==-1){Console.WriteLine("Unable to solve this model");}else{Console.WriteLine("Solved in "+bestTime+" seconds with Method: "+bestMethod);}// Dispose of model and envmodel.Dispose();env.Dispose();}catch(GRBExceptione){Console.WriteLine("Error code: "+e.ErrorCode+". "+e.Message);}}}
/* Copyright 2025, Gurobi Optimization, LLC *//* Solve a model with different values of the Method parameter; show which value gives the shortest solve time. */importcom.gurobi.gurobi.*;publicclassLpmethod{publicstaticvoidmain(String[]args){if(args.length<1){System.out.println("Usage: java Lpmethod filename");System.exit(1);}try{// Read modelGRBEnvenv=newGRBEnv();GRBModelmodel=newGRBModel(env,args[0]);// Solve the model with different values of MethodintbestMethod=-1;doublebestTime=model.get(GRB.DoubleParam.TimeLimit);for(inti=0;i<=2;++i){model.reset();model.set(GRB.IntParam.Method,i);model.optimize();if(model.get(GRB.IntAttr.Status)==GRB.Status.OPTIMAL){bestTime=model.get(GRB.DoubleAttr.Runtime);bestMethod=i;// Reduce the TimeLimit parameter to save time// with other methodsmodel.set(GRB.DoubleParam.TimeLimit,bestTime);}}// Report which method was fastestif(bestMethod==-1){System.out.println("Unable to solve this model");}else{System.out.println("Solved in "+bestTime+" seconds with Method: "+bestMethod);}// Dispose of model and environmentmodel.dispose();env.dispose();}catch(GRBExceptione){System.out.println("Error code: "+e.getErrorCode()+". "+e.getMessage());}}}
functionlpmethod(filename)% Copyright 2025, Gurobi Optimization, LLC%% Solve a model with different values of the Method parameter;% show which value gives the shortest solve time.% Read modelfprintf('Reading model %s\n',filename);model=gurobi_read(filename);bestTime=inf;bestMethod=-1;fori=0:4params.method=i;res=gurobi(model,params);ifstrcmp(res.status,'OPTIMAL')bestMethod=i;bestTime=res.runtime;params.TimeLimit=bestTime;endend% Report which method was fastestifbestMethod==-1fprintf('Unable to solve this model\n');elsefprintf('Solved in %g seconds with Method %d\n',bestTime,bestMethod);end
#!/usr/bin/env python3# Copyright 2025, Gurobi Optimization, LLC# Solve a model with different values of the Method parameter;# show which value gives the shortest solve time.importsysimportgurobipyasgpfromgurobipyimportGRBiflen(sys.argv)<2:print("Usage: lpmethod.py filename")sys.exit(0)# Read modelm=gp.read(sys.argv[1])# Solve the model with different values of MethodbestTime=m.Params.TimeLimitbestMethod=-1foriinrange(3):m.reset()m.Params.Method=im.optimize()ifm.Status==GRB.OPTIMAL:bestTime=m.RuntimebestMethod=i# Reduce the TimeLimit parameter to save time with other methodsm.Params.TimeLimit=bestTime# Report which method was fastestifbestMethod==-1:print("Unable to solve this model")else:print(f"Solved in{bestTime:g} seconds with Method{bestMethod}")
# Copyright 2025, Gurobi Optimization, LLC## Solve a model with different values of the Method parameter;# show which value gives the shortest solve time.library(Matrix)library(gurobi)args<-commandArgs(trailingOnly=TRUE)if(length(args)<1){stop('Usage: Rscript lpmethod.R filename\n')}# Read modelcat('Reading model',args[1],'...')model<-gurobi_read(args[1])cat('... done\n')# Solve the model with different values of Methodparams<-list()bestTime<-InfbestMethod<--1for(iin0:4){params$method<-ires<-gurobi(model,params)if(res$status=='OPTIMAL'){bestMethod<-ibestTime<-res$runtimeparams$TimeLimit<-bestTime}}# Report which method was fastestif(bestMethod==-1){cat('Unable to solve this model\n')}else{cat('Solved in',bestTime,'seconds with Method:',bestMethod,'\n')}rm(params,model)
' Copyright 2025, Gurobi Optimization, LLC'' Solve a model with different values of the Method parameter;' show which value gives the shortest solve time.ImportsSystemImportsGurobiClasslpmethod_vbSharedSubMain(ByValargsAsString())Ifargs.Length<1ThenConsole.WriteLine("Usage: lpmethod_vb filename")ReturnEndIfTry' Read model and verify that it is a MIPDimenvAsNewGRBEnv()DimmodelAsNewGRBModel(env,args(0))' Solve the model with different values of MethodDimbestMethodAsInteger=-1DimbestTimeAsDouble=model.get(GRB.DoubleParam.TimeLimit)ForiAsInteger=0To2model.Reset()model.Parameters.Method=imodel.Optimize()Ifmodel.Status=GRB.Status.OPTIMALThenbestTime=model.RuntimebestMethod=i' Reduce the TimeLimit parameter to save time' with other methodsmodel.Parameters.TimeLimit=bestTimeEndIfNext' Report which method was fastestIfbestMethod=-1ThenConsole.WriteLine("Unable to solve this model")ElseConsole.WriteLine("Solved in "&bestTime&_" seconds with Method: "&bestMethod)EndIf' Dispose of model and envmodel.Dispose()env.Dispose()CatcheAsGRBExceptionConsole.WriteLine("Error code: "&e.ErrorCode&". "&e.Message)EndTryEndSubEndClass
Help and Feedback