def test_uniform_distribution(): """check that every gene of the tensor are going to be flipped equally Note: # ! We need enough iterations and chromosomes to reduce collision # ! and ensure numerical stability """ NUM_ITERATIONS = 300 GENOME_SHAPE = (20, 4, 4) population = B.randint(0, 1024, GENOME_SHAPE) population_fraction = 1 crossover_probability = (0.5, 0.5) OP = RandomMutations2D(population_fraction, crossover_probability) # diff matrix previous_population = B.copy(population) population = OP(population) diff = B.clip(abs(population - previous_population), 0, 1) for _ in range(NUM_ITERATIONS - 1): previous_population = B.copy(population) population = OP(population) curr_diff = B.clip(abs(population - previous_population), 0, 1) # acumulating diff matrix diff += curr_diff print(curr_diff) for c in diff: print(c) print('mean', B.mean(c), 'min', B.min(c), 'max', B.max(c)) assert B.min(c) > NUM_ITERATIONS // 15 assert B.max(c) < NUM_ITERATIONS // 2 assert NUM_ITERATIONS // 8 < B.mean(c) < NUM_ITERATIONS // 2
def test_uniformpop(): shape = (100, 100) pop = uniform_population(shape) assert pop.shape == shape assert B.max(pop) == 99 assert B.min(pop) == 0 for chrm in pop: _, _, count = B.unique_with_counts(chrm) assert B.max(count) == 1
def test_binary_val_default_params(): pop_shape = (6, 4) population = randint_population(pop_shape, 2) population = RandomMutations1D(max_gene_value=1, debug=1)(population) print(population) assert B.max(population) == 1 assert not B.min(population)
def test_randintpop(): shape = (100, 100, 10) pop = randint_population(shape, 42, 1) assert pop.shape == shape assert B.max(pop) == 42 assert B.min(pop) == 1 assert pop.dtype == B.intx()
def test_mutation2d_eager(): pop_shape = (10, 4, 4) max_gene_value = 10 min_gene_value = 0 population_fraction = 1 mutations_probability = (0.5, 0.5) min_mutation_value = 1 max_mutation_value = 1 population = randint_population(pop_shape, max_gene_value) # save original original_population = B.copy(population) cprint('[Initial genepool]', 'blue') cprint(original_population, 'blue') for _ in range(3): RM = RandomMutations2D(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value, debug=True) population = RM(population) cprint('\n[Mutated genepool]', 'yellow') cprint(population, 'yellow') cprint('\n[Diff]', 'magenta') diff = population - original_population cprint(diff, 'magenta') assert B.is_tensor(population) assert population.shape == pop_shape assert B.max(diff) <= max_mutation_value ok = 0 for chromosome in diff: if B.sum(chromosome) >= 2: ok += 1 if (ok / len(diff)) > 0.5: break assert (ok / len(diff)) > 0.5
def test_1D_tensor_values_maintained(): POPULATION_SHAPE = (6, 8) # need a uniform population otherwise its not testable. population = uniform_population(POPULATION_SHAPE) population_fraction = 0.5 max_reverse_probability = 0.2 cprint(population, 'cyan') population = Reverse1D(population_fraction, max_reverse_probability, debug=1)(population) cprint(population, 'yellow') for chrm in population: _, _, count = B.unique_with_counts(chrm) cprint(count, 'green') assert B.max(count) == 1
def test_uniform_2Dcrossover_randomness_shape(): GENOME_SHAPE = (10, 4, 4) population = B.randint(0, 1024, GENOME_SHAPE) population_fraction = 0.5 crossover_probability = (0.5, 0.5) original_population = copy(population) OP = UniformCrossover2D(population_fraction, crossover_probability) population = OP(population) diff = B.clip(abs(population - original_population), 0, 1) print(diff) expected_mutations = original_population.shape[0] * population_fraction mutated_chromosomes = [] for c in diff: if B.max(c): mutated_chromosomes.append(c) num_mutations = len(mutated_chromosomes) # sometime we have a collision so we use a delta assert abs(num_mutations - expected_mutations) < 2 mutated_rows = crossover_probability[0] * GENOME_SHAPE[1] mutated_cells = crossover_probability[0] * GENOME_SHAPE[2] for cidx, c in enumerate(mutated_chromosomes): mr = 0.0 mc = 0.0 for r in c: s = B.cast(B.sum(r), B.floatx()) if s: mr += 1 mc += s assert abs(mutated_rows - mr) <= 2 assert abs(B.cast(mutated_cells, B.floatx()) - (mc / mutated_rows)) <= 3.0 # 2.5
def test_clip(): pop_shape = (100, 100) population = randint_population(pop_shape, 100) population = RandomMutations1D(max_gene_value=100, debug=1)(population) assert B.max(population) <= 100 assert not B.min(population)
population = UniformCrossover2D(population_fraction, crossover_probability)(population) print(population) # diff matrix diff = B.clip(abs(population - original_population), 0, 1) print(diff) quit() # expected mutated chromosomes expected_mutated = population.shape[0] * population_fraction cprint( "Expected mutated chromosome:%d (+/- 1 due to collision)" % (expected_mutated), 'cyan') # select mutated chromosomes mutated_chromosomes = [] for c in diff: if B.max(c): mutated_chromosomes.append(c) cprint("mutated chromosome:%d" % (len(mutated_chromosomes)), 'magenta') cprint("[example of mutated chromosome]", 'yellow') cprint(mutated_chromosomes[0], 'cyan') OP = UniformCrossover2D(population_fraction, crossover_probability) res = OP(population) for _ in range(100): prev = copy(res) res = OP(population) # acumulating diff matrix diff += B.clip(abs(res - prev), 0, 1) cprint("[cumulative diff matrix 100 runs]", 'yellow')
pop_shape = (6, 4, 4) max_gene_value = 10 min_gene_value = 0 population_fraction = 0.5 mutations_probability = (0.5, 0.5) min_mutation_value = 1 max_mutation_value = 1 population = B.randint(0, max_gene_value, pop_shape) OP = RandomMutations2D(population_fraction=population_fraction, mutations_probability=mutations_probability, min_gene_value=min_gene_value, max_gene_value=max_gene_value, min_mutation_value=min_mutation_value, max_mutation_value=max_mutation_value) chromosomes_sav = copy(population) cprint('[Initial genepool]', 'blue') cprint(chromosomes_sav, 'blue') population = OP(population) cprint('\n[Mutated genepool]', 'yellow') cprint(population, 'yellow') cprint('\n[Diff]', 'magenta') diff = population - chromosomes_sav cprint(diff, 'magenta') assert B.max(diff) <= max_mutation_value