我使用anaconda安装gurobi。
然后我用anaconda为python 3.6.3创建一个环境。 安装gurobi后,我找不到包gurobipy。
我不知道为什么会这样......
具体:
Last login: Wed Nov 29 01:39:25 on ttys000
appledeMacBook-Pro-5:~ apple$ source activate python36
(python36) appledeMacBook-Pro-5:~ apple$ conda install gurobi
Fetching package metadata .............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /Users/apple/anaconda3/envs/python36:
#
gurobi 7.5.2 py36_0 gurobi
(python36) appledeMacBook-Pro-5:~ apple$
当我使用pycharm
时from gurobipy import *
try:
# Create a new model
m = 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")
m.optimize()
for v in m.getVars():
print(v.varName, v.x)
print('Obj:', m.objVal)
except GurobiError:
print('Error reported')
结果是
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
我发现anaconda一定有问题。我将包从anaconda复制到其他环境,它的工作原理。必须与gurobi发生一些包装冲突。这就是退出代码139的原因。我也在anaconda中安装了mosek。这个解算器可以在anaconda的环境下工作。
from cvxpy import *
import mosek
# Create two scalar optimization variables.
x = Variable()
y = Variable()
# Create two constraints.
constraints = [x + y == 1,
x - y >= 1]
# Form objective.
obj = Minimize(square(x - y))
# Form and solve problem.
prob = Problem(obj, constraints)
prob.solve(solver=MOSEK)
# The optimal dual variable (Lagrange multiplier) for
# a constraint is stored in constraint.dual_value.
print("optimal (x + y == 1) dual variable", constraints[0].dual_value)
print("optimal (x - y >= 1) dual variable", constraints[1].dual_value)
print("x - y value:", (x - y).value)
print(installed_solvers())
结果是
/Users/apple/anaconda3/envs/python36/bin/python "/Users/apple/Documents/GitHub/optimal-transport/作业/homework 5/practice.py"
optimal (x + y == 1) dual variable -0.0
optimal (x - y >= 1) dual variable 2.00000600065164
x - y value: 1.0000000011
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
anaconda的包裹
CVXcanon 0.1.1 0.1.1
Mosek 8.1.33 7.0.123
ca-certificates 2017.08.26
certifi 2017.11.5 2017.11.5
cvxcanon 0.1.1
cvxpy 0.4.9 0.4.11
dill 0.2.7.1 0.2.7.1
ecos 2.0.4 2.0.5
ecos 2.0.5 2.0.5
fastcache 1.0.2 1.0.2
gurobi 7.5.2
intel-openmp 2018.0.0 2018.0.0
libcxx 4.0.1
libcxxabi 4.0.1
libedit 3.1
libffi 3.2.1
libgcc 4.8.5
libgfortran 3.0.1
mkl 2018.0.1 2018.0.0
multiprocess 0.70.4 0.70.5
ncurses 6.0
numpy 1.13.3 1.13.3
openssl 1.0.2m
pip 9.0.1 9.0.1
python 3.6.3
readline 7.0 6.2.4.1
scipy 1.0.0 1.0.0rc2
scs 1.2.6 2.0.1
setuptools 36.5.0.post20170921 38.2.3
setuptools 36.5.0 38.2.3
six 1.11.0 1.11.0
sqlite 3.20.1
tk 8.6.7
toolz 0.8.2 0.8.2
wheel 0.30.0 0.30.0a0
xz 5.2.3
zlib 1.2.11
答案 0 :(得分:0)
将Gurobi与Anaconda Python一起使用的最简单方法是使用the instructions中的Conda安装。
在终端中打开Gurobi并使用
复制您的密钥grbgetkey your_gurobi_key
将gurobi频道添加到conda使用的存储库列表
conda config --add channels http://conda.anaconda.org/gurobi
安装Gurobi Anaconda Python库
conda install gurobi
检查您在iPython中的安装:
m = grb.Model()
vars = [m.addVar(name="x." + str(i)) for i in range(n)]
m.update()
constrs = [m.addConstr(vars[i] <= i) for i in range(n)]
m.update()
retval = m.optimize()
或使用此simple script检查是否已安装库和许可证:
python abrecheck.py
答案 1 :(得分:0)
这可能与在macOS / linux上运行Gurobi 7.5+和Anaconda以及python 3.x的一些报道问题有关(不确定版本)。
e.g。这个讨论https://groups.google.com/forum/#!topic/gurobi/6lw13VjvXsE
确定配置组合究竟是什么打破了它,但我在遇到这个段错误问题后得到了python 2.7。
conda info...
conda version : 4.5.0
conda-build version : 3.7.2
python version : 2.7.14.final.0
platform : osx-64
user-agent : conda/4.5.0 requests/2.18.4 CPython/2.7.14 Darwin/17.5.0 OSX/10.13.4
conda list...
gurobi 7.5.2 py27_0 gurobi
我已经读到降级版本的anaconda但仍保留python 3.x也可能有效,但无法重复该解决方案。