【问题标题】:If else statement returning TypeError:object of type 'int' has no len() - Not sure whyIf else 语句返回 TypeError:'int' 类型的对象没有 len() - 不知道为什么
【发布时间】:2020-04-29 07:33:14
【问题描述】:

所以,我正在尝试解决一个优化问题。我想弄清楚的是,当我运行代码时,我的函数调用“to_fp_Cx”会引发错误,我不明白为什么。

回溯一直指向我定义的函数。我通过使用不同的值调用这些函数来独立测试这些函数,并且按预期工作。所以,我不确定发生了什么。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-3f717a4f07e1> in <module>
     37 # intermediate variables with explicit equations
     38 
---> 39 if(fload_c3_1 < 0.1):
     40     alt_fload_c3_1 = m.Intermediate(0)
     41 else:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\gekko\gk_operators.py in __len__(self)
     23         return self.name
     24     def __len__(self):
---> 25         return len(self.value)
     26     def __getitem__(self,key):
     27         return self.value[key]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\gekko\gk_operators.py in __len__(self)
    142 
    143     def __len__(self):
--> 144         return len(self.value)
    145 
    146     def __getitem__(self,key):

TypeError: object of type 'int' has no len()

顾名思义,我是一个蟒蛇菜鸟,我被迷惑了。任何帮助,将不胜感激。谢谢

import numpy as np

# import gekko, pip install if needed
from gekko import GEKKO


# Compressor Performance curves
# Fraction capacity to Fractional power conversion

# Compressor C3
def to_fp_c3(fc):

    a = 5.16102738
    b = -16.25992208
    c = 18.52731113
    d = -8.859480201
    e = 2.096698885
    f = 0.334319989

    if (fc < 0.1):
        fp = 0.0
    else:
        fp = (a*fc**5)+(b*fc**4)+(c*fc**3)+(d*fc**2)+(e*fc**1)+(f*fc**0)

    return fp

...

### Optimization Model ####

# create new model
m = GEKKO(remote = False)

# Solver option - 1: APOPT, 2: BPOPT, 3: IPOPT 0:Benchmark all available
m.options.SOLVER = 3

# declare model parameters
maxcap_c3_1 = m.Param(value = 900)
maxcap_c3_2 = m.Param(value = 900)


load = m.Param(value = 1500)


## Model variables

# load distribution
fload_c3_1 = m.Var(value=0.50,lb=0.0,ub=1.0, integer = False)
fload_c3_2 = m.Var(value=0.50,lb=0.0,ub=1.0, integer = False)

# declare variables and initial guesses
#totalpowerdraw = m.Var()

# intermediate variables with explicit equations

if(fload_c3_1 < 0.1):
    alt_fload_c3_1 = m.Intermediate(0)
else:
    alt_fload_c3_1 = m.Intermediate(fload_c3_1)


if(fload_c3_2 < 0.1):
    alt_fload_c3_2 = m.Intermediate(0)
else:
    alt_fload_c3_2 = m.Intermediate(fload_c3_2)



assignedload_c3_1 = m.Intermediate(alt_fload_c3_1 * maxcap_c3_1)
assignedload_c3_2 = m.Intermediate(alt_fload_c3_2 * maxcap_c3_2)

powerdraw_c3_1 = m.Intermediate(to_fp_c3(alt_fload_c3_1) * maxcap_c3_1)
powerdraw_c3_2 = m.Intermediate(to_fp_c3(alt_fload_c3_2) * maxcap_c3_2)

totalpowerdraw = m.Intermediate(powerdraw_c3_1 + powerdraw_c3_2)


# implicit equations
m.Equation(load == assignedload_c3_1 + assignedload_c3_2 )


# minimize weight1
m.Obj(totalpowerdraw)

# solve optimization
m.solve()  # remote=False for local solve

print ('')
print ('--- Results of the Optimization Problem ---')
print (alt_fload_c3_1.value, powerdraw_c3_1.value)
print (alt_fload_c3_1.value, powerdraw_c3_2.value)

【问题讨论】:

  • 代码有整数,如fp = 0。这是我们需要处理的大量代码。你能做一些简短的东西来证明这个问题吗?
  • @tdelaney 缩短了代码。希望这更容易导航。我可以在这里附加一个 jupyter 笔记本吗?
  • @usr2564301 请查看编辑。谢谢!
  • 你能把0传给Intermediate吗?文档说它收到了一个方程式。
  • @SergioR,好建议。中间方程m.Intermediate(0) 是有效的。问题在于if 语句。我可能需要更新文档以澄清。

标签: python typeerror gekko


【解决方案1】:

尝试使用 Gekko 中的 m.if3()(或 m.if2())函数,根据 Gekko 变量进行条件语句切换。在Question about the conditional statement ('m.if3') in the GEKKO中有更多关于条件语句的信息

# use gekko if3 (or if2)
alt_fload_c3_1 = m.if3(fload_c3_1-0.1,0,fload_c3_1)
alt_fload_c3_2 = m.if3(fload_c3_2-0.1,0,fload_c3_2)

这是您的程序的一个版本,它提供了一个成功的解决方案。

import numpy as np
from gekko import GEKKO

# Compressor Performance curves
# Fraction capacity to Fractional power conversion
# Compressor C3
def to_fp_c3(fc):
    a = 5.16102738
    b = -16.25992208
    c = 18.52731113
    d = -8.859480201
    e = 2.096698885
    f = 0.334319989
    fp = m.if3(fc-0.1,0,(a*fc**5)+(b*fc**4)+(c*fc**3)\
               +(d*fc**2)+(e*fc**1)+(f*fc**0))
    return fp

### Optimization Model ####
# create new model
m = GEKKO(remote = False)

# declare model parameters
maxcap_c3_1 = m.Param(value = 900)
maxcap_c3_2 = m.Param(value = 900)
load = m.Param(value = 1500)

## Model variables

# load distribution
fload_c3_1 = m.Var(value=0.50,lb=0.0,ub=1.0, integer = False)
fload_c3_2 = m.Var(value=0.50,lb=0.0,ub=1.0, integer = False)

# use gekko if3 (or if2)
alt_fload_c3_1 = m.if3(fload_c3_1-0.1,0,fload_c3_1)
alt_fload_c3_2 = m.if3(fload_c3_2-0.1,0,fload_c3_2)
assignedload_c3_1 = m.Intermediate(alt_fload_c3_1 * maxcap_c3_1)
assignedload_c3_2 = m.Intermediate(alt_fload_c3_2 * maxcap_c3_2)
powerdraw_c3_1 = m.Intermediate(to_fp_c3(alt_fload_c3_1) * maxcap_c3_1)
powerdraw_c3_2 = m.Intermediate(to_fp_c3(alt_fload_c3_2) * maxcap_c3_2)
totalpowerdraw = m.Intermediate(powerdraw_c3_1 + powerdraw_c3_2)

# implicit equations
m.Equation(load == assignedload_c3_1 + assignedload_c3_2 )

# minimize weight1
m.Obj(totalpowerdraw)

# solve optimization
m.solve()  # remote=False for local solve

print ('')
print ('--- Results of the Optimization Problem ---')
print (alt_fload_c3_1.value, powerdraw_c3_1.value)
print (alt_fload_c3_1.value, powerdraw_c3_2.value)

解决办法:

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.0313 sec
 Objective      :  1576.7914326000025
 Successful solution
 ---------------------------------------------------

--- Results of the Optimization Problem ---
[0.66761123885] [677.4476587]
[0.66761123885] [899.3437739]

【讨论】:

  • 教授@John Hendengren,谢谢!这就像一个魅力。
【解决方案2】:

你必须使用这个

如果(fload_c3_1.value

【讨论】:

  • 如果条件语句是在初始化时确定的,这是一个很好的解决方案。对于条件语句取决于优化变量的问题,我使用m.if3()m.if2() 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2019-12-28
  • 2021-02-26
相关资源
最近更新 更多