class MutationService: def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier(self.__algorithm_configuration.chromosome_config) def handle_mut(self, pop_to_mut): return [self.__apply_mut(chromosome) for chromosome in pop_to_mut] def __apply_mut(self, chromosome): mut_type = self.__algorithm_configuration.chromosome_config.mut_type if mut_type == MutationTypes.ONE_POINT.name: return self.__chromosome_modifier.boundary_mutation_one_point(chromosome) if mut_type == MutationTypes.TWO_POINTS.name: return self.__chromosome_modifier.boundary_mutation_two_points(chromosome)
class InversionService: def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier(self.__algorithm_configuration.chromosome_config) def handle_inv(self, pop_to_inv): return [self.__chromosome_modifier.inversion(chromosome) for chromosome in pop_to_inv]
class MutationService: def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier( algorithm_configuration.chromosome_config, algorithm_configuration.left_range_number, algorithm_configuration.right_range_number) def handle_mut(self, pop_to_mut): return [self.__apply_mut(chromosome) for chromosome in pop_to_mut] def __apply_mut(self, chromosome): mut_type = self.__algorithm_configuration.chromosome_config.mut_type if mut_type == MutationTypes.INDICES_SWAP.name: return self.__chromosome_modifier.mutation_indices_swap(chromosome) if mut_type == MutationTypes.STEADY.name: return self.__chromosome_modifier.mutation_steady(chromosome)
class CrossService: def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier(self.__algorithm_configuration.chromosome_config) def handle_cross(self, pop_to_cross): pop_to_cross_len = len(pop_to_cross) number_of_missing_chromosomes = self.__algorithm_configuration.population_number - pop_to_cross_len - \ self.__algorithm_configuration.elite_amount missing_chromosomes = [] while len(missing_chromosomes) < number_of_missing_chromosomes: first_chromosome = pop_to_cross[randrange(pop_to_cross_len)] second_chromosome = pop_to_cross[randrange(pop_to_cross_len)] first_chromosome, second_chromosome = self.__apply_cross(first_chromosome, second_chromosome) missing_chromosomes.append(first_chromosome) missing_chromosomes.append(second_chromosome) if len(missing_chromosomes) is 0: return pop_to_cross return np.concatenate((pop_to_cross, missing_chromosomes[0:number_of_missing_chromosomes]), axis=0) def __apply_cross(self, first_chromosome, second_chromosome): cross_type = self.__algorithm_configuration.chromosome_config.cross_type if cross_type == CrossTypes.ONE_POINT.name: return self.__chromosome_modifier.cross_one_point(first_chromosome, second_chromosome) if cross_type == CrossTypes.TWO_POINTS.name: return self.__chromosome_modifier.cross_two_point(first_chromosome, second_chromosome) if cross_type == CrossTypes.THREE_POINTS.name: return self.__chromosome_modifier.cross_three_point(first_chromosome, second_chromosome) if cross_type == CrossTypes.H**O.name: return self.__chromosome_modifier.cross_homogeneous(first_chromosome, second_chromosome)
def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier(self.__algorithm_configuration.chromosome_config)
def __init__(self, algorithm_configuration): self.__algorithm_configuration = algorithm_configuration self.__chromosome_modifier = ChromosomeModifier( algorithm_configuration.chromosome_config, algorithm_configuration.left_range_number, algorithm_configuration.right_range_number)