Tune Examples#
This section includes source code for all of the Gurobi tune examples.
The same source code can be found in the examples
directory of the
Gurobi distribution.
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example reads a model from a file and tunes it.
It then writes the best parameter settings to a file
and solves the model using these parameters. */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int tuneresultcount;
int error = 0;
if (argc < 2) {
fprintf(stderr, "Usage: tune_c filename\n");
exit(1);
}
/* Create environment */
error = GRBloadenv(&env, "tune_c.log");
if (error) goto QUIT;
/* Read model from file */
error = GRBreadmodel(env, argv[1], &model);
if (error) goto QUIT;
/* Set the TuneResults parameter to 2
*
* The first parameter setting is the result for the first solved
* setting. The second entry the parameter setting of the best parameter
* setting.
*/
error = GRBsetintparam(GRBgetenv(model), GRB_INT_PAR_TUNERESULTS, 2);
if (error) goto QUIT;
/* Tune the model */
error = GRBtunemodel(model);
if (error) goto QUIT;
/* Get the number of tuning results */
error = GRBgetintattr(model, GRB_INT_ATTR_TUNE_RESULTCOUNT, &tuneresultcount);
if (error) goto QUIT;
if (tuneresultcount >= 2) {
/* Load the best tuned parameters into the model's environment
*
* Note, the first parameter setting is associated to the first solved
* setting and the second parameter setting to best tune result.
*/
error = GRBgettuneresult(model, 1);
if (error) goto QUIT;
/* Write tuned parameters to a file */
error = GRBwrite(model, "tune.prm");
if (error) goto QUIT;
/* Solve the model using the tuned parameters */
error = GRBoptimize(model);
if (error) goto QUIT;
}
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free model */
GRBfreemodel(model);
/* Free environment */
GRBfreeenv(env);
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example reads a model from a file and tunes it.
It then writes the best parameter settings to a file
and solves the model using these parameters. */
#include "gurobi_c++.h"
#include <cmath>
using namespace std;
int
main(int argc,
char *argv[])
{
if (argc < 2) {
cout << "Usage: tune_c++ filename" << endl;
return 1;
}
GRBEnv *env = 0;
try {
env = new GRBEnv();
// Read model from file
GRBModel model = GRBModel(*env, argv[1]);
// Set the TuneResults parameter to 2
//
// The first parameter setting is the result for the first solved
// setting. The second entry the parameter setting of the best
// parameter setting.
model.set(GRB_IntParam_TuneResults, 2);
// Tune the model
model.tune();
// Get the number of tuning results
int resultcount = model.get(GRB_IntAttr_TuneResultCount);
if (resultcount >= 2) {
// Load the tuned parameters into the model's environment
//
// Note, the first parameter setting is associated to the first solved
// setting and the second parameter setting to best tune result.
model.getTuneResult(1);
// Write tuned parameters to a file
model.write("tune.prm");
// Solve the model using the tuned parameters
model.optimize();
}
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch (...) {
cout << "Error during tuning" << endl;
}
delete env;
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example reads a model from a file and tunes it.
It then writes the best parameter settings to a file
and solves the model using these parameters. */
using System;
using Gurobi;
class tune_cs
{
static void Main(string[] args)
{
if (args.Length < 1) {
Console.Out.WriteLine("Usage: tune_cs filename");
return;
}
try {
GRBEnv env = new GRBEnv();
// Read model from file
GRBModel model = new GRBModel(env, args[0]);
// Set the TuneResults parameter to 2
//
// The first parameter setting is the result for the first solved
// setting. The second entry the parameter setting of the best
// parameter setting.
model.Parameters.TuneResults = 2;
// Tune the model
model.Tune();
// Get the number of tuning results
int resultcount = model.TuneResultCount;
if (resultcount >= 2) {
// Load the tuned parameters into the model's environment
//
// Note, the first parameter setting is associated to the first
// solved setting and the second parameter setting to best tune
// result.
model.GetTuneResult(1);
// Write the tuned parameters to a file
model.Write("tune.prm");
// Solve the model using the tuned parameters
model.Optimize();
}
// Dispose of model and environment
model.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
}
}
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example reads a model from a file and tunes it.
It then writes the best parameter settings to a file
and solves the model using these parameters. */
import com.gurobi.gurobi.*;
public class Tune {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java Tune filename");
System.exit(1);
}
try {
GRBEnv env = new GRBEnv();
// Read model from file
GRBModel model = new GRBModel(env, args[0]);
// Set the TuneResults parameter to 2
//
// The first parameter setting is the result for the first solved
// setting. The second entry the parameter setting of the best
// parameter setting.
model.set(GRB.IntParam.TuneResults, 2);
// Tune the model
model.tune();
// Get the number of tuning results
int resultcount = model.get(GRB.IntAttr.TuneResultCount);
if (resultcount >= 2) {
// Load the tuned parameters into the model's environment
//
// Note, the first parameter setting is associated to the first
// solved setting and the second parameter setting to best tune
// result.
model.getTuneResult(1);
// Write the tuned parameters to a file
model.write("tune.prm");
// Solve the model using the tuned parameters
model.optimize();
}
// Dispose of model and environment
model.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". "
+ e.getMessage());
}
}
}
#!/usr/bin/env python3.11
# Copyright 2024, Gurobi Optimization, LLC
# This example reads a model from a file and tunes it.
# It then writes the best parameter settings to a file
# and solves the model using these parameters.
import sys
import gurobipy as gp
if len(sys.argv) < 2:
print("Usage: tune.py filename")
sys.exit(0)
# Read the model
model = gp.read(sys.argv[1])
# Set the TuneResults parameter to 2
#
# The first parameter setting is the result for the first solved
# setting. The second entry the parameter setting of the best parameter
# setting.
model.Params.TuneResults = 2
# Tune the model
model.tune()
if model.TuneResultCount >= 2:
# Load the best tuned parameters into the model
#
# Note, the first parameter setting is associated to the first solved
# setting and the second parameter setting to best tune result.
model.getTuneResult(1)
# Write tuned parameters to a file
model.write("tune.prm")
# Solve the model using the tuned parameters
model.optimize()
' Copyright 2024, Gurobi Optimization, LLC */
'
' This example reads a model from a file and tunes it.
' It then writes the best parameter settings to a file
' and solves the model using these parameters.
Imports System
Imports Gurobi
Class tune_vb
Shared Sub Main(ByVal args As String())
If args.Length < 1 Then
Console.Out.WriteLine("Usage: tune_vb filename")
Return
End If
Try
Dim env As New GRBEnv()
' Read model from file
Dim model As New GRBModel(env, args(0))
' Set the TuneResults parameter to 2
'
' The first parameter setting is the result for the first solved
' setting. The second entry the parameter setting of the best
' parameter setting.
model.Parameters.TuneResults = 2
' Tune the model
model.Tune()
' Get the number of tuning results
Dim resultcount As Integer = model.TuneResultCount
If resultcount >= 2 Then
' Load the tuned parameters into the model's environment
'
' Note, the first parameter setting is associated to the first
' solved setting and the second parameter setting to best tune
' result.
model.GetTuneResult(1)
' Write the tuned parameters to a file
model.Write("tune.prm")
' Solve the model using the tuned parameters
model.Optimize()
End If
' Dispose of model and environment
model.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message)
End Try
End Sub
End Class