def create_variants(self, n, desc, category, constructor): def assign_2nd_alg(archipelago, algo): if category == 'rings': for island in archipelago.topology.every_other_island(): island.algorithm = algo elif hasattr(archipelago.topology, 'endpoints'): for island in archipelago.topology.endpoints: island.algorithm = algo elif isinstance(archipelago.topology, FullyConnectedTopology): for island in islice(archipelago.topology.islands, None, None, 2): island.algorithm = algo return archipelago def assign_algs(archipelago, algos): ''' Evenly partitions and assigns algorithms to islands. ''' for island,algo in zip(archipelago.topology.islands, cycle(algos)): island.algorithm = algo g = self.generations self.new_topology( desc='{}, de'.format(desc), category=category, algorithms=['de'], archipelago=Archipelago(constructor(de(gen=g),n))) self.new_topology( desc='{}, de1220'.format(desc), category=category, algorithms=['de1220'], archipelago=Archipelago(constructor(de1220(gen=g),n))) self.new_topology( desc='{}, sade'.format(desc), category=category, algorithms=['sade'], archipelago=Archipelago(constructor(sade(gen=g),n))) self.new_topology( desc='{}, bee_colony'.format(desc), category=category, algorithms=['bee_colony'], archipelago=Archipelago(constructor(bee_colony(gen=g),n))) # de + nelder mead combo self.new_topology( desc='{}, de+nelder mead'.format(desc), category=category, algorithms=['de','neldermead'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_nelder_mead())) # de + praxis combo self.new_topology( desc='{}, de+praxis'.format(desc), category=category, algorithms=['de','praxis'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_praxis())) # de + sade combo self.new_topology( desc='{}, de+sade'.format(desc), category=category, algorithms=['de','sade'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), sade(gen=g)))
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 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 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 sade(problem, population_size, params): ''' Execute the Pygmo SADE algorithm on an optimisation problem with the population size and parameters specified. The SADE possible set of parameters are: * variant: mutation variant: 1 -> best/1/exp 10 -> rand/2/bin 2 -> rand/1/exp 11 -> rand/3/exp 3 -> rand-to-best/1/exp 12 -> rand/3/bin 4 -> best/2/exp 13 -> best/3/exp 5 -> rand/2/exp 14 -> best/3/bin 6 -> best/1/bin 15 -> rand-to-current/2/exp 7 -> rand/1/bin 16 -> rand-to-current/2/bin 8 -> rand-to-best/1/bin 17 -> rand-to-best-and-current/2/exp 9 -> best/2/bin 18 -> rand-to-best-and-current/2/bin * variant_adptv: scale factor F and crossover rate CR adaptation scheme to be used * ftol: stopping criteria on the function tolerance * xtol: stopping criteria on the step tolerance Parameters ---------- - problem: the problem to optimise. It must comply to the Pygmo requirements, i.e. being an instance of an UDP class - population_size: The size of the population - params: dictionnary of parameters for the SADE algorithm Return ------ - log: the logs of the execution of the optimisation problem with the population size - duration: the total duration of the resolution of the problem ''' # Extract algorithm parameters nb_generation = params["nb_generation"] variant = params["variant"] variant_adptv = params["variant_adptv"] ftol = params["ftol"] xtol = params["xtol"] algo = pg.algorithm( pg.sade(gen=nb_generation, variant=variant, variant_adptv=variant_adptv, ftol=ftol, xtol=xtol)) algo.set_verbosity(1) solution = pg.population(problem, size=population_size, b=None) startt = datetime.now() solution = algo.evolve(solution) duration = (datetime.now() - startt) uda = algo.extract(pg.sade) log = uda.get_log() return (log, duration, solution.champion_f, solution.champion_x)
def optimize_pagmo(): solo_mgar = solo_mgar_udp([7000, 8000]) for i in range(6000): prob = pg.problem(solo_mgar) pop = pg.population(prob=prob, size=32) alg = pg.algorithm(pg.sade(memory=True, gen=1)) pop = alg.evolve(pop) print(i, pop.champion_f, solo_mgar.fitness(pop.champion_x))
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 genetic_algorithm(q_function, total_evals=100): prob = pg.problem(GeneticAlgoProblem(q_function)) population_size = 20 generations = total_evals / population_size algo = pg.algorithm(pg.sade(gen=generations)) pop = pg.population(prob, size=population_size) pop = algo.evolve(pop) print - pop.champion_f return pop.champion_x, -pop.champion_f
def solver(dimension, lower_bound, upper_bound, optim, bias, popsize): global algo global pop global niter global log global curve prob = pg.problem(rastrigin_prob(dimension, lower_bound, upper_bound, optim, bias)) algo = pg.algorithm(pg.sade(gen=14000, variant=2, variant_adptv=1, ftol=1e-06, xtol=1e-06)) algo.set_verbosity(1) pop = pg.population(prob, popsize) pop = algo.evolve(pop) log = algo.extract(pg.sade).get_log() curve = [x[2] for x in log] niter = log[-1][0] return prob, algo, pop, log, niter, curve
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 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 pygmo(self, x0, bnds, options): class pygmo_objective_fcn: def __init__(self, obj_fcn, bnds): self.obj_fcn = obj_fcn self.bnds = bnds def fitness(self, x): return [self.obj_fcn(x)] def get_bounds(self): return self.bnds def gradient(self, x): return pygmo.estimate_gradient_h(lambda x: self.fitness(x), x) timer_start = timer() pop_size = int(np.max([35, 5 * (len(x0) + 1)])) if options['stop_criteria_type'] == 'Iteration Maximum': num_gen = int(np.ceil(options['stop_criteria_val'] / pop_size)) elif options['stop_criteria_type'] == 'Maximum Time [min]': num_gen = int(np.ceil(1E20 / pop_size)) prob = pygmo.problem(pygmo_objective_fcn(self.obj_fcn, tuple(bnds))) pop = pygmo.population(prob, pop_size) pop.push_back(x=x0) # puts initial guess into the initial population # all coefficients/rules should be optimized if they're to be used if options['algorithm'] == 'pygmo_DE': #F = (0.107 - 0.141)/(1 + (num_gen/225)**7.75) F = 0.2 CR = 0.8032 * np.exp(-1.165E-3 * num_gen) algo = pygmo.algorithm(pygmo.de(gen=num_gen, F=F, CR=CR, variant=6)) elif options['algorithm'] == 'pygmo_SaDE': algo = pygmo.algorithm(pygmo.sade(gen=num_gen, variant=6)) elif options['algorithm'] == 'pygmo_PSO': # using generational version algo = pygmo.algorithm(pygmo.pso_gen(gen=num_gen)) elif options['algorithm'] == 'pygmo_GWO': algo = pygmo.algorithm(pygmo.gwo(gen=num_gen)) elif options['algorithm'] == 'pygmo_IPOPT': algo = pygmo.algorithm(pygmo.ipopt()) pop = algo.evolve(pop) x = pop.champion_x obj_fcn, x, shock_output = self.Scaled_Fit_Fun(x, optimizing=False) msg = 'Optimization terminated successfully.' success = True res = { 'x': x, 'shock': shock_output, 'fval': obj_fcn, 'nfev': pop.problem.get_fevals(), 'success': success, 'message': msg, 'time': timer() - timer_start } return res
def evolve_population_DE_algorithm(gen, variant, variant_adpt, population): algo = pygmo.algorithm(pygmo.sade(gen, variant, variant_adpt)) algo.set_verbosity(1) new_pop = algo.evolve(population) return new_pop, algo
# print('Image NOT added!') # # plt.close('all') return [f2] # tpch c1 sigma1 c2 sigma2 tpch2 def get_bounds(self): return ([0], [0.5]) def ceff(T, Tpch, Tpch2, c0, c1, c2, sigma1, sigma2): return c0 + c1 * np.exp((-(T - Tpch) * (T - Tpch)) / sigma1) + c2 * np.exp( (-(T - Tpch2) * (T - Tpch2)) / sigma2) algo = pg.algorithm(pg.sade(gen=100)) algo.set_verbosity(1) prob = pg.problem(heat_f2()) pop = pg.population(prob, 7) pop = algo.evolve(pop) # isl = island(algo = de(10), prob = heat_f2(), size=20, udi=thread_island()) # islands = [island(algo = de(gen = 100000, F=effe, CR=cross), prob=heat_f2(), size=20, seed=32) for effe in [0.3,0.5,0.7,0.9] for cross in [0.3,0.5,0.7,0.9]] # _ = [isl.evolve() for isl in islands] # _ = [isl.wait() for isl in islands] d_sol = pop.champion_x[0] print(d_sol) # bounds = [(0.005, 0.5, 0.5), (0.025, 1, 1)] # if __name__ == '__main__':
def optimize(self, niter=500, minimizer_kwargs=None, nmin=1000, kforce=100., gradient=False, print_fun=None, popsize=50, stepsize=0.05, optimizer="evolution", seed=None): self.kforce = kforce if type(seed) == type(None): seed = np.random.randint(999999) else: seed = int(seed) np.random.seed(seed) pygmo.set_global_rng_seed(seed=seed) self.set_x0() bounds = gist_bounds(self.xmin, self.xmax, Tconst=True) min_bounds = bounds.get_bounds_for_minimizer() if optimizer == "evolution": ### This works , because pygmo makes deepcopies of this object ### in order to remain "thread safe" during all following operations prob = pygmo.problem(self) if self.decomp: if (popsize % 4) != 0: popsize = (popsize / 4) * 4 if popsize < 5: popsize = 8 pop = pygmo.population(prob=prob, size=popsize) if self.decomp: ### For NSGA2, popsize must be >4 and also ### a multiple of four. algo = pygmo.algorithm(pygmo.nsga2(gen=niter)) #algo = pygmo.algorithm(pygmo.moead(gen=niter)) else: algo = pygmo.algorithm(pygmo.sade(gen=niter)) if self.verbose: algo.set_verbosity(1) pop = algo.evolve(pop) for x in pop.get_x(): print_fun(x) print_fun.flush() elif optimizer == "brute": self.anal_grad = False self.anal_boundary = False N_dim = self._x0.size niter_count = np.zeros(self._x0.size, dtype=int) for i in range(self._x0.size): self._x0[i] = min_bounds[i][0] _diff = min_bounds[i][1] - min_bounds[i][0] niter_count[i] = int(_diff / stepsize) prop = propagator(self._x0, N_dim, stepsize, niter_count) stop = False _stop = False if nmin > 0: self.anal_grad = True self.anal_boundary = False prob = pygmo.problem(self) pop = pygmo.population(prob=prob, size=1) algo = pygmo.algorithm(pygmo.nlopt("slsqp")) algo.maxeval = nmin if self.verbose: algo.set_verbosity(1) while (not stop): if nmin > 0: self.anal_grad = gradient if self.anal_boundary: min_bounds = None bounds = None pop.set_x(0, self._x0) pop = algo.evolve(pop) x = pop.get_x()[0] else: x = self._x0 if print_fun != None: print_fun(x) print_fun.flush() ### propagate self._x0 prop.add() if _stop: stop = True _stop = prop.are_we_done() elif optimizer == "basinhopping": prob = pygmo.problem(self) pop = pygmo.population(prob=prob, size=popsize) algo = pygmo.algorithm(uda=pygmo.mbh( pygmo.nlopt("slsqp"), stop=100, perturb=self.steps * 0.1)) if self.verbose: algo.set_verbosity(1) pop = algo.evolve(pop) for x in pop.get_x(): print_fun(x) print_fun.flush() else: raise ValueError("Optimizer %s is not known." % optimizer)
#archi = pg.archipelago(n=4,algo=algo, pop=pop1) print(archi) archi.evolve() archi.wait() archi.wait_check() print(archi) import pygmo as pg # The user-defined problem udp = pg.schwefel(dim=20) # The pygmo problem prob = pg.problem(udp) # For a number of generation based algorithms we can use a similar script to run and average over 25 runs. udas = [ pg.sade(gen=500), pg.de(gen=500), pg.de1220(gen=500), pg.pso(gen=500), pg.bee_colony(gen=250, limit=20) ] for uda in udas: logs = [] for i in range(25): algo = pg.algorithm(uda) algo.set_verbosity(1) # regulates both screen and log verbosity pop = pg.population(prob, 20) pop = algo.evolve(pop) logs.append(algo.extract(type(uda)).get_log()) logs = np.array(logs) avg_log = np.average(logs, 0)
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))
import pygmo as po import numpy as np import myUDPnodes import time generations = 400 sizePop = 250 #pathsave = '/home/oscar/Documents/PythonProjects/kuramotoAO/optimizationResults/' pathsave = '/Users/p277634/python/kaoModel/optimResult/' filenameTXT = 'sadeJ_nodes.txt' filenameNPZ = 'sadeJ_nodes.npz' # algorithm algo = po.algorithm( po.sade(gen=generations, variant=5, variant_adptv=1, ftol=1e-3, xtol=1e-3)) algo.set_verbosity(1) # problem prob = po.problem(myUDPnodes.KAOnodes()) # population pop = po.population(prob=prob, size=sizePop) # evolution start = time.time() popE = algo.evolve(pop) print('time evolution: ', time.time() - start) # save TXT fie with general description of the optimization bestFstr = 'champion fitness: ' + str( popE.champion_f[0]) + '; best fit possible: -1'
def fit(self): T = self.T self.p0 = self.x0 self.p0[-3:] = self.convert_Fcent(self.p0[-3:], 'base2opt') #s = np.array([4184, 1.0, 1E-2, 4184, 1.0, 1E-2, *(np.ones((1,4)).flatten()*1E-1)]) p_bnds = set_arrhenius_bnds(self.p0[0:3], default_arrhenius_coefNames) p_bnds = np.concatenate((p_bnds, set_arrhenius_bnds(self.p0[3:6], default_arrhenius_coefNames)), axis=1) p_bnds[:,1] = np.log(p_bnds[:,1]) # setting bnds to ln(A), need to do this better p_bnds[:,4] = np.log(p_bnds[:,4]) # setting bnds to ln(A), need to do this better Fcent_bnds = self.x_bnds(self.p0[-4:]) Fcent_bnds[-3:] = self.convert_Fcent(Fcent_bnds[-3:]) self.p_bnds = np.concatenate((p_bnds, Fcent_bnds), axis=1) if len(self.p_bnds) > 0: self.p0 = np.clip(self.p0, self.p_bnds[0, :], self.p_bnds[1, :]) self.p0 = self.p0[self.alter_idx] self.p_bnds = self.p_bnds[:,self.alter_idx] self.s = np.ones_like(self.p0) if self.algo['algorithm'] == 'scipy_curve_fit': with warnings.catch_warnings(): warnings.simplefilter('ignore', OptimizeWarning) x_fit, _ = curve_fit(self.ln_Troe, T, self.ln_k, p0=self.p0, method='trf', bounds=self.p_bnds, # dogbox #jac=fit_func_jac, x_scale='jac', max_nfev=len(self.p0)*1000) jac='2-point', x_scale='jac', max_nfev=len(self.p0)*1000, loss='huber') #print('scipy:', x_fit) #cmp = np.array([T, Fcent, np.exp(fit_func(T, *x_fit))]).T #for entry in cmp: # print(*entry) #print('') #scipy_fit = np.exp(self.function(T, *x_fit)) else: # maybe try pygmo with cstrs_self_adaptive or unconstrain or decompose p0_opt = np.zeros_like(self.p0) self.s = self.calc_s(p0_opt) bnds = (self.p_bnds-self.p0)/self.s ''' opt = nlopt.opt(nlopt.AUGLAG, len(self.p0)) #opt = nlopt.opt(self.algo['algorithm'], len(self.p0)) opt.set_min_objective(self.objective) #opt.add_inequality_constraint(self.Fcent_constraint, 0.0) #opt.add_inequality_constraint(self.constraints, 1E-8) opt.set_maxeval(self.algo['max_eval']) #opt.set_maxtime(10) opt.set_xtol_rel(self.algo['xtol_rel']) opt.set_ftol_rel(self.algo['ftol_rel']) opt.set_lower_bounds(bnds[0]) opt.set_upper_bounds(bnds[1]) opt.set_initial_step(self.algo['initial_step']) #opt.set_population(int(np.rint(10*(len(idx)+1)*10))) sub_opt = nlopt.opt(self.algo['algorithm'], len(self.p0)) sub_opt.set_initial_step(self.algo['initial_step']) sub_opt.set_xtol_rel(self.algo['xtol_rel']) sub_opt.set_ftol_rel(self.algo['ftol_rel']) opt.set_local_optimizer(sub_opt) x_fit = opt.optimize(p0_opt) # optimize! #print('Fcent_constraint: ', self.Fcent_constraint(x_fit)) #print('Arrhe_constraint: ', self.Arrhenius_constraint(x_fit)) ''' class pygmo_objective_fcn: def __init__(self, obj_fcn, bnds): self.obj_fcn = obj_fcn self.bnds = bnds def fitness(self, x): return [self.obj_fcn(x)] def get_bounds(self): return self.bnds def gradient(self, x): return pygmo.estimate_gradient_h(lambda x: self.fitness(x), x) pop_size = int(np.max([35, 5*(len(p0_opt)+1)])) num_gen = int(np.ceil(self.algo['max_eval']/pop_size)) prob = pygmo.problem(pygmo_objective_fcn(self.objective, bnds)) pop = pygmo.population(prob, pop_size) pop.push_back(x = p0_opt) # puts initial guess into the initial population #F = (0.107 - 0.141)/(1 + (num_gen/225)**7.75) #F = 0.2 #CR = 0.8032*np.exp(-1.165E-3*num_gen) #algo = pygmo.algorithm(pygmo.de(gen=num_gen, F=F, CR=CR, variant=6)) algo = pygmo.algorithm(pygmo.sade(gen=num_gen, variant=6)) #algo = pygmo.algorithm(pygmo.pso_gen(gen=num_gen)) #algo = pygmo.algorithm(pygmo.ipopt()) #algo = pygmo.algorithm(pygmo.gwo(gen=num_gen)) pop = algo.evolve(pop) x_fit = pop.champion_x x_fit = self.set_x_from_opt(x_fit) #print('nlopt:', x_fit) #cmp = np.array([T, Fcent, self.function(T, *x_fit)]).T ##cmp = np.array([T, Fcent, scipy_fit, np.exp(self.function(T, *x_fit))]).T #for entry in cmp: # print(*entry) #print('') # change ln_A to A x_fit[1] = np.exp(x_fit[1]) x_fit[4] = np.exp(x_fit[4]) #res = {'x': x_fit, 'fval': opt.last_optimum_value(), 'nfev': opt.get_numevals()} res = {'x': x_fit, 'fval': pop.champion_f[0], 'nfev': pop.problem.get_fevals()} return res
#Can we set the time of flight for each legs ? Does not seem to work when I tried # In[102]: pop_fb = pg.population(prob_fb,1) ax_fb = traj_fb.plot(pop_fb.champion_x) plt.show() print (traj_fb.pretty(pop_fb.champion_x)) # In[106]: algo_fb = pg.algorithm(pg.sade(gen = 500)) l = list() for i in range (10) : pop_fb = pg.population(prob_fb,20) pop_fb = algo.evolve(pop_fb) 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]:
import myUDPw import time generations = 500 sizePop = 20 #pathsave = '/home/oscar/Documents/PythonProjects/kuramotoAO/optimizationResults/' pathsave = '/Users/p277634/python/kaoModel/optimResult/' filenameTXT = 'sadeJw_fixKL.txt' filenameNPZ = 'sadeJw_fixKL.npz' # algorithm algo = po.algorithm(po.sade(gen=generations, variant=5, variant_adptv=1, ftol=1e-3, xtol=1e-3)) algo.set_verbosity(1) # problem prob = po.problem(myUDPw.Testkao()) # population pop = po.population(prob=prob,size=sizePop) # evolution start = time.time() popE = algo.evolve(pop) print('time evolution: ',time.time()-start)
def create_variants(self, n, desc, category, constructor): def assign_2nd_alg(archipelago, algo): if category == 'rings': for island in archipelago.topology.every_other_island(): island.algorithm = algo elif hasattr(archipelago.topology, 'endpoints'): for island in archipelago.topology.endpoints: island.algorithm = algo elif isinstance(archipelago.topology, FullyConnectedTopology): for island in islice(archipelago.topology.islands, None, None, 2): island.algorithm = algo return archipelago def assign_algs(archipelago, algos): ''' Evenly partitions and assigns algorithms to islands. ''' for island,algo in zip(archipelago.topology.islands, cycle(algos)): island.algorithm = algo g = self.generations self.new_topology( desc='{}, de'.format(desc), category=category, algorithms=['de'], archipelago=Archipelago(constructor(de(gen=g),n))) self.new_topology( desc='{}, de1220'.format(desc), category=category, algorithms=['de1220'], archipelago=Archipelago(constructor(de1220(gen=g),n))) self.new_topology( desc='{}, sade'.format(desc), category=category, algorithms=['sade'], archipelago=Archipelago(constructor(sade(gen=g),n))) self.new_topology( desc='{}, ihs'.format(desc), category=category, algorithms=['ihs'], archipelago=Archipelago(constructor(ihs(gen=g),n))) self.new_topology( desc='{}, pso'.format(desc), category=category, algorithms=['pso'], archipelago=Archipelago(constructor(pso(gen=g),n))) self.new_topology( desc='{}, pso_gen'.format(desc), category=category, algorithms=['pso_gen'], archipelago=Archipelago(constructor(pso_gen(gen=g),n))) # self.new_topology( # desc='{}, simulated_annealing'.format(desc), # category=category, # algorithms=['simulated_annealing'], # archipelago=Archipelago(constructor(simulated_annealing(),n))) self.new_topology( desc='{}, bee_colony'.format(desc), category=category, algorithms=['bee_colony'], archipelago=Archipelago(constructor(bee_colony(gen=g),n))) self.new_topology( desc='{}, cmaes'.format(desc), category=category, algorithms=['cmaes'], archipelago=Archipelago(constructor(cmaes(gen=g),n))) self.new_topology( desc='{}, nsga2'.format(desc), category=category, algorithms=['nsga2'], archipelago=Archipelago(constructor(nsga2(gen=g),n))) self.new_topology( desc='{}, xnes'.format(desc), category=category, algorithms=['xnes'], archipelago=Archipelago(constructor(xnes(gen=g),n))) # de + nelder mead combo self.new_topology( desc='{}, de+nelder mead'.format(desc), category=category, algorithms=['de','neldermead'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_nelder_mead())) # de + praxis combo self.new_topology( desc='{}, de+praxis'.format(desc), category=category, algorithms=['de','praxis'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_praxis())) # de + nsga2 combo self.new_topology( desc='{}, de+nsga2'.format(desc), category=category, algorithms=['de','nsga2'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), nsga2(gen=g))) # de + de1220 combo self.new_topology( desc='{}, de+de1220'.format(desc), category=category, algorithms=['de','de1220'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), de1220(gen=g))) # de + sade combo self.new_topology( desc='{}, de+sade'.format(desc), category=category, algorithms=['de','sade'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), sade(gen=g))) # de + pso combo self.new_topology( desc='{}, de+pso'.format(desc), category=category, algorithms=['de','pso'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), pso(gen=g))) # extra configurations for fully connected topology if constructor is self.factory.createFullyConnected: self.new_topology( desc='{}, de+pso+praxis'.format(desc), category=category, algorithms=['de','pso','praxis'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis()))) self.new_topology( desc='{}, de+pso+praxis+nsga2'.format(desc), category=category, algorithms=['de','pso','praxis','nsga2'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), nsga2(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+cmaes'.format(desc), category=category, algorithms=['de','pso','praxis','cmaes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), cmaes(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+xnes'.format(desc), category=category, algorithms=['de','pso','praxis','xnes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), xnes(gen=g))))
def _setup_algorithm(self, parameters): alg = pg.sade(**self._alg_attrs) return alg
def optimize_switches(hand_lengths, num_switches, num_passes=2, iter_success=100): bs = (-80, 80) bf = (0, 80) bx = (hand_lengths[0] - hand_lengths[3], np.sum(hand_lengths) + switch_size) by = (-np.sum(hand_lengths[1:]), hand_lengths[0]) bf = (0, 80) def scale(a, s): return a * (s[1] - s[0]) + s[0] class Problem: def fitness(self, params): switch_angles = scale(params[:num_switches], bs) finger_angles = scale(params[num_switches:-2], bf) switch_pos = np.array((scale(params[-2], bx), scale(params[-1], by))) positions, forbidden_area = calculate_switches( switch_pos, switch_angles) r = 0 for sp, sa, fa in zip(positions, switch_angles, finger_angles): r += calculate_finger(sp, sa, fa, hand_lengths, forbidden_area)[0] return (r, ) def get_bounds(self): num_params = num_switches * 2 + 2 return (np.full(num_params, 0), np.full(num_params, 1)) prob = pg.problem(Problem()) #0.328703 #0.32865749 #0.32484355963 #0.324843559629 generation = 0 batch = 50 pop_size = 20 archi = pg.archipelago() num_islands = 18 for variant in range(num_islands): algo = pg.algorithm(pg.sade(gen=batch, variant_adptv=2, memory=True)) archi.push_back(algo=algo, prob=prob, size=pop_size) old_fevals = np.full(num_islands, 0) def get_archi_champion(archi): f = np.array(archi.get_champions_f()).flatten() s = np.argsort(f) x = archi.get_champions_x()[s[0]] f = archi.get_champions_f()[s[0]][0] return x, f, s[0] pg.mp_island.init_pool() pg.mp_island.resize_pool(pg.mp_island.get_pool_size() - 1) best = np.finfo(np.float64).max num_stagnated = 0 for i in range(500): archi.evolve(1) archi.wait() islands = list(archi) best_x_f = [] x = np.array(archi.get_champions_x()) f = np.array(archi.get_champions_f()) dt = np.dtype([("x", x.dtype, (x.shape[1], )), ("f", f.dtype, (f.shape[1], ))]) all_x_f = np.empty(0, dtype=dt) for island in islands: pop = island.get_population() f = pop.champion_f x = pop.champion_x best_x_f.append((x, f)) f = pop.get_f() x = pop.get_x() a = np.empty(len(x), dtype=dt) a["x"] = x a["f"] = f all_x_f = np.concatenate((all_x_f, a)) generation += batch x, f, i = get_archi_champion(archi) if np.isclose(f, best, atol=1e-9, rtol=1e-20): num_stagnated += 1 #if num_stagnated > 20: # break else: best = f num_stagnated = 0 print("Generation %i: variant %i: best %f, same for: %i" % (generation, i, f, num_stagnated)) print(np.array(best_x_f).T[1]) _, i = np.unique(np.round(all_x_f["x"], 10), axis=0, return_index=True) unique_x_f = np.empty(len(i), dtype=dt) unique_x_f["x"] = all_x_f["x"][i] unique_x_f["f"] = all_x_f["f"][i] if True: for i, island in enumerate(islands): if False: pop = island.get_population() f = pop.get_f().flatten() s = np.argsort(f) j = 1 for fx in best_x_f: if fx[1] != pop.champion_f: pop.set_xf(int(s[-j]), fx[0], fx[1]) j = j + 1 else: num_islands = len(islands) pop = island.get_population() x = np.array(pop.get_x()) f = np.array(pop.get_f()) _, unique_pop = np.unique(np.round(x, 10), axis=0, return_index=True) unique_x = x[unique_pop] unique_f = f[unique_pop] s = np.argsort(unique_f.flatten()) unique_x = unique_x[s] unique_f = unique_f[s] if len(unique_f) > pop_size - num_islands - 1: unique_x = unique_x[:pop_size - (num_islands - 1)] unique_f = unique_f[:pop_size - (num_islands - 1)] for xf in best_x_f: if xf[1] != pop.champion_f: unique_x = np.concatenate((unique_x, (xf[0], ))) unique_f = np.concatenate((unique_f, (xf[1], ))) np.random.shuffle(unique_pop) unique_x = np.concatenate( (unique_x, x[unique_pop][:pop_size - len(unique_x)])) unique_f = np.concatenate( (unique_f, f[unique_pop][:pop_size - len(unique_x)])) for i, (x, f) in enumerate(zip(unique_x, unique_f)): pop.set_xf(i, x, f) island.set_population(pop) new_fevals = np.array( [i.get_population().problem.get_fevals() for i in islands]) fevals = new_fevals - old_fevals print("Evaluations %s" % (fevals)) for i, num_evals in enumerate(fevals): if num_evals < 3 * pop_size: new_island = random.randrange(len(islands)) print("Stagnated island %i, replacing with %i" % (i, new_island)) pop = islands[i].get_population() new_pop = islands[new_island].get_population() xs = new_pop.get_x() fs = new_pop.get_f() best_idx = pop.best_idx() for j, (x, f) in enumerate(zip(xs, fs)): if j != best_idx: pop.set_xf(j, x, f) islands[i].set_population(pop) old_fevals = new_fevals x = get_archi_champion(archi)[0] switch_angles = scale(x[:num_switches], bs) finger_angles = scale(x[num_switches:-2], bf) switch_pos = np.array((scale(x[-2], bx), scale(x[-1], by))) switch_positions, forbidden_area = calculate_switches( switch_pos, switch_angles) total_effort = 0 switches = [] for sp, sa, fa in zip(switch_positions, switch_angles, finger_angles): effort, angles = calculate_finger(sp, sa, fa, hand_lengths, forbidden_area) switches.append(OptimizationResultSwitch(sp, sa, effort, angles)) total_effort += effort return OptimizationResult(switches, total_effort)
p_size = 10 n_gens = 50 D = 5 pg.set_global_rng_seed(seed=32) # The user-defined problem udp = pg.rastrigin(dim=D) # The pygmo problem prob = pg.problem(udp) shift = np.linspace(np.pi * 4, np.pi * 4, D) prob1 = pg.translate(prob=prob, translation=shift) # For a number of generation based algorithms we can use a similar script to run and average over 25 runs. udas = [ pg.sade(gen=n_gens, variant=6, variant_adptv=1, memory=False, seed=1234) ] #pg.de(gen=n_gens, variant=7, F=0.6, CR=0.9, seed=1234), #pg.pso(gen=n_gens, neighb_type=4, memory=True, seed=1234)] global_results = [] for uda in udas: logs = [] algo = pg.algorithm(uda) results_trial = [] for i in range(0, n_trials): log_trial = [] # regulates both screen and log verbosity algo.set_verbosity(9) pop = pg.population(prob, p_size) algo.evolve(pop)
def __call__(self, function): scanner_options = { 'sade': dict(gen=self.gen, variant=self.variant, variant_adptv=self.variant_adptv, ftol=self.ftol, xtol=self.xtol, memory=self.memory, seed=self.seed), 'gaco': dict(gen=self.gen, ker=self.ker, q=self.q, oracle=self.oracle, acc=self.acc, threshold=self.threshold, n_gen_mark=self.n_gen_mark, impstop=self.impstop, evalstop=self.evalstop, focus=self.focus, memory=self.memory, seed=self.seed), 'maco': dict(gen=self.gen, ker=self.ker, q=self.q, threshold=self.threshold, n_gen_mark=self.n_gen_mark, evalstop=self.evalstop, focus=self.focus, memory=self.memory, seed=self.seed), 'gwo': dict(gen=self.gen, seed=self.seed), 'bee_colony': dict(gen=self.gen, limit=self.limit, seed=self.seed), 'de': dict(gen=self.gen, F=self.F, CR=self.CR, variant=self.variant, ftol=self.ftol, xtol=self.xtol, seed=self.seed), 'sea': dict(gen=self.gen, seed=self.seed), 'sga': dict(gen=self.gen, cr=self.cr, eta_c=self.eta_c, m=self.m, param_m=self.param_m, param_s=self.param_s, crossover=self.crossover, mutation=self.mutation, selection=self.selection, seed=self.seed), 'de1220': dict(gen=self.gen, allowed_variants=self.allowed_variants, variant_adptv=self.variant_adptv, ftol=self.ftol, xtol=self.xtol, memory=self.memory, seed=self.seed), 'cmaes': dict(gen=self.gen, cc=self.cc, cs=self.cs, c1=self.c1, cmu=self.cmu, sigma0=self.sigma0, ftol=self.ftol, xtol=self.xtol, memory=self.memory, force_bounds=self.force_bounds, seed=self.seed), 'moead': dict(gen=self.gen, weight_generation=self.weight_generation, decomposition=self.decomposition, neighbours=self.neighbours, CR=self.CR, F=self.F, eta_m=self.eta_m, realb=self.realb, limit=self.limit, preserve_diversity=self.preserve_diversity, seed=self.seed), 'compass_search': dict(max_fevals=self.max_fevals, start_range=self.start_range, stop_range=self.stop_range, reduction_coeff=self.reduction_coeff), 'simulated_annealing': dict(Ts=self.Ts, Tf=self.Tf, n_T_adj=self.n_T_adj, n_range_adj=self.n_range_adj, bin_size=self.bin_size, start_range=self.start_range, seed=self.seed), 'pso': dict(gen=self.gen, omega=self.omega, eta1=self.eta1, eta2=self.eta2, max_vel=self.max_vel, variant=self.variant, neighb_type=self.neighb_type, neighb_param=self.neighb_param, memory=self.memory, seed=self.seed), 'pso_gen': dict(gen=self.gen, omega=self.omega, eta1=self.eta1, eta2=self.eta2, max_vel=self.max_vel, variant=self.variant, neighb_type=self.neighb_type, neighb_param=self.neighb_param, memory=self.memory, seed=self.seed), 'nsga2': dict(gen=self.gen, cr=self.cr, eta_c=self.eta_c, m=self.m, eta_m=self.eta_m, seed=self.seed), 'nspso': dict(gen=self.gen, omega=self.omega, c1=self.c1, c2=self.c2, chi=self.chi, v_coeff=self.v_coeff, leader_selection_range=self.leader_selection_range, diversity_mechanism=self.diversity_mechanism, memory=self.memory, seed=self.seed), 'mbh': dict(algo=self.algo, stop=self.stop, perturb=self.perturb, seed=self.seed), 'cstrs_self_adaptive': dict(iters=self.iters, algo=self.algo, seed=self.seed), 'ihs': dict(gen=self.gen, phmcr=self.phmcr, ppar_min=self.ppar_min, ppar_max=self.ppar_max, bw_min=self.bw_min, bw_max=self.bw_max, seed=self.seed), 'xnes': dict(gen=self.gen, eta_mu=self.eta_mu, eta_sigma=self.eta_sigma, eta_b=self.eta_b, sigma0=self.sigma0, ftol=self.ftol, xtol=self.xtol, memory=self.memory, force_bounds=self.force_bounds, seed=self.seed) } if self.log_data: xl = [] yl = [] log_data = self.log_data # class interf_function: def __init__(self, dim): self.dim = dim def fitness(self, x): x = np.expand_dims(x, axis=0) y = function(x) # x = x[0] y = y.tolist() if log_data: xl.append(x) yl.append(y) # print (x, y[0]) return y[0] if function.is_differentiable(): def gradient(self, x): x = np.expand_dims(x, axis=0) g = function(x) g = g.tolist() return g[0] def get_bounds(self): lb = [] ub = [] bounds = function.get_ranges() # warning # check for infinities for i in range(len(bounds)): lb.append(bounds[i, 0]) ub.append(bounds[i, 1]) r = (np.array(lb), np.array(ub)) return r # I need to call pygmo functions directly prob = pg.problem(interf_function(function)) # print (prob.get_thread_safety()) if self.scanner == "sade": # I need a dictionary with algorithms and options algo = pg.algorithm(pg.sade(**scanner_options[self.scanner])) elif self.scanner == "gaco": algo = pg.algorithm(pg.gaco(**scanner_options[self.scanner])) # elif self.scanner == "maco": # is not implemented though in webpage # looks it is # algo = pg.algorithm(pg.maco(**scanner_options[self.scanner])) elif self.scanner == "gwo": algo = pg.algorithm(pg.gwo(**scanner_options[self.scanner])) elif self.scanner == "bee_colony": algo = pg.algorithm(pg.bee_colony(**scanner_options[self.scanner])) elif self.scanner == "de": algo = pg.algorithm(pg.de(**scanner_options[self.scanner])) elif self.scanner == "sea": algo = pg.algorithm(pg.sea(**scanner_options[self.scanner])) elif self.scanner == "sga": algo = pg.algorithm(pg.sga(**scanner_options[self.scanner])) elif self.scanner == "de1220": algo = pg.algorithm(pg.de1220(**scanner_options[self.scanner])) elif self.scanner == "cmaes": algo = pg.algorithm(pg.cmaes(**scanner_options[self.scanner])) # elif self.scanner == "moead": #multiobjective algorithm # algo = pg.algorithm(pg.moead(**scanner_options[self.scanner])) elif self.scanner == "compass_search": algo = pg.algorithm( pg.compass_search(**scanner_options[self.scanner])) elif self.scanner == 'simulated_annealing': algo = pg.algorithm( pg.simulated_annealing(**scanner_options[self.scanner])) elif self.scanner == 'pso': algo = pg.algorithm(pg.pso(**scanner_options[self.scanner])) elif self.scanner == 'pso_gen': algo = pg.algorithm(pg.pso_gen(**scanner_options[self.scanner])) # elif self.scanner == 'nsga2': #multiobjective algorithm # algo = pg.algorithm(pg.nsga2(**scanner_options[self.scanner])) # elif self.scanner == 'nspso': is not implemented though in webpage # looks it is # algo = pg.algorithm(pg.nspso(**scanner_options[self.scanner])) elif self.scanner == 'mbh': if scanner_options[self.scanner]['algo'] == 'de': algo = pg.algorithm( pg.mbh(pg.algorithm(pg.de(**scanner_options['de'])))) # elif self.scanner == 'ihs': #does not work # algo = pg.algorithm(ihs(**scanner_options[self.scanner])) # elif self.scanner == 'xnes': #does not work # algo = pg.algorithm(xnes(**scanner_options[self.scanner])) # uda = algo.extract(xnes) else: print( 'The ' + self.scanner + ' algorithm is not implemented. The ' 'list of algorithms available is', algorithms) sys.exit() # add verbosing flag if self.verbose > 1: algo.set_verbosity(self.verbose) pop = pg.population(prob, self.size) if self.verbose > 9: print('prob', prob) opt = algo.evolve(pop) if self.verbose > 9: print('algo', algo) # best_x = np.expand_dims(opt.champion_x, axis=0) # best_fitness = np.expand_dims(opt.get_f()[opt.best_idx()], axis=0) best_x = np.expand_dims(opt.champion_x, axis=0) best_fitness = np.expand_dims(opt.champion_f, axis=0) if self.verbose > 0: print('best fit:', best_x, best_fitness) if self.log_data: x = np.squeeze(xl, axis=(1, )) y = np.squeeze(yl, axis=(2, )) if self.log_data: return (x, y) else: return (best_x, best_fitness)