注意:在anaconda虚拟环境中安装gurobi库是不需要在本地下载gurobi这个软件的,只需要conda install gurobi
即可,或者指定版本的安装conda install -c gurobi gurobi=11.0.3
。
step0:安装ananconda
step1:获得学术许可,点击网址进入https://support.gurobi.com/hc/en-us/articles/13210193318033-What-is-an-Academic-WLS-license,点击红框链接,用学校邮箱注册账号。
注册完成后,选择Named-User Academic,适用于个人使用
然后就得到了grbgetkey,记住这个key,后面会用到,至此学术许可申请成功。step2:激活某一虚拟环境step3:执行conda install gurobi
或conda install -c gurobi gurobi=11.0.3
step4:执行grbgetkey xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
,可以看到在指定文件夹下生成了gurobi.lic文件。
step5:将该文件复制到C:\gurobi
下替换。
step6:打开ide,选则安装gurobi的虚拟环境,执行代码:
import gurobipy as gp
from gurobipy import GRB# 创建一个简单的模型
model = gp.Model()# 添加一个变量
x = model.addVar(name="x")# 设置目标函数
model.setObjective(x, GRB.MAXIMIZE)# 优化模型
model.optimize()print("Optimization complete")
看到结果如下:
Academic license - for non-commercial use only - expires 2025-11-13
Gurobi Optimizer version 12.0.0 build v12.0.0rc1 (win64 - Windows 11+.0 (27729.2))CPU model: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz, instruction set [SSE2|AVX|AVX2|AVX512]
Thread count: 4 physical cores, 8 logical processors, using up to 8 threadsOptimize a model with 0 rows, 1 columns and 0 nonzeros
Model fingerprint: 0xb569a9c0
Coefficient statistics:Matrix range [0e+00, 0e+00]Objective range [1e+00, 1e+00]Bounds range [0e+00, 0e+00]RHS range [0e+00, 0e+00]
Presolve time: 0.02sSolved in 0 iterations and 0.02 seconds (0.00 work units)
Unbounded model
Optimization complete
step7:安装结束
参考:
- https://support.gurobi.com/hc/en-us/articles/12872889819921-How-do-I-use-conda-to-install-Gurobi-in-Anaconda