'Kr': Kr, 'Lr': Lr, 'd': d, 'q': q, 'alphastar': alphastar, 'astar': astar, 'lattice': lattice, 'Hh': Hh, 'Kh': Kh, 'Lh': Lh } lowerm = [0, 0] upperm = [100, pi / 2] p0,jmin=anneal(chisq_an,p0,args=(Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr),\ schedule='simple',lower=lowerm,upper=upperm,\ maxeval=None, maxaccept=None,dwell=500,maxiter=6000,T0=None) dof = len(y) - len(p0) fake_dof = len(y) chimin = (cost_func(p0, Hr, Kr, Lr, d, q, alphastar, astar, lattice, Hh, Kh, Lh, y, yerr)**2).sum() chimin = chimin / dof if dof > 0 else chimin / fake_dof print 'p0_anneal', p0[0], N.degrees(p0[1]) print 'chi_anneal', chimin m = mpfit(myfunctlin, p0, parinfo=parinfo, functkw=fa) print 'status = ', m.status print 'params = ', m.params p1 = m.params covariance = m.covar
'Hr':Hr ,'Kr':Kr ,'Lr':Lr ,'d':d ,'q':q ,'alphastar':alphastar, 'astar':astar, 'lattice':lattice, 'Hh':Hh, 'Kh':Kh, 'Lh':Lh} lowerm=[0,0,0,0,0] upperm=[100,pi,pi/2,2*pi,pi] p0,jmin=anneal(chisq_an,p0,args=(Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr),\ schedule='simple',lower=lowerm,upper=upperm,\ maxeval=None, maxaccept=None,dwell=50,maxiter=60,T0=None) dof=len(y)-len(p0) fake_dof=len(y) chimin=(cost_func(p0,Hr,Kr,Lr,d,q,alphastar,astar,lattice,Hh,Kh,Lh,y,yerr)**2).sum() chimin=chimin/dof if dof>0 else chimin/fake_dof print 'p0_anneal',p0[0],N.degrees(p0[1:]) print 'chi_anneal', chimin m = mpfit(myfunctlin, p0, parinfo=parinfo,functkw=fa) print 'status = ', m.status print 'params = ', m.params p1=m.params
#scale by nuclear factor y = y / A yerr = yerr / A if 1: print 'annealing' #myschedule='fast' myschedule = 'simple' lowerm = 1e-4 * N.ones(len(pfit)) #lowerm[0:3]=[-1,-1,-1] #upperm=N.ones(len(pfit)) myargs = (Qlist, y, yerr, corrections) upperm = np.array( [1.0, 2.0, pi, pi, pi, pi, 2 * pi, 2 * pi, 2 * pi, 2 * pi]) pout,jmin=anneal(chisq_an,pfit,args=myargs,\ schedule=myschedule,lower=lowerm,upper=upperm,\ maxeval=1000, maxaccept=None,dwell=2000,maxiter=120,feps=1e-2,full_output = 0) print 'annealed', pout pfit = pout parbase = { 'value': 0., 'fixed': 0, 'limited': [0, 0], 'limits': [0., 0.] } parinfo = [] for i in range(len(pfit)): parinfo.append(copy.deepcopy(parbase)) for i in range(len(pfit)):
print 'fm',fm #scale by nuclear factor y=y/A yerr=yerr/A if 0: print 'annealing' #myschedule='fast' myschedule='simple' lowerm=1e-4*N.ones(len(pfit)) #lowerm[0:3]=[-1,-1,-1] #upperm=N.ones(len(pfit)) myargs=(Qlist,y,yerr,corrections) upperm=np.array([1000.0]) pout,jmin=anneal(chisq_an,pfit,args=myargs,\ schedule=myschedule,lower=lowerm,upper=upperm,\ maxeval=1000, maxaccept=None,dwell=2000,maxiter=120,feps=1e-2,full_output = 0) print 'annealed',pout pfit=pout parbase={'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]} parinfo=[] for i in range(len(pfit)): parinfo.append(copy.deepcopy(parbase)) for i in range(len(pfit)): parinfo[i]['value']=pfit[i] fa = {'x':Qlist, 'y':y, 'err':yerr, 'correction':corrections} m = mpfit(myfunctlin, pfit, parinfo=parinfo,functkw=fa)
if 0: scipy.optimize.optimize.fmin_l_bfgs_b(max_wrap, p0, fprime = None, args=h_args, approx_grad = 0, \ bounds = None, m = 10, factr = 10000000.0, \ pgtol = 1.0000000000000001e-05, epsilon = 1e-08, iprint = -Const(1), maxfun = 15) if 0: print 'fmin' pout=scipy.optimize.optimize.fmin(max_wrap,p0,maxiter = 5, maxfun = 100,disp=1,args=h_args) if 0: print 'annealing' myschedule='fast' #myschedule='simple' pout,jmin=anneal(max_wrap,p0,args=h_args,\ schedule=myschedule,lower=lowerm,upper=upperm,\ maxeval=100, maxaccept=None,dwell=20,maxiter=20,feps=1e-1,full_output = 0) if 0: multipliers=pout[0:3] print 'multipliers',multipliers pout=pout[3::] if 0: myfilestr=r'c:\structfactors_density.dat' x2,y2,z2=N.loadtxt(myfilestr).T P2=z2.reshape((Mx,Mz)) #P2=P2/P2.max() fq_density=calc_model(P2,h,k,l,fq,fqerr,x,z,cosmat_list,coslist,flist) X3,Z3,P3=plotdensity(h,k,l,fq)
def getmatrix(sx,sy,sz,mytol=1e-45,maxiter=2): print "(sx, sy, sz) = ", (sx, sy, sz) p0=N.array([0,1,0,1],'d') p0=N.array([0,0,0,1],'d') #p0=N.array([0,1,0,1],'d') lowerm=[-2,-2,-2,-2] upperm=[2,2,2,2] lowerm=[-1,-1,-1,-1] upperm=[1,1,1,1] flag=True smat=N.empty((3,1),'float64') smat=N.matrix(smat) smat[0]=0 smat[1]=0 smat[2]=1 iter=0 pbest=p0 currbest=chisq_an(p0,sx,sy,sz) while flag: print 'iter',iter p0,jmin=anneal(chisq_an,p0,args=(sx,sy,sz),\ schedule='simple',lower=lowerm,upper=upperm,\ maxeval=None, maxaccept=None,dwell=10,maxiter=600,T0=10000) print 'p0',p0 p = NLSP(chisq, p0,xtol=1e-37) #p = NLP(chisq_an, p0,xtol=1e-17) p.args.f=(sx,sy,sz) p.iprint = 10 p.ftol=1e-30 p.maxFunEvals = 1e10 #p.checkdf() r = p.solve('nssolve') #r = p.solve('scipy_fsolve') #r = p.solve('nlp:ralg') #r = p.solve('ralg') #r = p.solve('scipy_cobyla') print 'solution:', r.xf a,b,c,s=r.xf if 1: p0=[a,b,c,s] p=scipy.optimize.minpack.fsolve(chisq_f,p0,args=(sx,sy,sz),xtol=1e-37) a,b,c,s=p print '2nd soln',p amat=genmat(a,b,c,s) if 1: array_err=1e-2 for i in range(3): for j in range(3): if N.absolute(amat[i,j])<array_err: amat[i,j]=0 sout=amat*smat curr_score=chisq_an(p0,sx,sy,sz) if curr_score<currbest: currbest=curr_score pbest=p if (N.abs(sout[0]-sx)<mytol and N.abs(sout[1]-sy)<mytol and N.abs(sout[2]-sz)<mytol) or iter>maxiter: flag=False iter=iter+1 a,b,c,s=pbest amat=genmat(a,b,c,s) if 1: array_err=1e-5 for i in range(3): for j in range(3): if N.absolute(amat[i,j])<array_err: amat[i,j]=0 #print 'amat',amat print "AMAT\n\n\nAMAT: \n", amat, "\nSMAT\n" , smat, "\nAMAT * SMAT\n\n", amat*smat, "\nsx, sy, sz\n", (sx,sy,sz) return amat
pgtol = 1.0000000000000001e-05, epsilon = 1e-08, iprint = -Const(1), maxfun = 15) if 0: print 'fmin' pout = scipy.optimize.optimize.fmin(max_wrap, p0, maxiter=5, maxfun=100, disp=1, args=h_args) if 0: print 'annealing' myschedule = 'fast' #myschedule='simple' pout,jmin=anneal(max_wrap,p0,args=h_args,\ schedule=myschedule,lower=lowerm,upper=upperm,\ maxeval=100, maxaccept=None,dwell=20,maxiter=20,feps=1e-1,full_output = 0) if 0: multipliers = pout[0:3] print 'multipliers', multipliers pout = pout[3::] if 0: myfilestr = r'c:\structfactors_density.dat' x2, y2, z2 = N.loadtxt(myfilestr).T P2 = z2.reshape((Mx, Mz)) #P2=P2/P2.max() fq_density = calc_model(P2, h, k, l, fq, fqerr, x, z, cosmat_list, coslist, flist)
def getmatrix(sx, sy, sz, mytol=1e-45, maxiter=2): print "(sx, sy, sz) = ", (sx, sy, sz) p0 = N.array([0, 1, 0, 1], 'd') p0 = N.array([0, 0, 0, 1], 'd') #p0=N.array([0,1,0,1],'d') lowerm = [-2, -2, -2, -2] upperm = [2, 2, 2, 2] lowerm = [-1, -1, -1, -1] upperm = [1, 1, 1, 1] flag = True smat = N.empty((3, 1), 'float64') smat = N.matrix(smat) smat[0] = 0 smat[1] = 0 smat[2] = 1 iter = 0 pbest = p0 currbest = chisq_an(p0, sx, sy, sz) while flag: print 'iter', iter p0,jmin=anneal(chisq_an,p0,args=(sx,sy,sz),\ schedule='simple',lower=lowerm,upper=upperm,\ maxeval=None, maxaccept=None,dwell=10,maxiter=600,T0=10000) print 'p0', p0 p = NLSP(chisq, p0, xtol=1e-37) #p = NLP(chisq_an, p0,xtol=1e-17) p.args.f = (sx, sy, sz) p.iprint = 10 p.ftol = 1e-30 p.maxFunEvals = 1e10 #p.checkdf() r = p.solve('nssolve') #r = p.solve('scipy_fsolve') #r = p.solve('nlp:ralg') #r = p.solve('ralg') #r = p.solve('scipy_cobyla') print 'solution:', r.xf a, b, c, s = r.xf if 1: p0 = [a, b, c, s] p = scipy.optimize.minpack.fsolve(chisq_f, p0, args=(sx, sy, sz), xtol=1e-37) a, b, c, s = p print '2nd soln', p amat = genmat(a, b, c, s) if 1: array_err = 1e-2 for i in range(3): for j in range(3): if N.absolute(amat[i, j]) < array_err: amat[i, j] = 0 sout = amat * smat curr_score = chisq_an(p0, sx, sy, sz) if curr_score < currbest: currbest = curr_score pbest = p if (N.abs(sout[0] - sx) < mytol and N.abs(sout[1] - sy) < mytol and N.abs(sout[2] - sz) < mytol) or iter > maxiter: flag = False iter = iter + 1 a, b, c, s = pbest amat = genmat(a, b, c, s) if 1: array_err = 1e-5 for i in range(3): for j in range(3): if N.absolute(amat[i, j]) < array_err: amat[i, j] = 0 #print 'amat',amat print "AMAT\n\n\nAMAT: \n", amat, "\nSMAT\n", smat, "\nAMAT * SMAT\n\n", amat * smat, "\nsx, sy, sz\n", ( sx, sy, sz) return amat