예제 #1
0
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)
예제 #2
0
def test_bidir_chain():
    '''
    Tests the migration on a one way chain.
    '''
    from sabaody.topology import TopologyFactory

    def make_problem():
        import pygmo as pg
        return pg.problem(pg.rosenbrock(3))

    def make_algorithm():
        return pg.de(gen=10)

    domain_qual = partial(getQualifiedName,
                          'com.how2cell.sabaody.test_bidir_chain_migration')
    topology_factory = TopologyFactory(make_problem, domain_qual, 'localhost',
                                       11211)
    topology = topology_factory.createBidirChain(make_algorithm, 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 BestSPolicy, FairRPolicy, sort_by_fitness
    import pygmo as pg
    try:
        process = start_migration_service()
        sleep(2)
        migrator = CentralMigrator('http://localhost:10100',
                                   BestSPolicy(migration_rate=1),
                                   FairRPolicy())

        from collections import OrderedDict
        for k in (1, 2):
            islands = OrderedDict((i.id,
                                   pg.island(algo=i.algorithm_constructor(),
                                             prob=i.problem_constructor(),
                                             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()
예제 #3
0
 def select_selection_policy(cls,
                             policy_name,
                             migration_rate=None,
                             pop_fraction=None):
     from sabaody.migration import BestSPolicy
     if policy_name == 'best-s-policy' or policy_name == 'best':
         if migration_rate is not None and pop_fraction is not None:
             raise RuntimeError(
                 'Specify either migration rate or fraction, not both')
         if migration_rate is not None:
             return BestSPolicy(migration_rate=migration_rate)
         elif pop_fraction is not None:
             return BestSPolicy(pop_fraction=pop_fraction)
         else:
             raise RuntimeError(
                 'Neither migration rate nor fraction specified')
     else:
         raise RuntimeError('Unknown selection policy')
예제 #4
0
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.]]))
예제 #5
0
def test_one_way_ring_migration():
    '''
    Tests the migration on a one way ring.
    '''
    from sabaody.topology import TopologyFactory

    def make_problem():
        import pygmo as pg
        return pg.problem(pg.rosenbrock(3))

    def make_algorithm():
        return pg.de(gen=10)

    domain_qual = partial(getQualifiedName,
                          'com.how2cell.sabaody.test_one_way_ring_migration')
    topology_factory = TopologyFactory(make_problem, domain_qual, 'localhost',
                                       11211)
    topology = topology_factory.createOneWayRing(make_algorithm, 5)
    assert len(topology.island_ids) == 5

    from sabaody.migration_central import CentralMigrator, start_migration_service
    from sabaody.migration import BestSPolicy, FairRPolicy, sort_by_fitness
    import pygmo as pg
    try:
        process = start_migration_service()
        sleep(2)
        migrator = CentralMigrator('http://localhost:10100',
                                   BestSPolicy(migration_rate=1),
                                   FairRPolicy())

        from collections import OrderedDict
        islands = OrderedDict((i.id,
                               pg.island(algo=i.algorithm_constructor(),
                                         prob=i.problem_constructor(),
                                         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_constructor(),
                                         prob=i.problem_constructor(),
                                         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()