Linux+Gurobi-python调用

获取sudo权限

ubuntu@VM-0-17-ubuntu:/home/zjc/GUROBI/gurobi911/linux64$ sudo visudo 
# 修改visudo文件
# 添加自己的用户信息,如下 user ALL=(ALL:ALL) ALL
# user是你的用户名
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
user     ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
ubuntu  ALL=(ALL:ALL) NOPASSWD: ALL

gurobi下载与安装

官网下载安装包:https://www.gurobi.com/downloads/gurobi-software/

选择想要的版本。

user@VM-0-17-ubuntu:~/GUROBI$ tar -xzvf gurobi9.1.1_linux64.tar.gz
user@VM-0-17-ubuntu:~/GUROBI$ cd ./gurobi911/linux64
user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ ls
bin  build  docs  EULA.pdf  examples  include  lib  matlab  R  ReleaseNotes.html  setup.py  src
user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ sudo python setup.py install
[sudo] password for user: 
running install
running build
running build_py
running install_lib
creating /usr/local/lib/python2.7/dist-packages/gurobipy
copying build/lib.linux-x86_64-2.7/gurobipy/__init__.py -> /usr/local/lib/python2.7/dist-packages/gurobipy
copying build/lib.linux-x86_64-2.7/gurobipy/gurobipy.so -> /usr/local/lib/python2.7/dist-packages/gurobipy
byte-compiling /usr/local/lib/python2.7/dist-packages/gurobipy/__init__.py to __init__.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/dist-packages/gurobipy-9.1.1.egg-info
removing /home/zjc/GUROBI/gurobi911/linux64/build
user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ 

添加环境变量

user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ vim ~/.bashrc
# 修改 ~/.bashrc 文件在文件最后插入 /home/.../为文件路径


export GUROBI_HOME="/home/.../gurobi911/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"

:wq保存

user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ source ~/.bashrc

激活Gurobi

从Gurobi获取免费的学术 license  一个账号可以申请好多个

user@VM-0-17-ubuntu:~/GUROBI/gurobi911/linux64$ grbgetkey ********-7bd9-11eb-9f6c-************

验证成功后生成gurobi.lic  。 将gurobi.lic的位置/home/....../加入到环境变量里头,

即将下面的指令加入到 ~/.bashrc中,步骤同上。

export GRB_LICENSE_FILE="/home/....../gurobi.lic" 

这里有个小问题,不知道是不是一定要用学校的ipgrbgetkey才行,我在远程 sever上测试不行。(报错如下,估计是我版本下载错了,需要下remote server)

user@$ grbgetkey ******-7bd9-11eb-9f6c-**********
info  : grbgetkey version , build v9.1.1rc0
info  : Contacting Gurobi license server...
error : ERROR 303: hostname
not recognized as belonging to an academic domain
info  : Is your computer connected to the university network?
info  : Your computer must be connected to the university network for the grbgetkey
info  : command to work. You can connect your computer either directly or via a VPN
info  : that tunnels all traffic through the university network.  Once the license
info  : file is setup, your computer does not need to be connected to the university
info  : network. You can repeat this process to get licenses for additional computers.
info  : If you are unable to setup this license file, please visit our support website
info  : https://support.gurobi.com for further assistance. 

如果在自己个PC上,一般是不会有问题的。

然后验证是否安装成功:gurobi.sh

user@laptap:~$ gurobi.sh
Python 3.7.4 (default, Oct 29 2019, 10:15:53) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Academic license - for non-commercial use only - expires 2021-04-25
Using license file /home/zjc/gurobi.lic
Set parameter LogFile to value gurobi.log

Gurobi Interactive Shell (linux64), Version 9.1.1
Copyright (c) 2020, Gurobi Optimization, LLC
Type "help()" for help

gurobi> 

 python调用

python3 test.py lp_file.lp
原文地址:https://www.cnblogs.com/PiaYie/p/14473783.html