def test_polish(self): solver = desolver.DESolver(error_func, [(-100,100)]*3, 30, 200, method = desolver.DE_RAND_1, args=[self.xData,self.yData], scale=0.8, crossover_prob=0.9, goal_error=.01, polish=True, verbose=False, use_pp = False, pp_modules=['numpy']) assert(solver.best_error <= .01)
def test_seed(self): solver = desolver.DESolver(error_func, [(-100,100)]*3, 30, 200, method = desolver.DE_LOCAL_TO_BEST_1, args=[self.xData,self.yData], seed = numpy.random.uniform(-1,1,size=(4,3)), scale=0.5, crossover_prob=0.9, goal_error=.01, polish=False, verbose=False, use_pp = False, pp_modules=['numpy']) assert(solver.best_error <= .01)
def test_pickle(self): # run the example solver solver = desolver.DESolver(error_func, [(-100,100)]*3, 30, 200, method = desolver.DE_RAND_1, args=[self.xData,self.yData], scale=0.8, crossover_prob=0.9, goal_error=.01, polish=False, verbose=False, use_pp = False, pp_modules=['numpy']) # pickle and unpickle it pstr = pickle.dumps(solver) solver2 = pickle.loads(pstr)
xData = numpy.array([5.357, 9.861, 5.457, 5.936, 6.161, 6.731]) yData = numpy.array([0.376, 7.104, 0.489, 1.049, 1.327, 2.077]) # start timer tStart = time.time() # set up the solver solver = desolver.DESolver( dep_error_func, [(-100, 100)] * 3, 30, 600, #method = desolver.DE_BEST_1, #method = desolver.DE_BEST_1_JITTER, #method = desolver.DE_LOCAL_TO_BEST_1, method=desolver.DE_RAND_1, args=[xData, yData], scale=[0.5, 1.0], crossover_prob=0.9, goal_error=.01, polish=False, verbose=True, use_pp=True, pp_modules=['global_fun'], pp_depfuncs=[]) tElapsed = time.time() - tStart print print "Best generation:", solver.best_generation print "Best individual:", solver.best_individual print "Best error:", solver.best_error, \ ": Elapsed time", tElapsed, \ 'seconds for', solver.generation+1, 'generation(s)'
return numpy.prod(30.*numpy.sign(-x[include]-5.12)) testbed1 = [(lambda x:(x*x).sum(), [-5.12]*3, [5.12]*3, 1e-6,3,10,0.9,0.1), # NP changed from 5 to 10! (lambda x:(x[0]**2-x[1])**2+(1.-x[0])**2,[-2.048]*2,[2.048]*2,1e-6,2,10,0.9,0.9), (testbed1_3, [-5.12]*5, [5.12]*5, 1e-6,5,10,0.9,0.0)] class Job: def __init__(self,costfunction): self.costfunction = costfunction self.nfe = 0 def evaluateFitness(self,x): self.nfe += 1 cost = self.costfunction(x) return -cost nrepeat = 20 for iproblem,(cost,minbounds,maxbounds,VTR,npar,NP,F,CR) in enumerate(testbed1): job = Job(cost) solver = desolver.DESolver(job,NP,10000,minbounds,maxbounds, F=F,CR=CR,strictbounds=False,verbose=False, reltol=0.,abstol=0.,minfitnesstoreach=-VTR) print 'Problem %i' % iproblem nfes = [] for ireplica in range(nrepeat): job.nfe = 0 sol = solver.Solve() nfe = job.nfe print ' solution %i: %s, FV = %s, NFE=%i' % (ireplica,sol,-job.evaluateFitness(sol),nfe) nfes.append(job.nfe) print ' mean NFE = %i' % (float(sum(nfes))/len(nfes))