高校师生可免费获取gurobi的学术license,有效期一年,可无限续期。不过在其英文官网申请的license,中国区一般无法通过其EDU IP验证。在国内申请只需发邮件给gurobi中国请求一个无需认证的license即可,详见http://www.gurobi.cn/NewsView1.Asp?id=4
提交表格后,半小时左右就会回复。
在此处可下载安装文件
首先将tar.gz解压到/opt下
tar xvfz gurobi9.1.2_linux64.tar.gz
mv gurobi912 /opt
将如下环境变量写入~/.bashrc文件末尾
export GUROBI_HOME="/opt/gurobi912/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin" export
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib“
gurobi引擎自带的shell并不是特别常用,其python接口在gurobi编程中应用最广泛
执行安装路径/opt/gurobi912/linux64中的setup.py文件安装gurobi引擎的python接口
python setup.py install
gurobi新版本进一步完善了其matlab接口,已和其python接口相差无几。
进入matlab执行:
cd /opt/gurobi912/linux64/matlab
gurobi_setup
gurobi 高效数学规划引擎 | python3 配置、使用及建模实例
Gurobi 生产计划调度学习案例(含代码实现) (生产切换、装配计划)
中文官网资料包
视频教程
公众号
如下脚本可用于测试gurobi系统安装的正确性
python
#!/usr/bin/env python3.7
# Copyright 2021, Gurobi Optimization, LLC
# This example formulates and solves the following simple MIP model:
# maximize
# x + y + 2 z
# subject to
# x + 2 y + 3 z <= 4
# x + y >= 1
# x, y, z binary
import gurobipy as gp
from gurobipy import GRB
try:
# Create a new model
m = gp.Model("mip1")
# Create variables
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
# Set objective
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
# Optimize model
m.optimize()
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal)
except gp.GurobiError as e:
print('Error code ' + str(e.errno) + ': ' + str(e))
except AttributeError:
print('Encountered an attribute error')
matlab
function mip1()
% Copyright 2021, Gurobi Optimization, LLC
% This example formulates and solves the following simple MIP model:
% maximize
% x + y + 2 z
% subject to
% x + 2 y + 3 z <= 4
% x + y >= 1
% x, y, z binary
names = {'x'; 'y'; 'z'};
model.A = sparse([1 2 3; 1 1 0]);
model.obj = [1 1 2];
model.rhs = [4; 1];
model.sense = '<>';
model.vtype = 'B';
model.modelsense = 'max';
model.varnames = names;
gurobi_write(model, 'mip1.lp');
params.outputflag = 0;
result = gurobi(model, params);
disp(result);
for v = 1:length(names)
fprintf('%s %d\n', names{v}, result.x(v));
end
fprintf('Obj: %e\n', result.objval);
end