总结了一下目前模型、变量、目标函数等比较常用的属性,后续遇到了其他了会陆续补充
完整的详见官网:Attributes - Gurobi Optimization
Model attributes:
Attribute name | Description |
NumVars | Number of variables |
NumConstrs | Number of linear constraints |
ModelName | Model name |
ModelSense | Model sense (minimization or maximization) |
ObjVal | Objective value for current solution |
Status | Current optimization status |
Model.getAttr()
getAttr ( attrname, objs=None )
Arguments:
attrname: Name of the attribute.
objs (optional): List or dictionary containing either constraints or variables
Example usage:
print(model.numintvars) print(model.getAttr("numIntVars")) print(model.getAttr(GRB.Attr.NumIntVars)) print(model.getAttr("X", model.getVars())) print(model.getAttr("Pi", model.getConstrs()))
Variable attributes:
Attribute name | Description |
LB | Lower bound |
UB | Upper bound |
Obj | Linear objective coefficient |
VType | Variable type (continuous, binary, integer, etc.) |
VarName | Variable name |
X | Value in the current solution |
Xn | Value in a sub-optimal MIP solution |
Var.getAttr()
getAttr ( attrname )
Arguments:
attrname: The attribute being queried.
Return value:
The current value of the requested attribute.
Example usage:
print(var.getAttr(GRB.Attr.X)) print(var.getAttr("x"))
Linear constraint attributes:(其他类型约束类似)
Attribute name | Description |
---|---|
Sense | Constraint sense ('<', '>', or '=') |
RHS | Right-hand side value |
ConstrName | Constraint name |
Pi | Dual value (also known as the shadow price) |
Slack | Slack in the current solution (松弛约束) |
Constr.getAttr()
getAttr ( attrname )
Arguments:
attrname: The attribute being queried.
Return value:
The current value of the requested attribute.
Example usage:
print(constr.getAttr(GRB.Attr.Slack)) print(constr.getAttr("slack"))
Multi-objective attributes:
Attribute name | Description |
---|---|
ObjN | Objectives of multi-objectives |
ObjNCon | Constant terms of multi-objectives |
ObjNPriority | Priorities of multi-objectives |
ObjNWeight | Weights of multi-objectives |
ObjNVal | Objective value of multi-objectives solutions |
ObjNName | Names of multi-objectives |
NumObj | Number of multi-objectives |