Ejemplo n.º 1
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()
Ejemplo n.º 2
0
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()
Ejemplo n.º 3
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()