def test_non_population_based_migration(): import pygmo as pg from pygmo import de, nlopt, rosenbrock from sabaody.topology import TopologyFactory topology_factory = TopologyFactory(island_size=5, migrant_pool_size=5) topology = topology_factory.createBidirChain(de, number_of_islands=2) assert len(topology.island_ids) == 2 assert len(topology.endpoints) == 2 de_island = pg.island(algo=de(gen=10), prob=rosenbrock(3), size=topology.islands[0].size) nm = nlopt('neldermead') nm.selection = 'random' nm.replacement = 'random' nm_island = pg.island(algo=nm, prob=rosenbrock(3), size=topology.islands[1].size) from sabaody.migration import BestSPolicy, FairRPolicy selection_policy = BestSPolicy(migration_rate=2) replacement_policy = FairRPolicy() from sabaody.migration import MigrationPolicyEachToAll migration_policy = MigrationPolicyEachToAll() # get the candidates from the de island p_de = de_island.get_population() candidates, candidate_f = selection_policy.select(p_de) # try migrating to the nelder mead island p_nm = nm_island.get_population() replacement_policy.replace(p_nm, candidates, candidate_f) nm_island.set_population(p_nm) # finally, try to evolve it new_pop = nm_island.evolve(n=10)
def benchmark_differential_evolution(): island = pg_island(algo=de(gen=10), prob=problem(rosenbrock(5)), size=10) N = 10 print('Differential Evolution (pop. size {})'.format( island.get_population().get_f().size)) for k in range(N): island.evolve() island.wait() d = sqrt( float(((island.get_population().champion_x - rosenbrock(5).best_known())**2).mean())) print('DE {:2}/{}: best fitness {:9.2f}, deviation {:9.2f}, fevals {}'. format(k, N, float(island.get_population().champion_f[0]), d, island.get_population().problem.get_fevals()))
def should_stop(self, pg_island, monitor): from numpy import mean, sqrt best_x = monitor.get_best_x() if best_x is None: return False else: return sqrt(mean((best_x-rosenbrock(self.dim).best_known())**2.)) < self.cutoff
def benchmark_simulated_annealing(): island = pg_island(algo=simulated_annealing(Ts=1., Tf=.01), prob=problem(rosenbrock(5)), size=10) N = 10 print('Simulated Annealing (pop. size {})'.format( island.get_population().get_f().size)) for k in range(N): island.evolve() island.wait() d = sqrt( float(((island.get_population().champion_x - rosenbrock(5).best_known())**2).mean())) print('SA {:2}/{}: best fitness {:9.2f}, deviation {:9.2f}, fevals {}'. format(k, N, float(island.get_population().champion_f[0]), d, island.get_population().problem.get_fevals()))
def test_bidir_chain(): ''' Tests the migration on a one way chain. ''' from sabaody.topology import TopologyFactory import pygmo as pg domain_qual = partial(getQualifiedName, 'com.how2cell.sabaody.test_bidir_chain_migration') problem = pg.problem(pg.rosenbrock(3)) topology_factory = TopologyFactory(island_size=3, domain_qualifier=domain_qual, mc_host='localhost', mc_port=11211) topology = topology_factory.createBidirChain(pg.de(gen=10), number_of_islands=5) assert len(topology.island_ids) == 5 assert len(topology.endpoints) == 2 from sabaody.migration_central import CentralMigrator, start_migration_service from sabaody.migration import MigrationPolicyEachToAll, BestSPolicy, FairRPolicy, sort_by_fitness try: process = start_migration_service() sleep(2) migrator = CentralMigrator(MigrationPolicyEachToAll(), BestSPolicy(migration_rate=1), FairRPolicy(), 'http://localhost:10100') from collections import OrderedDict for k in (1, 2): islands = OrderedDict( (i.id, pg.island(algo=i.algorithm, prob=problem, size=i.size)) for i in topology.islands) for island_id in islands.keys(): migrator.defineMigrantPool(island_id, 3) if k == 1: # test forward migration seed_first(islands, topology) else: # test reverse migration seed_last(islands, topology) for n in range(1, 5 + 1): assert count_hits(islands.values()) == n # perform migration for island_id, i in islands.items(): migrator.sendMigrants(island_id, i, topology) for island_id, i in islands.items(): deltas, src_ids = migrator.receiveMigrants( island_id, i, topology) migrator.purgeAll() finally: process.terminate()
def test_selection_replacement_policies(): ''' Test the replacement and selection policies. ''' from sabaody.migration import BestSPolicy, FairRPolicy, sort_by_fitness from pygmo import population, rosenbrock # rosenbrock with dim 3 is just to suppress errors from pagmo, never evaluated p = population(prob=rosenbrock(3), size=0, seed=0) # create a fake population p.push_back(array([10., 11., 12.]), array([4.])) p.push_back(array([1., 2., 3.]), array([1.])) p.push_back(array([7., 8., 9.]), array([3.])) p.push_back(array([4., 5., 6.]), array([2.])) # test selection s = BestSPolicy(2) candidates, candidate_f = s.select(p) # test that selected candidates are top two assert array_equal(candidates, array([[1., 2., 3.], [4., 5., 6.]])) assert array_equal(candidate_f, array([[1.], [2.]])) # test rate vs fraction s2 = BestSPolicy(pop_fraction=0.5) # shoud be same number of candidates either way assert s2.select(p)[0].shape[0] == candidates.shape[0] == 2 # test replacement p2 = population(prob=rosenbrock(3), size=0, seed=0) p2.push_back(array([9., 9., 9.]), array([5.])) p2.push_back(array([8., 9., 9.]), array([6.])) p2.push_back(array([7., 9., 9.]), array([7.])) p2.push_back(array([6., 9., 9.]), array([8.])) r = FairRPolicy() # should replace worst two decision vectors r.replace(p2, candidates, candidate_f) sorted_candidates, sorted_f = sort_by_fitness(p2) assert array_equal( sorted_candidates, array([[1., 2., 3.], [4., 5., 6.], [9., 9., 9.], [8., 9., 9.]])) assert array_equal(sorted_f, array([[1.], [2.], [5.], [6.]]))
def applyACO(**kwargs): try: prob = pg.problem(pg.rosenbrock(dim=kwargs.get('dim'))) pop = pg.population(prob, size=kwargs.get('tamPopulation'), seed=23) algo = pg.algorithm( pg.gaco(kwargs.get('iters'), kwargs.get('kernel'), 1.0, 1e9, 0.0, 1, 7, 100000, 100000, 0.0, False, 23)) algo.set_verbosity(1) pop = algo.evolve(pop) uda = algo.extract(pg.gaco) return uda.get_log() except: raise
def test_weighted_selection(): ''' Test the replacement and selection policies. ''' from sabaody.migration import BestSPolicy, FairRPolicy, sort_by_fitness from pygmo import population, rosenbrock # rosenbrock with dim 3 is just to suppress errors from pagmo, never evaluated p = population(prob=rosenbrock(3), size=0, seed=0) # create a fake population p.push_back(array([10., 11., 12.]), array([4.])) p.push_back(array([1., 2., 3.]), array([1.])) p.push_back(array([7., 8., 9.]), array([3.])) p.push_back(array([4., 5., 6.]), array([2.])) # test selection s = WeightedSPolicy(2) print(array(p.get_f()[:, 0])) print(s.compute_weights())
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 test_migration_replacement_policy_integration(): ''' Test migration replacement policy. ''' from sabaody.migration_central import CentralMigrator, start_migration_service from sabaody.migration import BestSPolicy, FairRPolicy, sort_by_fitness from sabaody.utils import arrays_equal from pygmo import population, rosenbrock try: process = start_migration_service() sleep(2) m = CentralMigrator('http://localhost:10100', None, FairRPolicy()) island1 = uuid4() island2 = uuid4() m.defineMigrantPool(island1, 3) m.defineMigrantPool(island2, 3) # migrants to island 1 m.pushMigrant(island1, array([1., 1., 1.]), 1., 'manual1') m.pushMigrant(island1, array([2., 2., 2.]), 2., 'manual1') # population for island 1 p1 = population(prob=rosenbrock(3), size=0, seed=0) p1.push_back(array([9., 0., 1.]), array([3.])) p1.push_back(array([9., 0., 2.]), array([4.])) # migrants to island 2 m.pushMigrant(island2, array([3., 3., 3.]), 3.5, 'manual2') m.pushMigrant(island2, array([4., 4., 4.]), 4.5, 'manual2') # population for island 2 p2 = population(prob=rosenbrock(3), size=0, seed=0) p2.push_back(array([9., 9., 1.]), array([3.])) p2.push_back(array([9., 9., 2.]), array([4.])) migrants, fitness, src_island_id = m.pullMigrants(island1) assert array_equal(migrants, array([ [2., 2., 2.], [1., 1., 1.], ])) assert array_equal(fitness, array([[2.], [1.]])) assert src_island_id == ['manual1', 'manual1'] # some parts of the code use loops like this # (zip-type looping with 2d arrays), so make sure it works for candidate, f in zip(migrants, fitness): assert float(f) in (1., 2.) assert array_equal(candidate, array([2., 2., 2.])) or array_equal( candidate, array([1., 1., 1.])) # re-push the migrants m.pushMigrant(island1, array([1., 1., 1.]), 1.) m.pushMigrant(island1, array([2., 2., 2.]), 2.) deltas, src_ids = m.replace(island1, p1) assert array_equal( sort_by_fitness(p1)[0], array([[1., 1., 1.], [2., 2., 2.]])) assert deltas == [-3., -1.] # test island 2 deltas, src_ids = m.replace(island2, p2) assert array_equal( sort_by_fitness(p2)[0], array([[9., 9., 1.], [3., 3., 3.]])) finally: process.terminate()
def make_problem(): import pygmo as pg return pg.problem(pg.rosenbrock(5))
from uuid import uuid4 m = CentralMigrator('http://localhost:10100') island1 = uuid4() island2 = uuid4() m.defineMigrantPool(island1, 3) m.defineMigrantPool(island2, 3) # migrants to island 1 m.pushMigrant(island1, array([1., 1., 1.]), 1.) m.pushMigrant(island1, array([2., 2., 2.]), 2.) # population for island 1 p1 = population(prob=rosenbrock(3), size=0, seed=0) p1.push_back(array([9., 0., 1.]), array([3.])) p1.push_back(array([9., 0., 2.]), array([4.])) # migrants to island 2 m.pushMigrant(island2, array([3., 3., 3.]), 3.) m.pushMigrant(island2, array([4., 4., 4.]), 4.) # population for island 2 p2 = population(prob=rosenbrock(3), size=0, seed=0) p2.push_back(array([9., 0., 1.]), array([3.])) p2.push_back(array([9., 0., 2.]), array([4.])) migrants = m.pullMigrants(island1) print('Number of migrants: {}'.format(len(migrants))) for m in migrants: print(m)
import uda_basic import pygmo import pickle ub = pygmo.algorithm(uda_basic.uda_basic()) assert pickle.dumps(pickle.loads(pickle.dumps(ub))) == pickle.dumps(ub) isl = pygmo.island(algo=ub, prob=pygmo.rosenbrock(), size=20) risl = repr(isl) assert "Thread" in risl isl.evolve() isl.wait_check() assert risl == repr(isl) class py_udp(object): def get_bounds(self): return ([0, 0], [1, 1]) def fitness(self, a): return [42] if __name__ == '__main__': isl = pygmo.island(algo=ub, prob=py_udp(), size=20) isl.evolve() isl.wait_check() pygmo.mp_island.shutdown_pool() pygmo.mp_bfe.shutdown_pool() print("All good!")
def problem_constructor(): import pygmo as pg return pg.rosenbrock(5)
def test_one_way_ring_migration(): ''' Tests the migration on a one way ring. ''' from sabaody.topology import TopologyFactory import pygmo as pg domain_qual = partial(getQualifiedName, 'com.how2cell.sabaody.test_one_way_ring_migration') problem = pg.problem(pg.rosenbrock(3)) topology_factory = TopologyFactory(island_size=3, domain_qualifier=domain_qual, mc_host='localhost', mc_port=11211) topology = topology_factory.createOneWayRing(pg.de(gen=10), number_of_islands=5) assert len(topology.island_ids) == 5 from sabaody.migration_central import CentralMigrator, start_migration_service from sabaody.migration import MigrationPolicyEachToAll, BestSPolicy, FairRPolicy, sort_by_fitness import pygmo as pg try: process = start_migration_service() sleep(2) migrator = CentralMigrator(MigrationPolicyEachToAll(), BestSPolicy(migration_rate=1), FairRPolicy(), 'http://localhost:10100') from collections import OrderedDict islands = OrderedDict( (i.id, pg.island(algo=i.algorithm, prob=problem, size=i.size)) for i in topology.islands) for island_id in islands.keys(): migrator.defineMigrantPool(island_id, 3) # seed solution in one island seed_first(islands, topology) assert count_hits(islands.values()) == 1 for n in range(1, 5 + 1): assert count_hits(islands.values()) == n # perform migration for island_id, i in islands.items(): migrator.sendMigrants(island_id, i, topology) for island_id, i in islands.items(): deltas, src_ids = migrator.receiveMigrants( island_id, i, topology) assert n == 5 # reset & differentiate from chain islands = OrderedDict( (i.id, pg.island(algo=i.algorithm, prob=problem, size=i.size)) for i in topology.islands) seed_predecessor(islands, topology) # perform migration for island_id, i in islands.items(): migrator.sendMigrants(island_id, i, topology) for island_id, i in islands.items(): deltas, src_ids = migrator.receiveMigrants(island_id, i, topology) assert tuple(islands.values())[0].get_population().champion_f[0] == 0. assert count_hits(islands.values()) == 2 finally: process.terminate()
import pygmo as pg """ Okay, so the lesson learned here is: not sure :-D """ if __name__ == "__main__": prob = pg.problem(pg.rosenbrock(dim=5)) pool_creator = pg.mp_island() # pool_creator.resize_pool(1) pool_creator.init_pool(1) island = pg.island(udi=pool_creator, algo=pg.sga(gen=200), pop=pg.population(prob, 200)) island.evolve() island.wait() print("island: ***** \n", island)
# Copyright 2018-2019 Shaik Asifullah and J Kyle Medley from __future__ import print_function, division, absolute_import from sabaody.scripts.benchmarks.pagmo.launcher import PagmobenchLauncher from sabaody.terminator import TerminatorBase from pygmo import problem, rosenbrock from os.path import join, dirname, abspath, realpath class RosenbrockTerminator(TerminatorBase): def __init__(self, dim, cutoff): self.dim = dim self.cutoff = cutoff def should_stop(self, pg_island, monitor): from numpy import mean, sqrt best_x = monitor.get_best_x() if best_x is None: return False else: return sqrt(mean((best_x-rosenbrock(self.dim).best_known())**2.)) < self.cutoff script_dir = dirname(realpath(__file__)) py_files = ','.join(join(script_dir,p) for p in [ '../launcher.py', ]) config = PagmobenchLauncher.from_cmdline_args(app_name='rb-driver', problem=lambda dim: problem(rosenbrock(dim)), spark_files='', py_files=py_files, terminator=RosenbrockTerminator) config.run_command(config.command)