属性错误:“LpVariable”对象没有属性“log”;

发布于 2025-01-11 17:47:43 字数 2223 浏览 0 评论 0原文

我在尝试使用纸浆解决优化问题时遇到了这个问题。

代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文