def call(self, population): """ Create the mask use to generate mutations Args: population_shape (list): population tensor shape. Returns: tensor: mask Generation works by: 1. creating a slice that contains the mutation 2. Inserting it into the mask 3. Shuffle the mask in every dimension to distribute them """ affected_population = int(population.shape[0] * self.population_fraction) # Build sub tensors & slices by iterating through tensor dimensions sub_tensor_shape = [affected_population] slices = [slice(0, affected_population)] for idx, pop_size in enumerate(population.shape[1:]): midx = idx - 1 # recall dim1 are genes. max_genes = int(pop_size * self.mutations_probability[midx] + 1) num_genes = B.randint(1, high=max_genes) sub_tensor_shape.append(num_genes) slices.append(slice(0, num_genes)) slices = tuple(slices) tslices = slices2array(slices) self.print_debug("sub_tensor_shape", sub_tensor_shape) # drawing mutations mutations = B.randint(self.min_mutation_value, self.max_mutation_value + 1, shape=sub_tensor_shape) # blank mask mask = B.zeros(population.shape, dtype=mutations.dtype) # add mutations # print('mtuation', mutations.dtype) # print('mask', mask.dtype) mask = B.assign(mask, mutations, tslices) # shuffle mask every axis mask = B.full_shuffle(mask) # mutate population = population + mask # normalize if self.max_gene_value or self.min_gene_value: self.print_debug("min_gen_val", self.min_gene_value) self.print_debug("max_gen_val", self.max_gene_value) population = B.clip(population, min_val=self.min_gene_value, max_val=self.max_gene_value) return population
def call(self, population): if not self.debug: population = B.shuffle(population) # how many chromosomes to crossover num_reversed_chromosomes = int(population.shape[0] * self.population_fraction) self.print_debug('num chromosomes', num_reversed_chromosomes) # compute the shape needed for the mutation mutations_shape = [num_reversed_chromosomes] for idx, frac in enumerate(self.max_reverse_probability): max_genes = int(population.shape[idx + 1] * frac + 1) # ! not an error: reverse need at least 2 indices to make sense. if max_genes > 2: num_genes = B.randint(2, high=max_genes) else: num_genes = 2 mutations_shape.append(num_genes) self.print_debug(idx, 'num_genes', num_genes, 'max', max_genes) self.print_debug("population_shape:", population.shape) self.print_debug("mutation_shape:", mutations_shape) # compute the fancy indexing dynamlically # ! the start point must be randomized slices = [slice(0, num_reversed_chromosomes)] for idx, crossover_size in enumerate(mutations_shape[1:]): # ! making indexing explicit as its a huge pitfall mutation_dim = idx + 1 max_start = population.shape[mutation_dim] - crossover_size + 1 start = B.randint(0, max_start) # start = random.randint(0, max_start) slices.append(slice(start, crossover_size + start)) slices = tuple(slices) tslices = slices2array(slices) self.print_debug('slices', slices) # revesing reversed_population = population[slices] axis = B.tensor([x for x in range(1, len(reversed_population.shape))]) reversed_population = B.reverse(reversed_population, axis) self.print_debug('reversed population', reversed_population) # assigning population = B.assign(population, reversed_population, tslices) return population
def test_crossover1D_output_shape(): POPULATION_SHAPE = (8, 6) population = B.randint(0, 1024, POPULATION_SHAPE) population_fraction = 0.5 mutations_probability = 0.2 original_population = copy(population) population = DualCrossover1D(population_fraction, mutations_probability, debug=True)(population) cprint(population, 'cyan') cprint(original_population, 'yellow') assert population.shape == POPULATION_SHAPE # measuring mutation rate diff = B.clip(abs(population - original_population), 0, 1) # row test num_ones_in_row = 0 for col in diff: num_ones_in_row = max(list(col).count(1), num_ones_in_row) max_one_in_row = int(POPULATION_SHAPE[1] * mutations_probability) assert num_ones_in_row == max_one_in_row assert num_ones_in_row
def test_ND(): "test various tensor size random" TEST_INPUTS = [ [UniformCrossover1D, (2, 4), 0.5], [UniformCrossover2D, (2, 4, 4), (0.5, 0.5)], [UniformCrossover3D, (2, 4, 4, 4), (0.5, 0.5, 0.5)], ] for inputs in TEST_INPUTS: OP = inputs[0] pop_shape = inputs[1] mutations_probability = inputs[2] population_fraction = 1 population = B.randint(0, 1024, pop_shape) # eager RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability) population = RM(population) assert B.is_tensor(population) assert population.shape == pop_shape # graph RM = OP(population_fraction=population_fraction, mutations_probability=mutations_probability) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
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 call(self, population): # mix genomes shuffled_population = B.copy(population) shuffled_population = B.shuffle(shuffled_population) # how many chromosomes to crossover num_crossovers = int(population.shape[0] * self.population_fraction) self.print_debug('num_crossovers', num_crossovers) # compute the shape needed for the mutation mutations_shape = [num_crossovers] for idx, frac in enumerate(self.max_crossover_probability): max_genes = int(population.shape[idx + 1] * frac + 1) if max_genes > 1: num_genes = B.randint(1, high=max_genes) else: num_genes = 1 mutations_shape.append(num_genes) self.print_debug("mutation_shape: %s" % mutations_shape) slices = [] for crossover_size in mutations_shape: slices.append(slice(0, crossover_size)) slices = tuple(slices) tslices = slices2array(slices) self.print_debug('slices', slices) # crossover cross_section = shuffled_population[slices] population = B.assign(population, cross_section, tslices) return population
def test_max_gene_val_2d(): MAX_VAL = 10 t = B.randint(0, MAX_VAL + 1, (10, 10, 10)) max_sum_value = MAX_VAL * 10 * 10 v = Sum(max_sum_value=max_sum_value).call(t) assert v.shape == (10, ) for t in v: assert t < 1
def call(self, population): # mix genomes shuffled_population = B.copy(population) shuffled_population = B.shuffle(shuffled_population) # how many chromosomes to crossover num_crossover_chromosomes = int(population.shape[0] * self.population_fraction) self.print_debug('num chromosomes', num_crossover_chromosomes) # compute the shape needed for the mutation mutations_shape = [num_crossover_chromosomes] for idx, frac in enumerate(self.max_crossover_probability): max_genes = int(population.shape[idx + 1] * frac + 1) if max_genes > 1: num_genes = B.randint(1, high=max_genes) else: num_genes = 1 mutations_shape.append(num_genes) mutations_shape = mutations_shape self.print_debug("population_shape:", population.shape) self.print_debug("mutation_shape:", mutations_shape) # compute the fancy indexing dynamlically # ! the start point must be randomized slices = [slice(0, num_crossover_chromosomes)] for idx, crossover_size in enumerate(mutations_shape[1:]): # ! making indexing explicit as its a huge pitfall mutation_dim = idx + 1 max_start = population.shape[mutation_dim] - crossover_size + 1 start = B.randint(0, max_start) slices.append(slice(start, crossover_size + start)) slices = tuple(slices) tslices = slices2array(slices) self.print_debug('slices', slices) # crossover cross_section = shuffled_population[slices] population = B.assign(population, cross_section, tslices) return population
def call(self, population): # mix genomes population_copy = B.copy(population) population_copy = B.shuffle(population_copy) # how many chromosomes to crossover? num_crossovers = int(population.shape[0] * self.population_fraction) self.print_debug("population size %s" % population.shape[0]) self.print_debug("num_crossovers %s" % num_crossovers) # crossover matrix x_matrix = B.zeros(population.shape, dtype=population.dtype) self.print_debug("Creating x_matrix and mutation_matrix") # We need to accounting for the fact that the population # can be of rank N which makes the fancy indexing painful. # we need a shape for the mutation which is this: # [num_crossover, num_mutation, ..., num_mutations] mutations_shape = [num_crossovers] for idx, frac in enumerate(self.max_crossover_probability): max_genes = int(population.shape[idx + 1] * frac + 1) if max_genes > 1: num_genes = B.randint(1, high=max_genes) else: num_genes = max_genes mutations_shape.append(num_genes) self.print_debug("mutation_shape: %s" % mutations_shape) # create tensor mutations = B.ones(mutations_shape, dtype=population.dtype) # compute the fancy indexing dynamically slices = [] for size in mutations_shape: slices.append(slice(0, size)) slices = tuple(slices) tslices = slices2array(slices) # injecting mutations x_matrix = B.assign(x_matrix, mutations, tslices) x_matrix = B.full_shuffle(x_matrix) # invert crossover matrix inv_x_matrix = B.abs((x_matrix) - 1) # copy chromosomes that stays the same population = population * inv_x_matrix # add the mutations population += (population_copy * x_matrix) return population
def test_cosine_2d(backends): INSERTION_POINTS = [0, 10, 20] # where we copy the ref chromosome ref_chromosome = B.randint(0, 2, (32, 32)) ref_pop = B.randint(0, 2, (64, 32, 32)) ref_pop = B.as_numpy_array(ref_pop) inserstion = B.as_numpy_array(ref_chromosome) for idx in INSERTION_POINTS: ref_pop[idx] = inserstion ref_pop = B.tensor(ref_pop) cs = InvertedCosineSimilarity(ref_chromosome) distances = cs(ref_pop) print(distances) for idx, dst in enumerate(distances): if idx in INSERTION_POINTS: assert B.assert_near(dst, 1.0, absolute_tolerance=0.001) else: assert dst < 1 assert dst > 0
def randint_population(shape, max_value, min_value=0): """Generate a random population made of Integers Args: (set of ints): shape of the population. Its of the form (num_chromosomes, chromosome_dim_1, .... chromesome_dim_n) max_value (int): Maximum value taken by a given gene. min_value (int, optional): Min value a gene can take. Defaults to 0. Returns: Tensor: random population. """ high = max_value + 1 return B.randint(low=min_value, high=high, shape=shape, dtype=B.intx())
def test_1D_shape(): POPULATION_SHAPE = (64, 16) population = B.randint(0, 1024, POPULATION_SHAPE) population_fraction = 0.5 crossover_size_fraction = 0.2 original_population = copy(population) population = SingleCrossover1D(population_fraction, crossover_size_fraction, debug=1)(population) cprint(population, 'cyan') cprint(original_population, 'yellow') assert population.shape == POPULATION_SHAPE # measuring mutation rate diff = B.clip(abs(population - original_population), 0, 1) cprint('diff', 'cyan') cprint(diff, 'cyan') # row test num_ones_in_row = 0 for col in diff: num_ones = list(col).count(1) num_ones_in_row = max(num_ones, num_ones_in_row) max_one_in_row = POPULATION_SHAPE[1] * crossover_size_fraction assert num_ones_in_row <= max_one_in_row assert num_ones_in_row # col diff = B.transpose(diff) num_ones_in_col = 0 for col in diff: num_ones_in_col = max(list(col).count(1), num_ones_in_col) max_one_in_col = POPULATION_SHAPE[0] * population_fraction assert max_one_in_col - 3 <= num_ones_in_col <= max_one_in_col
def test_mutation2d_graph_mode(): "make sure the boxing / unboxing works in graph mode" pop_shape = (2, 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 = B.randint(0, max_gene_value, pop_shape) 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._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
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
**kwargs) if __name__ == '__main__': from copy import copy from evoflow.utils import op_optimization_benchmark NUM_RUNS = 10 pop_shape = (100, 100, 10) 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 = 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, ) op_optimization_benchmark(population, OP, NUM_RUNS).report() quit() # display pop_shape = (6, 4, 4) max_gene_value = 10 min_gene_value = 0
super(Reverse3D, self).__init__(population_fraction=population_fraction, max_reverse_probability=max_reverse_probability, **kwargs) if __name__ == '__main__': from copy import copy from termcolor import cprint from evoflow.utils import op_optimization_benchmark NUM_RUNS = 3 # 100 # pop_shape = (100, 100, 100) pop_shape = (100, 100, 100) population = B.randint(0, 256, pop_shape) population_fraction = 0.5 max_reverse_probability = (0.5, 0.5) OP = Reverse2D(population_fraction, max_reverse_probability) op_optimization_benchmark(population, OP, NUM_RUNS).report() quit() GENOME_SHAPE = (6, 4) population = B.randint(0, 256, GENOME_SHAPE) population_fraction = 0.5 max_reverse_probability = 0.5 cprint(population, 'green') original_population = copy(population) # ! population will be shuffle if not debug population = Reverse1D(population_fraction,
def test_call_vs_get(): shape = (128, 64) population = B.randint(1, 10, shape=shape) inputs = Input(shape) inputs.assign(population) assert B.tensor_equal(inputs.get(), inputs.call(''))
def test_2d(): shape = (128, 64, 64) population = B.randint(1, 10, shape=shape) inputs = Input(shape) inputs.assign(population) assert B.tensor_equal(inputs.get(), population)