Params Examples#
This section includes source code for all of the Gurobi params examples.
The same source code can be found in the examples
directory of the
Gurobi distribution.
/* Copyright 2024, Gurobi Optimization, LLC */
/* Use parameters that are associated with a model.
A MIP is solved for a few seconds with different sets of parameters.
The one with the smallest MIP gap is selected, and the optimization
is resumed until the optimal solution is found.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL, *modelenv = NULL, *bestenv = NULL;
GRBmodel *model = NULL, *bestmodel = NULL;
int error = 0;
int ismip, i, mipfocus;
double bestgap, gap;
if (argc < 2)
{
fprintf(stderr, "Usage: params_c filename\n");
exit(1);
}
error = GRBloadenv(&env, "params.log");
if (error) goto QUIT;
/* Read model and verify that it is a MIP */
error = GRBreadmodel(env, argv[1], &model);
if (error) goto QUIT;
error = GRBgetintattr(model, "IsMIP", &ismip);
if (error) goto QUIT;
if (ismip == 0)
{
printf("The model is not an integer program\n");
exit(1);
}
/* Set a 2 second time limit */
modelenv = GRBgetenv(model);
if (!modelenv) {
printf("Cannot retrieve model environment\n");
exit(1);
}
error = GRBsetdblparam(modelenv, "TimeLimit", 2);
if (error) goto QUIT;
/* Now solve the model with different values of MIPFocus */
bestmodel = GRBcopymodel(model);
if (!bestmodel) {
printf("Cannot copy model\n");
exit(1);
}
error = GRBoptimize(bestmodel);
if (error) goto QUIT;
error = GRBgetdblattr(bestmodel, "MIPGap", &bestgap);
if (error) goto QUIT;
for (i = 1; i <= 3; ++i)
{
error = GRBreset(model, 0);
if (error) goto QUIT;
modelenv = GRBgetenv(model);
if (!modelenv) {
printf("Cannot retrieve model environment\n");
exit(1);
}
error = GRBsetintparam(modelenv, "MIPFocus", i);
if (error) goto QUIT;
error = GRBoptimize(model);
if (error) goto QUIT;
error = GRBgetdblattr(model, "MIPGap", &gap);
if (error) goto QUIT;
if (bestgap > gap)
{
GRBmodel *tmp = bestmodel;
bestmodel = model;
model = tmp;
bestgap = gap;
}
}
/* Finally, free the extra model, reset the time limit and
continue to solve the best model to optimality */
GRBfreemodel(model);
bestenv = GRBgetenv(bestmodel);
if (!bestenv) {
printf("Cannot retrieve best model environment\n");
exit(1);
}
error = GRBsetdblparam(bestenv, "TimeLimit", GRB_INFINITY);
if (error) goto QUIT;
error = GRBoptimize(bestmodel);
if (error) goto QUIT;
error = GRBgetintparam(bestenv, "MIPFocus", &mipfocus);
if (error) goto QUIT;
printf("Solved with MIPFocus: %i\n", mipfocus);
QUIT:
/* Error reporting */
if (error)
{
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free best model */
GRBfreemodel(bestmodel);
/* Free environment */
GRBfreeenv(env);
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* Use parameters that are associated with a model.
A MIP is solved for a few seconds with different sets of parameters.
The one with the smallest MIP gap is selected, and the optimization
is resumed until the optimal solution is found.
*/
#include "gurobi_c++.h"
using namespace std;
int
main(int argc,
char *argv[])
{
if (argc < 2)
{
cout << "Usage: params_c++ filename" << endl;
return 1;
}
GRBEnv* env = 0;
GRBModel *bestModel = 0, *m = 0;
try
{
// Read model and verify that it is a MIP
env = new GRBEnv();
m = new GRBModel(*env, argv[1]);
if (m->get(GRB_IntAttr_IsMIP) == 0)
{
cout << "The model is not an integer program" << endl;
return 1;
}
// Set a 2 second time limit
m->set(GRB_DoubleParam_TimeLimit, 2);
// Now solve the model with different values of MIPFocus
bestModel = new GRBModel(*m);
bestModel->optimize();
for (int i = 1; i <= 3; ++i)
{
m->reset();
m->set(GRB_IntParam_MIPFocus, i);
m->optimize();
if (bestModel->get(GRB_DoubleAttr_MIPGap) >
m->get(GRB_DoubleAttr_MIPGap))
{
swap(bestModel, m);
}
}
// Finally, delete the extra model, reset the time limit and
// continue to solve the best model to optimality
delete m;
m = 0;
bestModel->set(GRB_DoubleParam_TimeLimit, GRB_INFINITY);
bestModel->optimize();
cout << "Solved with MIPFocus: " <<
bestModel->get(GRB_IntParam_MIPFocus) << endl;
}
catch (GRBException e)
{
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
}
catch (...)
{
cout << "Error during optimization" << endl;
}
delete bestModel;
delete m;
delete env;
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* Use parameters that are associated with a model.
A MIP is solved for a few seconds with different sets of parameters.
The one with the smallest MIP gap is selected, and the optimization
is resumed until the optimal solution is found.
*/
using System;
using Gurobi;
class params_cs
{
static void Main(string[] args)
{
if (args.Length < 1) {
Console.Out.WriteLine("Usage: params_cs filename");
return;
}
try {
// Read model and verify that it is a MIP
GRBEnv env = new GRBEnv();
GRBModel m = new GRBModel(env, args[0]);
if (m.IsMIP == 0) {
Console.WriteLine("The model is not an integer program");
Environment.Exit(1);
}
// Set a 2 second time limit
m.Parameters.TimeLimit = 2.0;
// Now solve the model with different values of MIPFocus
GRBModel bestModel = new GRBModel(m);
bestModel.Optimize();
for (int i = 1; i <= 3; ++i) {
m.Reset();
m.Parameters.MIPFocus = i;
m.Optimize();
if (bestModel.MIPGap > m.MIPGap) {
GRBModel swap = bestModel;
bestModel = m;
m = swap;
}
}
// Finally, delete the extra model, reset the time limit and
// continue to solve the best model to optimality
m.Dispose();
bestModel.Parameters.TimeLimit = GRB.INFINITY;
bestModel.Optimize();
Console.WriteLine("Solved with MIPFocus: " +
bestModel.Parameters.MIPFocus);
// Clean up bestModel and environment
bestModel.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " +
e.Message);
}
}
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* Use parameters that are associated with a model.
A MIP is solved for a few seconds with different sets of parameters.
The one with the smallest MIP gap is selected, and the optimization
is resumed until the optimal solution is found.
*/
import com.gurobi.gurobi.*;
public class Params {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java Params filename");
System.exit(1);
}
try {
// Read model and verify that it is a MIP
GRBEnv env = new GRBEnv();
GRBModel m = new GRBModel(env, args[0]);
if (m.get(GRB.IntAttr.IsMIP) == 0) {
System.out.println("The model is not an integer program");
System.exit(1);
}
// Set a 2 second time limit
m.set(GRB.DoubleParam.TimeLimit, 2);
// Now solve the model with different values of MIPFocus
GRBModel bestModel = new GRBModel(m);
bestModel.optimize();
for (int i = 1; i <= 3; ++i) {
m.reset();
m.set(GRB.IntParam.MIPFocus, i);
m.optimize();
if (bestModel.get(GRB.DoubleAttr.MIPGap) >
m.get(GRB.DoubleAttr.MIPGap)) {
GRBModel swap = bestModel;
bestModel = m;
m = swap;
}
}
// Finally, delete the extra model, reset the time limit and
// continue to solve the best model to optimality
m.dispose();
bestModel.set(GRB.DoubleParam.TimeLimit, GRB.INFINITY);
bestModel.optimize();
System.out.println("Solved with MIPFocus: " +
bestModel.get(GRB.IntParam.MIPFocus));
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
function params(filename)
% Copyright 2024, Gurobi Optimization, LLC
%
% Use parameters that are associated with a model.
%
% A MIP is solved for a few seconds with different sets of parameters.
% The one with the smallest MIP gap is selected, and the optimization
% is resumed until the optimal solution is found.
% Read model
fprintf('Reading model %s\n', filename);
model = gurobi_read(filename);
ivars = find(model.vtype ~= 'C');
if length(ivars) <= 0
fprintf('All variables of the model are continuous, nothing to do\n');
return;
end
% Set a 2 second time limit
params.TimeLimit = 2;
% Now solve the model with different values of MIPFocus
params.MIPFocus = 0;
result = gurobi(model, params);
bestgap = result.mipgap;
bestparams = params;
for i = 1:3
params.MIPFocus = i;
result = gurobi(model, params);
if result.mipgap < bestgap
bestparams = params;
bestgap = result.mipgap;
end
end
% Finally, reset the time limit and Re-solve model to optimality
bestparams.TimeLimit = Inf;
result = gurobi(model, bestparams);
fprintf('Solution status: %s, objective value %g\n', ...
result.status, result.objval);
end
#!/usr/bin/env python3.11
# Copyright 2024, Gurobi Optimization, LLC
# Use parameters that are associated with a model.
#
# A MIP is solved for a few seconds with different sets of parameters.
# The one with the smallest MIP gap is selected, and the optimization
# is resumed until the optimal solution is found.
import sys
import gurobipy as gp
if len(sys.argv) < 2:
print("Usage: params.py filename")
sys.exit(0)
# Read model and verify that it is a MIP
m = gp.read(sys.argv[1])
if m.IsMIP == 0:
print("The model is not an integer program")
sys.exit(1)
# Set a 2 second time limit
m.Params.TimeLimit = 2
# Now solve the model with different values of MIPFocus
bestModel = m.copy()
bestModel.optimize()
for i in range(1, 4):
m.reset()
m.Params.MIPFocus = i
m.optimize()
if bestModel.MIPGap > m.MIPGap:
bestModel, m = m, bestModel # swap models
# Finally, delete the extra model, reset the time limit and
# continue to solve the best model to optimality
del m
bestModel.Params.TimeLimit = float("inf")
bestModel.optimize()
print(f"Solved with MIPFocus: {bestModel.Params.MIPFocus}")
# Copyright 2024, Gurobi Optimization, LLC
#
# Use parameters that are associated with a model.
#
# A MIP is solved for a few seconds with different sets of parameters.
# The one with the smallest MIP gap is selected, and the optimization
# is resumed until the optimal solution is found.
library(Matrix)
library(gurobi)
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1) {
stop('Usage: Rscript params.R filename\n')
}
# Read model
cat('Reading model',args[1],'...')
model <- gurobi_read(args[1])
cat('... done\n')
# Detect set of non-continuous variables
intvars <- which(model$vtype != 'C')
numintvars <- length(intvars)
if (numintvars < 1) {
stop('All model\'s variables are continuous, nothing to do\n')
}
# Set a 2 second time limit
params <- list()
params$TimeLimit <- 2
# Now solve the model with different values of MIPFocus
params$MIPFocus <- 0
result <- gurobi(model, params)
bestgap <- result$mipgap
bestparams <- params
for (i in 1:3) {
params$MIPFocus <- i
result <- gurobi(model, params)
if (result$mipgap < bestgap) {
bestparams <- params
bestgap <- result$mipgap
}
}
# Finally, reset the time limit and Re-solve model to optimality
bestparams$TimeLimit <- Inf
result <- gurobi(model, bestparams)
cat('Solved with MIPFocus:', bestparams$MIPFocus, '\n')
# Clear space
rm(model, params, result, bestparams)
' Copyright 2024, Gurobi Optimization, LLC */
' Use parameters that are associated with a model.
' A MIP is solved for a few seconds with different sets of parameters.
' The one with the smallest MIP gap is selected, and the optimization
' is resumed until the optimal solution is found.
Imports System
Imports Gurobi
Class params_vb
Shared Sub Main(args As String())
If args.Length < 1 Then
Console.Out.WriteLine("Usage: params_vb filename")
Return
End If
Try
' Read model and verify that it is a MIP
Dim env As New GRBEnv()
Dim m As New GRBModel(env, args(0))
If m.IsMIP = 0 Then
Console.WriteLine("The model is not an integer program")
Environment.Exit(1)
End If
' Set a 2 second time limit
m.Parameters.TimeLimit = 2.0
' Now solve the model with different values of MIPFocus
Dim bestModel As New GRBModel(m)
bestModel.Optimize()
For i As Integer = 1 To 3
m.Reset()
m.Parameters.MIPFocus = i
m.Optimize()
If bestModel.MIPGap > m.MIPGap Then
Dim swap As GRBModel = bestModel
bestModel = m
m = swap
End If
Next
' Finally, delete the extra model, reset the time limit and
' continue to solve the best model to optimality
m.Dispose()
bestModel.Parameters.TimeLimit = GRB.INFINITY
bestModel.Optimize()
Console.WriteLine("Solved with MIPFocus: " & bestModel.Parameters.MIPFocus)
Catch e As GRBException
Console.WriteLine("Error code: " + e.ErrorCode & ". " + e.Message)
End Try
End Sub
End Class