Gekko:我正在使用Gekko优化我的cad模型参数。我的变量是相互依赖的。当我解的时候,它会出错

2024-04-29 03:49:50 发布

您现在位置:Python中文网/ 问答频道 /正文

# 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 # Print result

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) and record_error: -> 2059 raise Exception(apm_error) 2060 2061 else: #solve on APM server

Exception: @error: Solution Not Found


Tags: theinimportvaluevarexceptionerrormax
1条回答
网友
1楼 · 发布于 2024-04-29 03:49:50

如果重新排列方程式以避免被零除,并将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)) 

关于best practices for model building还有其他建议

相关问题 更多 >