[英]Gekko: I am using the gekko to optimize but my cad models parameters. my variables are dependent on one another. When I solve it gives error
# import the gekko optimization package
from gekko import GEKKO
# create gekko model
m = GEKKO(remote=False)
# Constants
E = m.Const( 200000 )
by = m.Const( 1 )
phi = m.Const( 3.1456/2 )
F = m.Const( 5 )
# initialize needed variables
b = m.Var(value = 0.8 )
l = m.Var(value = 0.5 )
L = m.Var(value = 4 )
I = m.Var()
K = m.Var()
theta = m.Var()
a = m.Var()
h = m.Var( )
S_max = m.Var()
# Define the equation,
m.Equation( theta == m.asin(by/(L+(l/2))) )
m.Equation( a == l/2 + (L + l/2)*m.cos(theta) )
m.Equation( K == ( F*(L + l/2)*m.sin(phi-theta))/ theta )
m.Equation( I == (K*l)/E )
m.Equation( h == ( (12*I)/b) )
m.Equation(S_max == F*a*(h/2)/I )
# Constrains
m. Equation(S_max <= 4000)
# Define objective function
m.Obj( h )
# Set mode to steady state optimization (solution is not changing in time)
m.options.IMODE = 3
m.solve()
# Print result
print('h: ' + str(h.value))
Gekko:我正在使用 Gekko 來優化我的 cad 模型參數。 我的變量相互依賴。 當我解決它給出錯誤。 怎么解決 代碼報錯
Exception Traceback (most recent call last) in 52 m.options.IMODE = 3 53 ---> 54 m.solve() 55 56 # 打印結果
c:\users\rahdar\appdata\local\programs\python\python37\lib\site-packages\gekko\gekko.py 在solve(self, disp, debug, GUI, **kwargs) 2057
print("Error:", errs) 2058 if (debug >= 1) and record_error: -> 2059 raise Exception(apm_error) 2060 2061 else: #solve on APM server異常:@error:未找到解決方案
如果您重新排列方程以避免除以零並將theta==m.asin(by/(L+(l/2)))
更改為m.sin(theta)==by/(L+(l/2))
。
# import the gekko optimization package
from gekko import GEKKO
# create gekko model
m = GEKKO(remote=False)
# Constants
E = m.Const( 200000 )
by = m.Const( 1 )
phi = m.Const( 3.1456/2 )
F = m.Const( 5 )
# initialize needed variables
b = m.Var(value = 0.8 )
l = m.Var(value = 0.5 )
L = m.Var(value = 4 )
I = m.Var()
K = m.Var()
theta = m.Var()
a = m.Var()
h = m.Var()
S_max = m.Var()
# Define the equation,
m.Equation( m.sin(theta) == by/(L+(l/2)))
m.Equation( a == l/2 + (L + l/2)*m.cos(theta) )
m.Equation( K * theta == ( F*(L + l/2)*m.sin(phi-theta)))
m.Equation( I == (K*l)/E )
m.Equation( h * b == 12*I)
m.Equation(S_max * I == F*a*(h/2) )
# Constraints
m. Equation(S_max <= 4000)
# Define objective function
m.Obj( h )
# Set mode to steady state optimization (solution is not changing in time)
m.options.IMODE = 3
m.solve()
# Print result
print('h: ' + str(h.value))
還有關於 model 構建的最佳實踐的其他建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.