简体   繁体   English

通过稍微放松约束在 Gekko 中获得解决方案

[英]Obtain solutions in Gekko by slightly relaxing constraints

I have a modified version of the spring optimization Gekko model .我有弹簧优化 Gekko 模型的修改版本。

Is there a way to slightly relax constraints so that the solver can still give me solutions even if they start falling out of the range of my constraints?有没有办法稍微放松约束,即使求解器开始超出我的约束范围,它们仍然可以给我解决方案?

I'm aware of RTOL, but is there a way of specifying tolerances for individual equations?我知道 RTOL,但是有没有办法为单个方程指定公差?

One way to do this is create a new variable eps that has a lower bound of zero and an upper bound that is the maximum allowable violation.一种方法是创建一个新变量eps ,其下限为零,上限为最大允许违规。 This becomes the deviation of the inequality constraints that can be minimized with an importance factor (eg 10).这成为不等式约束的偏差,可以用一个importance因子(例如 10)最小化。

eps = m.Var(lb=0,ub=0.5)
m.Minimize(10*eps)

Here are modified equations with eps :以下是带有eps的修改后的方程:

m.Equations([
    d_coil / d_wire >= 4-eps,
    d_coil / d_wire <= 16+eps
    ])

完整脚本

and the full script:和完整的脚本:

from gekko import GEKKO

# Initialize Gekko model
m = GEKKO()

#Maximize force of a spring at its preload height h_0 of 1 inches
#The stress at Hs (solid height) must be less than Sy to protect from damage

# Constants
from numpy import pi

# Model Parameters
delta_0 = 0.4 # inches (spring deflection)
h_0 = 1.0 # inches (preload height)
Q = 15e4 # psi
G = 12e6 # psi
S_e = 45e3 # psi
S_f = 1.5 
w = 0.18

# Variables
# inches (wire diameter)
d_wire = m.Var(value=0.07247, lb = 0.01, ub = 0.2) 
# inches (coil diameter)
d_coil = m.Var(value=0.6775, lb = 0.0) 
# number of coils in the spring
n_coils = m.Var(value=7.58898, lb = 0.0) 
# inches (free height spring exerting no force)
h_f = m.Var(value=1.368117, lb = 1.0) 
F = m.Var() # Spring force

# Intermediates
S_y = m.Intermediate((0.44 * Q) / (d_wire**w))
h_s = m.Intermediate(n_coils * d_wire)
k = m.Intermediate((G * d_wire**4)/(8 * d_coil**3 * n_coils))
kW = m.Intermediate((4 * d_coil - d_wire)/(4 * (d_coil-d_wire)) \
                    + 0.62 * d_wire/d_coil)
n = m.Intermediate((8 * kW * d_coil)/(pi * d_wire**3))
tau_max = m.Intermediate((h_f - h_0 + delta_0) * k * n)
tau_min = m.Intermediate((h_f - h_0) * k * n)
tau_mean = m.Intermediate((tau_max + tau_min) / 2)
tau_alt = m.Intermediate((tau_max - tau_min) / 2)
h_def = m.Intermediate(h_0 - delta_0)
tau_hs = m.Intermediate((h_f - h_s) * k * n)

# Equations
eps = m.Var(lb=0,ub=0.5)
m.Minimize(10*eps)

m.Equations([
    F == k * (h_f - h_0),
    d_coil / d_wire >= 4-eps,
    d_coil / d_wire <= 16+eps,
    d_coil + d_wire < 0.75,
    h_def - h_s > 0.05,
    tau_alt < S_e / S_f,
    tau_alt + tau_mean < S_y / S_f,
    tau_hs < S_y / S_f
    ])

# Objective function
m.Maximize(F)

# Send to solver
m.solve()

# Print solution
print('Maximum force: ' + str(F[0]))
print('Optimal values: ')
print('d_wire : ' + str(d_wire[0]))
print('d_coil : ' + str(d_coil[0]))
print('n_coils : ' + str(n_coils[0]))
print('h_f : ' + str(h_f[0]))
print('eps : ' + str(eps[0]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM