在 Python 中优化一个函数的多个输出变量
Optimizing multiple output variables of a function in Python
我目前正在研究一种确定风力涡轮机支撑结构成本的算法。我正在编写的算法需要优化初始输入支撑结构的重量,以便应力水平不会超过但接近所用材料特性的失效标准。另一个要求是结构的自然频率需要限制在 2 个值之间。要优化结构,可以更改 4 个变量。
我能否使用 Scipy.Optimize 库中的函数,使用多个设计参数优化该结构的重量,同时考虑支撑结构中的固有频率和最大应力值?
我正在优化的函数如下所示:
def func(self, x):
self.properties.D_mp = x[0] # Set a new diameter for the monopile
self.properties.Dtrat_tower = x[1] # Set a new thickness ratio for the tower
self.properties.Dtrat_tp = x[2] # Set a new thickness ratio for the transition piece
self.properties.Dtrat_mud = x[3] # Set a new thickness ratio for the mudline region of the monopile
self.UpdateAll() # Update the support structure based on the changes in variables above
eig = self.GetEigenFrequency() # Get the natural frequency
maxUtil = self.GetMaximumUtilisationFactor() # Get the maximum utilisation ratio on the structure (more than 1 means stress is higher than maximum allowed)
# Natural frequency of 0.25 and utilisation ratio of 1 are ideal
# Create some penalty...
penalty = (100000 * abs((eig - 0.25)))
penalty += (100000 * abs(maxUtil - 1))
return self.GetTotalMass() + penalty
提前致谢!
可以使用spicy.optimize的leastsq函数。
在我的例子中,它是用两个变量拟合指数函数:
def func_exp(p, x, z):
# exponential function with multiple parameters
a, b, c, d, t, t2 = p[0], p[1], p[2], p[3], p[4], p[5]
return a*np.exp(b + pow(x,c)*t + pow(z,d)*t2)
但是要使用 leastsq 函数,您需要创建一个误差函数,您将对此进行优化。
def err(p, x,z, y):
# error function compare the previous to the estimate to
return func_exp(p, x,z) - y
# minimise the residuals
使用它:
p0=[1e4,-1e-3,1,1,-1e-2, -1e-6]
# First parameters
pfit_fin, pcov, infodict, errmsg, success = leastsq(err, p0, args=(X,Y,Z), full_output=1, epsfcn=0.000001)
所以这是 return 生成结果的最佳参数:
Y_2= func_exp(pfit_fin, X,Y)
希望对你有所帮助,
克里斯。
通过折叠频率和压力约束作为对整体适应性的惩罚,可能最容易将其变成单值优化问题,例如
LOW_COST = 10.
MID_COST = 150.
HIGH_COST = 400.
def weight(a, b, c, d):
return "calculated weight of structure"
def frequency(a, b, c, d):
return "calculated resonant frequency"
def freq_penalty(freq):
# Example linear piecewise penalty function -
# increasing cost for frequencies below 205 or above 395
if freq < 205:
return MID_COST * (205 - freq)
elif freq < 395:
return 0.
else:
return MID_COST * (freq - 395)
def stress_fraction(a, b, c, d):
return "calculated stress / failure criteria"
def stress_penalty(stress_frac):
# Example linear piecewise penalty function -
# low extra cost for stress fraction below 0.85,
# high extra cost for stress fraction over 0.98
if stress_frac < 0.85:
return LOW_COST * (0.85 - stress_frac)
elif stress_frac < 0.98:
return 0.
else:
return HIGH_COST * (stress_frac - 0.98)
def overall_fitness(parameter_vector):
a, b, c, d = parameter_vector
return (
# D'oh! it took me a while to get this right -
# we want _minimum_ weight and _minimum_ penalty
# to get _maximum_ fitness.
-weight(a, b, c, d)
- freq_penalty(frequency(a, b, c, d))
- stress_penalty(stress_fraction(a, b, c, d)
)
...您当然会想找到更合适的惩罚函数并使用相对权重,但这应该给您一个大概的想法。然后你可以像
一样最大化它
from scipy.optimize import fmin
initial_guess = [29., 45., 8., 0.06]
result = fmin(lambda x: -overall_fitness(x), initial_guess, maxfun=100000, full_output=True)
(使用 lambda 让 fmin(最小化器)找到 overall_fitness 的最大值)。
或者,fmin
允许在每次迭代后应用回调函数;如果您知道如何适当地调整 a、b、c、d,您可以使用它来严格限制频率 - 类似于
def callback(x):
a, b, c, d = x # unpack parameter vector
freq = frequency(a, b, c, d)
if freq < 205:
# apply appropriate correction to put frequency back in bounds
return [a, b, c + 0.2, d]
elif freq < 395:
return x
else:
return [a, b, c - 0.2, d]
我目前正在研究一种确定风力涡轮机支撑结构成本的算法。我正在编写的算法需要优化初始输入支撑结构的重量,以便应力水平不会超过但接近所用材料特性的失效标准。另一个要求是结构的自然频率需要限制在 2 个值之间。要优化结构,可以更改 4 个变量。
我能否使用 Scipy.Optimize 库中的函数,使用多个设计参数优化该结构的重量,同时考虑支撑结构中的固有频率和最大应力值?
我正在优化的函数如下所示:
def func(self, x):
self.properties.D_mp = x[0] # Set a new diameter for the monopile
self.properties.Dtrat_tower = x[1] # Set a new thickness ratio for the tower
self.properties.Dtrat_tp = x[2] # Set a new thickness ratio for the transition piece
self.properties.Dtrat_mud = x[3] # Set a new thickness ratio for the mudline region of the monopile
self.UpdateAll() # Update the support structure based on the changes in variables above
eig = self.GetEigenFrequency() # Get the natural frequency
maxUtil = self.GetMaximumUtilisationFactor() # Get the maximum utilisation ratio on the structure (more than 1 means stress is higher than maximum allowed)
# Natural frequency of 0.25 and utilisation ratio of 1 are ideal
# Create some penalty...
penalty = (100000 * abs((eig - 0.25)))
penalty += (100000 * abs(maxUtil - 1))
return self.GetTotalMass() + penalty
提前致谢!
可以使用spicy.optimize的leastsq函数。
在我的例子中,它是用两个变量拟合指数函数:
def func_exp(p, x, z):
# exponential function with multiple parameters
a, b, c, d, t, t2 = p[0], p[1], p[2], p[3], p[4], p[5]
return a*np.exp(b + pow(x,c)*t + pow(z,d)*t2)
但是要使用 leastsq 函数,您需要创建一个误差函数,您将对此进行优化。
def err(p, x,z, y):
# error function compare the previous to the estimate to
return func_exp(p, x,z) - y
# minimise the residuals
使用它:
p0=[1e4,-1e-3,1,1,-1e-2, -1e-6]
# First parameters
pfit_fin, pcov, infodict, errmsg, success = leastsq(err, p0, args=(X,Y,Z), full_output=1, epsfcn=0.000001)
所以这是 return 生成结果的最佳参数:
Y_2= func_exp(pfit_fin, X,Y)
希望对你有所帮助,
克里斯。
通过折叠频率和压力约束作为对整体适应性的惩罚,可能最容易将其变成单值优化问题,例如
LOW_COST = 10.
MID_COST = 150.
HIGH_COST = 400.
def weight(a, b, c, d):
return "calculated weight of structure"
def frequency(a, b, c, d):
return "calculated resonant frequency"
def freq_penalty(freq):
# Example linear piecewise penalty function -
# increasing cost for frequencies below 205 or above 395
if freq < 205:
return MID_COST * (205 - freq)
elif freq < 395:
return 0.
else:
return MID_COST * (freq - 395)
def stress_fraction(a, b, c, d):
return "calculated stress / failure criteria"
def stress_penalty(stress_frac):
# Example linear piecewise penalty function -
# low extra cost for stress fraction below 0.85,
# high extra cost for stress fraction over 0.98
if stress_frac < 0.85:
return LOW_COST * (0.85 - stress_frac)
elif stress_frac < 0.98:
return 0.
else:
return HIGH_COST * (stress_frac - 0.98)
def overall_fitness(parameter_vector):
a, b, c, d = parameter_vector
return (
# D'oh! it took me a while to get this right -
# we want _minimum_ weight and _minimum_ penalty
# to get _maximum_ fitness.
-weight(a, b, c, d)
- freq_penalty(frequency(a, b, c, d))
- stress_penalty(stress_fraction(a, b, c, d)
)
...您当然会想找到更合适的惩罚函数并使用相对权重,但这应该给您一个大概的想法。然后你可以像
一样最大化它from scipy.optimize import fmin
initial_guess = [29., 45., 8., 0.06]
result = fmin(lambda x: -overall_fitness(x), initial_guess, maxfun=100000, full_output=True)
(使用 lambda 让 fmin(最小化器)找到 overall_fitness 的最大值)。
或者,fmin
允许在每次迭代后应用回调函数;如果您知道如何适当地调整 a、b、c、d,您可以使用它来严格限制频率 - 类似于
def callback(x):
a, b, c, d = x # unpack parameter vector
freq = frequency(a, b, c, d)
if freq < 205:
# apply appropriate correction to put frequency back in bounds
return [a, b, c + 0.2, d]
elif freq < 395:
return x
else:
return [a, b, c - 0.2, d]