<p>尝试Gekko中的<code>m.if3()</code>(或<code>m.if2()</code>)函数,根据Gekko变量进行条件语句切换。在<a href="https://stackoverflow.com/questions/56676961/question-about-the-conditional-statement-m-if3-in-the-gekko">Question about the conditional statement ('m.if3') in the GEKKO</a>中有更多关于条件语句的信息</p>
<pre class="lang-py prettyprint-override"><code># 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)
</code></pre>
<p>这是您的程序的一个版本,它提供了一个成功的解决方案</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<p>对于解决方案:</p>
<pre class="lang-py prettyprint-override"><code> -
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]
</code></pre>