Article directory
GRBModel
Gurobi model object. Commonly used methods include addVar (adding a new decision variable to the model), addConstr (adding a new constraint to the model), optimize (optimizing the current model), and get (retrieving the value of an attribute).
GRBModel GRBModel(const GRBEnv& env)
Model constructor.
parameter | Detailed explanation |
---|---|
env | New model environment. |
return value | New model object. The model initially contains no variables or constraints. |
GRBModel GRBModel(const GRBEnv& env,
const string& filename)
Read the model from a file. Note that the file's type is encoded in the filename suffix. Valid suffixes are .mps, .rew, .lp, .rlp, .dua, .dlp, .ilp, or .opb. These files can be compressed and therefore accept the appended .zip, .gz, .bz2 or .7z suffix.
parameter | Detailed explanation |
---|---|
env | New model environment. |
modelname | The name of the file containing the model. |
return value | New model object. |
GRBModel GRBModel(const GRBModel& model)
Create a copy of an existing model. Note that due to the lazy update approach in Gurobi, you must call update before copying.
parameter | Detailed explanation |
---|---|
model: The model to be copied. | |
return value | New model object. The model is a clone of the input model. |
GRBModel::addConstr()
Add a single linear constraint to the model. Multiple signatures are available.
-
GRBConstr addConstr(const GRBLinExpr& lhsExpr, char sense, const GRBLinExpr& rhsExpr, string name="" )
Add a single linear constraint to the model.
parameter Detailed explanation lhsExpr The left-hand expression of the new linear constraint. sense Be aware of new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL). rhsExpr The right-hand side expression of the new linear constraint. names (optional) The name of the new constraint. return value New constraint object. -
GRBConstr addConstr(const GRBLinExpr& lhsExpr, char sense, GRBVar rhsVar, string name="" )
Add a single linear constraint to the model.
parameter Detailed explanation lhsExpr The left-hand expression of the new linear constraint. sense Be aware of new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL). rhsWhere The right-hand side variable of the new linear constraint. name (optional) The name of the new constraint. return value New constraint object. -
GRBConstr addConstr (const GRBLinExpr& lhsExpr, char sense, double rhsVal, string name="")
Add a single linear constraint to the model.
parameter Detailed explanation lhsExpr The left-hand expression of the new linear constraint. sense Be aware of new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL). rhsVal The right-hand side value of the new linear constraint. name (optional) The name of the new constraint. return value New constraint object. -
GRBConstr addConstr(GRBVar lhsVar, char sense, GRBVar rhsVar, string name="" )
Add a single linear constraint to the model.
parameter Detailed explanation lhsWhere The left-hand variable of the new linear constraint. sense Be aware of new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL). rhsWhere The right-hand side variable of the new linear constraint. name (optional) The name of the new constraint. return value New constraint object. -
GRBConstr addConstr(GRBVar lhsVar, char sense, double rhsVal, string name="")
Add a single linear constraint to the model.
parameter Detailed explanation lhsWhere The left-hand variable of the new linear constraint. sense Be aware of new linear constraints (GRB_LESS_EQUAL, GRB_EQUAL, or GRB_GREATER_EQUAL). rhsVal The right-hand side value of the new linear constraint. name (optional) The name of the new constraint. return value New constraint object. -
GRBConstr addConstr(GRBTempConstr& tc, string name="")
Add a single linear constraint to the model.
parameter Detailed explanation tc Temporary constraint objects, created using overloaded comparison operators. See GRBTempConstr for details. name (optional) The name of the new constraint. return value New constraint object.
GRBModel::addVar()
Add a single decision variable to the model.
Template 1
GRBVar addVar (double lb,
double ub,
double obj,
char type,
string name="" )
Add a variable; non-zero entries will be added later.
parameter | Detailed explanation |
---|---|
lb | The lower bound of the new variable. |
ub | The upper limit of the new variable. |
obj | The objective coefficient of the new variable. |
type | 新变量的变量类型(GRB_CONTINUOUS、GRB_BINARY、GRB_INTEGER、GRB_SEMICONT 或 GRB_SEMIINT)。 |
names(可选) | 新变量的名称。 |
返回值 | 新的变量对象。 |
模板2
GRBVar addVar (double lb,
double ub,
double obj,
char type,
int numnz,
const GRBConstr* constrs,
const double* coeffs,
string name="" )
添加一个变量,以及相关的非零系数。
参数 | 详解 |
---|---|
lb | 新变量的下限。 |
ub | 新变量的上限。 |
obj | 新变量的客观系数。 |
type | 新变量的变量类型(GRB_CONTINUOUS、GRB_BINARY、GRB_INTEGER、GRB_SEMICONT 或 GRB_SEMIINT)。 |
numnz | 这个新变量参与的约束数量。 |
constrs | 变量参与的约束数组。 |
coeffs | 变量参与的每个约束的系数数组。 |
names(可选) | 新变量的名称。 |
返回值 | 新的变量对象。 |
模板3
GRBVar addVar (double lb,
double ub,
double obj,
char type,
const GRBColumn& col,
string name="" )
添加一个变量,以及相关的非零系数。
参数 | 详解 |
---|---|
lb | 新变量的下限。 |
ub | 新变量的上限。 |
obj | 新变量的客观系数。 |
type | 新变量的变量类型(GRB_CONTINUOUS、GRB_BINARY、GRB_INTEGER、GRB_SEMICONT 或 GRB_SEMIINT)。 |
col | GRBColumn 对象,用于指定新变量所属的一组约束。 |
names(可选) | 新变量的名称。 |
返回值 | 新的变量对象。 |
GRBModel::addVars()
向模型添加新的决策变量。
模板1
GRBVar* addVars(int count,
char type=GRB_CONTINUOUS)
向模型添加count个新的决策变量。 所有关联的属性都采用它们的默认值,但变量类型除外,它被指定为参数。
参数 | 详解 |
---|---|
count | 需要添加变量的数量 |
type (可选) | 新加变量的类型,可选类型: GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER, GRB_SEMICONT, or GRB_SEMIINT |
返回值 | 新变量对象的数组。 请注意,结果是堆分配的,并且必须由用户返回到堆中。 |
模板2
GRBVar* addVars(const double* lb,
const double* ub,
const double* obj,
const char* type,
const string* names,
int count )
向模型添加计数新决策变量。 此函数名允许使用数组来保存各种变量属性(例如下限、上限等)。
参数 | 详解 |
---|---|
lb | 新变量的下限。 可以为 NULL,在这种情况下,变量的下限为 0.0。 |
ub | 新变量的上限。 可以为 NULL,在这种情况下,变量获得无限的上限。 |
obj | 新变量的客观系数(Objective coefficients)。 可以为 NULL,在这种情况下,变量的客观系数为 0.0。 |
type | 新变量的变量类型(GRB_CONTINUOUS、GRB_BINARY、GRB_INTEGER、GRB_SEMICONT 或 GRB_SEMIINT)。 可以为 NULL,在这种情况下假定变量是连续的。 |
names | 新变量的名称。 可以为 NULL,在这种情况下,所有变量都被赋予默认名称。 |
count | 要添加的变量数。 |
返回值 | 新变量对象的数组。 请注意,结果是堆分配的,并且必须由用户返回到堆中。 |
模板3
GRBVar* addVars(const double* lb,
const double* ub,
const double* obj,
const char* type,
const string* names,
const GRBColumn* cols,
int count )
向模型添加新的决策变量。 此函数允许使用 GRBColumn 对象数组指定每个新变量所属的一组约束。
参数 | 详解 |
---|---|
lb | 新变量的下限。 可以为 NULL,在这种情况下,变量的下限为 0.0。 |
ub | 新变量的上限。 可以为 NULL,在这种情况下,变量获得无限的上限。 |
obj | 新变量的客观系数。 可以为 NULL,在这种情况下,变量的客观系数为 0.0。 |
type | 新变量的变量类型(GRB_CONTINUOUS、GRB_BINARY、GRB_INTEGER、GRB_SEMICONT 或 GRB_SEMIINT)。 可以为 NULL,在这种情况下假定变量是连续的。 |
names | 新变量的名称。 可以为 NULL,在这种情况下,所有变量都被赋予默认名称。 |
cols | GRBColumn 对象,用于指定每个新列所属的一组约束。 |
count | 要添加的变量数。 |
返回值 | 新变量对象的数组。 请注意,结果是堆分配的,并且必须由用户返回到堆中。 |