Python - Gurobi

申请 Gurobi 学术许可和安装 Gurobi

申请 Gurobi 学术许可和安装可以参考Gurobi最新安装与学术许可申请教程(2025) - 知乎

配置 Python 环境

首先打开命令行终端,用 Conda 单独创建一个环境:

1
conda create --name name_env

激活环境:

1
conda activate name_env

Conda 包管理安装 GurobiPython 库中[1]

1
conda install -c gurobi gurobi

安装完之后,可以用下面的例子测试 Gurobi 是否能够正常使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import gurobipy as gp
from gurobipy import GRB

def test_gurobi():
try:
# 创建一个新的模型
model = gp.Model("test_model")

# 添加变量
x = model.addVar(name="x", vtype=GRB.CONTINUOUS, lb=0)
y = model.addVar(name="y", vtype=GRB.CONTINUOUS, lb=0)

# 更新模型以整合新变量
model.update()

# 设置目标函数:最大化 3x + 2y
model.setObjective(3 * x + 2 * y, GRB.MAXIMIZE)

# 添加约束条件
# x + y <= 4
model.addConstr(x + y <= 4, "c0")

# x <= 2
model.addConstr(x <= 2, "c1")

# y <= 3
model.addConstr(y <= 3, "c2")

# 优化模型
model.optimize()

# 检查优化状态
if model.status == GRB.OPTIMAL:
print(f"最优目标值: {model.ObjVal}")
print(f"x = {x.x}")
print(f"y = {y.x}")
else:
print("没有找到最优解。")

except gp.GurobiError as e:
print(f"Gurobi错误: {e}")

except Exception as e:
print(f"其他错误: {e}")

if __name__ == "__main__":
test_gurobi()

如果能够正常使用会输出以下结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Optimize a model with 3 rows, 2 columns and 4 nonzeros
Model fingerprint: 0x5c2931af
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [2e+00, 3e+00]
Bounds range [0e+00, 0e+00]
RHS range [2e+00, 4e+00]
Presolve removed 2 rows and 0 columns
Presolve time: 0.00s
Presolved: 1 rows, 2 columns, 2 nonzeros

Iteration Objective Primal Inf. Dual Inf. Time
0 1.2000000e+01 2.000000e+00 0.000000e+00 0s
1 1.0000000e+01 0.000000e+00 0.000000e+00 0s

Solved in 1 iterations and 0.00 seconds (0.00 work units)
Optimal objective 1.000000000e+01
最优目标值: 10.0
x = 2.0
y = 2.0

References


Python - Gurobi
https://chen-huaneng.github.io/2025/03/23/2025-03-23-2025-03-23-gurobi/
作者
Abel
发布于
2025年3月23日
更新于
2025年3月23日
许可协议