Having problems with the Scipy Minimize function, it gives me odd results

Target function created
Restrictions added

The problem is what initial premise I use, minimization functions just keep using that number. for example: if I use 15 for the initial guess, then the solver will not try another number and say that the answer is 15. I'm sure the problem is with the code, but I'm not sure where.

CODE BELOW:

from scipy.optimize import minimize import numpy as np from pandas import * #---------------------------------------------------- #-------- Create Function ------------ #---------------------------------------------------- def MovingAverage(Input,N,test=0): # Create data frame df = DataFrame(Input, columns=['Revenue']) # Add columns df['CummSum'] = df['Revenue'].cumsum() df['Mavg'] = rolling_mean(df['Revenue'], N) df['Error'] = df['Revenue'] - df['Mavg'] df['MFE'] = (df['Error']).mean() df['MAD'] = np.fabs(df['Error']).mean() df['MSE'] = np.sqrt(np.square(df['Error']).mean()) df['TS'] = np.sum(df['Error'])/df['MAD'] print N, df.MAD[0] if test == 0: return df.MAD[0] else: return df #---------------------------------------------------- #-------- Input ------------ #---------------------------------------------------- data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5] #---------------------------------------------------- #-------- SOLVER ------------ #---------------------------------------------------- ## Objective Function fun = lambda x: MovingAverage(data, x[0]) ## Contraints cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2}, # N>=2 {'type': 'ineq', 'fun': lambda x: len(data) - x[0]}) # N<=len(data) ## Bounds (note sure what this is yet) bnds = (None,None) ## Solver res = minimize(fun, 15, method='SLSQP', bounds=bnds, constraints=cons) ##print res ##print res.status ##print res.success ##print res.njev ##print res.nfev ##print res.fun ##for i in res.x: ## print i ##print res.message ##for i in res.jac: ## print i ##print res.nit # print final results result = MovingAverage(data,res.x,1) print result 

List of possible values:
2 = 0.142857142857,
3 = 0.25641025641,
4 = 0.333333333333333,
5 = 0.363636363636,
6 = 0.333333333333333,
7 = 0.31746031746,
8 = 0.3125,
9 = 0.31746031746,
10 = 0.333333333333333,
11 = 0.363636363636,
12 = 0.416666666667,
13 = 0.487179487179,
14 = 0.571428571429,
15 = 0.666666666667

+4
source share
1 answer

Your function is piecewise constant between integer input values, as shown in the figure below (plotted in increments of 0.1 along the x axis):

function plot

So, the derivative is equal to zero at almost all points and therefore the gradient-based minimization method will return any given starting point as a local minimum.

To save the situation, you might consider using interpolation in the objective function to obtain the values โ€‹โ€‹of the intermediate functions for non-integer input values. If you combine this with gradient-based minimization, it can find a point around 8 as a local minimum when starting from 15.

+7
source

Source: https://habr.com/ru/post/1445847/


All Articles