如何在数值数据集上迭代地使用 Scipy 优化最小化

问题描述

问题陈述

我有一个工程计算机模型,我手动将长度参数放入其中并得出应力结果。我想优化参数,以便:

  • 所有长度参数都是非负的
  • 所有压力结果均为 208
  • 长度参数“Param-Tw”> 45
  • 找出满足上述目标的最小参数

我设想的工作流程是:

  • 从计算机模拟中获取一些初始数据,每个参数可能至少有两个不同的值
  • 使用 Scipy optimize.minimize 获得参数的下一个最佳猜测
  • 手动将这些参数输入到计算机模拟中,然后将结果输入回 Scipy optimize.minimize 包以获得下一个最佳猜测。

示例数据集

我收集的数据,其中我改变每个输入参数“Param”两次并得到结果“Res”是:

参数-Tft 参数-Tw 参数-Tfb Res-TopStress Res-SideStress Res-BotStress
110 60 30 151.4 167.5 193.6
100 60 30 162.1 172.2 190.6
100 50 30 169.2 197.8 193.8
100 50 25 173.6 197.5 223.1

尝试使用 Scipy optmize.minimize

根据我在 pyfmi 包如何使用它 (Section 4.4) 中所读到的内容,我需要一个无衍生方法,因此认的 Nelder-Mead 方法应该可以工作。然而,因为我有不等式,所以我开始使用 SLSQP 方法This question 很有见地,但并不全面。我对如何实现这一点的最佳解释如下。我仍然需要弄清楚如何:

  1. 把我的目标变成一个函数
  2. 正确描述我对 SLSQP 方法的约束,
  3. 利用上表中的初始数据获得下一个最佳猜测。
    import pandas as pd
from scipy import optimize

# Import values
stress_df = pd.read_excel('test_data.xls')      # same as data above

# Objective to minimise
def function (Param_Tft,Param_Tw,Param_Tfb):
        """ This is manually entered each time by the user after inputting to the computer model """
        print(f'Run computer simulation with Param_Tft={Param_Tft},Param_Tw={Param_Tw},Param_Tfb={Param_Tfb}:\n')
        Res_TopStress = input('Res_TopStress:')
        Res_SideStres = input('Res_SideStress:')
        Res_BotStress = input('Res_BotStress:')
        # Goal is to aim for zero in the below equation
        return Res_TopStress-208 + Res_SideStres-208 + Res_BotStress-208

# Constraints (i.e. stress must equal 208,Param-Tw > 45)
cons = ({'type': 'ineq','fun': stress_df['Res_TopStress'] - 208},# This is probaby a double up of the above function
        {'type': 'ineq','fun': stress_df['Res_SideStress'] - 208},{'type': 'ineq','fun': stress_df['Res_BotStress'] - 208},'fun': stress_df['Param_Tw'] > 45})

# Non negativity contraints  (i.e. for each of three Params)
bnds = ((0,None),(0,None))

# Best initial guesses (i.e. for Param-Tft,Param-Tw,Param-Tfb)
Param_0 = [90,45,23]

# The optimisation function
res = optimize.minimize(function,Param_0,method='SLSQP',bounds=bnds,constraints=cons)

# Print result
print(res)

我应该如何修改我的代码示例以将我的问题陈述正确地放入代码中?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)