def ipopcmaes(fitness_eval,domain,dim,maxevals): # restart with increasing population size (IPOP) bestever = cma.BestSolution() (min,max)=domain restevals = maxevals popsize = 4+int(3*math.log(dim)) initial = np.random.rand(dim)*(max-min)+min print "====================================" print "Popsize: %d" %popsize while restevals > 0: # for lam in 10 * 2**np.arange(7): # 10, 20, 40, 80, ..., 10 * 2**6 initial = np.random.rand(dim)*(max-min)+min es = cma.CMAEvolutionStrategy(initial, (max-min)/2.0, # initial std sigma0 # bounds { 'maxfevals': restevals, 'CMA_active': 1, # 'verb_disp': 500, # 'tolfun': 1e-12, 'popsize': popsize, 'verb_append': bestever.evalsall, 'bounds': [min,max]}) # pass options #logger = logging.getLogger('ipopcmaes') while not es.stop(): X = es.ask() # get list of new solutions fit = [fitness_eval(x) for x in X] # evaluate each solution es.tell(X, fit) # besides for termination only the ranking in fit is used # display some output #logger.add() # add a "data point" to the log, writing in files es.disp() # uses option verb_disp with default 100 #print('termination:', es.stop()) #cma.pprint(es.best.__dict__) bestever.update(es.best) # show a plot #logger.plot() if bestever.f < 1e-8: # global optimum was hit break cma.pprint(es.result()) #cma.pprint(bestever.x) #cma.pprint(bestever.f) #print "NumEvals: %d" %es.result()[3] if bestever.f < 1e-8: # global optimum was hit break else: evals = es.result()[3] restevals = restevals - evals print "It rests %d evaluations" %restevals popsize = 2*popsize print "Popsize: %d" %popsize print "-------------------------------" print "====================================" return (bestever.x,bestever.f)
import cma import sys # construct an object instance in 4-D, sigma0=1 es = cma.CMAEvolutionStrategy(4 * [1], 1, {'seed': 234}) id = 0 while not es.stop(): X = es.ask() print X es.tell(X, [cma.fcts.elli(x) for x in X]) es.disp() # by default sparse, see option verb_disp cma.pprint(es.result())
while X_work: pool = [] for ji in range(cma_jobs): if len(X_work) == 0: break print "Launching #%d: " % id ret_q = multiprocessing.Queue() #eval(X_work[0], id, ret_q) p = multiprocessing.Process(target=eval, args = (X_work[0], id , ret_q)) X_work = X_work[1:] p.start() pool.append( (id, p, ret_q) ) id += 1 for id_src, p, ret_q in pool: p.join() r = ret_q.get() if isinstance(r, KeyboardInterrupt): raise r elif isinstance(r, Exception): r = 0.0 print "got %f from %d" % (r, id_src) tells.append(r) print tells es.tell(X, tells) es.disp() # by default sparse, see option verb_disp cma.pprint(es.result())