What does AttributeError: 'LpElement' object has no attribute 'cat' and why is this error produced?
What does AttributeError: 'LpElement' object has no attribute 'cat' and why is this error produced?
我正在尝试使用 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()
这会产生以下 PuLP 错误:
Traceback (most recent call last):
File "C:\Python34\Cloud 3.py", line 446, in
resource(request, pmachine, l, q)
File "C:\Python34\Cloud 3.py", line 136, in resource
status = prob.solve()
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1643, in solve
status = solver.actualSolve(self, **kwargs)
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1303, in actualSolve
return self.solve_CBC(lp, **kwargs)
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1325, in solve_CBC
tmpMps, rename = 1)
File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1431, in writeMPS
if mip and v.cat == LpInteger:
AttributeError: 'LpElement' object has no attribute 'cat'
AttributeError: 'LpElement' object has no attribute 'cat'
是什么意思,为什么会产生这个错误?
当问题变量(在本例中为 prob = LpProblem('Resource', LpMaximize)
与添加的约束不兼容时,会产生 AttributeError: 'LpElement' object has no attribute 'cat'
。
当使用指定的 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)
我正在尝试使用 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()
这会产生以下 PuLP 错误:
Traceback (most recent call last): File "C:\Python34\Cloud 3.py", line 446, in resource(request, pmachine, l, q) File "C:\Python34\Cloud 3.py", line 136, in resource status = prob.solve() File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1643, in solve status = solver.actualSolve(self, **kwargs) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1303, in actualSolve return self.solve_CBC(lp, **kwargs) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\solvers.py", line 1325, in solve_CBC tmpMps, rename = 1) File "C:\Python34\lib\site-packages\pulp-1.6.1-py3.4.egg\pulp\pulp.py", line 1431, in writeMPS if mip and v.cat == LpInteger: AttributeError: 'LpElement' object has no attribute 'cat'
AttributeError: 'LpElement' object has no attribute 'cat'
是什么意思,为什么会产生这个错误?
当问题变量(在本例中为 prob = LpProblem('Resource', LpMaximize)
与添加的约束不兼容时,会产生 AttributeError: 'LpElement' object has no attribute 'cat'
。
当使用指定的 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)