【问题标题】:How to fix 'unable to import gurobipy', undefined variable 'model', and undefined variable 'GRB' in VSCode & Python?如何修复 VSCode 和 Python 中的“无法导入 gurobipy”、未定义变量“模型”和未定义变量“GRB”?
【发布时间】:2019-05-22 06:45:33
【问题描述】:

我正在尝试使用 Python 中的 Gurobi 为波音 777x 的座位布局编写优化代码,但我的代码遇到错误消息。具体来说,导入 gurobipy (pylint(import-error))、未定义变量“模型”(pylint(undefined-variable)) 和未定义变量“GRB”(pylint(undefined-variable)) 时出现 pylint 错误。

我对编码比较陌生,不确定 gurobipy 或 pylint 是否可能下载到错误的位置,并且无法通过 VSCode 访问。我有 gurobi.lic、gurobi.log、pylint 文件夹和我的代码 (BoeingOptimization.py) 都保存在我桌面上的同一个文件夹中。那些相同的文件(在 pylint 的情况下是文件夹)也在用户文件的其他地方。我认为可能存在某种路径问题,因为我相信所有内容都已下载,但可能位于错误的位置?

# !/usr/bin/python
from gurobipy import *

# Create the model to be used in the terminal
m = Model("777x Optimization")

# Name and Create the variables
x1 = m.addVar(name="First-Class Seat Pitch [in]")
x2 = m.addVar(name="First-Class Seat Width [in]")
x3 = m.addVar(name="First-Class Seat Thickness [in]")
x4 = m.addVar(name="Premium-Economy Seat Pitch [in]")
x5 = m.addVar(name="Premium-Economy Seat Width [in]")
x6 = m.addVar(name="Premium-Economy Seat Thickness [in]")
x7 = m.addVar(name="Number of First-Class Rows")
x8 = m.addVar(name="Number of Premium-Economy Rows")
x9 = m.addVar(name="Number of First-Class Seats Per Row")
x10 = m.addVar(name="Number of Premium-Economy Seats per Row")

# Set objective: f(x1,x2,x3,x4,x5,x6,x7,x8)
obj = x7*x9*(x1+x2+x3) + x8*x10*(x4+x5+x6)
m.setObjective(obj, GRB.MINIMIZE)

# Add constraints
m.addConstr((x7*x1*x2)+(x4*x5*x8) <= 342625, "Area Constraint")
m.addConstr(1512*x8 + 5410*x7 >= 202182, "Ticket Revenue             Constraint")
m.addConstr(0.25*x5 + 0.35*x6 +0.4*x4 >= 18.6975, "Premium-Economy    Comfort Level Constraint")
m.addConstr(0.25*x2 + 0.35*x3 +0.4*x1 >= 29.16, "First-Class Economy Comfort Constraint")
m.addConstr(x8 - 1.4*x7 >= 0 , "Ratio of First Class to Economy Class Seats Constraint")
m.addConstr(x1 >= 57, "Minimum First-Class Pitch Constraint")
m.addConstr(x2 >= 25, "Minimum First-Class Width Constraint")
m.addConstr(x3 >= 3, "Minimum First-Class Thickness Constraint")
m.addConstr(x4 >= 36, "Minimum Premium-Economy Pitch Constraint")
m.addConstr(x5 >= 18.5, "Minimum Premium-Economy Width Constraint")
m.addConstr(x6 >= 1.5, "Minimum Premium-Economy Thickness Constraint")
m.addConstr(x9*x2 <= 233.04, "Seats Per Aisle Constraint First-Class")
m.addConstr(x10*x5 <= 233.04, "Seats Per Aisle Constraint Premium-Economy")
m.optimize()

for v in m.getVars():
    print('%s %g' % (v.varName, v.x))

print('Obj: %g' % obj.getValue())

【问题讨论】:

  • 您是否检查过 Python 解释器(在 VS Code 的左下角)是您所期望的 - 相同的版本和目录?
  • 这是正确的解决方法。 VSCode 使用的是 Python 3.7,但为 Python 2.7.15 安装了 gurobi。感谢您的帮助。
  • 作为一个额外提示,明确导入您将要使用的内容,不要使用import *。它可能会混淆工具(和您自己)。另外,在 REPL 之外完成时,它会令人不悦。

标签: python terminal visual-studio-code path gurobi


【解决方案1】:

您需要选择 VSCode 使用的正确 Python 解释器(可从 VSCode 的左下角选择 - 检查它是否与您期望的版本和目录相同)。

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2019-12-26
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多