Try our new documentation site (beta).


/* 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");

    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");

      // 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);
      for (int i = 1; i <= 3; ++i) {
        m.Parameters.MIPFocus = i;
        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
      bestModel.Parameters.TimeLimit = GRB.INFINITY;
      Console.WriteLine("Solved with MIPFocus: " +

      // Clean up bestModel and environment
    } catch (GRBException e) {
      Console.WriteLine("Error code: " + e.ErrorCode + ". " +

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.