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

2024-06-07 23:12:03 发布

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

问题陈述

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

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

我设想的工作流程是:

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

示例数据集

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

^{tb1}$

尝试使用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},
        {'type': 'ineq', 'fun': stress_df['Param_Tw'] > 45})

# Non negativity contraints  (i.e. for each of three Params)
bnds = ((0, None), (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)

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


Tags: the数据方法df参数paramfunctionres

热门问题