所以,我试图解决一个优化问题。我想弄明白的是,当我运行代码时,我的函数调用“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()
顾名思义,我是一个Python迷,我被迷惑了。任何帮助都将不胜感激。谢谢
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)
尝试Gekko中的
m.if3()
(或m.if2()
)函数,根据Gekko变量进行条件语句切换。在Question about the conditional statement ('m.if3') in the GEKKO中有更多关于条件语句的信息这是您的程序的一个版本,它提供了一个成功的解决方案
对于解决方案:
你必须用这个
如果(fload_c3_1.0值<;0.1):
相关问题 更多 >
编程相关推荐