def test_tournament_selection2(): """If there are just two individuals in the population, and we set select_worst=True, then binary tournament selection will select the worse one with 75% probability.""" # Make a population where binary tournament_selection has an obvious # reproducible choice pop = [ Individual(np.array([0, 0, 0]), problem=MaxOnes()), Individual(np.array([1, 1, 1]), problem=MaxOnes()) ] # Assign a unique identifier to each individual pop[0].id = 0 pop[1].id = 1 # We first need to evaluate all the individuals so that # selection has fitnesses to compare pop = Individual.evaluate_population(pop) selected = ops.tournament_selection(pop, select_worst=True) N = 1000 p_thresh = 0.1 observed_dist = statistical_helpers.collect_distribution( lambda: next(selected).id, samples=N) expected_dist = {pop[0].id: 0.75 * N, pop[1].id: 0.25 * N} print(f"Observed: {observed_dist}") print(f"Expected: {expected_dist}") assert (statistical_helpers.stochastic_equals(expected_dist, observed_dist, p=p_thresh))
def test_tournament_selection_indices(): """If an empty list is provided to tournament selection, it should be populated with the index of the selected individual. If we select a second individual, the list should be cleared and populated with the index of the second individual.""" pop = test_population indices = [] op = ops.tournament_selection(indices=indices) # Select an individual s = next(op(pop)) # Ensure the returned index is correct assert (len(indices) == 1) idx = indices[0] assert (idx >= 0) assert (idx < len(pop)) assert (pop[idx] is s) # Select another individual s = next(op(pop)) # Ensure the returned index is correct assert (len(indices) == 1) idx = indices[0] assert (idx >= 0) assert (idx < len(pop)) assert (pop[idx] is s)
def test_tournament_selection(): """ This simple binary tournament_selection selection """ # Make a population where binary tournament_selection has an obvious reproducible choice pop = [ Individual([0, 0, 0], decoder=IdentityDecoder(), problem=MaxOnes()), Individual([1, 1, 1], decoder=IdentityDecoder(), problem=MaxOnes()) ] # We first need to evaluate all the individuals so that truncation selection has fitnesses to compare pop = Individual.evaluate_population(pop) best = next(ops.tournament_selection(pop)) pass
problem=problem, # Fitness function # Stopping condition: we stop when fitness or diversity drops below a threshold stop=lambda pop: (max(pop).fitness < fitness_threshold) or (probe.pairwise_squared_distance_metric(pop) < diversity_threshold), # Representation representation=Representation( # Initialize a population of integer-vector genomes initialize=create_real_vector( bounds=[problem.bounds] * l) ), # Operator pipeline pipeline=[ ops.tournament_selection(k=2), ops.clone, # Apply binomial mutation: this is a lot like # additive Gaussian mutation, but adds an integer # value to each gene mutate_gaussian(std=0.2, hard_bounds=[problem.bounds]*l, expected_num_mutations=1), ops.evaluate, ops.pool(size=pop_size), # Some visualization probes so we can watch what happens probe.CartesianPhenotypePlotProbe( xlim=problem.bounds, ylim=problem.bounds, contours=problem), probe.FitnessPlotProbe(),