def mutate_string_genome(string, p, char_set_size, **kwargs): if random.random() <= p: pos = random.randint(0, len(string) - 1) char_list = list(string) char_list[pos] = random.choice(char_set(char_set_size)) string = char_list_to_str(char_list) return string
def mutate_bit_genome(bitstring, p, **kwargs): if random.random() <= p: pos = random.randint(0, len(bitstring) - 1) char_list = list(bitstring) char_list[pos] = bit_flip(char_list[pos]) bitstring = char_list_to_str(char_list) return bitstring
def generate_initial_population(population_size, genome_size, char_set_size, **kwargs): S = char_set(char_set_size) return tuple( char_list_to_str(random.choice(S) for _ in range(genome_size)) for _ in range(population_size))
def generate_initial_population(population_size, genome_size, char_set_size, **kwargs): S = char_set(char_set_size) return tuple( char_list_to_str(random.choice(S) for _ in range(genome_size)) for _ in range(population_size) )
def mutate_string_components(string, p, char_set_size, **kwargs): return char_list_to_str(random.choice(char_set(char_set_size)) if random.random() <= p else c for c in string)
def mutate_bit_components(bitstring, p, **kwargs): return char_list_to_str(bit_flip(b) if random.random() <= p else b for b in bitstring)
def mix(a, b): """ for each component, randomly pick a value from a random parent """ return char_list_to_str(a[i] if random_bool() else b[i] for i in range(len(a)))
def mutate_string_components(string, p, char_set_size, **kwargs): return char_list_to_str( random.choice(char_set(char_set_size)) if random.random() <= p else c for c in string)
def mutate_bit_components(bitstring, p, **kwargs): return char_list_to_str( bit_flip(b) if random.random() <= p else b for b in bitstring)