Concepts
Features
Reference
Gurobi constraint object. Constraints are always associated with aparticular model. You create a constraint object by adding a constraintto a model (usingGRBModel.AddConstr), rather than byusing aGRBConstr constructor.
// Create variablesGRBVarx=model.AddVar(0.0,GRB.INFINITY,0.0,GRB.CONTINUOUS,"x");GRBVary=model.AddVar(0.0,GRB.INFINITY,0.0,GRB.CONTINUOUS,"y");// Add linear constraint x + y = 0 with name c1GRBConstrconstr=model.AddConstr(x+y==0,"c1");
' Create variablesDimxAsGRBVar=model.AddVar(0.0,GRB.INFINITY,0.0,GRB.CONTINUOUS,"x")DimyAsGRBVar=model.AddVar(0.0,GRB.INFINITY,0.0,GRB.CONTINUOUS,"y")' Add linear constraint x + y = 0 with name c1DimconstrAsGRBConstr=model.AddConstr(x+y==0,"c1")
The methods on constraint objects are used to get and set constraintattributes. For example, constraint right-hand sides can be queried bycallingGet (GRB.DoubleAttr.RHS).It can also be queried more directly usingconstr.RHS whereconstris aGRBConstr object.Note, however, that it is generally more efficient to query attributesfor a set of constraints at once. This is done using the attribute querymethod on theGRBModel object (GRBModel.Get).
The full list of attributescan be found in theAttributes section of thisdocument. Examples of how to query and set attributes can also be foundinthis section.
Query the value of an attribute.
attr – The attribute being queried.
The current value of the requested attribute.
// Get constraint sensecharsense=constr.Get(GRB.CharAttr.Sense);// Get RHSdoublerhs=constr.Get(GRB.DoubleAttr.RHS);// Get CBasis valueintcbasis=constr.Get(GRB.IntAttr.CBasis);// Get constraint namestringname=constr.Get(GRB.StringAttr.ConstrName);
' Get constraint senseDimsenseAsChar=constr.Get(GRB.CharAttr.Sense)' Get RHSDimrhsAsDouble=constr.Get(GRB.DoubleAttr.RHS)' Get CBasis valueDimcbasisAsInteger=constr.Get(GRB.IntAttr.CBasis)' Get constraint nameDimnameAsString=constr.Get(GRB.StringAttr.ConstrName)
This property returns the current index, or order, of the constraint inthe underlying constraint matrix.
Note that the index of a constraint may change after subsequent modelmodifications.
-2: removed, -1: not in model, otherwise: index of theconstraint in the model
intindex=constr.Index;
DimindexAsInteger=constr.Index
Check whether two constraint objects refer to the same constraint.
constr2 – The other constraint.
Boolean result indicates whether the two constraint objectsrefer to the same model constraint.
// Compare to a second constraintboolisSame=constr.SameAs(constr2);
' Compare to a second constraintDimisSameAsBoolean=constr.SameAs(constr2)
Set the value of an attribute.
attr – The attribute being modified.
newvalue – The desired new value of the attribute.
// Set constraint senseconstr.Set(GRB.CharAttr.Sense,'>');// Set RHSconstr.Set(GRB.DoubleAttr.RHS,2.0);// Set CBasis valueconstr.Set(GRB.IntAttr.CBasis,0);// Set constraint nameconstr.Set(GRB.StringAttr.ConstrName,"newName");
' Set constraint senseconstr.Set(GRB.CharAttr.Sense,">")' Set RHSconstr.Set(GRB.DoubleAttr.RHS,2.0)' Set CBasis valueconstr.Set(GRB.IntAttr.CBasis,0)' Set constraint nameconstr.Set(GRB.StringAttr.ConstrName,"newName")
Help and Feedback