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_non_tensor_input(): shape = (2, 4) population = [[1, 2, 3, 4], [1, 2, 3, 4]] inputs = Input(shape) inputs.assign(population) res = inputs.get() assert B.is_tensor(res)
def on_generation_end(self, generation, metrics, fitness_scores, populations): assert isinstance(generation, int) assert isinstance(metrics, dict) assert isinstance(fitness_scores, dict) assert isinstance(populations, list) for pop in populations: assert B.is_tensor(pop)
def test_ND(): "test various tensor size random" TEST_INPUTS = [ [RandomMutations1D, (2, 4), 0.5], [RandomMutations2D, (2, 4, 4), (0.5, 0.5)], [RandomMutations3D, (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] max_gene_value = 10 min_gene_value = 0 population_fraction = 1 min_mutation_value = 1 max_mutation_value = 1 population = randint_population(pop_shape, max_gene_value) # eager RM = OP(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) population = RM(population) assert B.is_tensor(population) assert population.shape == pop_shape # graph RM = OP(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) population = RM._call_from_graph(population) assert B.is_tensor(population) assert population.shape == pop_shape
def test_shuffle(): SHAPES = [(2, 4), (2, 4, 4), (2, 4, 4, 4)] for shape in SHAPES: population = randint_population(shape, max_value=255) previous_population = B.copy(population) population = Shuffle(1, debug=True)(population) diff = B.clip(abs(population - previous_population), 0, 1) assert B.is_tensor(population) assert population.shape == shape sum_diff = B.sum(diff) shape_size = int(B.prod(shape) / 2) assert sum_diff >= shape_size
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 __call__(self, ops): # boxing inputs if needed ops = box(ops) # graph computation or eager? if self.debug: input_types = [type(op) for op in ops] self.print_debug('inputs type:%s' % (input_types)) if issubclass(type(ops[0]), OP): # graph mode self.print_debug('graph mode') input_shapes = [] for op in ops: assert issubclass(type(op), OP) self.input_ops.append(op) input_shapes.append(op.get_output_shapes()) self.compute_output_shape(input_shapes) return self else: # eager mode self.print_debug('eager mode') # check inputs are valis for op in ops: if not B.is_tensor(op): raise ValueError("Expecting list(tensors) or a tensor") # input shapes input_shapes = [] for op in ops: input_shapes.append(op.shape) self.compute_output_shape(input_shapes) # compute concrete results - dispatch iterate through populations. return unbox(self.dispatch(ops, self.EAGER))
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 on_evolution_end(self, populations): assert isinstance(populations, list) for pop in populations: assert B.is_tensor(pop)