def test_distribution_type(self): """Testing whether the distribution_type property works""" # Ensure the default distribution type is point_to_point a = archipelago() self.assertEqual(a.distribution_type, distribution_type.point_to_point) for dist_type in [distribution_type.point_to_point, distribution_type.broadcast]: a = archipelago() a.distribution_type = dist_type self.assertEqual(a.distribution_type, dist_type)
def test_distribution_type(self): """Testing whether the distribution_type property works""" # Ensure the default distribution type is point_to_point a = archipelago() self.assertEqual(a.distribution_type, distribution_type.point_to_point) for dist_type in [ distribution_type.point_to_point, distribution_type.broadcast ]: a = archipelago() a.distribution_type = dist_type self.assertEqual(a.distribution_type, dist_type)
def example_2( algo=algorithm.de(1), prob=problem.rosenbrock(10), topo=topology.barabasi_albert( 3, 3), n_evolve=100, n_isl=1024, pop_size=20, color_code='rank'): from PyGMO import problem, algorithm, island, archipelago from matplotlib.pyplot import savefig, close archi = archipelago(algo, prob, n_isl, pop_size, topology=topo) print("Drawing Initial Condition .. ") pos = archi.draw( scale_by_degree=True, n_size=3, e_alpha=0.03, n_color=color_code) savefig('archi000', dpi=72) close() for i in range(1, n_evolve): archi.evolve(1) archi.join() print("Drawing" + str(i) + "-th evolution .. ") pos = archi.draw( layout=pos, scale_by_degree=True, n_size=3, e_alpha=0.03, n_color=color_code) savefig('archi%03d' % i, dpi=72) close()
def do_test_migr_setup(self, pop_xs, out_pop_xs, top, n_evolves): """ Generic procedure for testing whether the state of populations in 'pop_xs', after performing 'n_evolve' migration steps is equal to the expected 'out_pop_xs', given topology 'top'. """ prob = problem.identity() alg = algorithm.null() pops = [] for xs in pop_xs: pop = population(prob) for x in xs: pop.push_back(x) pops.append(pop) archi = archipelago( distribution_type=distribution_type.broadcast, migration_direction=migration_direction.destination) for pop in pops: archi.push_back( island(alg, pop, s_policy=migration.best_s_policy(), r_policy=migration.fair_r_policy())) archi.topology = top archi.evolve_batch(n_evolves, 1, False) out_xs = [] for ii, isl in enumerate(archi, 1): out_xs.append(tuple(sorted([i.cur_f for i in isl.population]))) out_xs = tuple(out_xs) self.assertEqual(out_xs, out_pop_xs)
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 __test_impl(self,isl_type,algo,prob): from PyGMO import archipelago, topology a = archipelago(topology = topology.ring()) for i in range(0,100): a.push_back(isl_type(algo,prob,6)) a.evolve(10) a.join()
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 test_pickle(self): from PyGMO import archipelago, island_list, problem_list, algorithm_list import pickle print('') for isl in island_list: for prob in problem_list: for algo in algorithm_list: print(isl,type(prob()),type(algo())) a = archipelago() a.push_back(isl(algo(),prob(),20)) a.push_back(isl(algo(),prob(),20)) pickle.loads(pickle.dumps(a))
def test_pickle(self): from PyGMO import archipelago, island_list, problem_list, algorithm_list import pickle print('') for isl in island_list: for prob in problem_list: for algo in algorithm_list: print(isl, type(prob()), type(algo())) a = archipelago() a.push_back(isl(algo(), prob(), 20)) a.push_back(isl(algo(), prob(), 20)) pickle.loads(pickle.dumps(a))
def test_pickle(self): from PyGMO import archipelago, island_list, problem_list, algorithm_list, problem import pickle from copy import deepcopy # We remove some problems that cannot be constructed without external txt data files prob_list = deepcopy(problem_list) prob_list.remove(problem.cec2013) print('') for isl in island_list: for prob in prob_list: for algo in algorithm_list: print(isl,type(prob()),type(algo())) a = archipelago() a.push_back(isl(algo(),prob(),20)) a.push_back(isl(algo(),prob(),20)) pickle.loads(pickle.dumps(a))
def _generate_solution(self, problem: Tuple[CoreProblem, SortedDict], n_individuals: int, n_threads: int, topol) -> Solution: """ Creates a new archipelago to solve the problem with the DE algorithm. It returns the best solution found after the optimization process. """ archi = archipelago(self._algorithm, problem[0], n_threads, n_individuals, topology=topol) archi.evolve(self._evol) archi.join() solution = self._get_best_solution(archi, problem) return solution
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 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 test_pickle(self): # We remove some problems that cannot be constructed without external # txt data files prob_list = deepcopy(problem_list) prob_list.remove(problem.cec2013) # Remove trajectory problems if PyKEP is not installed try: import PyKEP except ImportError: prob_list.remove(problem.py_pl2pl) print('') for isl in island_list: for prob in prob_list: for algo in algorithm_list: a = archipelago() a.push_back(isl(algo(), prob(), 20)) a.push_back(isl(algo(), prob(), 20)) pickle.loads(pickle.dumps(a))
def do_test_migr_setup(self, pop_xs, out_pop_xs, top, n_evolves): """ Generic procedure for testing whether the state of populations in 'pop_xs', after performing 'n_evolve' migration steps is equal to the expected 'out_pop_xs', given topology 'top'. """ prob = problem.identity() alg = algorithm.null() pops = [] for xs in pop_xs: pop = population(prob) for x in xs: pop.push_back(x) pops.append(pop) archi = archipelago(distribution_type=distribution_type.broadcast, migration_direction=migration_direction.destination) for pop in pops: archi.push_back(island(alg, pop, s_policy=migration.best_s_policy(), r_policy=migration.fair_r_policy())) archi.topology = top archi.evolve_batch(n_evolves, 1, False) out_xs = [] for ii, isl in enumerate(archi, 1): out_xs.append(tuple(sorted([i.cur_f for i in isl.population]))) out_xs = tuple(out_xs) self.assertEqual(out_xs, out_pop_xs)
def problem_solver(n_trials=25): from PyGMO import problem, algorithm, island, archipelago from PyGMO.topology import fully_connected from numpy import mean, median import csv results = list() prob = problem.cassini_2() de_variants = [11, 13, 15, 17] f_variants = [0, 0.2, 0.5, 0.8, 1] cr_variants = [0.3, 0.9] np_variants = [10, 25, 50] for f in f_variants: for cr in cr_variants: for np in np_variants: algos = [algorithm.de(gen=np, f=f, cr=cr) 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)) archi.evolve(30) results.append( min([isl.population.champion.f[0] for isl in archi])) with open('results.csv', 'a') as out: out.write('%f;' % f) out.write('%f;' % cr) out.write('%f;' % np) out.write('\n') out.write('%f;' % mean(results)) out.write('%f;' % median(results)) out.write('%f;' % min(results)) out.write('%f;' % max(results)) out.write('\n') out.close()
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 test_pickle(self): from PyGMO import archipelago, island_list, problem_list, algorithm_list, problem import pickle from copy import deepcopy # We remove some problems that cannot be constructed without external # txt data files prob_list = deepcopy(problem_list) prob_list.remove(problem.cec2013) # Remove trajectory problems if PyKEP is not installed try: import PyKEP except ImportError: prob_list.remove(problem.py_pl2pl) print('') for isl in island_list: for prob in prob_list: for algo in algorithm_list: a = archipelago() a.push_back(isl(algo(), prob(), 20)) a.push_back(isl(algo(), prob(), 20)) pickle.loads(pickle.dumps(a))
def problem_solver(n_trials=25): from PyGMO import problem, algorithm, island, archipelago from PyGMO.topology import fully_connected from numpy import mean, median import csv results = list() prob = problem.cassini_2() de_variants = [11,13,15,17] f_variants = [0, 0.2, 0.5, 0.8, 1] cr_variants = [0.3, 0.9] np_variants = [10, 25, 50] for f in f_variants: for cr in cr_variants: for np in np_variants: algos = [algorithm.de(gen=np, f=f, cr=cr) 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)) archi.evolve(30) results.append(min([isl.population.champion.f[0] for isl in archi])) with open('results.csv', 'a') as out: out.write('%f;' % f) out.write('%f;' % cr) out.write('%f;' % np) out.write('\n') out.write('%f;' % mean(results)) out.write('%f;' % median(results)) out.write('%f;' % min(results)) out.write('%f;' % max(results)) out.write('\n') out.close()