def test_simple_probs(self): """ Testing whether migration history matches the expected output for each migration_direction and distribution_type """ for migr_dir in [ migration_direction.source, migration_direction.destination ]: for dist_type in [ distribution_type.point_to_point, distribution_type.broadcast ]: prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 3, 20, migration_direction=migr_dir, distribution_type=dist_type) top = topology.ring(3) top.set_weight(0, 1, 0.0) top.set_weight(0, 2, 0.0) top.set_weight(1, 2, 0.0) top.set_weight(1, 0, 1.0) top.set_weight(2, 0, 1.0) top.set_weight(2, 1, 1.0) archi.topology = top archi.evolve(200) migr_hist = archi.dump_migr_history() # Below: After 200 evaluations, there should be some migrants from 1->0, 2->0 and 2->1 # There should be no migrants from 1->0, 2->0 and 2->1 self.assertTrue("(1,0,1)" not in migr_hist) self.assertTrue("(1,0,2)" not in migr_hist) self.assertTrue("(1,1,2)" not in migr_hist)
def run_example5(): from PyGMO import archipelago, problem from PyGMO.algorithm import jde from PyGMO.topology import ring 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')] prob = mga_1dsm(seq=seq) prob.set_tof(0.7, 3) prob.set_vinf(2.5) prob.set_launch_window(epoch(5844), epoch(6209)) prob.set_tof(0.7, 3) # We solve it!! algo = jde(100) topo = ring() archi = archipelago(algo, prob, 8, 20, topology=topo) print( "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" ) archi.evolve(10) archi.join() isl = min(archi, key=lambda x: x.population.champion.f[0]) print("Done!! Best solution found is: " + str(isl.population.champion.f[0] / 1000) + " km / sec") prob.pretty(isl.population.champion.x) prob.plot(isl.population.champion.x)
def run_example5(): from PyGMO import archipelago, problem from PyGMO.algorithm import jde from PyGMO.topology import ring from PyKEP import planet_ss,epoch from PyKEP.interplanetary import mga_1dsm #We define an Earth-Venus-Earth problem (single-objective) seq = [planet_ss('earth'),planet_ss('venus'),planet_ss('earth')] prob = mga_1dsm(seq=seq) prob.set_tof(0.7,3) prob.set_vinf(2.5) prob.set_launch_window(epoch(5844),epoch(6209)) prob.set_tof(0.7,3) print prob #We solve it!! algo = jde(100) topo = ring() archi = archipelago(algo,prob,8,20, topology=topo) print "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" archi.evolve(10); archi.join() isl = min(archi, key=lambda x:x.population.champion.f[0]) print "Done!! Best solution found is: " + str(isl.population.champion.f[0]/1000) + " km / sec" prob.pretty(isl.population.champion.x) prob.plot(isl.population.champion.x)
def run_pygmo(): algos = [('PyGMO_DE',algorithm.de(gen=1)), ('PyGMO_DE_v1',algorithm.de(gen=1,f=.2,cr=1.0,variant=4)), #('PyGMO_DE_v2',algorithm.de(gen=1,f=.1,cr=1.0,variant=4)), #('PyGMO_DE_v3',algorithm.de(gen=1,f=.3,cr=1.0,variant=4)), #('PyGMO_DE_v4',algorithm.de(gen=1,f=.2,cr=0.9,variant=4)), ('PyGMO_JDE',algorithm.jde(gen=1)), ('PyGMO_MDE_PBX',algorithm.mde_pbx(gen=1)), ('PyGMO_PSO',algorithm.pso(gen=1,variant=5)), ('PyGMO_PSO_GEN',algorithm.pso_gen(gen=1)), ('PyGMO_BEE_COLONY',algorithm.bee_colony(gen=1)), ('PyGMO_IHS',algorithm.ihs(iter=pop_size))] run_eval("PyGMO",algos)
def run_test(n_trials=200, pop_size = 20, n_gen = 500): number_of_trials = n_trials number_of_individuals = pop_size number_of_generations = n_gen prob_list = [problem.schwefel(dim = 10), problem.michalewicz(dim = 10), problem.rastrigin(dim = 10), problem.rosenbrock(dim = 10), problem.ackley(dim = 10), problem.griewank(dim = 10)] if __extensions__['gtop']: prob_list.append(problem.cassini_1()) prob_list.append(problem.cassini_2()) prob_list.append(problem.gtoc_1()) prob_list.append(problem.rosetta()) prob_list.append(problem.messenger_full()) prob_list.append(problem.tandem(prob_id = 6, max_tof = 10)) algo_list = [algorithm.pso(gen = number_of_generations), algorithm.de(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.jde(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.de_1220(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.sa_corana(iter = number_of_generations*number_of_individuals,Ts = 1,Tf = 0.01), algorithm.ihs(iter = number_of_generations*number_of_individuals), algorithm.sga(gen = number_of_generations), algorithm.cmaes(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.bee_colony(gen = number_of_generations/2)] print('\nTrials: ' + str(n_trials) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for prob in prob_list: print('\nTesting problem: ' + prob.get_name() + ', Dimension: ' + str(prob.dimension) ) print('With Population Size: ' + str(pop_size) ) for algo in algo_list: print(' ' + str(algo)) best = [] best_x = [] for i in range(0,number_of_trials): isl = island(algo,prob,number_of_individuals) isl.evolve(1) isl.join() best.append(isl.population.champion.f) best_x.append(isl.population.champion.x) print(' Best:\t' + str(min(best)[0])) print(' Mean:\t' + str(mean(best))) print(' Std:\t' + str(std(best)))
def jde_mga_1dsm(seq, t0, tof, slack=5, pop_size=50, n_evolve=10, dv_launch=6127., verbose=False): """Runs jDE with mga_1dsm problem.""" from PyGMO.problem import mga_1dsm_tof from PyGMO.algorithm import jde from PyGMO import population prob = mga_1dsm_tof(seq=[kep.planet_ss(name) for name in seq], t0=[kep.epoch(t0-slack), kep.epoch(t0+slack)], tof=[[t-slack, t+slack] for t in tof], vinf=[0., dv_launch/1000.], add_vinf_arr=False) algo = jde(gen=500, memory=True) pop = population(prob, pop_size) if verbose: print pop.champion.f[0] for i in xrange(n_evolve): pop = algo.evolve(pop) if verbose: print pop.champion.f
def test_topology_serialize(self): """ Testing whether the weights are retained after serialization of an archipelago """ prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 4, 20) top = topology.ring(4) top.set_weight(0, 1, 0.01) top.set_weight(0, 3, 0.03) top.set_weight(1, 2, 0.12) top.set_weight(2, 1, 0.21) archi.topology = top archi.evolve(5) import pickle pickle.loads(pickle.dumps(archi)) self.assertEqual(archi.topology.get_weight(0, 1), 0.01) self.assertEqual(archi.topology.get_weight(0, 3), 0.03) self.assertEqual(archi.topology.get_weight(1, 2), 0.12) self.assertEqual(archi.topology.get_weight(2, 1), 0.21)
def example_1(n_trials=25, variant_adptv=1, memory=True): from PyGMO import problem, algorithm, island, archipelago from PyGMO.topology import fully_connected from numpy import mean, median results = list() prob = problem.messenger_full() de_variants = [11, 13, 15, 17] algos = [ algorithm.jde( gen=50, variant=v, memory=memory, variant_adptv=variant_adptv) for v in de_variants] for trial in range(n_trials): archi = archipelago(topology=fully_connected()) for algo in algos: archi.push_back(island(algo, prob, 25)) print("Trial N: " + str(trial)) archi.evolve(30) results.append(min([isl.population.champion.f[0] for isl in archi])) return (mean(results), median(results), min(results), max(results))
def run(n_tri,n_gen,pop_size): import numpy #inital number of generations number_of_generations = 1 number_of_trials = n_tri number_of_individuals = pop_size algo_list = [algorithm.pso(gen = number_of_generations), algorithm.de(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.jde(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.de_1220(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), #algorithm.sa_corana(iter = number_of_generations*number_of_individuals,Ts = 1,Tf = 0.01), algorithm.ihs(iter = number_of_generations*number_of_individuals), #algorithm.sga(gen = number_of_generations), algorithm.cmaes(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.bee_colony(gen = number_of_generations) ] algo_means_list = [] algo_name_list = [] print('\nTrials: ' + str(n_tri) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for algo in algo_list: print(' ' + str(algo)) algo_name_list.append(algo.get_name()) trials = [] trials_max_len = 0 for i in range(number_of_trials): print ".", trials.append(single_test(algo,total_evals,pop_size)) t_minlen = min([len(t) for t in trials]) t_new = numpy.array([t[:t_minlen-1] for t in trials ]) t_new = numpy.mean(t_new,axis=0) algo_means_list.append(t_new[:,1]) plot_multi(algo_means_list,names=algo_name_list,trials=number_of_trials)
def test_simple_probs(self): """ Testing whether migration history matches the expected output for each migration_direction and distribution_type """ for migr_dir in [migration_direction.source, migration_direction.destination]: for dist_type in [distribution_type.point_to_point, distribution_type.broadcast]: prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 3, 20, migration_direction=migr_dir, distribution_type=dist_type) top = topology.ring(3) top.set_weight(0, 1, 0.0) top.set_weight(0, 2, 0.0) top.set_weight(1, 2, 0.0) top.set_weight(1, 0, 1.0) top.set_weight(2, 0, 1.0) top.set_weight(2, 1, 1.0) archi.topology = top archi.evolve(200) migr_hist = archi.dump_migr_history() # Below: After 200 evaluations, there should be some migrants from 1->0, 2->0 and 2->1 # There should be no migrants from 1->0, 2->0 and 2->1 self.assertTrue("(1,0,1)" not in migr_hist) self.assertTrue("(1,0,2)" not in migr_hist) self.assertTrue("(1,1,2)" not in migr_hist)
def run_test(n_trials=200, pop_size=20, n_gen=500): """ This function runs some tests on the algorthm. Use it to verify the correct installation of PyGMO. USAGE: PyGMO.run_test(n_trials=200, pop_size = 20, n_gen = 500) * n_trials: each algorithm will be called n_trials times on the same problem to then evaluate best, mean and std * pop_size: this determines the population size * n_gen: this regulates the maximim number of function evaluation """ from PyGMO import problem, algorithm, island from numpy import mean, std number_of_trials = n_trials number_of_individuals = pop_size number_of_generations = n_gen prob_list = [ problem.schwefel( dim=10), problem.rastrigin( dim=10), problem.rosenbrock( dim=10), problem.ackley( dim=10), problem.griewank( dim=10), problem.levy5(10)] if __extensions__['gtop']: prob_list.append(problem.cassini_1()) prob_list.append(problem.gtoc_1()) prob_list.append(problem.cassini_2()) prob_list.append(problem.messenger_full()) algo_list = [ algorithm.pso( gen=number_of_generations), algorithm.mde_pbx( gen=number_of_generations, xtol=1e-30, ftol=1e-30), algorithm.de( gen=number_of_generations, xtol=1e-30, ftol=1e-30), algorithm.jde( gen=number_of_generations, memory=False, xtol=1e-30, ftol=1e-30), algorithm.de_1220( gen=number_of_generations, memory=False, xtol=1e-30, ftol=1e-30), algorithm.sa_corana( iter=number_of_generations * number_of_individuals, Ts=1, Tf=0.01), algorithm.ihs( iter=number_of_generations * number_of_individuals), algorithm.sga( gen=number_of_generations), algorithm.cmaes( gen=number_of_generations, xtol=1e-30, ftol=1e-30, memory=False), algorithm.bee_colony( gen=number_of_generations / 2)] print('\nTrials: ' + str(n_trials) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for prob in prob_list: print('\nTesting problem: ' + prob.get_name() + ', Dimension: ' + str(prob.dimension)) print('With Population Size: ' + str(pop_size)) for algo in algo_list: print(' ' + str(algo)) best = [] best_x = [] for i in range(0, number_of_trials): isl = island(algo, prob, number_of_individuals) isl.evolve(1) isl.join() best.append(isl.population.champion.f) best_x.append(isl.population.champion.x) print(' Best:\t' + str(min(best)[0])) print(' Mean:\t' + str(mean(best))) print(' Std:\t' + str(std(best)))