Exemplo n.º 1
0
def test_one_way_ring_topology():
    '''
    Test the one way ring topology.
    '''
    from sabaody.topology import TopologyFactory
    domain_qual = partial(getQualifiedName,
                          'com.how2cell.sabaody.test_one_way_ring_topology')
    topology_factory = TopologyFactory(island_size=10,
                                       domain_qualifier=domain_qual,
                                       mc_host='localhost',
                                       mc_port=11211)

    t = topology_factory.createOneWayRing(None, number_of_islands=4)
    assert len(t.island_ids) == 4
    assert len(t.islands) == 4
    for island, id in zip(t.islands, t.island_ids):
        assert island.id == id
    for id in t.island_ids:
        # should be two neighbors, but one-way migration
        assert len(t.neighbor_islands(id)) == len(t.neighbor_ids(id)) == 2
        assert len(t.outgoing_islands(id)) == len(t.outgoing_ids(id)) == 1
        # outgoing node should be in neighbors
        assert frozenset(t.outgoing_ids(id)) < frozenset(t.neighbor_ids(id))
        assert frozenset(t.outgoing_islands(id)) < frozenset(
            t.neighbor_islands(id))
Exemplo n.º 2
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)
Exemplo n.º 3
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()
Exemplo n.º 4
0
def test_barabasi_albert():
    '''
    Test whether the hub is correctly assigned.
    '''
    topology_factory = TopologyFactory(island_size=20)
    topology = topology_factory.createBarabasiAlbert(m=2)
    for id in topology.island_ids:
        assert len(topology.neighbors(topology.hub.id)) >= len(
            topology.neighbors(id))
Exemplo n.º 5
0
 def generate_archipelago(self, topology_name, metric, monitor):
     from os.path import isfile
     from re import compile
     db_regex = compile(
         r'sql:(\w+)@([\w:]+),pw=([^,]+),db=([\w]+)\(name=(\w+),n_islands=(\d+),island_size=(\d+),migrant_pool_size=(\d+),generations=(\d+)\):(.*)'
     )
     if isfile(topology_name):
         import pickle
         with open(topology_name) as f:
             return pickle.load(f)['archipelago']
     elif db_regex.match(topology_name) is not None:
         m = db_regex.match(topology_name)
         from sabaody import TopologyGenerator, BiopredynTopologyGenerator
         name = m.group(5)
         if name == 'pagmo':
             generator_class = TopologyGenerator
         elif name == 'biopredyn':
             generator_class = BiopredynTopologyGenerator
         generator = generator_class(n_islands=int(m.group(6)),
                                     island_size=int(m.group(7)),
                                     migrant_pool_size=int(m.group(8)),
                                     generations=int(m.group(9)))
         topology, id = generator.find_in_database(desc=m.group(10),
                                                   user=m.group(1),
                                                   host=m.group(2),
                                                   pw=m.group(3),
                                                   db=m.group(4))
         self.topology_set_id = id
         self.topology_id = topology['id']
         self.generations = topology['generations']
         return Archipelago(
             TopologyFactory.prefixIds(topology['archipelago'].topology,
                                       prefix=self.run_id + '-'))
     else:
         # generate the topology from available presets via command line arguments
         topology_factory = TopologyFactory(
             problem=self.make_problem(),
             island_size=self.island_size,
             migrant_pool_size=self.migrant_pool_size,
             domain_qualifier=monitor.getNameQualifier(),
             mc_host=monitor.mc_host,
             mc_port=monitor.mc_port)
         if topology_name == 'ring' or topology_name == 'bidir-ring':
             return Archipelago(
                 topology_factory.createBidirRing(self.make_algorithm(),
                                                  self.n_islands), metric)
         elif topology_name == 'one-way-ring':
             return Archipelago(
                 topology_factory.createOneWayRing(self.make_algorithm(),
                                                   self.n_islands), metric)
         else:
             raise RuntimeError('Unrecognized topology')
Exemplo n.º 6
0
def test_bidir_ring_topology():
    '''
    Test the one way ring topology.
    '''
    from sabaody.topology import TopologyFactory
    domain_qual = partial(getQualifiedName,
                          'com.how2cell.sabaody.test_one_way_ring_topology')
    topology_factory = TopologyFactory(NoProblem, domain_qual, 'localhost',
                                       11211)

    t = topology_factory.createBidirRing(None, 4)
    assert len(t.island_ids) == 4
    assert len(t.islands) == 4
    for id in t.island_ids:
        assert len(t.neighbor_islands(id)) == len(t.neighbor_ids(id)) == 2
        assert len(t.outgoing_islands(id)) == len(t.outgoing_ids(id)) == 2
Exemplo n.º 7
0
def test_rim_topology():
    '''
    Test the rim topology.
    '''
    from sabaody.topology import TopologyFactory
    domain_qual = partial(getQualifiedName,
                          'com.how2cell.sabaody.test_rim_topology')
    topology_factory = TopologyFactory(NoProblem, domain_qual, 'localhost',
                                       11211)

    t = topology_factory.createRim(None, 5)
    assert len(t.island_ids) == 5
    assert len(t.islands) == 5
    # hub
    assert count_nodes_with_degree(t, 4) == 1
    # adjacent to hub
    assert count_nodes_with_degree(t, 2) == 2
    # not adjacent to hub
    assert count_nodes_with_degree(t, 3) == 2
Exemplo n.º 8
0
 def __init__(self, n_islands, island_size=20, migrant_pool_size=5, generations=10, seed=1):
     self.topologies = []
     from sabaody.topology import TopologyFactory
     self.factory =  TopologyFactory(island_size=island_size,
                                     migrant_pool_size=migrant_pool_size,
                                     seed=seed)
     self.name = 'pagmo'
     self.n_islands = n_islands
     self.island_size = island_size
     self.migrant_pool_size = migrant_pool_size
     self.generations = generations
     self._generate_all(n_islands)
Exemplo n.º 9
0
def test_uniform_migration_policy():
    '''
    Test the uniform migration policy.
    '''
    from sabaody.topology import TopologyFactory
    topology_factory = TopologyFactory(island_size=5, migrant_pool_size=5)
    topology = topology_factory.createBidirChain(None, number_of_islands=3)
    assert len(topology.island_ids) == 3
    assert len(topology.endpoints) == 2

    middle_node = topology.island(
        tuple(set(topology.island_ids) - set(topology.endpoints))[0])
    assert len(topology.neighbor_ids(middle_node.id)) == 2

    migrants = array([[9.] * 4] * 5)
    fitness = array([[0.]] * 5)
    from sabaody.migration import MigrationPolicyUniform
    uniform_policy = MigrationPolicyUniform()

    # test whether # migrants out = # migrants in
    from numpy import vstack
    all_migrants = vstack(
        map(
            lambda x: x[1],
            uniform_policy.disperse(middle_node.id, topology, migrants,
                                    fitness)))
    assert array_equal(
        all_migrants,
        array([[9., 9., 9., 9.], [9., 9., 9., 9.], [9., 9., 9., 9.],
               [9., 9., 9., 9.], [9., 9., 9., 9.]]))

    # test statistical properties like:
    # * sampling WITH replacement
    # * uniformity
    # since this is a statistical test, it has a chance to fail spuriously
    # use a large sample size to minimize probability of failure
    clique = topology_factory.createFullyConnected(None, number_of_islands=4)
    # graph is fully connected and hence symmetric - pick any node
    node = clique.islands[0]

    num_migrants = 3
    migrants = array([[9.] * 4] * num_migrants)
    fitness = array([[0.]] * num_migrants)

    with_replacement_at_least_once = False
    N = 1000
    sums = {id: 0. for id in clique.island_ids if not id == node.id}
    for k in range(N):
        for id, incoming, f in uniform_policy.disperse(node.id, clique,
                                                       migrants, fitness):
            if incoming.shape[0] > 1:
                with_replacement_at_least_once = True
            sums[id] += float(incoming.shape[0])
    averages = {id: float(sums[id]) / N for id in sums.keys()}
    # With three neighboring islands, the variance for number of migrants
    # per island is 2/3. By the Central Limit Theorem, the variance  of the
    # mean number of migrants per island will approach 2/(3*N) for large N.
    # Following a six sigma rule, a bounds check of sqrt(6*(2/(3*N))) = 2/sqrt(N)
    # will yield about 1 / 1 billion spurious failures.
    from math import sqrt, isclose
    for id, average in averages.items():
        assert isclose(average, 1., abs_tol=2. / sqrt(N))
    # check that at least one island had 2 or more migrants in the N runs
    assert with_replacement_at_least_once == True
Exemplo n.º 10
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()