async def get_best_solutions(task=None): assert task, 'No task specified' algorithm, problems = \ itemgetter('algorithm', 'problems')(task) name, params, hooks = \ itemgetter('name', 'params', 'hooks')(algorithm) assert name in ['de', 'pso'], \ 'Specified algorithm is not supported' X = [] if name == 'de': # build the archipelago archi = pg.archipelago() for prob in problems: isl = de.get_island(prob, params, hooks) archi.push_back(isl) archi.evolve() await asyncio.sleep(0) archi.wait() await asyncio.sleep(0) X = archi.get_champions_x() Y = archi.get_champions_f() hook_report = itemgetter('report')(hooks) if hook_report is not None: for i in range(len(Y)): gbest = (X[i], Y[i]) hook_report(gbest) elif name == 'pso': # build the archipelago archi = pg.archipelago() for prob in problems: isl = pso_origin.get_island(prob, params, hooks) archi.push_back(isl) archi.evolve() await asyncio.sleep(0) archi.wait() await asyncio.sleep(0) X = archi.get_champions_x() Y = archi.get_champions_f() hook_report = itemgetter('report')(hooks) if hook_report is not None: for i in range(len(Y)): gbest = (X[i], Y[i]) hook_report(gbest) X = np.array(X) return X
def run_example5(): import pygmo as pg from pykep import epoch from pykep.planet import jpl_lp from pykep.trajopt import mga_1dsm # We define an Earth-Venus-Earth problem (single-objective) seq = [jpl_lp('earth'), jpl_lp('venus'), jpl_lp('earth')] udp = mga_1dsm(seq=seq, t0=[epoch(5844), epoch(6209)], tof=[0.7 * 365.25, 3 * 365.25], vinf=[0.5, 2.5], add_vinf_dep=False, add_vinf_arr=True, multi_objective=False) pg.problem(udp) # We solve it!! uda = pg.sade(gen=100) archi = pg.archipelago(algo=uda, prob=udp, n=8, pop_size=20) print( "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" ) archi.evolve(10) archi.wait() sols = archi.get_champions_f() idx = sols.index(min(sols)) print("Done!! Solutions found are: ", archi.get_champions_f()) udp.pretty(archi.get_champions_x()[idx]) udp.plot(archi.get_champions_x()[idx])
def test2(): mma = pg.algorithm(pg.nlopt("mma")) p_toy = pg.problem(toy_problem_2()) archi = pg.archipelago(n=6, algo=mma, prob=p_toy, pop_size=1) archi.evolve() archi.wait_check() print("end of test2")
def goto_mars(): # We define an Earth-Mars problem (single-objective) seq = [jpl_lp('earth'), jpl_lp('mars')] udp = mga_1dsm(seq=seq, t0=[epoch(18 * 365.25 + 1), epoch(25 * 365.25 + 1)], tof=[0.7 * 365.25, 7 * 365.25], vinf=[0.5, 5], add_vinf_dep=False, add_vinf_arr=True, multi_objective=False) pg.problem(udp) # We solve it!! uda = pg.sade(gen=200) archi = pg.archipelago(algo=uda, prob=udp, n=8, pop_size=30) print( "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" ) archi.evolve(10) archi.wait() sols = archi.get_champions_f() idx = sols.index(min(sols)) print("Done!! Solutions found are: ", archi.get_champions_f()) print(f"\nThe best solution with Dv = {min(sols)[0]}:\n") udp.pretty(archi.get_champions_x()[idx]) udp.plot(archi.get_champions_x()[idx], savepath="plot.png")
def pygmo_es(): ARCHIPELAGO = True iterations = 10 uda = salimans_nes(iter=iterations) # user defined algorithm udp = saddle_space() # user defined problem prob = pg.problem(udp) # Beautiful white snow if ARCHIPELAGO: archi = pg.archipelago(algo=uda, prob=prob, n=1, pop_size=4) archi.evolve() # print(archi) # prints description of islands contained in archipelago # archi.wait() sols = archi.get_champions_f() idx = sols.index(min(sols)) sols_x = archi.get_champions_x() sol = sols[idx], sols_x[idx] else: pop = pg.population(prob=prob, size=4) pop.set_x(0, [-2.277654673852600, 0.047996554429844, 3.810000000000000]) pop.set_x(1, [-0.138042744751570, -0.144259374836607, 3.127288444444444]) pop.set_x(2, [-2.086814820119193, -0.000122173047640, 3.111181716545691]) uda.evolve(pop) sol = (pop.champion_f, pop.champion_x) print("Done! best solution is:") print(sol) return sol
def _test_archipelago(algo, problem, num=10000, stop_val=-1E99, log=logger()): udp = pygmo_udp(problem.fun, problem.bounds) prob = pg.problem(udp) best_y = math.inf best_x = None t0 = time.perf_counter() for _ in range(num): archi = pg.archipelago(n=mp.cpu_count(), algo=algo, prob=prob, pop_size=64) archi.evolve() archi.wait() ys = archi.get_champions_f() if not ys is None and len(ys) > 0: sort = np.argsort([y[0] for y in ys]) y = ys[sort[0]][0] if y < best_y: best_y = y best_x = archi.get_champions_x()[sort[0]] message = '{0} {1} {2} {3!s}'.format(problem.name, dtime(t0), best_y, list(best_x)) log.info(message) if best_y < stop_val: break return OptimizeResult(x=best_x, fun=best_y, success=True)
def run_example5(): import pygmo as pg from pykep import epoch from pykep.planet import jpl_lp from pykep.trajopt import mga_1dsm # We define an Earth-Venus-Earth problem (single-objective) seq = [jpl_lp('earth'), jpl_lp('venus'), jpl_lp('earth')] udp = mga_1dsm( seq=seq, t0=[epoch(5844), epoch(6209)], tof=[0.7 * 365.25, 3 * 365.25], vinf=[0.5, 2.5], add_vinf_dep=False, add_vinf_arr=True, multi_objective=False ) pg.problem(udp) # We solve it!! uda = pg.sade(gen=100) archi = pg.archipelago(algo=uda, prob=udp, n=8, pop_size=20) print( "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands") archi.evolve(10) archi.wait() sols = archi.get_champions_f() idx = sols.index(min(sols)) print("Done!! Solutions found are: ", archi.get_champions_f()) udp.pretty(archi.get_champions_x()[idx]) udp.plot(archi.get_champions_x()[idx])
def runTest(self): from dcgpy import symbolic_regression, generate_koza_quintic, kernel_set_double, gd4cgp import pygmo as pg import pickle X, Y = generate_koza_quintic() # Interface for the UDPs udp = symbolic_regression(points=X, labels=Y, rows=1, cols=20, levels_back=21, arity=2, kernels=kernel_set_double( ["sum", "diff", "mul", "pdiv"])(), n_eph=2, multi_objective=False, parallel_batches=0) prob = pg.problem(udp) pop = pg.population(prob, 10) # Interface for the UDAs uda = gd4cgp(max_iter=10, lr=0.1, lr_min=1e-6) algo = pg.algorithm(uda) algo.set_verbosity(0) # Testing some evolutions pop = algo.evolve(pop) # In parallel archi = pg.archipelago(prob=prob, algo=algo, n=16, pop_size=4) archi.evolve() archi.wait_check() # Pickling. self.assertTrue(repr(algo) == repr(pickle.loads(pickle.dumps(algo))))
def full_index_refine(self, rec_basis, num_ap, n_isl=20, pop_size=50, gen_num=2000, pos_tol=(0.007, 0.014, 0.06), rb_tol=0.12): """ Return refinement problems archipelago rec_basis - preliminary reciprocal lattice basis vectors matrix num_ap = [num_ap_x, num_ap_y] - convergent beam numerical apertures in x- and y-axes n_isl - number of islands of one frame pop_size - population size gen_num - maximum generations number of the refinement algorithm pos_tol - relative sample position tolerance rb_tol - lattice basis vectors matrix tolerance """ archi = pygmo.archipelago() for frame_idx, frame_strks in enumerate(iter(self)): frame_basis = rec_basis.dot( self.exp_set.rotation_matrix(frame_idx)) prob = frame_strks.rot_index_refine(rec_basis=frame_basis, num_ap=num_ap, pos_tol=pos_tol, rb_tol=rb_tol) pops = [ pygmo.population(size=pop_size, prob=prob, b=pygmo.mp_bfe()) for _ in range(n_isl) ] for pop in pops: archi.push_back(algo=pygmo.de(gen_num), pop=pop) return archi
def optim_alpha(data, phi, nclass, disttype, algo=None, archi=None, verbose=1, min_alpha=None, **kwargs): """Find optim alpha. Parameters ---------- data : np.ndarray Data to cluster. phi : float Degree of fuzziness or overlap of the generated clusters. Usually in the range [1, 2]. nclass : int Number of cluster to generate. disttype : {'euclidean', 'diagonal', 'mahalanobis'} Type of distance (the default is 'mahalanobis'). algo : pygmo.algorithm See `pygmo Algorithm documentation <https://esa.github.io/pagmo2/docs/python/py_algorithm.html>`_. archi : pygmo.archipelago See `pygmo Archipelago documentation <https://esa.github.io/pagmo2/docs/python/py_archipelago.html>`_. verbose : int, bool Show optimisation feedback (the default is 1). Currently not used. min_alpha : float The optimisation finds the optimal alpha value in the range [`min_alpha`, 1]. **kwargs : Parameters passed to the :func:`~.fkmeans._fuzzy_extragrades` function. Returns ------- float Optimum alpha value. """ prob = pg.problem( PTFOptim(data, phi, nclass, disttype, min_alpha, **kwargs)) if not algo: algo = pg.algorithm(pg.sade(gen=20, ftol=0.0005)) if not archi: logger.info('Initialising polulation') archi = pg.archipelago(n=multiprocessing.cpu_count(), algo=algo, prob=prob, pop_size=7) logger.info('Starting evolution...') archi.evolve() archi.wait() best = np.argmin(archi.get_champions_f()) logger.info('Done!') best_alpha = archi.get_champions_x()[best][0] logger.info('Optim alpha: {}'.format(best_alpha)) return best_alpha
def pygmo_es(): uda = salimans_nes(iter=3000) udp = saddle_space() prob = pg.problem(udp) archi = pg.archipelago(algo=uda, prob=udp, n=1000, pop_size=300) archi.evolve() sols = archi.get_champions_f() idx = sols.index(min(sols)) print("Done!! Solutions found are: ") print(archi.get_champions_f())
def archipielago_opt(f,n): start = time.time() funct=f(n) name=funct.get_name() a_cstrs_sa = pg.algorithm(pg.cstrs_self_adaptive(iters=1000)) t1=time.time() p_toy = pg.problem(funct) p_toy.c_tol = [1e-4, 1e-4] archi = pg.archipelago(n=16, algo=a_cstrs_sa, prob=p_toy, pop_size=10) archi.evolve(2) archi.wait_check()
def test1(): algo = pg.algorithm(pg.de(gen=1000, seed=126)) prob = pg.problem(toy_problem()) pop = pg.population(prob=prob, size=10) print(pop.champion_f) pop = algo.evolve(pop) print(pop.champion_f) # fine up to this point archi = pg.archipelago(n=6, algo=algo, prob=prob, pop_size=70) archi.evolve() archi.wait_check()
def archipelago(): udp = solo_mgar_udp([7000, 8000]) #uda = pg.sga(gen = 6000) uda = pg.sade(memory=True, variant=1, gen=6000) # instantiate an unconnected archipelago for _ in range(1000): archi = pg.archipelago(t=pg.topologies.unconnected()) for _ in range(32): alg = pg.algorithm(uda) #alg.set_verbosity(1) prob = pg.problem(udp) pop = pg.population(prob, 20) isl = pg.island(algo=alg, pop=pop) archi.push_back(isl) archi.evolve() archi.wait_check()
def nlopt_parallel_sample(): print("nlopt_parallel_sample started") prob = problem(sphere_function(3)) nl = nlopt('bobyqa') algo = algorithm(nl) archi = pg.archipelago(4, algo=algo, prob=prob, pop_size=20) print(archi) archi.evolve(10) archi.wait() res = [isl.get_population().champion_f for isl in archi] print(res) print("nlopt_parallel_sample completed")
def nlopt_parallel_sample(): print("nlopt_parallel_sample started") prob = problem(sphere_function(3)) nl = nlopt('bobyqa') algo = algorithm(nl) archi = pg.archipelago(4, algo=algo, prob=prob, pop_size=20) print(archi) archi.evolve(10) archi.wait() res = [isl.get_population().champion_f for isl in archi] print(res) print("nlopt_parallel_sample completed")
def archipelago(): print('interferometer sga archipelago') uda = pg.sga(gen=50000) # instantiate an unconnected archipelago archi = pg.archipelago(t=pg.topologies.unconnected()) t = time() for _ in range(8): alg = pg.algorithm(uda) #alg.set_verbosity(1) prob = pg.problem(udp) pop = pg.population(prob, 20) isl = pg.island(algo=alg, pop=pop) archi.push_back(isl) archi.evolve() archi.wait_check() print(f'archi: {time() - t:0.3f}s')
def _run_pygmo_parallel(self, algorithm, problem, number_of_islands=2, archipelago_gen=50): pygmo_archipelago = pg.archipelago(n=number_of_islands, algo=algorithm, prob=problem, pop_size=self.solver_args.popsize, seed=self.solver_args.seed) pygmo_archipelago.evolve(n=archipelago_gen) pygmo_archipelago.wait() champions_x = pygmo_archipelago.get_champions_x() champions_f = pygmo_archipelago.get_champions_f() champion_x, champion_f = self._select_best_pygmo_archipelago_solution( champions_x, champions_f) return PygmoSolutionWrapperParallel(champion_x=champion_x, champion_f=champion_f)
def pygmo_wrapper(optimizer, pop_generator, islands, pop_size, generations, evo_rounds, tolerance): archi = pg.archipelago(prob=pg.problem(optimizer), s_pol=pg.select_best(0.10), r_pol=pg.fair_replace(0.05), t=pg.fully_connected()) archi.set_migration_type(pg.migration_type.broadcast) for iteration in range(islands): pop = pg.population(pg.problem(optimizer)) for _ in range(pop_size): pop.push_back(pop_generator(optimizer)) archi.push_back(pop=pop, algo=pg.sade(gen=generations, variant=6, variant_adptv=2, ftol=tolerance, xtol=tolerance)) archi.evolve(evo_rounds) archi.wait() best_score = np.array(archi.get_champions_f()).min() best_index = archi.get_champions_f().index(best_score) best_model = archi.get_champions_x()[best_index] return best_model, best_score
def quick_start_demo(): # 1 - Instantiate a pygmo problem constructing it from a UDP # (user defined problem). prob = pg.problem(pg.schwefel(30)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sade(gen=100)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(10) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] print(res)
def quick_start_demo(): # 1 - Instantiate a pygmo problem constructing it from a UDP # (user defined problem). prob = pg.problem(pg.schwefel(30)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sade(gen=100)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(10) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] print(res)
def optimize_arch(popsize=10, generations=1, islands=1, evolutions=1,func=fonseca_fleming, dim=3): """ Function to optimize the given problem. Parameters can change to see their influence. """ info="Prob= {}".format(func(dim).get_name())+"\n"+\ "dim={} Pop={}, Gen={}, Isl={}, Evs={}".format(dim,popsize,generations,islands,evolutions) print(info) start=time.time() prob=pg.problem(func(dim)) """Start optimization process.""" # set up algorithm and optimize algo = pg.algorithm(pg.nspso(gen=generations)) archi = pg.archipelago(islands, algo=algo, prob=prob, pop_size=popsize) # evolution set_time=time.time() archi.evolve(evolutions) archi.wait_check() print("Build time :{}ms".format(round(1000*(set_time-start),2))) print("Calculation time :{}ms".format(round(1000*(time.time()-set_time),2))) #Get Data on final population fits_log, vectors_log = [], [] vectors = [isl.get_population().get_x() for isl in archi] vectors_log.append(vectors) fits = [isl.get_population().get_f() for isl in archi] fits_log.append(fits) table_fits=[[f[0], f[1]] for fit in fits for f in fit] #Plot the whole population to see the last evolution df = pd.DataFrame(table_fits) ax = df.plot(kind='scatter', x=0, y=1, grid=True) ax.set_xlabel('f1(x)') ax.set_ylabel('f2(x)') ax.set_xlim(0,1) ax.set_ylim(0,1) ax.set_title(info) #Save result to a file name= func(dim).get_name()+" "+func(dim).get_extra_info()
def rosenbrock_2d(x): generations = x[0] prob = pg.problem(pg.rosenbrock(2)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sga(gen=generations, cr = .90, eta_c = 1., m = 0.02, param_m = 1., param_s = 2, crossover = "exponential", mutation = "polynomial", selection = "tournament", seed = 3)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=20) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(10) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] import pdb; pdb.set_trace(); return res
def search(self, data : Data, model : Model, tid : int, **kwargs) -> np.ndarray: kwargs = kwargs['kwargs'] prob = SurrogateProblem(self.problem, self.computer, data, model, tid) try: algo = eval(f'pg.{kwargs["search_algo"]}(gen = kwargs["search_gen"])') except: raise Exception(f'Unknown optimization algorithm "{kwargs["search_algo"]}"') try: udi = eval(f'pg.{kwargs["search_udi"]}()') except: raise Exception('Unknown user-defined-island "{kwargs["search_udi"]}"') bestX = [] cond = False cpt = 0 while (not cond and cpt < kwargs['search_max_iters']): archi = pg.archipelago(n = kwargs['search_threads'], prob = prob, algo = algo, udi = udi, pop_size = kwargs['search_pop_size']) archi.evolve(n = kwargs['search_evolve']) archi.wait() champions_f = archi.get_champions_f() champions_x = archi.get_champions_x() indexes = list(range(len(champions_f))) indexes.sort(key=champions_f.__getitem__) for idx in indexes: if (champions_f[idx] < float('Inf')): cond = True # bestX.append(np.array(self.problem.PS.inverse_transform(np.array(champions_x[idx], ndmin=2))[0]).reshape(1, self.problem.DP)) bestX.append(np.array(champions_x[idx]).reshape(1, self.problem.DP)) break cpt += 1 if (kwargs['verbose']): print(tid, 'OK' if cond else 'KO'); sys.stdout.flush() # print("bestX",bestX) return (tid, bestX)
def human_readable_extra(self): return "n_iter=" + str(self.__n_iter) if __name__ == '__main__': gp = GrowthOptimization() prob = pg.problem(gp) algo = pg.algorithm(pg.pso(gen=300)) #algo = MonteCarlo(100) try: if not runParallel: pop = pg.population(prob, 40) print(dir(pop)) pop = algo.evolve(pop) print(pop.champion_f) else: archi = pg.archipelago(n=16, algo=algo, prob=prob, pop_size=20, seed=32) archi.evolve() print(archi) archi.wait() res = archi.get_champions_f() print(res) except: traceback.print_exc(file=sys.stdout)
def _minimize(self): # Gather the setup islands = self._setup_dict['islands'] pop_size = self._setup_dict['population_size'] evolution_cycles = self._setup_dict['evolution_cycles'] # Print some info print("\nPAGMO setup:") print("------------") print("- Number of islands: %i" % islands) print("- Population size per island: %i" % pop_size) print("- Evolutions cycles per island: %i\n" % evolution_cycles) Npar = len(self._internal_parameters) if is_parallel_computation_active(): wrapper = PAGMOWrapper(function=self.function, parameters=self._internal_parameters, dim=Npar) # use the archipelago, which uses the ipyparallel computation archi = pg.archipelago(udi=pg.ipyparallel_island(), n=islands, algo=self._setup_dict['algorithm'], prob=wrapper, pop_size=pop_size) archi.wait() # Display some info print("\nSetup before parallel execution:") print("--------------------------------\n") print(archi) # Evolve populations on islands print("Evolving... (progress not available for parallel execution)") # For some weird reason, ipyparallel looks for _winreg on Linux (where does # not exist, being a Windows module). Let's mock it with an empty module' mocked = False if os.path.exists("_winreg.py") is False: with open("_winreg.py", "w+") as f: f.write("pass") mocked = True archi.evolve() # Wait for completion (evolve() is async) archi.wait_check() # Now remove _winreg.py if needed if mocked: os.remove("_winreg.py") # Find best and worst islands fOpts = np.array(map(lambda x:x[0], archi.get_champions_f())) xOpts = archi.get_champions_x() else: # do not use ipyparallel. Evolve populations on islands serially wrapper = PAGMOWrapper(function=self.function, parameters=self._internal_parameters, dim=Npar) xOpts = [] fOpts = np.zeros(islands) with progress_bar(iterations=islands, title="pygmo minimization") as p: for island_id in range(islands): pop = pg.population(prob=wrapper, size=pop_size) for i in range(evolution_cycles): pop = self._setup_dict['algorithm'].evolve(pop) # Gather results xOpts.append(pop.champion_x) fOpts[island_id] = pop.champion_f[0] p.increase() # Find best and worst islands min_idx = fOpts.argmin() max_idx = fOpts.argmax() fOpt = fOpts[min_idx] fWorse = fOpts[max_idx] xOpt = np.array(xOpts)[min_idx] # Some information print("\nSummary of evolution:") print("---------------------") print("Best population has minimum %.3f" % (fOpt)) print("Worst population has minimum %.3f" % (fWorse)) print("") # Transform to numpy.array best_fit_values = np.array(xOpt) return best_fit_values, fOpt
import pygmo as pg import numpy as np prob = pg.problem(pg.cec2014(prob_id=5, dim=10)) algo = pg.algorithm(pg.cmaes(gen=100)) archi = pg.archipelago(8, algo=algo, prob=prob, pop_size=20) archi.evolve(1000) archi.wait() res = [isl.get_population().champion_f for isl in archi] res = np.array(res) print(f"Problem {5}: {res.mean()}")
def search(self, data : Data, models : Collection[Model], tid : int, **kwargs) -> np.ndarray: kwargs = kwargs['kwargs'] prob = SurrogateProblem(self.problem, self.computer, data, models, tid) try: udi = eval(f'pg.{kwargs["search_udi"]}()') except: raise Exception('Unknown user-defined-island "{kwargs["search_udi"]}"') if(self.problem.DO==1): # single objective try: algo = eval(f'pg.{kwargs["search_algo"]}(gen = kwargs["search_gen"])') except: raise Exception(f'Unknown optimization algorithm "{kwargs["search_algo"]}"') bestX = [] cond = False cpt = 0 while (not cond and cpt < kwargs['search_max_iters']): archi = pg.archipelago(n = kwargs['search_threads'], prob = prob, algo = algo, udi = udi, pop_size = kwargs['search_pop_size']) archi.evolve(n = kwargs['search_evolve']) archi.wait() champions_f = archi.get_champions_f() champions_x = archi.get_champions_x() indexes = list(range(len(champions_f))) indexes.sort(key=champions_f.__getitem__) for idx in indexes: if (champions_f[idx] < float('Inf')): cond = True # bestX.append(np.array(self.problem.PS.inverse_transform(np.array(champions_x[idx], ndmin=2))[0]).reshape(1, self.problem.DP)) bestX.append(np.array(champions_x[idx]).reshape(1, self.problem.DP)) break cpt += 1 else: # multi objective try: algo = eval(f'pg.algorithm(pg.{kwargs["search_algo"]}(gen = kwargs["search_gen"]))') except: raise Exception(f'Unknown optimization algorithm "{kwargs["search_algo"]}"') bestX = [] cond = False cpt = 0 while (not cond and cpt < kwargs['search_max_iters']): pop = pg.population(prob = prob, size = kwargs['search_pop_size'], seed = cpt+1) pop = algo.evolve(pop) """ It seems pop.get_f() is already sorted, no need to perform the following sorting """ # if(self.problem.DO==2): # front = pg.non_dominated_front_2d(pop.get_f()) # else: # ndf, dl, dc, ndr = pg.fast_non_dominated_sorting(pop.get_f()) # front = ndf[0] # fs = pop.get_f()[front] # xs = pop.get_x()[front] # bestidx = pg.select_best_N_mo(points = fs, N = kwargs['search_more_samples']) # xss = xs[bestidx] # fss = fs[bestidx] # # print('bestidx',bestidx) firstn = min(int(kwargs['search_more_samples']),np.shape(pop.get_f())[0]) fss = pop.get_f()[0:firstn] xss = pop.get_x()[0:firstn] # print('firstn',firstn,int(kwargs['search_more_samples']),np.shape(pop.get_f()),xss) if(np.max(fss)< float('Inf')): cond = True bestX.append(xss) break cpt += 1 if (kwargs['verbose']): print(tid, 'OK' if cond else 'KO'); sys.stdout.flush() # print("bestX",bestX) return (tid, bestX)
def _minimize(self): # Gather the setup islands = self._setup_dict['islands'] pop_size = self._setup_dict['population_size'] evolution_cycles = self._setup_dict['evolution_cycles'] # Print some info print("\nPAGMO setup:") print("------------") print("- Number of islands: %i" % islands) print("- Population size per island: %i" % pop_size) print("- Evolutions cycles per island: %i\n" % evolution_cycles) Npar = len(self._internal_parameters) if is_parallel_computation_active(): wrapper = PAGMOWrapper(function=self.function, parameters=self._internal_parameters, dim=Npar) # use the archipelago, which uses the ipyparallel computation archi = pg.archipelago(udi=pg.ipyparallel_island(), n=islands, algo=self._setup_dict['algorithm'], prob=wrapper, pop_size=pop_size) archi.wait() # Display some info print("\nSetup before parallel execution:") print("--------------------------------\n") print(archi) # Evolve populations on islands print( "Evolving... (progress not available for parallel execution)") # For some weird reason, ipyparallel looks for _winreg on Linux (where does # not exist, being a Windows module). Let's mock it with an empty module' mocked = False if os.path.exists("_winreg.py") is False: with open("_winreg.py", "w+") as f: f.write("pass") mocked = True archi.evolve() # Wait for completion (evolve() is async) archi.wait_check() # Now remove _winreg.py if needed if mocked: os.remove("_winreg.py") # Find best and worst islands fOpts = np.array(map(lambda x: x[0], archi.get_champions_f())) xOpts = archi.get_champions_x() else: # do not use ipyparallel. Evolve populations on islands serially wrapper = PAGMOWrapper(function=self.function, parameters=self._internal_parameters, dim=Npar) xOpts = [] fOpts = np.zeros(islands) with progress_bar(iterations=islands, title="pygmo minimization") as p: for island_id in range(islands): pop = pg.population(prob=wrapper, size=pop_size) for i in range(evolution_cycles): pop = self._setup_dict['algorithm'].evolve(pop) # Gather results xOpts.append(pop.champion_x) fOpts[island_id] = pop.champion_f[0] p.increase() # Find best and worst islands min_idx = fOpts.argmin() max_idx = fOpts.argmax() fOpt = fOpts[min_idx] fWorse = fOpts[max_idx] xOpt = np.array(xOpts)[min_idx] # Some information print("\nSummary of evolution:") print("---------------------") print("Best population has minimum %.3f" % (fOpt)) print("Worst population has minimum %.3f" % (fWorse)) print("") # Transform to numpy.array best_fit_values = np.array(xOpt) return best_fit_values, fOpt
def chi_optimize(method='pso', parallel=False, N_ind=100, N_gen=30, iset=0, show_results=False, dir='../data/', file_name='chi_fit'): # Load experimental data N_set = 2 ca_data = svu.loaddata('../data/herzog_data.pkl')[0] po_data = svu.loaddata('../data/fit_po.pkl')[0] lr_data = svu.loaddata('../data/fit_lra.pkl')[0] # Get boundary dictionary bounds_dict = models.model_bounds(model='chi') # Save effective bounds and scaling bounds, scaling = bounds_for_exploration(bounds_dict, normalized=False) # Prepare model parameters c0 = 5.0 # c0 = 15.0 params = np.asarray(np.r_[po_data[[0, 1, 0, 2, 3]], lr_data[[0, 1]], c0, iset]) args = (params, ca_data) #----------------------------------------------------------------------------------------------------------- # Effective evolution #----------------------------------------------------------------------------------------------------------- # Size parameters # N_var = problem_dimension(model='lra') N_individuals = N_ind N_var = problem_dimension(model='chi') N_generations = N_gen * N_var # Tolerances FTOL = 1e-6 XTOL = 1e-6 # prob = pg.problem(fit_problem('lra',args)) prob = pg.problem(fit_problem('chi', args)) # Optimization # NOTE: We keep each algorithm self contained here because different setting of parameters for evolution are # needed depending on the chose algorithm if method == 'pso': algo = pg.algorithm( pg.pso(gen=N_generations, variant=5, neighb_type=4, max_vel=0.8)) elif method == 'bee': algo = pg.algorithm(pg.bee_colony(gen=N_generations, limit=2)) elif method == 'de': algo = pg.algorithm(pg.de(gen=N_generations, ftol=FTOL, tol=XTOL)) # Single-node optimation to run on local machine / laptop # N_individuals = 100 # N_generations = 40 * N_var # verbosity = 20 if not parallel: verbosity = 20 algo.set_verbosity(verbosity) pop = pg.population(prob, size=N_individuals) pop = algo.evolve(pop) best_fitness = pop.get_f()[pop.best_idx()] print(best_fitness) x_rescaled = rescale_vector(pop.champion_x, model='chi', normalized=True, bounds=bounds, scaling=scaling) # Show results of fit if show_results: astro = models.Astrocyte( model='chi', d1=po_data[0], d2=po_data[1], d3=po_data[0], d5=po_data[2], a2=po_data[3], c0=c0, c1=0.5, rl=0.1, Ker=0.1, rc=lr_data[0], ver=lr_data[1], vbeta=x_rescaled[0], vdelta=x_rescaled[1], v3k=x_rescaled[2], r5p=x_rescaled[3], ICs=np.asarray([x_rescaled[6], x_rescaled[4], x_rescaled[5]])) options = su.solver_opts(t0=ca_data['time'][iset][0], tfin=ca_data['time'][iset][-1], dt=1e-4, atol=1e-8, rtol=1e-6, method="gsl_msadams") astro.integrate(algparams=options, normalized=True) ca_trace = ca_data['smoothed'][iset] plt.plot(ca_data['time'][0], ca_trace, 'k-', astro.sol['ts'], astro.sol['ca'], 'r-') plt.show() else: # Parallel version to run on cluster N_evolutions = 100 N_islands = 10 # Initiate optimization algo = pg.algorithm( pg.pso(gen=N_generations, variant=5, neighb_type=4, max_vel=0.8)) archi = pg.archipelago(n=N_islands, algo=algo, prob=prob, pop_size=N_individuals) archi.evolve(N_evolutions) archi.wait() imin = np.argmin(archi.get_champions_f()) x_rescaled = rescale_vector(archi.get_champions_x()[imin], model='chi', normalized=True, bounds=bounds, scaling=scaling) print archi.get_champions_f()[imin] print x_rescaled svu.savedata([x_rescaled], dir + file_name + '.pkl')
print (pop_fb.champion_f) l.append((pop_fb.champion_f,pop_fb.champion_x)) l = sorted(l, key = lambda x: x[0]) # Comments: # Using a different solution technique: pygmo's arichipelago/island # In[96]: algo_fb = pg.algorithm(pg.sade(gen = 200)) l = list() for i in range (10) : archi = pg.archipelago(n = 16, algo = algo_fb, prob = prob_fb, pop_size = 8, seed = 20) archi.evolve(15) best_island = sorted(archi, key = lambda x: x.get_population().champion_f[0])[0] l.append((best_island.get_population().champion_f,best_island.get_population().champion_x)) print (best_island.get_population().champion_f[0]) l = sorted(l, key = lambda x: x[0]) # In[103]: print (traj_fb.pretty(l[0][1])) # Lauch date around 8000 MJD2000, so we restrict the launch date around that point and run the optimization again
import pygmo as pg # 1 - Instantiate a pygmo problem constructing it from a UDP # (user defined problem). prob = pg.problem(pg.schwefel(30)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sade(gen=100)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, size=20) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(10) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] print(res)
def optimize(log_output=False, dest='Mars'): # AU # earth_radius = 0.00004258756 # AU^3/year^2 # earth_attractor = 0.0001184 phi = [np.random.uniform(0, 2 * np.pi) for i in range(2) ] + [0] + [np.random.uniform(0, 2 * np.pi) for i in range(5)] Earth = Planet(1, 1, phi[2], "Earth") Mercury = Planet(0.374496, 0.241, phi[0], "Mercury") Mars = Planet(1.5458, 1.8821, phi[3], "Mars") Venus = Planet(0.726088, 0.6156, phi[1], "Venus") Jupiter = Planet(5.328, 11.87, phi[4], "Jupiter") Saturn = Planet(9.5497, 29.446986, phi[5], "Saturn") Uranus = Planet(19.2099281, 84.01538, phi[6], "Uranus") Neptune = Planet(30.0658708, 164.78845, phi[7], "Neptune") num_gens = 1 num_evolutions = 75 pop_size = 200 cometX = Comet() if dest == "Comet": planets = [ Earth, Earth, Mercury, Mars, Venus, Jupiter, Saturn, Neptune, Uranus, cometX ] else: choices = [ Earth, Earth, Mercury, Mars, Venus, Jupiter, Saturn, Neptune, Uranus ] destination = [x for x in choices if x.get_name() == dest] choices.remove(destination[0]) planets = choices + [destination[0]] if dest == "Venus" or dest == "Mercury": max_enctrs = 1 else: max_enctrs = len(planets) - 2 times = [0] + [0.1] * (max_enctrs + 1) max_times = [5] * (max_enctrs + 2) # optimize t0 = time.time() udp = gprob(planets, times, max_times, max_enctr=max_enctrs) uda = pg.algorithm(pg.sade(gen=num_gens, memory=True)) if (not log_output ): # this avoids the persistent looping to get the fitness data archi = pg.archipelago(algo=uda, prob=udp, n=8, pop_size=pop_size) archi.evolve(num_evolutions) archi.wait() else: # this is where we loop and evolve and get the fitness data for each island archi = pg.archipelago(algo=uda, prob=udp, n=8, pop_size=pop_size) islands = [] for i in range(num_evolutions): archi.evolve() archi.wait() avgFit = [ -1.0 * np.average(island.get_population().get_f()) for island in archi ] islands.append(np.array(avgFit)) # islands.append(np.array(archi.get_champions_f())) # get the best scores from each island after each stage showlog(np.array(islands), 8, num_evolutions) t1 = time.time() sols = archi.get_champions_f() idx = sols.index(min(sols)) # print("index: {}, Scores: ".format(idx) + str(sols) + "\n\n") mission = udp.pretty(archi.get_champions_x()[idx]) # [print(str(l) + "\n") for l in mission] convert(mission[0], mission[1], mission[2]) logger.log(mission[1][0], mission[1][-1], phi) print("\n\nTime for soln: {} sec\n\n".format(t1 - t0))
############################################################################### ##### Scripts ################################################################ logs("Start Script", __file__) # 1 - Instantiate a pygmo problem constructing it from a UDP # (user defined problem). prob = pg.problem(pg.schwefel(10)) # 2 - Instantiate a pagmo algorithm algo = pg.algorithm(pg.sade(gen=10)) # 3 - Instantiate an archipelago with 16 islands having each 20 individuals archi = pg.archipelago(16, algo=algo, prob=prob, pop_size=10) # 4 - Run the evolution in parallel on the 16 separate islands 10 times. archi.evolve(5) # 5 - Wait for the evolutions to be finished archi.wait() # 6 - Print the fitness of the best solution in each island res = [isl.get_population().champion_f for isl in archi] print(res) ############################################################################### ##### End Script Rnme Folder ################################################## #### Rename folder ################