【问题标题】:What does AttributeError: 'LpElement' object has no attribute 'cat' and why is this error produced?什么 AttributeError: 'LpElement' object has no attribute 'cat' 为什么会产生这个错误?
【发布时间】:2016-09-20 15:43:16
【问题描述】:

我正在尝试使用 Python PuLP 对以下方程建模

我已经编写了以下 Python 代码

prob = LpProblem('Resource', LpMaximize)

# x variables

xs = [LpVariable("x{0}{1}{2}".format(i + 1, j + 1, k + 1), cat = "Binary")

    for i in range(0, 3)
    for j in range(0, 5)
    for k in range(0, 2)
]

print("\nX Variable\n")

for i in range(0, len(xs)):
    print(xs[i])

# y variables

ys = [LpVariable("y{0}{1}".format(i + 1, j + 1), cat = "Binary")

    for i in range(0, 3)
    for j in range(0, 5)
]

print("\nY Variable\n")
for i in range(0, len(ys)):
    print(ys[i])


for j in range(0, 5):
    for k in range(0, 2):
    for i in range(0, 3):
    con = "x{0}{1}{2} <= y{3}{4}".format(i + 1, j + 1, k + 1, i + 1, j + 1)
prob += LpAffineExpression(LpElement(con))
print(con)

status = prob.solve()

这会产生以下纸浆错误:

Traceback(最近一次调用最后一次): 文件“C:\Python34\Cloud 3.py”,第 446 行,在 资源(请求,pmachine,l,q) 资源中的文件“C:\Python34\Cloud 3.py”,第 136 行 状态 = prob.solve() 文件“C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py”,第 1643 行,在求解 status = solver.actualSolve(self, **kwargs) 文件“C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py”,第 1303 行,实际求解 return self.solve_CBC(lp, **kwargs) 文件“C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py”,第 1325 行,在 solve_CBC tmpMps,重命名 = 1) 文件“C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py”,第 1431 行,在 writeMPS 中 如果 mip 和 v.cat == LpInteger: AttributeError: 'LpElement' 对象没有属性 'cat'

AttributeError: 'LpElement' object has no attribute 'cat' 是什么意思,为什么会产生这个错误?

【问题讨论】:

    标签: python mathematical-optimization linear-programming pulp


    【解决方案1】:

    AttributeError: 'LpElement' object has no attribute 'cat' 是在问题变量(在这种情况下为 prob = LpProblem('Resource', LpMaximize))与添加的约束之间不兼容时生成的。

    当使用指定的 PuLP LpVariable.dicts 定义变量而不是像上面所做的那样动态生成变量时,可以通过摆脱冗余的 LpAffineExpression(LpElement(con)) 调用来解决此问题。

    合并这些更改的更好方法如下:

    prob = LpProblem('Resource', LpMaximize)
    
    xdict = LpVariable.dicts('', ["x{0}{1}{2}".format(i + 1, j + 1, k + 1)
            for i in range(0, len(request))
            for j in range(0, len(pmachine))
            for k in range(0, pmachine[j].npj)
        ],
        0, 1, cat = 'Binary')
    
    ydict = LpVariable.dicts('', ["y{0}{1}".format(i + 1, j + 1)
            for i in range(0, len(request))
            for j in range(0, len(pmachine))
        ],
        0, 1, cat = 'Binary')
    
    for i in range(0, len(request)):
        for j in range(0, len(pmachine)):
            for k in range(0, pmachine[j].npj):
                con = xdict["x{0}{1}{2}".format(i + 1, j + 1, k + 1)] <= ydict["y{0}{1}".format(i + 1, j + 1)]
                prob += con
                print(con)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-05
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      相关资源
      最近更新 更多