python - Complex non-linear function minimization with constraints using scipy.optimize.minimize -
this first ever post on stack - after week of scratching head figure it's time ask internet.
i'm trying minimize output of complex python function, takes in 3 (or more) coefficients polynomial, , returns slope-squared of plot should, in ideal world, represent flat line.
the function not mathematical - uses supplied polynomial modify (by multiplication) astronomical filter throughput curve (e.g. https://www.sdss3.org/images/camera_filters.jpg), used along model stellar atmospheres calculate bolometric corrections, , plot difference between calculated , observed astronomical magnitude (dm), function of colour (g-r), bunch of stars.
essentially, function "getslope()" black box, takes around 12-seconds compute output slope given input polynomial.
i've been using scipy.optimize.minimize minimize function , find polynomial parameters return flattest plot. unfortunately, flattest plot seems produced when polynomial returning negative values during range of interest. produces fudged filter throughput curve @ times below zero, not unphysical screws of black box calculations.
i thought use cobyla inequality constraints find minimum doesn't use negative polynomial evaluations, doesn't seem helping.
using function isvalid(), return +1 when polynomial ok within given limits, , -1 when returning negative values. e.g:
def isvalid(poly): p=polynomial(poly) x=np.linspace(startwl,endwl,2000) y=p(x) if np.any(y<0): print "returning minus one" return -1 else: return 1
i set constraint evaluate function "isvalid()".
poly = [1,0.0001,0.000000001] # polynomial parameters a1 + a2*x + a3*x^2 cons = ({'type':'ineq','fun':isvalid}) res = minimize(getslope,poly,method='cobyla',constraints=cons,tol=1e-11) print res
i had hoped find minimum within constraint limits, i.e. polynomial never evaluated negative during interval in interested. however, seems find minimum outside of constraints, , tell me such. i.e:
1.97336588518e-07 returning minus 1 2.16503874744e-06 returning minus 1 2.40318933164e-07 returning minus 1 status: 4 nfev: 80 maxcv: 1.0 success: false fun: 2.4031893316449186e-07 x: array([ 9.96886224e-01, 2.59958641e-01, -6.85767425e-05]) message: 'did not converge solution satisfying constraints. see `maxcv` magnitude of violation.'
this doesn't seem particularly helpful! can tell solution outside of constraints. implementing constraint process correctly? is there way force minimization function find best minimum within constraints, if there's lower minimum outside of constraints? i'm no expert statistics or programming, i'm feeling rather lost. , tips appreciated!
Comments
Post a Comment