GRBConstr#
- GRBConstr#
Gurobi 约束对象。约束总是与特定模型相关联。您通过向模型添加约束(使用
GRBModel.addConstr
)来创建约束对象,而不是通过使用GRBConstr
构造函数。约束对象上的方法用于获取和设置约束属性。例如,可以通过调用
get
(GRB.DoubleAttr.RHS
)来查询约束的右侧值。然而,通常更高效的做法是同时查询一组约束的属性。这是通过在GRBModel
对象上使用属性查询方法(GRBModel.get
)来完成的。- char get(GRB.CharAttr attr)#
查询一个字符值属性的值。
- Arguments:
attr – 正在查询的属性。
- Return value:
请求属性的当前值。
- Example:
// Get constraint sense char sense = constr.get(GRB.CharAttr.Sense);
- double get(GRB.DoubleAttr attr)#
查询一个双值属性的值。
- Arguments:
attr – 正在查询的属性。
- Return value:
请求属性的当前值。
- Example:
// Get RHS of the constraint double rhs = constr.get(GRB.DoubleAttr.RHS);
- int get(GRB.IntAttr attr)#
查询一个整数值属性的值。
- Arguments:
attr – 正在查询的属性。
- Return value:
请求属性的当前值。
- Example:
// Get CBasis value int cbasis = constr.get(GRB.IntAttr.CBasis);
- String get(GRB.StringAttr attr)#
查询字符串值属性的值。
- Arguments:
attr – 正在查询的属性。
- Return value:
请求属性的当前值。
- Example:
// Get constraint name String name = constr.get(GRB.StringAttr.ConstrName);
- int index()#
此方法返回约束在底层约束矩阵中的当前索引或顺序。
请注意,约束的索引可能会在后续模型修改后发生变化。
- Return value:
-2: 已移除, -1: 不在模型中, 否则: 模型中约束的索引
- Example:
// Get the index of the constraint in the model int index = constr.index();
- boolean sameAs(GRBConstr otherConstr)#
检查两个约束对象是否引用相同的约束。
- Arguments:
otherConstr – 另一个约束。
- Return value:
布尔结果指示两个约束对象是否引用相同的模型约束。
- Example:
// Compare the constraint with another constraint boolean isSame = constr.sameAs(otherConstr);
- void set(GRB.CharAttr attr, char newval)#
设置一个字符类型属性的值。
- Arguments:
attr – 正在修改的属性。
newval – 属性的期望新值。
- Example:
// Set constraint sense constr.set(GRB.CharAttr.Sense, '>');
- void set(GRB.DoubleAttr attr, double newval)#
设置一个双精度值属性的值。
- Arguments:
attr – 正在修改的属性。
newval – 属性的期望新值。
- Example:
// Set RHS of the constraint constr.set(GRB.DoubleAttr.RHS, 2.0);
- void set(GRB.IntAttr attr, int newval)#
设置一个整数值属性的值。
- Arguments:
attr – 正在修改的属性。
newval – 属性的期望新值。
- Example:
// Set CBasis value constr.set(GRB.IntAttr.CBasis, 0);
- void set(GRB.StringAttr attr, String newval)#
设置字符串类型属性的值。
- Arguments:
attr – 正在修改的属性。
newval – 属性的期望新值。
- Example:
// Set constraint name constr.set(GRB.StringAttr.ConstrName, "newName");