Island and Archipelago¶
NOTE: There are two different types of islands in PyGMO. The PyGMO.local_island and the PyGMO.py_island. The need for two different types of island is purely technical (it is necessary to give the user the possibility of implementing the problem or the algorithm in python directly) and the user needs not to know any details. We will thus here document only one class that we call island and that, in reality, is a helper function returning automatically the correct object type.
- class PyGMO.island¶
At the core of the PyGMO implementation of the generalized asynchronous island model is this class: the island. An island, in PyGMO, contains a PyGMO.population and a PyGMO.algorithm and is the object responsible to launch the thread that applies the PyGMO.algorithm to evolve the PyGMO.population
- __init__((PyGMO.algorithm)algo, (PyGMO.population)pop[, s_policy = best_s_policy(1), r_policy=fair_r_policy(1)])¶
Constructs an island from a population. The population will be evolved by algo. Migration occurs at the end of each evolution if the island belongs to a PyGMO.archipelago
from PyGMO import * prob = problem.schwefel(2) algo = algorithm.de(10) #instantiates differential evolution with default params and 10 generations pop = population(prob,20) isl = island(algo,pop)
- __init__((PyGMO.algorithm)algo, (PyGMO.problem)prob, (int)N=0[, s_policy = best_s_policy(1), r_policy=fair_r_policy(1)])
Constructs an island directly from a problem. The resulting population (of size N) will be evolved by algo. Migration occurs at the end of each evolution if the island belongs to an PyGMO.archipelago
from PyGMO import * prob = problem.schwefel(2) algo = algorithm.de(10) #instantiates differential evolution with default params and 10 generations isl = island(algo,prob,20)
- evolve((int)n)¶
Evolves the PyGMO.population in the island performing n calls to PyGMO.algorithm. At the end of each call migration occurs if the island belongs to a PyGMO.archipelago. Evolution happens in the background on a dedicated thread while the program flows continues.
from PyGMO import * prob = problem.schwefel(1500) algo = algorithm.de(100) #instantiates differential evolution with default params and 100 generations pop = population(prob,20) isl = island(algo,pop) isl.evolve(1) #calls algo to evolve pop. The program flow continues
- join()¶
Joins the thread (if still present) started by the PyGMO.island.evolve method. In other words it waits for PyGMO.island.evolve to have finished.
from PyGMO import * prob = problem.schwefel(1500) algo = algorithm.de(100) #instantiates differential evolution with default params and 100 generations pop = population(prob,20) isl = island(algo,pop) isl.evolve(1) #calls algo to evolve pop. The program flow continues isl.join() #Waits for the evolve to finish (i.e. waits for completion of the 100 generations of differential evolution)
- busy()¶
Returns True if evolution is ongoing in the island.
- set_x((int)idx, (list) x)¶
Sets a new chromosome for the idx-th PyGMO.individual in the PyGMO.population of the island to x.
from PyGMO import * prob = problem.ackley(5) algo = algorithm.de(10) #instantiates differential evolution with default params and 10 generations isl = island(algo,prob,10) isl.population.set_x(0,[1,2,3,4,5]) # This is completely uneffective # as the 'attribute' population returns a copy isl.set_x(0,[1,2,3,4,5]) # This works!!
- set_v((int)idx, (list) v)¶
Sets the velocity of the idx-th PyGMO.individual in the PyGMO.population of the island to v.
from PyGMO import * prob = problem.ackley(5) algo = algorithm.de(10) #instantiates differential evolution with default params and 10 generations isl = island(algo,prob,10) isl.population.set_v(0,[0.02,0.03,-0.3,0.12,0.1]) # This is completely uneffective # as the 'attribute' population returns a copy isl.set_v(0,[0.02,0.03,-0.3,0.12,0.1]) # This works!!
- get_evolution_time()¶
Returns the time PyGMO has spent on evolving that island in milliseconds
from PyGMO import * prob = problem.ackley(5) algo = algorithm.de(10) #instantiates differential evolution with default params and 10 generations isl = island(algo,prob,40) isl.evolve(100) isl.get_evolution_time()
- algorithm¶
The island PyGMO.algorithm. Can be set, but not modified via its methods.
- population¶
The island PyGMO.population. Can be set, but not modified via its methods.
- problem¶
A copy of the PyGMO.problem in the PyGMO.population. Cannot be set or modified via its methods.
- class PyGMO.archipelago¶
Probably the most important object in all PyGMO. An Archipelago is a container of PyGMO.island and is responsible to start the asynchronous island model. The solutions exchange among PyGMO.island is done following the routes allowed by the underlying topology
- __init__([topology = unconnected(), distribution_type = point_to_point, migration_direction = destination])¶
Constructs an empty archipelago from a topology (defaults to PyGMO.topology.unconnected()) a distribution type (defaults to PyGMO.distribution_type.point_to_point) and a migration direction (defaults to PyGMO.migration_direction.destination)
from PyGMO import * archi = archipelago() #constructs an empty archipelago with an unconnected topology archi = archipelago(topology = topology.ring()) #constructs an empty archipelago with a ring topology
- __init__((PyGMO.algorithm)algo, (PyGMO.problem)prob, (int)n_isl, (int)n_ind[, topology = unconnected(), distribution_type = point_to_point, migration_direction = destination])
Constructs an empty archipelago from a topology (defaults to PyGMO.topology.unconnected()) a distribution type (defaults to PyGMO.distribution_type.point_to_point) and a migration direction (defaults to PyGMO.migration_direction.destination). It then pushes back into the archipelago n_isl PyGMO.island constructed using defaults values for the kwargs and (algo,prob,n_ind) as args.
from PyGMO import * prob = problem.grewank(30) algo = algorithm.bee_colony(50) archi = archipelago(algo,prob,8,20) #constructs an archipelago having 8 islands with populations #of 20 individuals. All islands have a copy of the bee_colony solver #and a copy of the griewank(30) problem
- evolve((int)n)¶
Calls the method PyGMO.island.evolve (n) on all the PyGMO.island of the archipelago. In other words, it starts the asynchronous generalized island model that is at the core of PyGMO.
from PyGMO import * prob = problem.schwefel(15) algo = algorithm.de(500) #instantiates differential evolution with default #params and 100 generations archi = archipelago(algo,prob,8,20) archi.evolve(10) #starts the asynchronous generalized island model. #each of the 8 islands will call algo 10 times and try to migrate in between calls
- push_back((PyGMO.island) isl)¶
Pushes back isl in the archipelago taking also to also update the topological links between islands. This method also checks that the island inserted is compatible with the other islands present in the archipelago (i.e. it contains a :class`PyGMO.population` with a :class`PyGMO.problem that is compatible)
- join()¶
Joins all threads (if still present) started by the PyGMO.archipelago. In other words it waits for evolution to have finished.
from PyGMO import * prob = problem.schwefel(15) algo = algorithm.de(500) #instantiates differential evolution with default #params and 100 generations archi = archipelago(algo,prob,8,20) archi.evolve(10) #starts the asynchronous generalized island model. archi.join() #waits for it to finish [isl.population.champion.f for isl in archi] #builds a list with the best fittnesses found
- busy()¶
Returns True if evolution is ongoing in the archipelago.
- interrupt()¶
Halts evolution at the first occasion in all islands.
- archipelago.draw()¶
Draw a visualization of the archipelago using networkx.
USAGE: pos = archipelago.draw(layout = ‘spring’, color = ‘fitness’, n_size = 15, scale_by_degree = False, n_alpha = 0.5, e_alpha = 0.1, cmap = ‘default’, e_arrows=False)
- layout: Network layout. Can be ‘spring’ or ‘circular’ or a list of values pos returned
by a previous call of the method (so that positions of the islands can be kept fixed.
n_color = Defines the color code for the nodes. Can be one of ‘fitness’, ‘links’, ... or the standard matplotlib ‘blue’ .. etc.
n_size: The size of nodes. Becomes scaling factor when scale_by_degree=True.
n_alpha: Transparency of nodes. Takes value between 0 and 1.
e_arrows: Plots arrows on the edges for directed graphs
e_elpha: Transparency of edges. Takes value between 0 and 1.
scale_by_degree: When True, nodes will be sized proportional to their degree.
cmap: color map. one in matplotlib.pyplot.cm
from PyGMO import * prob = problem.rosenbrock(10) algo = algorithm.cmaes(gen=100) archi = archipelago(algo,prob,8,20,topology = topology.ring()) archi.draw()
- dump_migration_history()¶
Returns a temporal history of all the archipelago migrations in one string. Each entry is in the form (n,src_isl,dest_isl) and logs that n individuals, from the island having the index src_isl, successfully replaced n individuals in the population of the island having the index dest_isl.