【问题标题】:PYOMO : TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'PYOMO:TypeError:/:'int'和'NoneType'不支持的操作数类型
【发布时间】:2021-03-11 14:31:09
【问题描述】:

我正在尝试匹配最佳参数以拟合实验曲线。 为此,我希望我的求解器最小化平方残差的总和。有人知道我做错了什么吗? 谢谢!

print(len(X))
print(len(Y))

model = ConcreteModel()
model.FRAT5= Var(domain=PositiveReals , initialize =3)
model.PRICE0 = Var(domain=PositiveIntegers , initialize = 400)

model.price0_constraint = Constraint(expr = model.PRICE0 >= 51)
model.FRAT5_constraint = Constraint(expr = model.FRAT5 >= 0.05  )

residuals = [    (exp(- (-log(0.5)/(model.FRAT5 - 1 )) * (X[k]/model.PRICE0 -1) ) - Y[k] )  for k in range(len(X)) ]

model.obj = Objective(expr=sum(residuals[k]**2 for k in range(len(X))), sense=minimize)

SolverFactory('ipopt', executable='/content/ipopt').solve(model).write()

回答:

100
100

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-127-d00a8ec5b470> in <module>()
      9 model.FRAT5_constraint = Constraint(expr = model.FRAT5 >= 0.05  )
     10 
---> 11 residuals = [    (exp(- (-log(0.5)/(model.FRAT5 - 1 )) * (X[k]/model.PRICE0 -1) ) - Y[k] )  for k in range(len(X)) ]
     12 
     13 model.obj = Objective(expr=sum(residuals[k]**2 for k in range(len(X))), sense=minimize)

<ipython-input-127-d00a8ec5b470> in <listcomp>(.0)
      9 model.FRAT5_constraint = Constraint(expr = model.FRAT5 >= 0.05  )
     10 
---> 11 residuals = [    (exp(- (-log(0.5)/(model.FRAT5 - 1 )) * (X[k]/model.PRICE0 -1) ) - Y[k] )  for k in range(len(X)) ]
     12 
     13 model.obj = Objective(expr=sum(residuals[k]**2 for k in range(len(X))), sense=minimize)

TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'

【问题讨论】:

    标签: python pyomo


    【解决方案1】:

    pyomo 对象(包括变量)只是用于将数学构造传递给求解器的对象。因此,您不能在 residuals 的一般表达式中使用它们。

    应该能够直接在你的目标表达式中表达它们,所以试着把你的残差表达式移动到你的目标函数中,在那里平方,然后总结它们。

    ======= 编辑:

    我收回关于residuals 的话。该表达式确实有效(至少在我的版本上)。我有点惊讶。无论如何,这为我构建了模型(没有解决)。这正是您的代码,其中填充了名义上的 XY

    from pyomo.environ import *
    
    X = [1,2,3]
    Y = [4,5,6]
    model = ConcreteModel()
    model.FRAT5= Var(domain=PositiveReals , initialize =3)
    model.PRICE0 = Var(domain=PositiveIntegers , initialize = 400)
    
    model.price0_constraint = Constraint(expr = model.PRICE0 >= 51)
    model.FRAT5_constraint = Constraint(expr = model.FRAT5 >= 0.05  )
    
    residuals = [    (exp(- (-log(0.5)/(model.FRAT5 - 1 )) * (X[k]/model.PRICE0 -1) ) - Y[k] )  for k in range(len(X)) ]
    
    model.obj = Objective(expr=sum(residuals[k]**2 for k in range(len(X))), sense=minimize)
    
    model.pprint()
    

    生成:

    2 Var Declarations
        FRAT5 : Size=1, Index=None
            Key  : Lower : Value : Upper : Fixed : Stale : Domain
            None :     0 :     3 :  None : False : False : PositiveReals
        PRICE0 : Size=1, Index=None
            Key  : Lower : Value : Upper : Fixed : Stale : Domain
            None :     1 :   400 :  None : False : False : PositiveIntegers
    
    1 Objective Declarations
        obj : Size=1, Index=None, Active=True
            Key  : Active : Sense    : Expression
            None :   True : minimize : (exp(- 0.6931471805599453/(FRAT5 - 1)*(1/PRICE0 - 1)) - 4)**2 + (exp(- 0.6931471805599453/(FRAT5 - 1)*(2/PRICE0 - 1)) - 5)**2 + (exp(- 0.6931471805599453/(FRAT5 - 1)*(3/PRICE0 - 1)) - 6)**2
    
    2 Constraint Declarations
        FRAT5_constraint : Size=1, Index=None, Active=True
            Key  : Lower : Body  : Upper : Active
            None :  0.05 : FRAT5 :  +Inf :   True
        price0_constraint : Size=1, Index=None, Active=True
            Key  : Lower : Body   : Upper : Active
            None :  51.0 : PRICE0 :  +Inf :   True
    

    【讨论】:

    • 非常感谢您的提示。您的意思是这样的:model.obj = Objective(expr=sum([ (exp(- (-log(0.5)/(model.FRAT5 - 1 )) * (X[k]/model.PRICE0 -1) ) - Y[k] ) for k in range(len(X)) ]**2 for k in range(len(X))), sense=minimize) 不幸的是,这并不能解决错误,您还有其他想法吗?
    • 谢谢,我终于解决了这个问题。看到你的评论对我很有帮助。我仍然不知道为什么它不起作用,因为它确实适用于您建议的输入值 X,Y 但不适用于我的实数向量。更令人惊讶的是,当我输入 -(model.PRICE0 - X[k])/model.PRICE0) 时它可以工作,但不适用于 (X[k] - model.PRICE0 )/model.PRICE0) 无论如何,它现在可以工作了,非常感谢!
    猜你喜欢
    • 2016-09-07
    • 2018-04-16
    • 2017-12-27
    • 2014-03-31
    • 2021-05-23
    • 2015-10-15
    • 1970-01-01
    • 2014-11-15
    • 2016-08-25
    相关资源
    最近更新 更多