GUROBI之常用Attributes总结

总结了一下目前模型、变量、目标函数等比较常用的属性,后续遇到了其他了会陆续补充

完整的详见官网:Attributes - Gurobi Optimization

Model attributes:

Attribute nameDescription
NumVarsNumber of variables
NumConstrsNumber of linear constraints
ModelNameModel name
ModelSenseModel sense (minimization or maximization)
ObjValObjective value for current solution
StatusCurrent 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 nameDescription
LBLower bound
UBUpper bound
ObjLinear objective coefficient
VTypeVariable type (continuous, binary, integer, etc.)
VarNameVariable name
XValue in the current solution
XnValue 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 nameDescription
SenseConstraint sense ('<', '>', or '=')
RHSRight-hand side value
ConstrNameConstraint name
PiDual value (also known as the shadow price)
SlackSlack 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 nameDescription
ObjNObjectives of multi-objectives
ObjNConConstant terms of multi-objectives
ObjNPriorityPriorities of multi-objectives
ObjNWeightWeights of multi-objectives
ObjNValObjective value of multi-objectives solutions
ObjNNameNames of multi-objectives
NumObjNumber of multi-objectives

### Gurobi 常用命令与函数库 API 文档 Gurobi 是一种功能强大的优化求解器,支持线性规划(LP)、混合整数规划(MIP)以及其他复杂的优化模型。其提供了多种编程语言的支持,包括 Python 和 C++ 等。 #### 1. **Python API 的常用指令** 对于 Python 用户而言,`gurobipy` 库是主要的交互工具。以下是常用的函数及其说明: - `Model()`:创建一个新的优化模型对象[^3]。 - `addVar()` 或 `addVars()`:向模型中添加变量。 - `setObjective()`:设置目标函数,可以指定最大化或最小化方向。 - `addConstr()` 或 `addLConstr()`:添加约束条件到模型中。 - `optimize()`:调用求解器来解决当前定义的优化问题。 - `getAttr()`:获取模型属性值,例如变量取值、目标值等。 示例代码如下: ```python from gurobipy import Model, GRB # 创建模型 model = Model() # 添加变量 x = model.addVar(vtype=GRB.CONTINUOUS, name="x") # 设置目标函数 model.setObjective(x, GRB.MAXIMIZE) # 添加约束 model.addConstr(x <= 10, "c0") # 求解 model.optimize() ``` #### 2. **C++ API 的常用指令** 在 C++ 中,Gurobi 提供了一套完整的类和方法用于构建和求解优化模型。以下是一些核心组件: - `GRBEnv`:表示 Gurobi 环境的对象,所有模型都必须在一个环境中运行[^2]。 - `GRBModel`:代表一个具体的优化模型。 - `GRBVar`:表示单个决策变量。 - ` setObjective() `:设定目标函数。 - ` addConstr() `:增加约束条件。 - ` optimize() `:启动求解过程。 一段简单的 C++ 示例代码可能如下所示: ```cpp #include <iostream> #include <gurobi_c++.h> using namespace std; int main() { try { // 创建环境 GRBEnv env = GRBEnv(); // 创建模型 GRBModel model = GRBModel(env); // 定义变量 GRBVar x = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "x"); // 设定目标 model.setObjective(x, GRB_MAXIMIZE); // 加入约束 model.addConstr(x <= 10, "c0"); // 开始计算 model.optimize(); cout << "Optimal objective value: " << model.get(GRB_DoubleAttr_ObjVal) << endl; } catch (GRBException e) { cout << "Error code = " << e.getErrorCode() << endl; cout << e.getMessage() << endl; } } ``` #### 3. **API 参考文档链接** 为了更深入地了解各个函数的具体参数和返回值含义,建议查阅官方文档: - Python API 文档:https://www.gurobi.com/documentation/current/refman/py_python_api_overview.html - C++ API 文档:https://www.gurobi.com/documentation/current/refman/cpp_cpp_api_overview.html 此外,在第三方资源中也有丰富的案例分析和技术文章可供参考,比如开源项目中的实例[^1]或者技术博客上的讲解[^4]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值