# 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))
壁虎:我正在使用壁虎进行优化,但我的 CAD 模型参数。 我的变量是相互依赖的。当我解决它时,它会出错。如何解决 代码给出错误
异常回溯(最近一次调用( 最后( 在 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 in solve(self, disp, debug, GUI, **kwargs( 2057
print("Error:", errs( 2058 if (debug>= 1( 和 record_error: -> 2059 引发异常 (apm_error( 2060 2061 其他:在 APM 服务器上 #solve异常:@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))
关于模型构建的最佳实践,还有其他建议。