1 回答

TA贡献1821条经验 获得超6个赞
因为我不知道你的代码到底在做什么,所以我无法判断下面的代码是否正确。但只是为了指导如何使用scipy.optimize.minimize
函数,它应该类似于以下内容:
import numpy as np
import pandas as pd
from scipy.optimize import minimize
df = pd.read_csv('simulation_df_data_2018_2.csv')
for i in df.index:
# Since you said "alpha" and "beta" are the variable of "holts" function
# I changed all "alpha" and "beta" to "x[0]" and "x[1]" respectively.
def holts(x):
LO = np.int(df['average_demand'].loc[i])
TO = ((df['m2'].loc[i] - df['m3'].loc[i]) + (df['m1'].loc[i] - df['m2'].loc[i])) / 2
L1 = x[0] * df['m3'].loc[i] + (1 - x[0]) * (LO + TO)
T1 = x[1] * (L1 - LO) + (1 - x[1]) * TO
L2 = x[0] * df['m2'].loc[i] + (1 - x[0]) * (L1 + T1)
T2 = x[1] * (L2 - L1) + (1 - x[1]) * T1
L3 = x[0] * df['m1'].loc[i] + (1 - x[0]) * (L2 + T2)
MSE = ((df['m3'].loc[i] - L1) + (df['m2'].loc[i] - L2) + (df['m2'].loc[i] - L3)) ** 2 / 3
return MSE
x0 = np.array([0.1, 0.1]) # initial guess for the points "x" which "holts(x)" attains its minimum.
result = minimize(holts, x0, bounds=[(0, 1), (0, 1)], method="SLSQP", options={'disp': True})
print(result)
print()
哪个打印
Optimization terminated successfully (Exit mode 0)
Current function value: 16.921875002444228
Iterations: 5
Function evaluations: 15
Gradient evaluations: 5
fun: 16.921875002444228
jac: array([ 8.06808472e-04, -6.23427415e+00])
message: 'Optimization terminated successfully'
nfev: 15
nit: 5
njev: 5
status: 0
success: True
x: array([0.75000606, 1. ])
Optimization terminated successfully (Exit mode 0)
Current function value: 3.0792324286388828e-09
Iterations: 7
Function evaluations: 24
Gradient evaluations: 7
fun: 3.0792324286388828e-09
jac: array([-0.00833083, -0.00081578])
message: 'Optimization terminated successfully'
nfev: 24
nit: 7
njev: 7
status: 0
success: True
x: array([0.1495232 , 0.25665903])
Optimization terminated successfully (Exit mode 0)
Current function value: 1.9951658230345873e-10
Iterations: 9
Function evaluations: 32
Gradient evaluations: 9
fun: 1.9951658230345873e-10
jac: array([0.03791677, 0.00858221])
message: 'Optimization terminated successfully'
nfev: 32
nit: 9
njev: 9
status: 0
success: True
x: array([0.37155192, 0.19218713])
Optimization terminated successfully (Exit mode 0)
Current function value: 114.53090716252409
Iterations: 11
Function evaluations: 37
Gradient evaluations: 11
fun: 114.53090716252409
jac: array([ 3.06129456e-04, -3.04457455e+01])
message: 'Optimization terminated successfully'
nfev: 37
nit: 11
njev: 11
status: 0
success: True
x: array([0.7171298, 1. ])
Optimization terminated successfully (Exit mode 0)
Current function value: 33.333333333333336
Iterations: 2
Function evaluations: 6
Gradient evaluations: 2
fun: 33.333333333333336
jac: array([-403.33331966, -0. ])
message: 'Optimization terminated successfully'
nfev: 6
nit: 2
njev: 2
status: 0
success: True
x: array([1., 1.])
更新:似乎函数是函数不起作用的round原因,因为函数会在函数图上创建许多高原。我删除了功能并更新了打印的结果。minimizeroundholtsround
添加回答
举报