Topic: OpenOpt does not give consistant results
I have tried to use OpenOpt but I have some troubles because it does not always give the same result even though the input is exactly the same. Is there some kind of random initial guess build into the optimizer or am I missing something.
Below I have an example
from matplotlib.pylab import * from openopt import NLP import numpy as np from numpy import linspace from scipy import interpolate def residual(p, r, y): tck = interpolate.splrep(np.concatenate(([r], p[0:len(p)/2], [r[-1]])), np.concatenate(([y], p[len(p)/2::], [y[-1]]))) yFit = interpolate.splev(r, tck) resid = sum((y-yFit)**2) return resid def FitDistribution(r, y): rP = linspace(r, r[-1], 12) yP = np.interp(rP, r, y) p0 = np.concatenate((rP[1:-1], yP[1:-1])) # form box-bound constraints lb <= x <= ub lb = np.concatenate((np.zeros(len(rP)-2), np.ones(len(rP)-2)*(-np.inf))) # lower bound ub = np.concatenate((np.ones(len(rP)-2)*r[-1], np.ones(len(rP)-2)*(np.inf))) # upper bound ftol = 10e-5 f = lambda p: residual(p, r, y) p = NLP(f, p0, lb=lb, ub=ub, maxIter=5500, iprint=20, ftol=ftol) solution = p.solve('ralg', plot = False) pvec = solution.xf tck = interpolate.splrep(np.concatenate(([r], pvec[0:len(pvec)/2], [r[-1]])), np.concatenate(([y], pvec[len(pvec)/2::], [y[-1]]))) yFit = interpolate.splev(r, tck) return yFit r = linspace(1,100) y = r**2 yFit = FitDistribution(r, y) plot(r, y, label = 'func') plot(r, yFit, label = 'fit') legend(loc=0) grid() show()
In this case as you can see in the example I am trying to represent a curve with a cubic spline. As input to the spline I have 12 points – two end points and 10 inner points. I am trying to optimize these 10 inner points (both in x and y direction) in order to represent the curve as good as possible with this spline. But as said it does not always ends up with the same result. Sometimes you can run the code several times with the same result but suddenly the result differ.
What can be the reason for this?