def test_mutate_binomial_err1(): """If we fail to provide either expected_num_mutations or a probability parameter, an exception should occur when the operator is used.""" mutator = intrep_ops.mutate_binomial(std=1, bounds=[(0, 1), (0, 1)]) ind1 = Individual(np.array([0, 0])) ind2 = Individual(np.array([1, 1])) population = iter([ind1, ind2]) result = mutator(population) with pytest.raises(ValueError): # Pulse the iterator so mutation gets executed result = list(result)
def test_mutate_binomial_dist(): """When we apply binomial mutation repeatedly, the resulting distribution of offspring should follow the expected theoretical distribution.""" N = 5000 # Number of mutantes to generate binom_n = 10000 # "coin flips" parameter for the binomial std = 2.5 # Standard deviation of the mutation distribution # We'll set up our operator with infinite bounds, so we needn't worry about clipping operator = intrep_ops.mutate_binomial(std=std, expected_num_mutations=2, bounds=[(-float('inf'), float('inf')), (-float('inf'), float('inf'))]) # Any value could appear, but we'll focus on measuring just a few # nearby values genome = np.array([5, 10]) gene0_observed_dist = { '3': 0, '4': 0, '5': 0, '6': 0, '7':0 } gene1_observed_dist = { '8': 0, '9': 0, '10': 0, '11': 0, '12': 0 } # Count the observed mutations in N trials for i in range(N): population = iter([ Individual(genome.copy()) ]) mutated = next(operator(population)) gene0, gene1 = mutated.genome gene0, gene1 = str(int(gene0)), str(int(gene1)) # Count the observed values of the first gene if gene0 in gene0_observed_dist.keys(): gene0_observed_dist[gene0] += 1 # Count the observed values of the second gene if gene1 in gene1_observed_dist.keys(): gene1_observed_dist[gene1] += 1 # Set up the expected distribution by using SciPy's binomial PMF function binom_p = intrep_ops._binomial_p_from_std(binom_n, std) binom = stats.binom(binom_n, binom_p) mu = binom_n * binom_p # Mean of a binomial distribution is n*p gene0_expected_dist = { k: int(N*binom.pmf(int(mu - (genome[0] - int(k))))) for k in gene0_observed_dist.keys() } gene1_expected_dist = { k: int(N*binom.pmf(int(mu - (genome[1] - int(k))))) for k in gene1_observed_dist.keys() } # Toss all the other values under one value gene0_observed_dist['other'] = N - sum(gene0_observed_dist.values()) gene1_observed_dist['other'] = N - sum(gene1_observed_dist.values()) gene0_expected_dist['other'] = N - sum(gene0_expected_dist.values()) gene1_expected_dist['other'] = N - sum(gene1_expected_dist.values()) p = 0.01 assert(stat.stochastic_equals(gene0_expected_dist, gene0_observed_dist, p=p)) assert(stat.stochastic_equals(gene1_expected_dist, gene1_observed_dist, p=p))
def test_mutate_binomial_bounds(): """If we apply a wide mutation distribution repeatedly, it should never stray outside of the provided bounds. This test runs the stochastic function repeatedly, but we don't mark it as a stochastic test because it's and should never fail unless there is actually a fault.""" operator = intrep_ops.mutate_binomial(std=20, bounds=[(0, 10), (2, 20)], expected_num_mutations=1) N = 100 for i in range(N): population = iter([ Individual(np.array([5,10])) ]) mutated = next(operator(population)) assert(mutated.genome[0] >= 0) assert(mutated.genome[0] <= 10) assert(mutated.genome[1] >= 2) assert(mutated.genome[1] <= 20)
problem=problem, # Fitness function # Representation representation=Representation( # Initialize a population of integer-vector genomes initialize=create_int_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_binomial(std=1.5, 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(), probe.PopulationMetricsPlotProbe( metrics=[probe.pairwise_squared_distance_metric], title='Population Diversity'), probe.FitnessStatsCSVProbe(stream=sys.stdout) ])