Lpmethod Examples#
This section includes source code for all of the Gurobi lpmethod examples.
The same source code can be found in the examples
directory of the
Gurobi distribution.
/* Copyright 2024, Gurobi Optimization, LLC */
/* Solve a model with different values of the Method parameter;
show which value gives the shortest solve time. */
#include <stdlib.h>
#include <stdio.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL, *menv;
GRBmodel *m = NULL;
int error = 0;
int i;
int optimstatus;
int bestMethod = -1;
double bestTime;
if (argc < 2)
{
fprintf(stderr, "Usage: lpmethod_c filename\n");
exit(1);
}
error = GRBloadenv(&env, "lpmethod.log");
if (error) goto QUIT;
/* Read model */
error = GRBreadmodel(env, argv[1], &m);
if (error) goto QUIT;
menv = GRBgetenv(m);
error = GRBgetdblparam(menv, "TimeLimit", &bestTime);
if (error) goto QUIT;
/* Solve the model with different values of Method */
for (i = 0; i <= 2; ++i)
{
error = GRBreset(m, 0);
if (error) goto QUIT;
error = GRBsetintparam(menv, "Method", i);
if (error) goto QUIT;
error = GRBoptimize(m);
if (error) goto QUIT;
error = GRBgetintattr(m, "Status", &optimstatus);
if (error) goto QUIT;
if (optimstatus == GRB_OPTIMAL) {
error = GRBgetdblattr(m, "Runtime", &bestTime);
if (error) goto QUIT;
bestMethod = i;
/* Reduce the TimeLimit parameter to save time
with other methods */
error = GRBsetdblparam(menv, "TimeLimit", bestTime);
if (error) goto QUIT;
}
}
/* 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);
return 0;
}
/* Copyright 2024, 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"
using namespace std;
int
main(int argc,
char *argv[])
{
if (argc < 2)
{
cout << "Usage: lpmethod_c++ filename" << endl;
return 1;
}
try {
// Read model
GRBEnv env = GRBEnv();
GRBModel m = GRBModel(env, argv[1]);
// Solve the model with different values of Method
int bestMethod = -1;
double bestTime = m.get(GRB_DoubleParam_TimeLimit);
for (int i = 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 methods
m.set(GRB_DoubleParam_TimeLimit, bestTime);
}
}
// Report which method was fastest
if (bestMethod == -1) {
cout << "Unable to solve this model" << endl;
} else {
cout << "Solved in " << bestTime
<< " seconds with Method: " << bestMethod << endl;
}
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch(...) {
cout << "Exception during optimization" << endl;
}
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* Solve a model with different values of the Method parameter;
show which value gives the shortest solve time. */
using System;
using Gurobi;
class lpmethod_cs
{
static void Main(string[] args)
{
if (args.Length < 1) {
Console.Out.WriteLine("Usage: lpmethod_cs filename");
return;
}
try {
// Read model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env, args[0]);
// Solve the model with different values of Method
int bestMethod = -1;
double bestTime = model.Parameters.TimeLimit;
for (int i = 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 methods
model.Parameters.TimeLimit = bestTime;
}
}
// Report which method was fastest
if (bestMethod == -1) {
Console.WriteLine("Unable to solve this model");
} else {
Console.WriteLine("Solved in " + bestTime
+ " seconds with Method: " + bestMethod);
}
// Dispose of model and env
model.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
}
}
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* Solve a model with different values of the Method parameter;
show which value gives the shortest solve time. */
import com.gurobi.gurobi.*;
public class Lpmethod {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java Lpmethod filename");
System.exit(1);
}
try {
// Read model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env, args[0]);
// Solve the model with different values of Method
int bestMethod = -1;
double bestTime = model.get(GRB.DoubleParam.TimeLimit);
for (int i = 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 methods
model.set(GRB.DoubleParam.TimeLimit, bestTime);
}
}
// Report which method was fastest
if (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 environment
model.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". "
+ e.getMessage());
}
}
}
function lpmethod(filename)
% Copyright 2024, Gurobi Optimization, LLC
%
% Solve a model with different values of the Method parameter;
% show which value gives the shortest solve time.
% Read model
fprintf('Reading model %s\n', filename);
model = gurobi_read(filename);
bestTime = inf;
bestMethod = -1;
for i = 0:4
params.method = i;
res = gurobi(model, params);
if strcmp(res.status, 'OPTIMAL')
bestMethod = i;
bestTime = res.runtime;
params.TimeLimit = bestTime;
end
end
% Report which method was fastest
if bestMethod == -1
fprintf('Unable to solve this model\n');
else
fprintf('Solved in %g seconds with Method %d\n', bestTime, bestMethod);
end
#!/usr/bin/env python3.11
# Copyright 2024, Gurobi Optimization, LLC
# Solve a model with different values of the Method parameter;
# show which value gives the shortest solve time.
import sys
import gurobipy as gp
from gurobipy import GRB
if len(sys.argv) < 2:
print("Usage: lpmethod.py filename")
sys.exit(0)
# Read model
m = gp.read(sys.argv[1])
# Solve the model with different values of Method
bestTime = m.Params.TimeLimit
bestMethod = -1
for i in range(3):
m.reset()
m.Params.Method = i
m.optimize()
if m.Status == GRB.OPTIMAL:
bestTime = m.Runtime
bestMethod = i
# Reduce the TimeLimit parameter to save time with other methods
m.Params.TimeLimit = bestTime
# Report which method was fastest
if bestMethod == -1:
print("Unable to solve this model")
else:
print(f"Solved in {bestTime:g} seconds with Method {bestMethod}")
# Copyright 2024, 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 model
cat('Reading model',args[1],'...')
model <- gurobi_read(args[1])
cat('... done\n')
# Solve the model with different values of Method
params <- list()
bestTime <- Inf
bestMethod <- -1
for (i in 0:4) {
params$method <- i
res <- gurobi(model, params)
if (res$status == 'OPTIMAL') {
bestMethod <- i
bestTime <- res$runtime
params$TimeLimit <- bestTime
}
}
# Report which method was fastest
if (bestMethod == -1) {
cat('Unable to solve this model\n')
} else {
cat('Solved in', bestTime, 'seconds with Method:', bestMethod, '\n')
}
rm(params, model)
' Copyright 2024, Gurobi Optimization, LLC
'
' Solve a model with different values of the Method parameter;
' show which value gives the shortest solve time.
Imports System
Imports Gurobi
Class lpmethod_vb
Shared Sub Main(ByVal args As String())
If args.Length < 1 Then
Console.WriteLine("Usage: lpmethod_vb filename")
Return
End If
Try
' Read model and verify that it is a MIP
Dim env As New GRBEnv()
Dim model As New GRBModel(env, args(0))
' Solve the model with different values of Method
Dim bestMethod As Integer = -1
Dim bestTime As Double = model.get(GRB.DoubleParam.TimeLimit)
For i As Integer = 0 To 2
model.Reset()
model.Parameters.Method = i
model.Optimize()
If model.Status = GRB.Status.OPTIMAL Then
bestTime = model.Runtime
bestMethod = i
' Reduce the TimeLimit parameter to save time
' with other methods
model.Parameters.TimeLimit = bestTime
End If
Next
' Report which method was fastest
If bestMethod = -1 Then
Console.WriteLine("Unable to solve this model")
Else
Console.WriteLine("Solved in " & bestTime & _
" seconds with Method: " & bestMethod)
End If
' Dispose of model and env
model.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message)
End Try
End Sub
End Class