属性错误:“LpVariable”对象没有属性“log”;
我在尝试使用纸浆解决优化问题时遇到了这个问题。
代码:
import pulp
import numpy as np
import math
prob = pulp.LpProblem("example", pulp.LpMaximize)
# Variable represent number of times device i is used
d = pulp.LpVariable("d", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
var = pulp.LpVariable("var", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
# The objective function that we want to maximize
n = len(y_arfima)
prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)
# Print out the results
for v in prob.variables():
print (v.name, "=", v.varValue)
错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in <module>
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
AttributeError Traceback(最近一次调用最后) AttributeError: 'LpVariable' object has no attribute 'log'
上述异常是以下异常的直接原因:
TypeError Traceback (most最近的调用最后) 〜\AppData\Local\Temp/ipykernel_12336/3027528854.py 中 11 # 我们想要最大化的目标函数 12 n = len(y_arfima) ---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)math.gamma(t-2d)) )/(math.gamma(td)**2)) 对于 t 在范围(1,n+1)])/2 - 1/2 14 15 # 实际上解决问题,这调用了GLPK,所以你需要安装它
TypeError: Loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
你能帮我吗?
谢谢!
I had this problem when trying to solve a optimization problem using pulp.
The code:
import pulp
import numpy as np
import math
prob = pulp.LpProblem("example", pulp.LpMaximize)
# Variable represent number of times device i is used
d = pulp.LpVariable("d", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
var = pulp.LpVariable("var", cat=pulp.LpContinuous,lowBound=0,upBound=np.inf)
# The objective function that we want to maximize
n = len(y_arfima)
prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
# Actually solve the problem, this calls GLPK so you need it installed
pulp.GLPK().solve(prob)
# Print out the results
for v in prob.variables():
print (v.name, "=", v.varValue)
The error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in <module>
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)*math.gamma(t-2*d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
AttributeError Traceback (most recent call last)
AttributeError: 'LpVariable' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12336/3027528854.py in
11 # The objective function that we want to maximize
12 n = len(y_arfima)
---> 13 prob += -(n/2) * np.log(var) - np.sum([np.log((math.gamma(t)math.gamma(t-2d))/(math.gamma(t-d)**2)) for t in range(1,n+1)])/2 - 1/2
14
15 # Actually solve the problem, this calls GLPK so you need it installed
TypeError: loop of ufunc does not support argument 0 of type LpVariable which has no callable log method
Can you help me?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论