def Fit(self, obs, weights, estimatetau, metaheuristic): """Fit a Gaussian to the state distributions after observing the data. Parameters ----------- obs : ndarray Observation sequence. weights : ndarray The weights attached to each state (posterior distribution). estimatetau : bool Find optimal tau's Yes/No metaheuristic : {'local', 'sa', 'genetic'}, optional The meta-heuristic to be used to solve the objective in the M-step. 'local' is simple local search. 'genetic' is genetic algorithm and 'sa' is simulated annealing. Notes -------- Estimation of the tau parameters is done every alternate iteration of the QDHMM EM algorithm. Call viz.plotcontour only if you wish to view the search propogation. Recommended for search in a small space. """ if estimatetau: if metaheuristic=='genetic': t=time.time() genetic, history=optim.geneticalgorithm(self, obs, 20, weights) print 'tau: ', genetic print 'genetic search time:', time.time()-t self.tau = genetic elif metaheuristic=='local': t=time.time() local, history = optim.localsearch(self, obs, 1, 20, weights) print 'tau: ', local print 'local search time:', time.time()-t self.tau = local elif metaheuristic=='sa': t=time.time() aneal, history= optim.simulated_annealing(self, weights, obs, 20) print 'aneal search time:', time.time()-t print 'tau: ',aneal print 'Scipy anneal' pdb.set_trace() print anneal(optim.new_objective, self.tau, (self.K, obs, weights), schedule='cauchy', T0=10000, Tf=0.0001) self.tau = aneal #viz.plotcontour(self.K, self.tau, weights, obs, history, str(metaheuristic+str(iteration))) normalizer=np.zeros((self.K, weights.shape[1])) x = np.array(xrange(weights.shape[0])) state = np.digitize(x, self.tau, right=True) for k in xrange(self.K): normalizer[k,:] = (np.sum(weights[state==k],0) / np.sum(np.sum(weights[state==k],0)[np.newaxis,:], axis = 1)[:, np.newaxis]) self.mu[:,k] = np.dot( normalizer[k,:] , obs.T) obs_bar = obs - self.mu[:,k][:,np.newaxis] self.var[k,:,:] = np.dot( normalizer[k,:]*obs_bar , obs_bar.T)
def optimize(zscores, resolution, values): zscvals = sorted(reduce(lambda x, y: x+y, [zscores[z].values() for z in zscores])) # lower bound must be higher than percentil 10 of zscores lzsc = zscvals[int(len(zscvals)*0.1)] # upper bound must be lower than percentil 90 of zscores uzsc = zscvals[int(len(zscvals)*0.9)] # print [(0.,uzsc),(lzsc,0.),(400, 1500)] # print anneal(to_optimize, (uzsc/2, lzsc/2, 700), args=(zscores, resolution, values, 500, 100, 8), lower=(0, lzsc, 400), upper=(uzsc, 0, 2000), full_output=True)
def optimize(zscores, resolution, values): zscvals = sorted( reduce(lambda x, y: x + y, [zscores[z].values() for z in zscores])) # lower bound must be higher than percentil 10 of zscores lzsc = zscvals[int(len(zscvals) * 0.1)] # upper bound must be lower than percentil 90 of zscores uzsc = zscvals[int(len(zscvals) * 0.9)] # print[(0., uzsc), (lzsc, 0.), (400, 1500)] # print anneal(to_optimize, (uzsc / 2, lzsc / 2, 700), args=(zscores, resolution, values, 500, 100, 8), lower=(0, lzsc, 400), upper=(uzsc, 0, 2000), full_output=True)
def run_simulated_annealing(CPVs, OFE_budgets, randomSeed): dwell = int(CPVs[0]) #equibavent to population size in evolutionary algorithms func = evaluation_history_recording_wrapper( Ros_ND, dwell, solution_valid ) optimize.anneal(func, x0 = -0.5 * numpy.random.rand(5), #x0 = -2.048 + 2*2.048*numpy.random.rand(10), #if used make sure tMOPSO sample size greater than 100 m = CPVs[1], T0 = 500.0, lower= -2.048, upper= 2.048, dwell=dwell, maxeval = max( OFE_budgets ), #termination criteria feps = 0.0, Tf = 0.0) return get_F_vals_at_specified_OFE_budgets(F=func.f_hist, E=func.OFE_hist, E_desired=OFE_budgets)
def fmin_anneal(f, x0, iter=100,callback=None): """Calls scipy simulated annealing optimizer (unconstrained). """ try: from scipy import optimize except ImportError: raise "SciPy is not installed, SciPy optimizers are not available" global bestsol, it bestsol = (x0, f(x0)) it = 0 def myf(x): global it, bestsol it += 1 r= f(x) if r < bestsol[1]: bestsol = (x, r) print "%d:"%it,r return r M=1.e7 M = 5 min=[-M]*len(x0) max=[M]*len(x0) x, retval = optimize.anneal(myf, x0, lower = min, upper = max, maxiter = iter, schedule = "fast") print "retval", retval return x
def fitAnneal(x, y, guess, bounds=None, risePower=1.0): def errFn(v, x, y): err = y - v[0] * pspInnerFunc(x-v[1], v[2], v[3], risePower) return (err**2).sum() fit = opt.anneal(errFn, guess, args=(x, y))[0] return fit
def findOptimum(cogDict, cogWeightDictList, taxDist): """ Finds values of cogReg, genReg, mixReg achieving maximum correlation between cogDist and taxDist :return: None """ lowerBounds = [COG_REG_LOWER, -7., 0.12] upperBounds = [COG_REG_LOWER + COG_REG_STEP * COG_REG_STEP_COUNT, -1.0, 0.26] cycleCount = 0 while True: initParamVector = bestParamVector for i in range(len(lowerBounds)): lowerBounds[i] += (bestParamVector[i] - lowerBounds[i]) * 0.2 upperBounds[i] += (bestParamVector[i] - upperBounds[i]) * 0.2 cycleCount += 1 print "CYCLE ", cycleCount print "initParamVector ", initParamVector print "BOUNDS ", lowerBounds, upperBounds func = UtilCaller(optimizingFunction, taxDist, cogDict, cogWeightDictList, lowerBounds, upperBounds) result = anneal(func, initParamVector, schedule='boltzmann', full_output=True, maxiter=80, lower=lowerBounds, upper=upperBounds, disp=True) print result
def anneal_schedule(self, schedule='fast', use_wrapper=False): """ Call anneal algorithm using specified schedule """ n = 0 # index of test function if use_wrapper: opts = { 'upper': self.upper[n], 'lower': self.lower[n], 'ftol': 1e-3, 'maxiter': self.maxiter, 'schedule': schedule, 'disp': False } x, info = minimize(self.fun[n], self.x0[n], method='anneal', options=opts, full_output=True) retval = info['status'] else: x, retval = anneal(self.fun[n], self.x0[n], full_output=False, upper=self.upper[n], lower=self.lower[n], feps=1e-3, maxiter=self.maxiter, schedule=schedule, disp=False) assert_almost_equal(x, self.sol[n], 2) return retval
def __call__(self, inputs,states,refs): if self.a0 == None: a0 = np.random.random((3,3))*2-1 if self.b0 == None: b0 = np.random.random((3,3))*2-1 if self.z0 == None: z0 = np.random.random()*2-1 params = np.array(list(a0.flatten())+list(b0.flatten())+[z0]) print "Initial parameters:", params assert params.shape[0] == 19 #TODO: assert all arrays have same dimensions # Do optimization if self.opt_method == "anneal": #Do the optimization 100 times par, fopt, it, fcalls, t5, t6, t7 = anneal(self.error,params, lower=-2, upper=2, full_output=True) elif self.opt_method == "simplex": #Do the optimization 100 times #best_fopt = 10e50 #for i in range(2): # print "\nStep", i # params = np.random.random((19))*0.2-0.1 # print "Initial guess", params # self.do_pp = True #par_tmp, ftol, it, fcalls,wf, allv_tmp = fmin(self.error,params,xtol=-1,ftol=-1,maxiter=2000,maxfun=10e10,full_output=True,retall=True) par, fopt, it, fcalls,wf, allv_tmp = fmin(self.error,params,maxiter=2000,maxfun=10e10,full_output=True,retall=True) # print fopt,# par_tmp # print "Final:", par_tmp # print "Veränderungen:", par_tmp-params # if fopt < best_fopt: # best_fopt=fopt # par=par_tmp # allv = allv_tmp # print "besser!", #par, ftol, it, fcalls,wf, allv = fmin(self.error,params,xtol=-1,ftol=-1,maxiter=2000,maxfun=10e10,full_output=True,retall=True) #print "params:", params #par, ftol, it, fcalls,wf, allv = fmin(self.error,params,full_output=True,retall=True) #print par, ftol, it, fcalls, wf, allv #print np.array(allv).shape #allv = np.array(allv) #p.figure(3) #for i in range(allv.shape[1]): # p.plot(allv[:,i]) #p.show() #time.sleep(3) elif self.opt_method == "brute": par = brute(self.error,[slice(-2,2,2j) for i in range(19)]) print par #par = par[0] elif self.opt_method == "powell": par = fmin_powell(self.error,params) print "Veränderungen:", par-params else: raise ValueError("Optimization method not known") par = np.array(par) return par[:9].reshape((3,3)), par[9:18].reshape((3,3)), par[18]
def anneal_schedule(self, schedule="fast", use_wrapper=False): """ Call anneal algorithm using specified schedule """ n = 0 # index of test function if use_wrapper: opts = { "upper": self.upper[n], "lower": self.lower[n], "ftol": 1e-3, "maxiter": self.maxiter, "schedule": schedule, "disp": False, } with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) res = minimize(self.fun[n], self.x0[n], method="anneal", options=opts) x, retval = res["x"], res["status"] else: with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) x, retval = anneal( self.fun[n], self.x0[n], full_output=False, upper=self.upper[n], lower=self.lower[n], feps=1e-3, maxiter=self.maxiter, schedule=schedule, disp=False, ) assert_almost_equal(x, self.sol[n], 2) return retval
def scipyAnneal(function, params): # result is the set of theta parameters which optimize the likelihood given x, y, yerr result = op.anneal(function, params.ravel()) params = result["x"] return params, result
def anneal(self, maxiter=1000000): self.param = [self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3] result = anneal(self.residu2, self.param, args=(self.data[:, 0], self.data[:, 1], self.data[:, 2]), lower=[self._dist_min, self._poni1_min, self._poni2_min, self._rot1_min, self._rot2_min, self._rot3_min], upper=[self._dist_max, self._poni1_max, self._poni2_max, self._rot1_max, self._rot2_max, self._rot3_max], maxiter=maxiter) new_param = result[0] oldDeltaSq = self.chi2() / self.data.shape[0] newDeltaSq = self.chi2(new_param) / self.data.shape[0] logger.info("Anneal %s --> %s", oldDeltaSq, newDeltaSq) if newDeltaSq < oldDeltaSq: i = abs(self.param - new_param).argmax() d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"] logger.info("maxdelta on %s : %s --> %s ", d[i], self.param[i], new_param[i]) self.set_param(new_param) return newDeltaSq else: return oldDeltaSq
def fmin_anneal(f, x0, iter=100, callback=None): """Calls scipy simulated annealing optimizer (unconstrained). """ try: from scipy import optimize except ImportError: raise "SciPy is not installed, SciPy optimizers are not available" global bestsol, it bestsol = (x0, f(x0)) it = 0 def myf(x): global it, bestsol it += 1 r = f(x) if r < bestsol[1]: bestsol = (x, r) print "%d:" % it, r return r M = 1.e7 M = 5 min = [-M] * len(x0) max = [M] * len(x0) x, retval = optimize.anneal(myf, x0, lower=min, upper=max, maxiter=iter, schedule="fast") print "retval", retval return x
def fitAnneal(x, y, guess, bounds=None, risePower=1.0): def errFn(v, x, y): err = y - v[0] * pspInnerFunc(x - v[1], v[2], v[3], risePower) return (err**2).sum() fit = opt.anneal(errFn, guess, args=(x, y))[0] return fit
def anneal_schedule(self, schedule='fast', use_wrapper=False): """ Call anneal algorithm using specified schedule """ n = 0 # index of test function if use_wrapper: opts = {'upper': self.upper[n], 'lower': self.lower[n], 'ftol': 1e-3, 'maxiter': self.maxiter, 'schedule': schedule, 'disp': False} with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) res = minimize(self.fun[n], self.x0[n], method='anneal', options=opts) x, retval = res['x'], res['status'] else: with warnings.catch_warnings(): warnings.simplefilter("ignore", DeprecationWarning) x, retval = anneal(self.fun[n], self.x0[n], full_output=False, upper=self.upper[n], lower=self.lower[n], feps=1e-3, maxiter=self.maxiter, schedule=schedule, disp=False) assert_almost_equal(x, self.sol[n], 2) return retval
def optimize_anneal(self, **kwargs): self.set_parameters_from_array( anneal(self.get_cost_function, self.par.get_array(), lower=self.par.get_lower_bounds(), upper=self.par.get_upper_bounds(), **kwargs)) return self.par
def run_simulated_annealing(CPVs, OFE_budgets, randomSeed): dwell = int( CPVs[0]) #equibavent to population size in evolutionary algorithms func = evaluation_history_recording_wrapper(Ros_ND, dwell, solution_valid) optimize.anneal( func, x0=-0.5 * numpy.random.rand(5), #x0 = -2.048 + 2*2.048*numpy.random.rand(10), #if used make sure tMOPSO sample size greater than 100 m=CPVs[1], T0=500.0, lower=-2.048, upper=2.048, dwell=dwell, maxeval=max(OFE_budgets), #termination criteria feps=0.0, Tf=0.0) return func.f_hist, func.OFE_hist
def searchImage(self, imageData, idealMaskData, parameters, plot=False): # Requires: -image and idealMask is an RGB based image # -parameters is of type Parameters # Modifies: parameters # Effects: - Searches the parameter space using simulated annealing # to find an optimal solution, returns most optimal parameter found # -plots fitness vs. generation of search if plot set to True print "Running Anneal Search" # runs random search to find best from initial population # note, random search sets initial upper limit print "Calling random search for initial random guess" randSearch = RandomSearch.randomSearch(self.segmenter, self.fitness) parameters = randSearch.searchImage(imageData, idealMaskData, parameters) # set no upper limit (infinity) for fitness function parameters.setUpperLimit(float("inf")) # hold references to image data and ideal mask data # to conform with scipy's need for a simple fitness function (not using multiple arguments) self.imageData = imageData self.idealMaskData = idealMaskData # if color based segmenter, run anneal accordingkly if type(self.segmenter) == type(segmentation.colorSegmenter()): # make initial guess based off random search initialGuess = numpy.array(sum(parameters.colorRanges, [])) # runs scipy's anneal results = optimize.anneal(self.segmentAndFitnessColor, x0=initialGuess, args=(parameters, ), schedule='cauchy', full_output=True, dwell=50, lower=0.0, upper=1.0, disp=True, T0=.005) # prints anneal's final fitness print "Final Fitness " + str(results[1]) # for color segmenters colorRanges = results[0] colorRanges = [ colorRanges[0:2], colorRanges[2:4], colorRanges[4:6] ] parameters.setColorRanges(colorRanges) # if other kind of segmenter, add below return parameters
def main(): searchdir = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/MODIS_KANSAS_2007-2012/reprojected/Classified/test1/fullpxonly/clip1refs/KansasNDVI_2012_clip1_SLSQP/" outFile = os.path.join(searchdir, "classified.tif") cropimgpath = "/Users/phoetrymaster/Documents/School/Geography/Thesis/Data/polygonclip_20130929223024_325071991/resampled/newclips/2012clip1.tif" searchstringsvals = [("soy.", 5), ("wwheat.", 24), ("corn", 1), ("sorghum", 4), ("wwheatsoydbl", 26)] # nodata = -3000 accuracyreport = os.path.join(searchdir, "accuracy.txt") gdal.AllRegister() # np.set_printoptions(threshold=np.nan) # Crop image is constant for all iterations cropimg = gdal.Open(cropimgpath, GA_ReadOnly) if cropimg is None: raise Exception("Could not open: {0}".format(cropimgpath)) else: rows = cropimg.RasterYSize cols = cropimg.RasterXSize projection = cropimg.GetProjection() transformation = cropimg.GetGeoTransform() band = cropimg.GetRasterBand(1) datatype = band.DataType croparray = band.ReadAsArray(0, 0, cols, rows) band = "" cropimg = "" print "Opened crop img" # filelist = [] files = os.listdir(searchdir) for f in files: for string, val in searchstringsvals: if f.endswith(".tif"): if string in f: filelist.append((os.path.join(searchdir, f), val)) # thresholds = generate_thresholds(500, 300, 4, len(filelist)) # writestring = "" # bestacc = 0 # bestthresh = "" rranges = ( slice(500, 2000, 50), slice(500, 2000, 50), slice(500, 2000, 50), slice(500, 2000, 50), slice(500, 2000, 50), ) x0 = np.array([800, 800, 800, 800, 800]) res = optimize.anneal(classify_with_threshold, x0, args=(croparray, filelist, searchdir, searchstringsvals)) print res
def opt_ts_an(lc1, lc2, dispersionmethod, plot=False, verbose=True): """ A first atempt to optimize the timeshift using simulated annealing. We will shift lc2 in time so that it matches lc1. We return only the result code of the annealing. We do not optimize microlensing here. """ if plot: d2vals = [] timeshifts = [] inittimeshift = lc2.timeshift initd2val = dispersionmethod(lc1, lc2)['d2'] def d2(timeshift): lc2.timeshift = timeshift ret = dispersionmethod(lc1, lc2)['d2'] if plot: d2vals.append(ret) timeshifts.append(timeshift) return ret # http://www.scipy.org/doc/api_docs/SciPy.optimize.anneal.html res = spopt.anneal(d2, lc2.timeshift, schedule='fast', lower=-10, upper=10, dwell=6, learn_rate=0.5, T0 = 1.0e-3, Tf = 1.0e-9, full_output = 1) #print res # Do not forget to shift lc2 to optimal position : lc2.timeshift = res[0] if verbose : print "Optimal absolute shift for lc2 : %12.8f" % res[0] print "Dispersion value : %7.3f" % res[1] print "Final temperature : %.3g" % res[2] print "Dispersion calls : %i" % res[3] if res[6] == 0: print "Cooled to global minimum." elif res[6] == 1: print "Cooled to minimum temperature." else: print "### WARNING : %i ###" % res[6] if plot: plt.plot(timeshifts, d2vals, color="#DDDDDD") plt.plot(timeshifts, d2vals, "b.") plt.plot([inittimeshift], [initd2val], "r.") plt.plot([res[0]], [res[1]], "r.") plt.title("Dispersion calls : %i" % len(timeshifts)) plt.xlabel("Absolute lc2 timeshift [days]") plt.ylabel("Dispersion") plt.show() return res[6]
def find_P_prob_params_corr3(N, mean, corr2, corr3, method="tnc"): # ----------------------------------------------------------------------------- """ Taking the parameters it returns P_probs - the probability distribution for all Ps, including P=0 """ #from scipy.optimize import fmin_l_bfgs_b from scipy.optimize import fmin_tnc # seems better from scipy.optimize import anneal from scipy.optimize import brute assert N > 2 assert mean < 1 # TODO - assert corr2 < f(N,mean) # TODO - assert corr3 < f(N,mean,corr2) mean_des = mean # desired average corr_des = corr2 # desired correlation corr3_des = corr3 # desired correlation P_probs_m = np.zeros(N) # candidate P_probs_m P_probs_m[-1] = 1. # candidate parameter J if mean == 0: result = np.zeros(N+1) result[0] = 1. return result def opt_func(P_probs_m, N, mean_des, corr_des): P_probs = __get_P_probs__(N, mean_des, P_probs_m) corr_curr = corr2_from_P_probs(N, P_probs) corr_diff = (corr_curr - corr_des) corr3_curr = corr3_from_P_probs(N, P_probs) corr3_diff = (corr3_curr - corr3_des) #print corr_curr, corr3_curr, P_probs_m #return (abs(corr_diff)/abs(corr_des + 0.0000001) + abs(corr3_diff)/abs(corr3_des + 0.0000001))**.1 return 2*abs(corr_diff) + abs(corr3_diff) if method == "tnc": bounds = [(0,1) for i in xrange(N)] opt_output = fmin_tnc(opt_func, P_probs_m, args=(N,mean_des,corr_des),\ approx_grad = True, bounds=bounds) print opt_output opt_result = opt_output[0] if method == "anneal": opt_output = anneal(opt_func, P_probs_m, args=(N,mean_des,corr_des),\ lower = np.zeros(N), upper = np.ones(N)) print opt_output opt_result = opt_output[0] if method == "brute": opt_result = brute(opt_func, args=(N,mean_des,corr_des),\ ranges = [(0.00000000001,0.99999999999) for i in xrange(N)], Ns=20) return __get_P_probs__(N, mean_des, opt_result)
def fitAnneal(self, sampleSpec, initValues=None, vrot=None, priors=None): f = minuitFunction(self, sampleSpec, priors=priors) if vrot != None: varnames = list(self.params) + ["vrot"] f.varnames(*varnames) return optimize.anneal(f, initValues) else: f.varnames(*self.params) return optimize.fmin_bfgs(f, initValues)
def main(): #experiment() #run_experiment([1,1,1,1,1,1,1,1]) x0 = [1.0,1.0,3.0,3.0,1.0,1.0,3.0,3.0] np.random.seed(555) # Seeded to allow replication. res = optimize.anneal(run_experiment, x0, schedule='boltzmann', full_output=True, maxiter=500, lower=B, upper=T, dwell=30, disp=True) print(res) plt.plot(results) plt.ylabel('avg waiting time') plt.show()
def fitAnneal(self, sampleSpec, initValues=None, vrot=None, priors=None): f = minuitFunction(self, sampleSpec, priors=priors) if vrot != None: varnames = list(self.params) + ['vrot'] f.varnames(*varnames) return optimize.anneal(f, initValues) else: f.varnames(*self.params) return optimize.fmin_bfgs(f, initValues)
def optimizeMagicNumbers(): from scipy.optimize import fmin from scipy.optimize import minimize from scipy.optimize import fmin_tnc from scipy.optimize import anneal xopt = anneal(minimizeFunction, [0.0385, 0.00832], maxeval=50, disp=True, lower=[0.0, 0.0], upper=[0.1, 0.1] ) print "MonteCarlo optimzation results: " + str(xopt) print "Starting gradient descent with the following starting point: MinimumSpreadBuy=" + str(xopt[0][0]) + "MinimumSpreadSell=" +str(xopt[0][1]) xopt = fmin(minimizeFunction, [xopt[0][0], xopt[0][1]], maxiter=100, maxfun=4000) print "Gradient descent optimization results: " + str(xopt) print "Result: MinimumSpreadBuy=" + str(xopt[0]) + " MinimumSpreadSell=" + str(xopt[1])
def run_simulated_annealing(CPVs, OFE_budget, randomSeed): R = optimize.anneal(Ros_ND, x0 = -2.048 + 2*2.048*numpy.random.rand(5), dwell=int(CPVs[0]), m = CPVs[1], T0 = 500.0, lower= -2.048, upper= 2.048, maxeval = OFE_budget, #termination criteria feps = 0.0, Tf = 0.0) return Ros_ND(R[0]) # where xMin = R[0]
def ArgMinM(RKHSN1, stockData, florenZmirouData): en = 2 tau = Approximation.TauCurveFittingN2(RKHSN1, stockData, florenZmirouData) f_b = Approximation.RegularizedSolutionRKHSN2(tau, stockData, florenZmirouData)#appoximates 1/sigma^2 sigma_bSquared = 1/f_b s = min(stockData) b = max(stockData) a = b - (1/3) * (b - s) params = (sigma_bSquared, a, b, en,stockData,florenZmirouData) m0 = np.array([5]) print datetime.datetime.now() m_bar, argminVal,T,feval,iters,accept,status =optimize.anneal(Approximation.AnnealingFunction, m0, args=params, schedule='fast', full_output=True, maxiter=5, lower=.001,upper=10, dwell=10, disp=False) print datetime.datetime.now() return (m_bar, argminVal,T,feval,iters,accept,status,f_b)
def searchImage(self, imageData, idealMaskData, parameters, plot = False): # Requires: -image and idealMask is an RGB based image # -parameters is of type Parameters # Modifies: parameters # Effects: - Searches the parameter space using simulated annealing # to find an optimal solution, returns most optimal parameter found # -plots fitness vs. generation of search if plot set to True print "Running Anneal Search" # runs random search to find best from initial population # note, random search sets initial upper limit print "Calling random search for initial random guess" randSearch = RandomSearch.randomSearch(self.segmenter, self.fitness) parameters = randSearch.searchImage(imageData, idealMaskData, parameters) # set no upper limit (infinity) for fitness function parameters.setUpperLimit(float("inf")) # hold references to image data and ideal mask data # to conform with scipy's need for a simple fitness function (not using multiple arguments) self.imageData = imageData self.idealMaskData = idealMaskData # if color based segmenter, run anneal accordingkly if type(self.segmenter) == type(segmentation.colorSegmenter()): # make initial guess based off random search initialGuess = numpy.array(sum(parameters.colorRanges, [])) # runs scipy's anneal results = optimize.anneal(self.segmentAndFitnessColor, x0 = initialGuess, args=(parameters,), schedule='cauchy', full_output=True, dwell=50, lower=0.0, upper=1.0, disp=True, T0 = .005) # prints anneal's final fitness print "Final Fitness " + str(results[1]) # for color segmenters colorRanges = results[0] colorRanges = [colorRanges[0:2], colorRanges[2:4], colorRanges[4:6]] parameters.setColorRanges(colorRanges) # if other kind of segmenter, add below return parameters
def anneal_zernike_amps(): noof_zernikes = 20 max_func_evs = 2000 f = get_countrates_at_zernike_amp x0 = np.random.rand(noof_zernikes) - 0.5 res = opt_lib.anneal(f, x0, args=(dm.get_cur_voltages()), schedule='boltzmann', full_output=True, maxeval=max_func_evs, lower=-5., upper=5., dwell=50, disp=True) return res
def computeAxes1(landmarkGeom, figureGeom): if math2d.length(figureGeom) == 0: raise InsaneExample("Degenertie figure.") landmarkAsLine = math2d.polygonToLine(landmarkGeom) l = math2d.length(landmarkAsLine) resolution = 100 fLength = math2d.length(figureGeom) def score(xArr): axis = [ math2d.pointOnLine(landmarkAsLine, xArr[0] % l), math2d.pointOnLine(landmarkAsLine, xArr[1] % l) ] score = 0.0 for p1, p2 in zip( math2d.stepAlongLine(figureGeom, fLength / resolution), math2d.stepAlongLine(axis, math2d.length(axis) / resolution)): score += math2d.squaredist(p1, p2) return score #plot2d.plotLine(landmarkGeom, "r+-") #plot2d.plotLine(figureGeom, "r+-") guessMajor, guessMinor = computeAxes2(landmarkGeom, figureGeom) if guessMinor == None: return None, None guess = [ math2d.distAlongLine(landmarkAsLine, guessMinor[0]), math2d.distAlongLine(landmarkAsLine, guessMinor[1]) ] #xopt = optimize.fmin(score, guess) xopt, retval = optimize.anneal(score, guess) #pylab.show() #print "opt", xopt p1 = math2d.pointOnLine(landmarkAsLine, xopt[0] % l) p2 = math2d.pointOnLine(landmarkAsLine, xopt[1] % l) minor = [p1, p2] major = math2d.perpendicular(minor) return major, minor
def optimizeMagicNumbers(): from scipy.optimize import fmin from scipy.optimize import minimize from scipy.optimize import fmin_tnc from scipy.optimize import anneal xopt = anneal(minimizeFunction, [0.0385, 0.00832], maxeval=50, disp=True, lower=[0.0, 0.0], upper=[0.1, 0.1]) print "MonteCarlo optimzation results: " + str(xopt) print "Starting gradient descent with the following starting point: MinimumSpreadBuy=" + str( xopt[0][0]) + "MinimumSpreadSell=" + str(xopt[0][1]) xopt = fmin(minimizeFunction, [xopt[0][0], xopt[0][1]], maxiter=100, maxfun=4000) print "Gradient descent optimization results: " + str(xopt) print "Result: MinimumSpreadBuy=" + str( xopt[0]) + " MinimumSpreadSell=" + str(xopt[1])
def anneal_schedule(self, schedule='fast', use_wrapper=False): """ Call anneal algorithm using specified schedule """ n = 0 # index of test function if use_wrapper: opts = {'upper' : self.upper[n], 'lower' : self.lower[n], 'ftol' : 1e-3, 'maxiter' : self.maxiter, 'schedule': schedule, 'disp' : False} x, info = minimize(self.fun[n], self.x0[n], method='anneal', options=opts, full_output=True) retval = info['status'] else: x, retval = anneal(self.fun[n], self.x0[n], full_output=False, upper=self.upper[n], lower=self.lower[n], feps=1e-3, maxiter=self.maxiter, schedule=schedule, disp=False) assert_almost_equal(x, self.sol[n], 2) return retval
def _fit_anneal(self, X, y, X_val, Y_val, activations, deltas, coef_grads, intercept_grads, layer_units): if scipy.__version__ == '0.14.0': # Store meta information for the parameters self._coef_indptr = [] self._intercept_indptr = [] start = 0 # Save sizes and indices of coefficients for faster unpacking for i in range(self.n_layers_ - 1): n_fan_in, n_fan_out = layer_units[i], layer_units[i + 1] end = start + (n_fan_in * n_fan_out) self._coef_indptr.append((start, end, (n_fan_in, n_fan_out))) start = end # Save sizes and indices of intercepts for faster unpacking for i in range(self.n_layers_ - 1): end = start + layer_units[i + 1] self._intercept_indptr.append((start, end)) start = end # Run Simulated Annealing packed_coef_inter = _pack(self.coefs_, self.intercepts_) result = optimize.anneal(x0=packed_coef_inter, T0=self.T, stepsize=self.stepsize, func=self._loss_func, maxiter=self.max_iter, args=(X, y, activations, deltas, coef_grads, intercept_grads)) optimal_parameters = result.x self.loss = result.fun self._unpack(optimal_parameters) else: raise ImportError('Anneal method require scipy version <= 0.14.0')
def anneal_fitting(self): # Annealing fit # Not updated... because it is too slow (130522) print "Minimization with Annealing..." anneal_args = {} anneal_args['schedule'] = 'fast' anneal_args['T0'] = 0.0001 anneal_args['dwell'] = 1000 anneal_args['boltzmann'] = 1.0 anneal_args['full_output'] = 1 anneal_args['maxeval'] = 10000 anneal_args['maxiter'] = 1000 anneal_args['feps'] = 1.e-30 anneal_args['lower'] = N.array([-2.5, 21., -1., 0.1, 0.1]) anneal_args['upper'] = N.array([-0.5, 28., 1., 1.0, 1.0]) anneal_args['args'] = mlfunc_args (r, jmin, T, feval, niter, accept, retval) = optimize.anneal(self.mlfunc, self.guess, **anneal_args) self.output = np.zeros(6) self.output[:5] = r self.output[5] = self.phistar self.maxlikelihood = jmin
def search_P_for_rate_compute_MMSE_alpha(P_con, A, H, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme='sym_quan', beta=[]): (M, L) = (H.nrows(), H.ncols()) if beta == []: beta = vector(RR, [1] * L) cof_pow = lambda x: -sum_rate_computation_MMSE_alpha_two_hop( L, M, x, H, A, is_dual_hop, rate_sec_hop, mod_scheme, beta) Pranges = ((0.1, P_con), ) * L initial_guess = [0.5 * P_con] * L if P_Search_Alg == 'brute': res_cof = optimize.brute(cof_pow, Pranges, Ns=brute_number, full_output=True, finish=None) P_opt = list(res_cof[0]) sum_rate_opt = -res_cof[1] # negative! see minus sign in cof_pow elif P_Search_Alg == 'TNC': res_cof = optimize.minimize(cof_pow, initial_guess, method='TNC', bounds=Pranges, options={'maxiter': 100}) P_opt = list(res_cof.x) sum_rate_opt = -res_cof.fun # negative! see minus sign in cof_pow elif P_Search_Alg == 'anneal': res_cof = optimize.anneal(cof_pow, initial_guess, schedule='boltzmann', \ full_output=True, maxiter=20, lower=2, upper=P_con, dwell=20, disp=True) P_opt = list(res_cof[0]) sum_rate_opt = -res_cof[1] else: raise Exception('error: algorithm not supported') return (P_opt, sum_rate_opt)
def search_P_for_rate_compute_MMSE_alpha(P_con, A, H, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme='sym_quan', beta=[]): (M, L) = (H.nrows(), H.ncols()) if beta == []: beta = vector(RR, [1]*L) cof_pow = lambda x: -sum_rate_computation_MMSE_alpha_two_hop(L, M, x, H, A, is_dual_hop, rate_sec_hop, mod_scheme, beta) Pranges = ((0.1, P_con), )*L initial_guess = [0.5*P_con]*L if P_Search_Alg == 'brute': res_cof = optimize.brute(cof_pow, Pranges, Ns=brute_number, full_output=True, finish=None) P_opt = list(res_cof[0]) sum_rate_opt = -res_cof[1] # negative! see minus sign in cof_pow elif P_Search_Alg == 'TNC': res_cof = optimize.minimize(cof_pow, initial_guess, method='TNC', bounds=Pranges, options={'maxiter': 100}) P_opt = list(res_cof.x) sum_rate_opt = -res_cof.fun # negative! see minus sign in cof_pow elif P_Search_Alg == 'anneal': res_cof = optimize.anneal(cof_pow, initial_guess, schedule='boltzmann', \ full_output=True, maxiter=20, lower=2, upper=P_con, dwell=20, disp=True) P_opt = list(res_cof[0]) sum_rate_opt = -res_cof[1] else: raise Exception('error: algorithm not supported') return (P_opt, sum_rate_opt)
def maximize(scorer, params): params = merge_params(params) param_list = np.array([params[p] for p in spec.INDEX_TO_PARAM], object) used = np.array([isinstance(p, tuple) for p in param_list]) used_indices = np.where(used) not_used = np.logical_not(used) ndim = used.sum() x0 = np.array([sum(p)/2.0 for u, p in zip(used, param_list) if u]) base_full_x = param_list.copy() base_full_x[used] = x0 def objective(x): full_x = base_full_x.copy() full_x[used] = x try: s = spec.spec_from_list(full_x) except spec.ParameterValueError: return 1e3 print s s.save() m = runner.run(s) result.from_model(m).save() score = scorer.score(m.render()) print score return 1.0 - score # invert score to maximize return anneal(objective, x0, full_output=True, Tf=1e-8, dwell=50, disp=True, maxeval=6000, lower=[lower for lower, upper in param_list[used]], upper=[upper for lower, upper in param_list[used]])
def hallo_scipy(): print('hallo scipy') A = matrix([[1, 1, 1], [4, 4, 3], [7, 8, 5]]) b = matrix([1, 2, 1]).transpose() print(det(A)) # 值 print(inv(A)*b) # print(eig(A)) # 特征矩阵 print(svd(A)) x = arange(-10, 10, 0.1) plt.plot(x, f(x)) #plt.show() # find the min point by BFGS, 拟牛顿法 print(optimize.fmin_bfgs(f, 0)) print(optimize.fmin_bfgs(f, 3)) grid = (-10, 10, 0.1) xmin_global1 = optimize.brute(f, (grid, )) # 暴力尝试寻找全局最小 xmin_global2 = optimize.anneal(f, 0.0) # 模拟退火 print('global min', xmin_global1, xmin_global2) print('fminbound', optimize.fminbound(f, 0, 10)) print('fsolve formula', optimize.fsolve(f, 2.5)) # 回归的参数估计 guess = [2, 2] xdata = linspace(-10, 10, num=20) ydata = f(xdata) + random.random(xdata.size) params, params_convariance = optimize.curve_fit(f2, xdata, ydata, guess) print('curve params', params) plt.plot(xdata, f2(xdata, params[0], params[1])) plt.show()
def run_optimization(FitClass): '''\ run optimization based on FitClass\ ''' from my_io import print_List if FitClass.OptAlgo == 'leastsq': # leastsq algorithm plsq = opt.leastsq(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass])) tmpresult = [float(x) for x in plsq[0]] print_List(FitClass.IOut, tmpresult, 4, Info='Optimized result by leastsq algorithm') FitClass.InitGuess = tmpresult[:] elif FitClass.OptAlgo[:11] == 'fmin_cobyla': # fmin algorithm try: from opt_func import opt_func_constrained except: print_Error(FitClass.IOut,'\"opt_func_constrained\"'+\ ' is required for \"fmin_cobyla\"') interval = 1.0 # Set initial changes for i in FitClass.InitGuess: if abs(i) < interval: interval = abs(i) if i == 0.0: interval = 1.0 break plsq = opt.fmin_cobyla(calc_statistic,FitClass.InitGuess,\ cons=opt_func_constrained, args=tuple([FitClass]),rhobeg=interval) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by fmin_cobyla algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:4] == 'fmin': # fmin algorithm plsq = opt.fmin(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass])) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by fmin algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:5] == 'brute': # brute algorithm plsq = opt.brute(calc_statistic,\ ((-4.,-2.),(-1.,1.),(-4.,-2.),(-1.,1.)),\ args=(FitClass)) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by brute algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:6] == 'anneal': # anneal algorithm plsq = opt.anneal(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass]),dwell=200,maxiter=2000,lower=0.0,upper=1.0, T0=200.0) print_List(FitClass.IOut, plsq[0], 4, Info='Optimized result by anneal algorithm') FitClass.InitGuess = plsq[0][:] elif FitClass.OptAlgo[:5] == 'batch': # Just DEBUGING calc_statistic(FitClass.InitGuess, FitClass) return
def groupAnneal(recipe, groups): """Simple annealing optmizer that works with the above recipe. Optimize variables in related groups using scipy.optimize.anneal. This function does the work of selecting groups of variables from the groups list at random and schedules the temperature down-ramp. When the groups contain atom coordinates, this essentially the Reverse Monte Carlo (RMC [1]) refinement approach. With constraint and restraint flexibility afforded by SrFit, we can include as much information about the system as we have. recipe -- The recipe to optimize. groups -- List of groups of variables or varible names. During a refinement step, a group is selected for perturbation. For RMC-like refinement, these groups represent atom positions (x, y, z), but in general, they can be anything. [1] RL McGreevy and L Pusztai, Reverse Monte Carlo Simulation: A New Technique for the Determination of Disordered Structures, Molecular Simulation 1, 359-367, 1988 """ from scipy.optimize import anneal # Maximum number of iterations maxiter = 10000 # Counter for above niter = 0 # How long to dwell at each temperature dwell = len(groups) # How long to wait for an improvement before exiting maxwait = 5 * dwell # Counter for the above waitcount = 0 # Temperature T = T0 = 300 # Current error err = 0 # Minimum error minerr = None # Fix all parameters recipe.fix("all") while 1: if niter >= maxiter: print "*** Maximum interations exceeded ***" return if waitcount >= maxwait: print "*** Improvement waiting period exceeded ***" return niter += 1 # Pick an atom and get the names of its positions. Free these # variables. pars = random.choice(groups) print "iter =", niter print("%s " * len(pars)) % pars recipe.free(*pars) # Get the bounds for the variables. These are used by anneal. lb, ub = recipe.getBounds2() out = anneal(recipe, recipe.values, T0=T, lower=lb, upper=ub, maxeval=1, full_output=True, dwell=1) err = out[1] if minerr is None: minerr = err # Record the minimum error. If we go waitcount steps without a # reduction in error, then we're out of here. if err < minerr: minerr = err waitcount = 0 else: waitcount += 1 # Update temperature if not niter % dwell: T = T0 / numpy.log(10 + niter * 1.0 / dwell) print "T =", T if out[5]: print "move accepted" reject = 0 else: print "move rejected" reject += 1 print "residual =", err # Clean up for next iteration recipe._applyValues(out[0]) recipe.fix(*pars) return out
def CoF_compute_search_pow_flex(P_con, H_a, is_dual_hop, rate_sec_hop=[], mod_scheme='sym_mod', quan_scheme='sym_quan', beta=[]): (M, L) = (H_a.nrows(), H_a.ncols()) global P_Search_Alg if beta == []: beta = vector(RR, [1]*L) cof_pow = lambda x: -CoF_compute_fixed_pow_flex(x, P_con, False, H_a, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme, beta) cof_pow_beta = lambda x: -CoF_compute_fixed_pow_flex(x[0:L], P_con, False, H_a, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme, vector(RR, x[L:L+M])) #Pranges = ((P_con/brute_number, P_con), )*L # (slice(0, P_con+0.1, P_con/brute_number), )*L Pranges = ((0, P_con), )*L initial_guess = [0.5*P_con]*L try: if P_Search_Alg == 'brute': res_cof = optimize.brute(cof_pow, Pranges, Ns=brute_number, full_output=True, finish=None) P_opt = res_cof[0] sum_rate_opt = -res_cof[1] # negative! see minus sign in cof_pow elif P_Search_Alg == 'TNC': #res_cof = optimize.minimize(cof_pow, initial_guess, method='TNC', bounds=Pranges, options={'maxiter': 400, 'approx_grad': True}) #P_opt = list(res_cof.x) #sum_rate_opt = -res_cof.fun # negative! see minus sign in cof_pow res_cof = optimize.fmin_tnc(cof_pow, initial_guess, bounds=list(Pranges), approx_grad=True, epsilon=1, stepmx=10) P_opt = res_cof[0] sum_rate_opt = CoF_compute_fixed_pow_flex(P_opt, P_con, False, H_a, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme, beta) elif P_Search_Alg == 'anneal': res_cof = optimize.anneal(cof_pow, initial_guess, schedule='cauchy', T0=1, Tf=1e-6, \ full_output=True, maxiter=30, lower=[1, 1], upper=[P_con, P_con], dwell=30, disp=True) P_opt = list(res_cof[0]) sum_rate_opt = -res_cof[1] elif P_Search_Alg == 'brute_fmin': res_brute = optimize.brute(cof_pow, Pranges, Ns=brute_fmin_number, full_output=True, finish=None) P_brute_opt = res_brute[0] sum_rate_brute = -res_brute[1] # negative! see minus sign in cof_pow res_fmin = optimize.fmin(cof_pow, P_brute_opt, xtol=1, ftol=0.01, maxiter=brute_fmin_maxiter, full_output=True) #P_fmin_opt = res_fmin[0] P_opt = res_fmin[0] sum_rate_opt = -res_fmin[1] elif P_Search_Alg == 'brute_brute': res_brute1 = optimize.brute(cof_pow, Pranges, Ns=brute_brute_first_number, full_output=True, finish=None) P_brute_opt1 = res_brute1[0] sum_rate_brute1 = -res_brute1[1] # negative! see minus sign in cof_pow Pranges_brute_2 = tuple([(max(0,P_i-P_con/brute_brute_first_number), min(P_con,P_i+P_con/brute_brute_first_number)) for P_i in P_brute_opt1]) res_brute2 = optimize.brute(cof_pow, Pranges_brute_2, Ns=brute_brute_second_number, full_output=True, finish=None) P_brute_opt2 = res_brute2[0] sum_rate_brute2 = -res_brute2[1] # negative! see minus sign in cof_pow sum_rate_opt = sum_rate_brute2 elif P_Search_Alg == 'brute_fmin_beta': res_brute = optimize.brute(cof_pow, Pranges, Ns=brute_fmin_number, full_output=True, finish=None) P_brute_opt = res_brute[0] sum_rate_brute = -res_brute[1] # negative! see minus sign in cof_pow res_fmin_beta = optimize.fmin(cof_pow_beta, list(P_brute_opt)+[1]*M, xtol=0.01, ftol=0.01, maxiter=brute_fmin_maxiter*50, full_output=True) P_fmin_opt = res_fmin_beta[0] sum_rate_opt = -res_fmin_beta[1] elif P_Search_Alg == 'brute_fmin_cobyla': res_brute = optimize.brute(cof_pow, Pranges, Ns=brute_fmin_number, full_output=True, finish=None) P_brute_opt = res_brute[0] def pow_constraint(x): return x sum_rate_brute = -res_brute[1] # negative! see minus sign in cof_pow p_cobyla = optimize.fmin_cobyla(cof_pow, P_brute_opt, pow_constraint, maxfun=100) sum_rate_fmin_cobyla = CoF_compute_fixed_pow_flex(p_cobyla, P_con, False, H_a, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme, beta) sum_rate_opt = sum_rate_fmin_cobyla elif P_Search_Alg == 'brute_fmin_cobyla_beta': res_brute = optimize.brute(cof_pow, Pranges, Ns=brute_fmin_number, full_output=True, finish=None) P_brute_opt = res_brute[0] def pow_beta_constraint(x): return x sum_rate_brute = -res_brute[1] # negative! see minus sign in cof_pow p_cobyla = optimize.fmin_cobyla(cof_pow_beta, list(P_brute_opt)+[1]*M, pow_beta_constraint, maxfun=200) sum_rate_fmin_cobyla = CoF_compute_fixed_pow_flex(p_cobyla, P_con, False, H_a, is_dual_hop, rate_sec_hop, mod_scheme, quan_scheme, beta) sum_rate_opt = sum_rate_fmin_cobyla #Add differential evolution elif P_Search_Alg =="differential_evolution": bounds=((0,P_con),)*L res_brute=optimize.differential_evolution(cof_pow,bounds) P_opt=res_brute.x sum_rate_opt=-res_brute.fun #Add Genetic Algorithm elif P_Search_Alg=="genetic": res_cof=GeneticAlgorithm(P_con,H_a) P_opt=res_cof[0] sum_rate_opt=res_cof[1] #The Genetic Algorithm End else: raise Exception('error: algorithm not supported') except: print 'error in search algorithms' raise return sum_rate_opt
#rft.plot_ez_ref() ''' fdtd = Fdtd1d(nx) fdtd.set_src_pt(src_pt) fdtd.set_npml(npml=8, sigma_e=4605, sigma_h=1) fdtd.animate_update_pml(1000, 50) ''' from datetime import datetime t0 = datetime.now() npml = int(sys.argv[1]) for sa in ['fast', 'cauchy', 'boltzmann']: results = anneal(rft.get_max_r, np.array([0, 0]), args=(npml, 25, None), schedule=sa, full_output=True) print('[%s][%s] npml = %d, %s' % (datetime.now() - t0, sa, npml, results)) ''' n = 5000 rs = np.zeros(n) se_list = np.arange(n) npml_list = [64, 32, 16, 10, 8] f = h5.File('scan_sigma_e_1.h5', 'w') f.attrs['bin'] = n for npml in npml_list: print('npml = %d' % npml) for i, sigma_e in enumerate(se_list): rs[i] = rft.get_max_r(np.array([sigma_e, 1]), npml, 25, None)
def ex7c(exclude=sc.array([1,2,3,4]),plotfilename='ex7c.png'): """ex7d: solve exercise 7 using a simulated annealing optimization Input: exclude - ID numbers to exclude from the analysis plotfilename - filename for the output plot Output: plot History: 2009-06-02 - Written - Bovy (NYU) """ #Read the data data= read_data('data_yerr.dat') ndata= len(data) nsample= ndata- len(exclude) #First find the chi-squared solution, which we will use as an #initial gues for the bi-exponential optimization #Put the dat in the appropriate arrays and matrices Y= sc.zeros(nsample) X= sc.zeros(nsample) A= sc.ones((nsample,2)) C= sc.zeros((nsample,nsample)) yerr= sc.zeros(nsample) jj= 0 for ii in range(ndata): if sc.any(exclude == data[ii][0]): pass else: Y[jj]= data[ii][1][1] X[jj]= data[ii][1][0] A[jj,1]= data[ii][1][0] C[jj,jj]= data[ii][2]**2. yerr[jj]= data[ii][2] jj= jj+1 #Now compute the best fit and the uncertainties bestfit= sc.dot(linalg.inv(C),Y.T) bestfit= sc.dot(A.T,bestfit) bestfitvar= sc.dot(linalg.inv(C),A) bestfitvar= sc.dot(A.T,bestfitvar) bestfitvar= linalg.inv(bestfitvar) bestfit= sc.dot(bestfitvar,bestfit) initialguess= sc.array([bestfit[0],bestfit[1]]) #With this initial guess start off the annealing procedure Qs= [1.,2.] bestfitssoft= sc.zeros((2,len(Qs))) initialchisq= 0. for jj in range(nsample): initialchisq= initialchisq+(Y[jj]-X[jj]*initialguess[1]-initialguess[0])**2/(yerr[jj]**2) chisqQ= sc.zeros(len(Qs)) for ii in range(len(Qs)): chisqQ[ii]= initialchisq bestfit= initialguess nonglobal= True print "Working on Q = "+str(Qs[ii]) print "Performing 10 runs of the simulating annealing optimization algorithm" for jj in range(10):#Do ten runs of the sa algorithm sc.random.seed(jj+1) #In the interest of reproducibility (if that's a word) bestfitsoft= optimize.anneal(softchisquared,initialguess,(X,Y,yerr,Qs[ii]), schedule='boltzmann',full_output=1)#,dwell=200,maxiter=1000) if bestfitsoft[1] < chisqQ[ii]: bestfit= bestfitsoft[0] chisqQ[ii]= bestfitsoft[1] if bestfitsoft[6] == 0: nonglobal= False if nonglobal: print "Did not cool to the global optimum" try: x=raw_input('continue to plot? [yn]\n') except EOFError: print "Since you are in non-interactive mode I will assume 'y'" x='y' if x == 'n': print "returning..." return -1 bestfitssoft[:,ii]= bestfit #Now plot the solution fig_width=5 fig_height=5 fig_size = [fig_width,fig_height] params = {'axes.labelsize': 12, 'text.fontsize': 11, 'legend.fontsize': 12, 'xtick.labelsize':10, 'ytick.labelsize':10, 'text.usetex': True, 'figure.figsize': fig_size} rcParams.update(params) #Plot data errorbar(X,Y,yerr,color='k',marker='o',color='k',linestyle='None') xlabel(r'$x$') ylabel(r'$y$') #Plot the best fit line for the different Qs linestyles= ('--',':', '-.') for jj in range(len(Qs)): xlim(0,300) ylim(0,700) xmin, xmax= xlim() nsamples= 1001 xs= sc.linspace(xmin,xmax,nsamples) ys= sc.zeros(nsamples) for ii in range(nsamples): ys[ii]= bestfitssoft[0,jj]+bestfitssoft[1,jj]*xs[ii] if bestfitssoft[0,jj] < 0: sgn_str= '-' else: sgn_str= '+' label= r'$Q= '+'%i: y = %4.2f\, x'% (Qs[jj], bestfitssoft[1,jj]) +sgn_str+ '%4.0f ' % m.fabs(bestfitssoft[0,jj])+r'; \chi^2_Q = '+ '%3.1f' % chisqQ[jj]+'$' plot(xs,ys,color='k',ls=linestyles[jj],label=label) l=legend(loc=(.2,.1),numpoints=8) l.draw_frame(False) xlim(0,300) ylim(0,700) savefig(plotfilename,format='png') return 0
def __call__(self, inputs, states, refs): if self.a0 == None: a0 = np.random.random((3, 3)) * 2 - 1 if self.b0 == None: b0 = np.random.random((3, 3)) * 2 - 1 if self.z0 == None: z0 = np.random.random() * 2 - 1 params = np.array(list(a0.flatten()) + list(b0.flatten()) + [z0]) print "Initial parameters:", params assert params.shape[0] == 19 #TODO: assert all arrays have same dimensions # Do optimization if self.opt_method == "anneal": #Do the optimization 100 times par, fopt, it, fcalls, t5, t6, t7 = anneal(self.error, params, lower=-2, upper=2, full_output=True) elif self.opt_method == "simplex": #Do the optimization 100 times #best_fopt = 10e50 #for i in range(2): # print "\nStep", i # params = np.random.random((19))*0.2-0.1 # print "Initial guess", params # self.do_pp = True #par_tmp, ftol, it, fcalls,wf, allv_tmp = fmin(self.error,params,xtol=-1,ftol=-1,maxiter=2000,maxfun=10e10,full_output=True,retall=True) par, fopt, it, fcalls, wf, allv_tmp = fmin(self.error, params, maxiter=2000, maxfun=10e10, full_output=True, retall=True) # print fopt,# par_tmp # print "Final:", par_tmp # print "Veränderungen:", par_tmp-params # if fopt < best_fopt: # best_fopt=fopt # par=par_tmp # allv = allv_tmp # print "besser!", #par, ftol, it, fcalls,wf, allv = fmin(self.error,params,xtol=-1,ftol=-1,maxiter=2000,maxfun=10e10,full_output=True,retall=True) #print "params:", params #par, ftol, it, fcalls,wf, allv = fmin(self.error,params,full_output=True,retall=True) #print par, ftol, it, fcalls, wf, allv #print np.array(allv).shape #allv = np.array(allv) #p.figure(3) #for i in range(allv.shape[1]): # p.plot(allv[:,i]) #p.show() #time.sleep(3) elif self.opt_method == "brute": par = brute(self.error, [slice(-2, 2, 2j) for i in range(19)]) print par #par = par[0] elif self.opt_method == "powell": par = fmin_powell(self.error, params) print "Veränderungen:", par - params else: raise ValueError("Optimization method not known") par = np.array(par) return par[:9].reshape((3, 3)), par[9:18].reshape((3, 3)), par[18]
start_point = [] for param in config["parameters"]: parameters_ids.append(param["id"]) lower_limit.append(param["min"]) upper_limit.append(param["max"]) start_point.append(param["start_value"]) scalarm = Scalarm(config['user'], config['password'], config['experiment_id'], config['http_schema'], config["address"], parameters_ids, config['verifySSL'] if 'verifySSL' in config else False) res = scopt.anneal(func=call_scalarm, x0=start_point, full_output=True, schedule=config['schedule'], lower=lower_limit, upper=upper_limit, maxiter=config['maxiter'], dwell=config['dwell']) print 'mark_as_complete' scalarm.mark_as_complete({'result': res[1], 'values': to_csv(res[0])})
#!/usr/bin/env python # # Author: Patrick Hung (patrickh @caltech) # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 1997-2016 California Institute of Technology. # Copyright (c) 2016-2017 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE """ Similar to test_mogi.py but trying to use scipy's levenberg marquardt. """ from test_mogi import * from scipy.optimize import anneal import pylab if __name__ == '__main__': lower = array([1000,-1000,0,0]) upper = array([5000,-0,20,0.5]) sol = anneal(cost_function, [1000., -500., -10., 0.1], lower=lower, upper=upper, feps=1e-10, dwell=100,T0=10) print "scipy solution: ", sol[0] plot_noisy_data() plot_sol(sol[0],'r-') pylab.show() # end of file
# Copyright (c) 2016-2018 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/mystic/blob/master/LICENSE """ Similar to test_mogi.py but trying to use scipy's levenberg marquardt. """ from test_mogi import * from scipy.optimize import anneal import matplotlib.pyplot as plt if __name__ == '__main__': lower = array([1000, -1000, 0, 0]) upper = array([5000, -0, 20, 0.5]) sol = anneal(cost_function, [1000., -500., -10., 0.1], lower=lower, upper=upper, feps=1e-10, dwell=100, T0=10) print("scipy solution: %s" % sol[0]) plot_noisy_data() plot_sol(sol[0], 'r-') plt.show() # end of file
def keptransit(inputfile,outputfile,datacol,errorcol,periodini_d,rprsini,T0ini, Eccini,arsini,incini,omegaini,LDparams,secini,fixperiod,fixrprs,fixT0, fixEcc,fixars,fixinc,fixomega,fixsec,fixfluxoffset,removeflaggeddata,ftol=0.0001,fitter='nothing',norm=False, clobber=False, plot=True,verbose=0,logfile='logfile.dat',status=0,cmdLine=False): """ tmod.lightcurve(xdata,period,rprs,T0,Ecc,ars, incl, omega, ld, sec) input transit parameters are Period in days T0 rplanet / rstar a / rstar inclination limb darkening code number: 0 = uniform 1 = linear 2 = quadratic 3 = square root 4 = non linear LDarr: u -- linear limb-darkening (set NL=1) a, b -- quadratic limb-darkening (set NL=2) c, d -- root-square limb-darkening (set NL= -2) a1, a2, a3, a4 -- nonlinear limb-darkening (set NL=4) Nothing at all -- uniform limb-darkening (set NL=0) """ np.seterr(all="ignore") #write to a logfile hashline = '----------------------------------------------------------------------------' kepmsg.log(logfile,hashline,verbose) call = 'KEPTRANSIT -- ' call += 'inputfile='+inputfile+' ' call += 'outputfile='+outputfile+' ' call += 'datacol='+str(datacol)+' ' call += 'errorcol='+str(errorcol)+' ' call += 'periodini_d='+str(periodini_d)+' ' call += 'rprsini='+str(rprsini)+' ' call += 'T0ini='+str(T0ini)+' ' call += 'Eccini='+str(Eccini)+' ' call += 'arsini='+str(arsini)+' ' call += 'incini='+str(incini)+' ' call += 'omegaini='+str(omegaini)+' ' call += 'LDparams='+str(LDparams)+' ' call += 'secini='+str(secini)+' ' call += 'fixperiod='+str(fixperiod)+' ' call += 'fixrprs='+str(fixrprs)+' ' call += 'fixT0='+str(fixT0)+' ' call += 'fixEcc='+str(fixEcc)+' ' call += 'fixars='+str(fixars)+' ' call += 'fixinc='+str(fixinc)+' ' call += 'fixomega='+str(fixomega)+' ' call += 'fixsec='+str(fixsec)+' ' call += 'fixfluxoffset='+str(fixfluxoffset)+' ' call += 'removeflaggeddata='+str(removeflaggeddata)+' ' call += 'ftol='+str(ftol)+' ' call += 'fitter='+str(fitter)+' ' call += 'norm='+str(norm)+' ' plotit = 'n' if (plot): plotit = 'y' call += 'plot='+plotit+ ' ' overwrite = 'n' if (clobber): overwrite = 'y' call += 'clobber='+overwrite+ ' ' #chatter = 'n' #if (verbose): chatter = 'y' #call += 'verbose='+chatter+' ' call += 'logfile='+logfile kepmsg.log(logfile,call+'\n',verbose) kepmsg.clock('KEPTRANSIT started at',logfile,verbose) # test log file logfile = kepmsg.test(logfile) # clobber output file if clobber: status = kepio.clobber(outputfile,logfile,verbose) if kepio.fileexists(outputfile): message = 'ERROR -- KEPTRANSIT: ' + outputfile + ' exists. Use clobber=yes' status = kepmsg.err(logfile,message,verbose) # open input file if status == 0: instr, status = kepio.openfits(inputfile,'readonly',logfile,verbose) if status == 0: tstart, tstop, bjdref, cadence, status = kepio.timekeys(instr, inputfile,logfile,verbose,status) if status == 0: try: work = instr[0].header['FILEVER'] cadenom = 1.0 except: cadenom = cadence # fudge non-compliant FITS keywords with no values if status == 0: instr = kepkey.emptykeys(instr,file,logfile,verbose) # read table structure if status == 0: table, status = kepio.readfitstab(inputfile,instr[1],logfile,verbose) if status == 0: intime_o = table.field('time') influx_o = table.field(datacol) inerr_o = table.field(errorcol) try: qualflag = table.field('SAP_QUALITY') except: qualflag = np.zeros(len(intime_o)) if status == 0: intime, indata, inerr, baddata = cutBadData(intime_o, influx_o, inerr_o,removeflaggeddata,qualflag) if status == 0 and norm: #first remove outliers before normalizing threesig = 3.* np.std(indata) mask = np.logical_and(indata< indata + threesig,indata > indata - threesig) #now normalize indata = indata / np.median(indata[mask]) if status == 0: #need to check if LD params are sensible and in right format LDparams = [float(i) for i in LDparams.split()] incini = incini * np.pi / 180. omegaini = omegaini * np.pi / 180. if arsini*np.cos(incini) > 1.0 + rprsini: message = 'The guess inclination and a/r* values result in a non-transing planet' status = kepmsg.err(logfile,message,verbose) if status == 0: fixed_dict = fix_params(fixperiod,fixrprs,fixT0, fixEcc,fixars,fixinc,fixomega,fixsec,fixfluxoffset) #force flux offset to be guessed at zero fluxoffsetini = 0.0 if status == 0: guess_params = [periodini_d,rprsini,T0ini,Eccini,arsini, incini, omegaini, secini,fluxoffsetini] print('cleaning done: about to fit transit') if fitter == 'leastsq': fit_output = leastsq(fit_tmod,guess_params, args=[LDparams,intime,indata,inerr,fixed_dict,guess_params], full_output=True,ftol=ftol) elif fitter == 'fmin': fit_output = fmin(fit_tmod2,guess_params, args=[LDparams,intime,indata,inerr,fixed_dict,guess_params], full_output=True,ftol=ftol,xtol=ftol) elif fitter == 'anneal': fit_output = anneal(fit_tmod2,guess_params, args=[LDparams,intime,indata,inerr,fixed_dict,guess_params], full_output=True) if status == 0: if fixed_dict['period'] == True: newperiod = guess_params[0] print('Fixed period (days) = ' + str(newperiod)) else: newperiod = fit_output[0][0] print('Fit period (days) = ' + str(newperiod)) if fixed_dict['rprs'] == True: newrprs = guess_params[1] print('Fixed R_planet / R_star = ' + str(newrprs)) else: newrprs = fit_output[0][1] print('Fit R_planet / R_star = ' + str(newrprs)) if fixed_dict['T0'] == True: newT0 = guess_params[2] print('Fixed T0 (BJD) = ' + str(newT0)) else: newT0 = fit_output[0][2] print('Fit T0 (BJD) = ' + str(newT0)) if fixed_dict['Ecc'] == True: newEcc = guess_params[3] print('Fixed eccentricity = ' + str(newEcc)) else: newEcc = fit_output[0][3] print('Fit eccentricity = ' + str(newEcc)) if fixed_dict['ars'] == True: newars = guess_params[4] print('Fixed a / R_star = ' + str(newars)) else: newars = fit_output[0][4] print('Fit a / R_star = ' + str(newars)) if fixed_dict['inc'] == True: newinc = guess_params[5] print('Fixed inclination (deg) = ' + str(newinc* 180. / np.pi)) else: newinc = fit_output[0][5] print('Fit inclination (deg) = ' + str(newinc* 180. / np.pi)) if fixed_dict['omega'] == True: newomega = guess_params[6] print('Fixed omega = ' + str(newomega)) else: newomega = fit_output[0][6] print('Fit omega = ' + str(newomega)) if fixed_dict['sec'] == True: newsec = guess_params[7] print('Fixed seconary eclipse depth = ' + str(newsec)) else: newsec = fit_output[0][7] print('Fit seconary eclipse depth = ' + str(newsec)) if fixfluxoffset == False: newfluxoffset = fit_output[0][8] print('Fit flux offset = ' + str(newfluxoffset)) modelfit = tmod.lightcurve(intime,newperiod,newrprs,newT0,newEcc, newars,newinc,newomega,LDparams,newsec) if fixfluxoffset == False: modelfit += newfluxoffset #output to a file phi, fluxfold, modelfold, errorfold, phiNotFold = fold_data(intime, modelfit,indata,inerr,newperiod,newT0) make_outfile(instr,outputfile,phiNotFold,modelfit, baddata) # end time if (status == 0): message = 'KEPTRANSIT completed at' else: message = '\nKEPTRANSIT aborted at' kepmsg.clock(message,logfile,verbose) if plot and status == 0: do_plot(intime,modelfit,indata,inerr,newperiod,newT0,cmdLine)
m[pm].blade_tri_vyy= -(np.array(m[pm].blade_shear_data['nu_xx'])/m[pm].dt-0.5*m[pm].vkk_blade) print('Done!') Nframes= len(m[pm].frames)-1 m[pm].hinge_shear_h= np.exp(np.cumsum(m[pm].hinge_piv_vyy[:Nframes]*m[pm].dt[:Nframes])) m[pm].hinge_shear_L= np.exp(np.cumsum(m[pm].hinge_piv_vxx[:Nframes]*m[pm].dt[:Nframes])) m[pm].blade_shear_h= np.exp(np.cumsum(m[pm].blade_piv_vyy[:Nframes]*m[pm].dt[:Nframes])) m[pm].blade_shear_L= np.exp(np.cumsum(m[pm].blade_piv_vxx[:Nframes]*m[pm].dt[:Nframes])) pm= 'dp' fitting_shift= 100 def scaling_func(beta): return lib.square_difference(beta*m[pm].hinge_shear_h[fitting_shift:], m[pm].hinge_fcy_h[fitting_shift:-1]) m[pm].beta_hinge_h= optimize.anneal(scaling_func, m[pm].hinge_fcy_h[0])[0] def scaling_func(beta): return lib.square_difference(beta*m[pm].hinge_shear_L[fitting_shift:], m[pm].hinge_fcy_L[fitting_shift:-1]) m[pm].beta_hinge_L= optimize.anneal(scaling_func, m[pm].hinge_fcy_L[0])[0] def scaling_func(beta): return lib.square_difference(beta*m[pm].blade_shear_h[fitting_shift:], m[pm].blade_fcy_h[fitting_shift:-1]) m[pm].beta_blade_h= optimize.anneal(scaling_func, m[pm].blade_fcy_h[0])[0] def scaling_func(beta): return lib.square_difference(beta*m[pm].blade_shear_L[fitting_shift:], m[pm].blade_fcy_L[fitting_shift:-1]) m[pm].beta_blade_L= optimize.anneal(scaling_func, m[pm].blade_fcy_L[0])[0] f, ((axBH, axHH),(axBL, axHL))= plt.subplots(2,2)
def ex7c(exclude=sc.array([1, 2, 3, 4]), plotfilename='ex7c.png'): """ex7d: solve exercise 7 using a simulated annealing optimization Input: exclude - ID numbers to exclude from the analysis plotfilename - filename for the output plot Output: plot History: 2009-06-02 - Written - Bovy (NYU) """ #Read the data data = read_data('data_yerr.dat') ndata = len(data) nsample = ndata - len(exclude) #First find the chi-squared solution, which we will use as an #initial gues for the bi-exponential optimization #Put the dat in the appropriate arrays and matrices Y = sc.zeros(nsample) X = sc.zeros(nsample) A = sc.ones((nsample, 2)) C = sc.zeros((nsample, nsample)) yerr = sc.zeros(nsample) jj = 0 for ii in range(ndata): if sc.any(exclude == data[ii][0]): pass else: Y[jj] = data[ii][1][1] X[jj] = data[ii][1][0] A[jj, 1] = data[ii][1][0] C[jj, jj] = data[ii][2]**2. yerr[jj] = data[ii][2] jj = jj + 1 #Now compute the best fit and the uncertainties bestfit = sc.dot(linalg.inv(C), Y.T) bestfit = sc.dot(A.T, bestfit) bestfitvar = sc.dot(linalg.inv(C), A) bestfitvar = sc.dot(A.T, bestfitvar) bestfitvar = linalg.inv(bestfitvar) bestfit = sc.dot(bestfitvar, bestfit) initialguess = sc.array([bestfit[0], bestfit[1]]) #With this initial guess start off the annealing procedure Qs = [1., 2.] bestfitssoft = sc.zeros((2, len(Qs))) initialchisq = 0. for jj in range(nsample): initialchisq = initialchisq + (Y[jj] - X[jj] * initialguess[1] - initialguess[0])**2 / (yerr[jj]**2) chisqQ = sc.zeros(len(Qs)) for ii in range(len(Qs)): chisqQ[ii] = initialchisq bestfit = initialguess nonglobal = True print "Working on Q = " + str(Qs[ii]) print "Performing 10 runs of the simulating annealing optimization algorithm" for jj in range(10): #Do ten runs of the sa algorithm sc.random.seed( jj + 1) #In the interest of reproducibility (if that's a word) bestfitsoft = optimize.anneal( softchisquared, initialguess, (X, Y, yerr, Qs[ii]), schedule='boltzmann', full_output=1) #,dwell=200,maxiter=1000) if bestfitsoft[1] < chisqQ[ii]: bestfit = bestfitsoft[0] chisqQ[ii] = bestfitsoft[1] if bestfitsoft[6] == 0: nonglobal = False if nonglobal: print "Did not cool to the global optimum" try: x = raw_input('continue to plot? [yn]\n') except EOFError: print "Since you are in non-interactive mode I will assume 'y'" x = 'y' if x == 'n': print "returning..." return -1 bestfitssoft[:, ii] = bestfit #Now plot the solution fig_width = 5 fig_height = 5 fig_size = [fig_width, fig_height] params = { 'axes.labelsize': 12, 'text.fontsize': 11, 'legend.fontsize': 12, 'xtick.labelsize': 10, 'ytick.labelsize': 10, 'text.usetex': True, 'figure.figsize': fig_size } rcParams.update(params) #Plot data errorbar(X, Y, yerr, color='k', marker='o', color='k', linestyle='None') xlabel(r'$x$') ylabel(r'$y$') #Plot the best fit line for the different Qs linestyles = ('--', ':', '-.') for jj in range(len(Qs)): xlim(0, 300) ylim(0, 700) xmin, xmax = xlim() nsamples = 1001 xs = sc.linspace(xmin, xmax, nsamples) ys = sc.zeros(nsamples) for ii in range(nsamples): ys[ii] = bestfitssoft[0, jj] + bestfitssoft[1, jj] * xs[ii] if bestfitssoft[0, jj] < 0: sgn_str = '-' else: sgn_str = '+' label = r'$Q= ' + '%i: y = %4.2f\, x' % (Qs[jj], bestfitssoft[ 1, jj]) + sgn_str + '%4.0f ' % m.fabs(bestfitssoft[ 0, jj]) + r'; \chi^2_Q = ' + '%3.1f' % chisqQ[jj] + '$' plot(xs, ys, color='k', ls=linestyles[jj], label=label) l = legend(loc=(.2, .1), numpoints=8) l.draw_frame(False) xlim(0, 300) ylim(0, 700) savefig(plotfilename, format='png') return 0
config = json.load(config_file) config_file.close() parameters_ids = [] lower_limit = [] upper_limit = [] start_point = [] for param in config["parameters"]: parameters_ids.append(param["id"]) lower_limit.append(param["min"]) upper_limit.append(param["max"]) start_point.append(param["start_value"]) scalarm = Scalarm(config['user'], config['password'], config['experiment_id'], config['http_schema'], config["address"], parameters_ids, config['verifySSL'] if 'verifySSL' in config else False) res = scopt.anneal(func=call_scalarm, x0=start_point, full_output=True, schedule=config['schedule'], lower=lower_limit, upper=upper_limit, maxiter=config['maxiter'], dwell=config['dwell']) print 'mark_as_complete' scalarm.mark_as_complete({'result': res[1], 'values': to_csv(res[0])})
params = (2, 3, 7, 8, 9, 10, 44, -1, 2, 26, 1, -2, 0.5) def f1(z, *params): x, y = z a, b, c, d, e, f, g, h, i, j, k, l, scale = params return (a * x**2 + b * x * y + c * y**2 + d*x + e*y + f) def f2(z, *params): x, y = z a, b, c, d, e, f, g, h, i, j, k, l, scale = params return (-g*np.exp(-((x-h)**2 + (y-i)**2) / scale)) def f3(z, *params): x, y = z a, b, c, d, e, f, g, h, i, j, k, l, scale = params return (-j*np.exp(-((x-k)**2 + (y-l)**2) / scale)) def f(z, *params): # x, y = z # a, b, c, d, e, f, g, h, i, j, k, l, scale = params return f1(z, *params) + f2(z, *params) + f3(z, *params) import numpy as np x0 = np.array([2., 2.]) # Initial guess. from scipy import optimize np.random.seed(666) # Seeded to allow replication. res = optimize.anneal(f, x0, args=params, schedule='boltzmann', full_output=True, maxiter=500, lower=-10, upper=10, dwell=250, disp=True) print res
global iternum iternum += 1 print 'Iteration : ',iternum #chisqarray = [avg_low_redux,avg_mid1_redux-3.0,avg_mid2_redux-10.0,avg_mid3_redux-3.0,avg_high_redux] chisqarray = [avg_low_redux,avg_mid_redux-5.0,avg_high_redux] print "Difference between fitted and desired inh reduxes =",chisqarray sys.stdout.flush() return sum([i**2 for i in chisqarray]) ######### ## leastsq() uses a modified version of Levenberg-Marquardt algorithm ## it optimizes M equations in N unknowns, where M>=N. ## Hence chisqfunc must return M numbers in an array ## which must be >= the number of params N in params0, ## else: 'TypeError: Improper input parameters.' iternum = 0 params_init = array( [ mitral_granule_AMPA_Gbar_init, granule_mitral_GABA_Gbar_init,\ self_mitral_GABA_Gbar_init, mitB_current_init ] ) ## the range within which params can vary lower_params = array( [ mitral_granule_AMPA_Gbar_init*0.8, granule_mitral_GABA_Gbar_init*0.2,\ self_mitral_GABA_Gbar_init, mitB_current_init*0.5 ] ) upper_params = array( [ mitral_granule_AMPA_Gbar_init*2.0, granule_mitral_GABA_Gbar_init*2.0,\ self_mitral_GABA_Gbar_init*20.0, mitB_current_init*1.5 ] ) params = optimize.anneal( chisq_ADI, params_init,\ full_output=1, upper = upper_params, lower = lower_params ) print params # print the status message params = params[0] # take only fitted params; leastsq returns a tuple with errmsg, etc. print "mitral_granule_AMPA_Gbar, granule_mitral_GABA_Gbar,"\ "self_mitral_GABA_Gbar, mitB_current",params print "Difference between fitted and desired inh reduxes =",chisq_ADI(params)
from scipy.optimize import anneal result = anneal(func, x0, args=(), schedule='fast', full_output=True, T0=None, Tf=1e-12, maxeval=None, maxaccept=None, maxiter=400, boltzmann=1.0, learn_rate=0.5, feps=1e-06, quench=1.0, m=1.0, n=1.0, lower=-100, upper=100, dwell=50, disp=True) print(result)
def opt_ts_an(lc1, lc2, dispersionmethod, plot=False, verbose=True): """ A first atempt to optimize the timeshift using simulated annealing. We will shift lc2 in time so that it matches lc1. We return only the result code of the annealing. We do not optimize microlensing here. """ if plot: d2vals = [] timeshifts = [] inittimeshift = lc2.timeshift initd2val = dispersionmethod(lc1, lc2)['d2'] def d2(timeshift): lc2.timeshift = timeshift ret = dispersionmethod(lc1, lc2)['d2'] if plot: d2vals.append(ret) timeshifts.append(timeshift) return ret # http://www.scipy.org/doc/api_docs/SciPy.optimize.anneal.html res = spopt.anneal(d2, lc2.timeshift, schedule='fast', lower=-10, upper=10, dwell=6, learn_rate=0.5, T0=1.0e-3, Tf=1.0e-9, full_output=1) #print res # Do not forget to shift lc2 to optimal position : lc2.timeshift = res[0] if verbose: print "Optimal absolute shift for lc2 : %12.8f" % res[0] print "Dispersion value : %7.3f" % res[1] print "Final temperature : %.3g" % res[2] print "Dispersion calls : %i" % res[3] if res[6] == 0: print "Cooled to global minimum." elif res[6] == 1: print "Cooled to minimum temperature." else: print "### WARNING : %i ###" % res[6] if plot: plt.plot(timeshifts, d2vals, color="#DDDDDD") plt.plot(timeshifts, d2vals, "b.") plt.plot([inittimeshift], [initd2val], "r.") plt.plot([res[0]], [res[1]], "r.") plt.title("Dispersion calls : %i" % len(timeshifts)) plt.xlabel("Absolute lc2 timeshift [days]") plt.ylabel("Dispersion") plt.show() return res[6]
def bivariate_fit(self, mlfunc_args, guess, drop, boundary, technique="l_bfgs_b", verbose=2, maxiter=200): """ Cases for minimization technique: simplex: not always robust anneal: very slow bfgs: good performance, but do not support constraints l_bfgs_b: supports constraints test_pipe: randomly generates an output (to test that everything before the minimization step is correct test_mlfunc: returns the log-likelihood for the given values of the parameters Arguments: mlfunc_args: arguments to mlfunc, in the exact order guess: initial guess of the parameters drop: name of the dropout band boundary: parameter boundaries (an Nx2 array, N=number of parameters) technique: minimization algorithm; possible options are simplex, anneal, bfgs, l_bfgs_b verbose: frequency to print intermediate steps of optimization maxiter: max. number of iterations before reporting results """ t1 = time.time() M0 = mlfunc_args[6] # ENTERING FITTING ALGORITHM # Simplex fit if technique == "simplex": simplex_overrides = { "xtol": 1.e-4, "ftol": 1.e-6, "maxiter": 1000, "full_output": 1 } sys.stdout.flush() print "ftol =", simplex_overrides["ftol"] (r, fopt, niters, funcalls, warnflag) = optimize.fmin(self.mlfunc, guess, args=mlfunc_args, **simplex_overrides) print "output array:", xopt print "alpha = %10.5f" % r[0] print "Mstar = %10.5f" % (r[1]) print "rpeak = %10.5f pixels at M0 = " % (10.**r[2]), M0 print "sigma = %10.5f" % (r[3]) print "beta = %10.5f" % (r[4]) print "-Log(L) = %10.2f" % (fopt) print "phistar = %.3e" % (self.phistar[-1]) print "iter = %6d" % (niters) print r output = r t2 = time.time() print "Total fitting time: %.2f minutes" % ((t2 - t1) / 60.) return output # Annealing fit # Not updated... because it is too slow (130522) if technique == "anneal": if kgrid1 != None: print "uses kgrid1 in anneal fitting" else: print "no kgrid1" if kgrid2 != None: print "uses kgrid2 in anneal fitting" else: print "no kgrid2" anneal_args = {} anneal_args['schedule'] = 'fast' anneal_args['T0'] = 0.0001 anneal_args['dwell'] = 1000 anneal_args['boltzmann'] = 1.0 anneal_args['full_output'] = 1 anneal_args['maxeval'] = 10000 anneal_args['maxiter'] = 1000 anneal_args['feps'] = 1.e-30 anneal_args['lower'] = N.array([-2.5, 21., -1., 0.1, 0.1]) anneal_args['upper'] = N.array([-0.5, 28., 1., 1.0, 1.0]) anneal_args['args'] = mlfunc_args (r, jmin, T, feval, iter, accept, retval) = optimize.anneal(self.mlfunc, guess, **anneal_args) print "output array:", xopt print "alpha = %10.5f" % (r[0]) print "Mstar = %10.5f" % (r[1]) print "rpeak = %10.5f pixels at M0 = " % (10.**r[2]), M0 print "sigma = %10.5f" % (r[3] / N.log(10.)) print "beta = %10.5f" % (r[4]) print "-Log(L) = %10.2f" % (jmin) print "phistar = %.3e" % (self.phistar[-1]) print "iter = %6d" % (iter) output = r t2 = time.time() print "Total fitting time: %.2f minutes" % ((t2 - t1) / 60.) return output # BFGS algorithm if technique == 'bfgs': print "maxiter =", maxiter gtol = 1.e-5 print "gtol =", gtol sys.stdout.flush() all_output = optimize.fmin_bfgs(self.mlfunc, guess, args=mlfunc_args, full_output=1, maxiter=maxiter, gtol=gtol, retall=0) (xopt, fopt, gopt, Bopt, func_calls, grad_calls, warnflag) = all_output print "output array:", xopt print "alpha = %10.5f" % (xopt[0]) print "Mstar = %10.5f" % (xopt[1]) print "rpeak = %10.5f pixels at M0 = " % (10.**xopt[2]), M0 print "sigma = %10.5f" % (xopt[3]) print "beta = %10.5f" % (xopt[4]) print "-Log(L) = %10.2f" % (fopt) print "phistar = %.3e" % (self.phistar[-1]) #print "func_calls = %6d" % (func_calls) #print "grad_calls = %6d" % (grad_calls) output = xopt t2 = time.time() print "Total fitting time: %.2f minutes" % ((t2 - t1) / 60.) return output if technique == 'l_bfgs_b': # parameter boundaries print "boundary:", boundary factr = 1.e7 # 1e12 for low accuracy; 1.e7 for moderate accuracy; # 10.0 for high accuracy print "factor: %.2e" % factr sys.stdout.flush() # Do optimization (xopt, fopt, d_fit) = optimize.fmin_l_bfgs_b(self.mlfunc, guess, args=mlfunc_args, fprime=None, approx_grad=True, factr=factr, epsilon=1.e-5, bounds=boundary, iprint=verbose) # Print results print "output array:", xopt print "alpha = %.5f" % xopt[0] print "Mstar = %10.5f" % xopt[1] print "rpeak = %10.5f pixels at M0 = " % (10.**xopt[2]), M0 print "sigma = %10.5f" % (xopt[3]) print "beta = %10.5f" % (xopt[4]) print "-Log(L) = %10.2f" % (fopt) print "phistar = %.3e" % (self.phistar[-1]) print "fitting information:", d_fit output = xopt t2 = time.time() print "Total fitting time: %.2f minutes" % ((t2 - t1) / 60.) return output # Not updated (130522) if technique == "test_pipe": print "boundary", boundary print "guess", guess xout = N.zeros(len(guess)) for i in range(len(guess)): if None in boundary[i]: xout[i] = N.random.normal(guess[i], 1.0) else: xout[i] = N.random.uniform(boundary[i][0], boundary[i][1]) print "xout:", xout return xout # Not updated (130522) if technique == "test_mlfunc": value = self.mlfunc(guess, *mlfunc_args) print value