GRBConstr#
- GRBConstr#
Gurobi 约束对象。约束总是与特定模型相关联。您通过向模型添加约束(使用
GRBModel.AddConstr
)来创建约束对象,而不是通过使用GRBConstr
构造函数。约束对象上的方法用于获取和设置约束属性。例如,可以通过调用
Get
(GRB.DoubleAttr.RHS
)来查询约束的右侧值。然而,通常更高效的做法是同时查询一组约束的属性。这是通过在GRBModel
对象上使用属性查询方法(GRBModel.Get
)来完成的。- Example:
// Create variables GRBVar x = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x"); GRBVar y = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y"); // Add linear constraint x + y = 0 with name c1 GRBConstr constr = model.AddConstr(x + y == 0, "c1");
' Create variables Dim x As GRBVar = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x") Dim y As GRBVar = model.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y") ' Add linear constraint x + y = 0 with name c1 Dim constr As GRBConstr = model.AddConstr(x + y == 0, "c1")
- char Get(GRB.CharAttr attr)#
查询一个字符值属性的值。
- Parameters:
attr – 被查询的属性。
- Returns:
请求属性的当前值。
- Example:
// Get constraint sense char sense = constr.Get(GRB.CharAttr.Sense);
' Get constraint sense Dim sense As Char = constr.Get(GRB.CharAttr.Sense)
- double Get(GRB.DoubleAttr attr)#
查询一个双值属性的值。
- Parameters:
attr – 被查询的属性。
- Returns:
请求属性的当前值。
- Example:
// Get RHS double rhs = constr.Get(GRB.DoubleAttr.RHS);
' Get RHS Dim rhs As Double = constr.Get(GRB.DoubleAttr.RHS)
- int Get(GRB.IntAttr attr)#
查询一个整数值属性的值。
- Parameters:
attr – 被查询的属性。
- Returns:
请求属性的当前值。
- Example:
// Get CBasis value int cbasis = constr.Get(GRB.IntAttr.CBasis);
' Get CBasis value Dim cbasis As Integer = constr.Get(GRB.IntAttr.CBasis)
- string Get(GRB.StringAttr attr)#
查询字符串值属性的值。
- Parameters:
attr – 被查询的属性。
- Returns:
请求属性的当前值。
- Example:
// Get constraint name string name = constr.Get(GRB.StringAttr.ConstrName);
' Get constraint name Dim name As String = constr.Get(GRB.StringAttr.ConstrName)
- int Index#
此属性返回约束在基础约束矩阵中的当前索引或顺序。
请注意,约束的索引可能会在后续模型修改后发生变化。
- Returns:
-2: 已移除, -1: 不在模型中, 否则: 模型中约束的索引
- Example:
int index = constr.Index;
Dim index As Integer = constr.Index
- bool SameAs(GRBConstr constr2)#
检查两个约束对象是否引用相同的约束。
- Parameters:
constr2 – 另一个约束。
- Returns:
布尔结果指示两个约束对象是否引用相同的模型约束。
- Example:
// Compare to a second constraint bool isSame = constr.SameAs(constr2);
' Compare to a second constraint Dim isSame As Boolean = constr.SameAs(constr2)
- void Set(GRB.CharAttr attr, char newvalue)#
设置一个字符类型属性的值。
- Parameters:
attr – 正在修改的属性。
newvalue – 属性的期望新值。
- Example:
// Set constraint sense constr.Set(GRB.CharAttr.Sense, '>');
' Set constraint sense constr.Set(GRB.CharAttr.Sense, ">")
- void Set(GRB.DoubleAttr attr, double newvalue)#
设置一个双精度值属性的值。
- Parameters:
attr – 正在修改的属性。
newvalue – 属性的期望新值。
- Example:
// Set RHS constr.Set(GRB.DoubleAttr.RHS, 2.0);
' Set RHS constr.Set(GRB.DoubleAttr.RHS, 2.0)
- void Set(GRB.IntAttr attr, int newvalue)#
设置一个整数值属性的值。
- Parameters:
attr – 正在修改的属性。
newvalue – 属性的期望新值。
- Example:
// Set CBasis value constr.Set(GRB.IntAttr.CBasis, 0);
' Set CBasis value constr.Set(GRB.IntAttr.CBasis, 0)
- void Set(GRB.StringAttr attr, string newvalue)#
设置字符串类型属性的值。
- Parameters:
attr – 正在修改的属性。
newvalue – 属性的期望新值。
- Example:
// Set constraint name constr.Set(GRB.StringAttr.ConstrName, "newName");
' Set constraint name constr.Set(GRB.StringAttr.ConstrName, "newName")