gurobi

Gurobi is a powerful mathematical optimization solver primarily used for solving optimization problems such as linear programming, integer programming, mixed-integer linear programming, and quadratic programming. It is widely applied in operations research, engineering, finance, and business decision-making to help companies and researchers find optimal or feasible solutions under given constraints.

Installation (with installPlugin)

Required server version: DolphinDB 200.12 or higher.

Supported OS: windows x64 and Linux.

Installation Steps:

(1) Use listRemotePlugins to check plugin information in the plugin repository.

Note: For plugins not included in the provided list, you can install through precompiled binaries or compile from source. These files can be accessed from our GitHub repository by switching to the appropriate version branch.

login("admin", "123456")
listRemotePlugins(, "http://plugins.dolphindb.com/plugins/")

(2) Invoke installPlugin for plugin installation.

installPlugin("gurobi")

(3) Use loadPlugin to load the plugin before using the plugin methods.

loadPlugin("gurobi")

Method References

model

Syntax

model([params])

Details

The method creates and returns the created Gurobi model object.

Parameters

  • params (optional): A dictionary with keys and values of type STRING. If empty, a default model object is created. For more information, refer to Gurobi’s documentation on Parameters.

Examples

gurobi::model(dict(["NonConvex"], ["2"]))

addVars

Syntax

addVars(model, lb, ub, obj, type, varName)

Details

The method adds new decision variables to the model and returns an array.

Parameters

  • model: A Gurobi model object created through the model.
  • lb: A DOUBLE array, where each element represents the lower bounds for variables.
  • ub: A DOUBLE array, where each element represents the upper bounds for variables.
  • obj: A DOUBLE array, where each element represents the coefficient of the corresponding variable in the objective function. This parameter can be NULL and the default value of all coefficients is 0.0.
  • type: A CHAR array, where each character represents the type of the corresponding variable. This parameter can be NULL and the default value of types is 'C':
    • 'C': continuous variable
    • 'B': binary variable
    • 'I': integer variable
    • 'S': semi-continuous variable
    • 'N': semi-integer variable
  • varName: A STRING array indicating variable names.

linExpr

Syntax

linExpr(model, coefficient, varName)

Details

The method creates a linear expression object and returns it.

Parameters

  • model: A Gurobi model object created through the model.
  • coefficient: A DOUBLE array, where each element represents the coefficient of a variable.
  • varName: A STRING array indicating variable names.

quadExpr

Syntax

quadExpr(model, quadMatrix, varNames, [linExpr])

Details

The method creates a quadratic expression object and returns it.

Parameters

  • model: A Gurobi model object created through the model.

  • quadMatrix: A DOUBLE matrix. The quadratic expression is obtained by (quadMatrix[r][c]) * (varName[r]) * (varName[c]).

    Note: DolphinDB stores matrices by columns and theoretically they should be transposed. However, because the effects of quadMatrix[r][c] and quadMatrix[c][r] are completely equivalent (since c * obj_r * obj_c = c * obj_c * obj_r), there is no need to transpose in practical use. It can be used directly as a row-priority matrix.

  • varName: A STRING array indicating variable names

  • linExpr (optional): A linear expression object created through the linExpr method, used to initialize the quadratic expression object.

addConstr

Syntax

addConstr(model, lhsExpr, sense, rhsVal)

Details

The method adds a single linear or quadratic constraints to the model.

Parameters

  • model: A Gurobi model object created through the model.
  • lhsExpr: An expression object created through the linExpr or quadExpr.
  • sense: A CHAR type indicating the constraint type:
    • '<': GRB_LESS_EQUAL
    • '>': GRB_GREATER_EQUAL
    • '=': GRB_EQUAL
  • rhsVal: A DOUBLE scalar indicating the constraint value of the constraint condition.

setObjective

Syntax

setObjective(model, expr, sense)

Details

The method defines the optimization objective for the model.

Parameters

  • model: A Gurobi model object created through the model.
  • lhsExpr: An expression object created through the linExpr or quadExpr.
  • sense: An INT type indicating the optimization direction:
    • -1: GRB_MAXIMIZE
    • 1: GRB_MINIMIZE

optimize

Syntax

optimize(model)

Details

The method finds the optimal solution based on the defined objective function and constraints, and returns the model's solution status as an INT, with meanings as specified in Optimization Status Codes.

Parameters

  • model: A Gurobi model object created through the model.

getResult

Syntax

getResult(model)

Details

The method retrieves the values of parameters after optimization and returns a dictionary containing the names and corresponding values of all variables in the optimized model.

Parameters

  • model: A Gurobi model object created through the model.

getObjective

Syntax

getObjective(model)

Details

The method retrieves optimized objective value and returns the value of the objective function after optimization, of type DOUBLE.

Parameters

  • model: A Gurobi model object created through the model.

Usage Examples

/// Initialize the model
model = gurobi::model()

/// Add variables
lb = 0 0 0 0 0 0 0 0 0 0
ub = 1 1 1 1 1 1 1 1 1 1
stocks = ["A001", "A002", "A003", "A004", "A005", "A006", "A007", "A008", "A009", "A010"]
gurobi::addVars(model, lb, ub, , , stocks)

/// Add linear constraints
A = [1 1 1 0 0 0 0 0 0 0,
     0 0 0 1 1 1 0 0 0 0,
     0 0 0 0 0 0 1 1 1 1,
     -1 -1 -1 0 0 0 0 0 0 0,
     0 0 0 -1 -1 -1 0 0 0 0,
     0 0 0 0 0 0 -1 -1 -1 -1]
rhs = 0.38 0.48 0.38 -0.22 -0.32 -0.22

for (i in 0:6) {
	lhsExpr = gurobi::linExpr(model, A[i], stocks)
	gurobi::addConstr(model, lhsExpr, '<', rhs[i])
}

lhsExpr = gurobi::linExpr(model, 1 1 1 1 1 1 1 1 1 1, stocks)
gurobi::addConstr(model, lhsExpr, '=', 1)

/// Set the optimization objective to a quadratic expression
coefficients = 0.1 0.02 0.01 0.05 0.17 0.01 0.07 0.08 0.09 0.10
linExpr = gurobi::linExpr(model, coefficients, stocks)

H = [-1 0 0 0 0 0 0 0 0 0,
     0 -1 0 0 0 0 0 0 0 0,
     0 0 -1 0 0 0 0 0 0 0,
     0 0 0 -1 0 0 0 0 0 0,
     0 0 0 0 -1 0 0 0 0 0,
     0 0 0 0 0 -1 0 0 0 0,
     0 0 0 0 0 0 -1 0 0 0,
     0 0 0 0 0 0 0 -1 0 0,
     0 0 0 0 0 0 0 0 -1 0,
     0 0 0 0 0 0 0 0 0 -1]
quadExpr = gurobi::quadExpr(model, matrix(H), stocks, linExpr)

gurobi::setObjective(model, quadExpr, -1)

/// Optimize
status = gurobi::optimize(model)

/// Get the optimization results
result = gurobi::getResult(model)