Untitled
unknown
plain_text
8 months ago
4.2 kB
4
Indexable
Never
import random import string MyString = "Ria_Sharma*58828" #Ideal String population = 500 # population size pc = 0.5 # crossover probability pm = 0.9 # mutation probability length = len(MyString) generations = 500 # no.of generations #generates random strings def generate_random_string(): parts = MyString.split('_') first_name = parts[0] last_name, student_id = parts[1].split('*') first_name_length = len(first_name) last_name_length = len(last_name) student_id_length = len(student_id) generated_first_name = ''.join(random.choice(string.ascii_letters) for _ in range(first_name_length)) generated_last_name = ''.join(random.choice(string.ascii_letters) for _ in range(last_name_length)) generated_student_id = ''.join(random.choice(string.digits) for _ in range(student_id_length)) generated_string = f"{generated_first_name}_{generated_last_name}*{generated_student_id}" return generated_string #Randomly chooses a crossover point and implements crossover def crossover(parent1, parent2): crossover_point = random.randint(0, length - 1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] return child1, child2 # mutates the two childeren randomly def mutation(child1, child2): mutated_child1 = list(child1) mutated_child2 = list(child2) for i in range(length): if random.random() < pm: mutated_child1[i] = random.choice(string.ascii_letters + string.digits) if random.random() < pm: mutated_child2[i] = random.choice(string.ascii_letters + string.digits) mutated_child1 = ''.join(mutated_child1) mutated_child2 = ''.join(mutated_child2) return mutated_child1, mutated_child2 #Calculates fitness def fitness(input_string, target_string): fit_score = sum(1 for i in range(length) if input_string[i] == target_string[i]) fitness = fit_score / length return fitness # Generates the initial population individuals = [generate_random_string() for _ in range(population)] # Evolution loop over generations for generation in range(generations): # Calculate fitness scores for individuals in the population and stores them in a list fitness_scores = [fitness(individual, MyString) for individual in individuals] # Check if the termination condition for i, individual in enumerate(individuals): if fitness_scores[i] >= 0.98: print(f"Perfect match found in generation {generation + 1}!") print(f"Individual: {individual}, Fitness: {fitness_scores[i]}") exit() # Sort individuals based on fitness scores sorted_individuals = [individual for _, individual in sorted(zip(fitness_scores, individuals), reverse=True)] # Keep the top population fittest individuals individuals = sorted_individuals[:population] # Perform crossover and mutation new_individuals = [] for _ in range(population): parent1 = random.choice(individuals) parent2 = random.choice(individuals) if random.random() < pc: child1, child2 = crossover(parent1, parent2) else: child1, child2 = parent1, parent2 if random.random() < pm: child1, child2 = mutation(child1, child2) new_individuals.append(child1) new_individuals.append(child2) # Append mutated children to the population individuals.extend(new_individuals) # Keep only the top population fittest individuals after appending mutated children and set them to the new population fitness_scores = [fitness(individual, MyString) for individual in individuals] sorted_individuals = [individual for _, individual in sorted(zip(fitness_scores, individuals), reverse=True)] individuals = sorted_individuals[:population] best_individual = sorted_individuals[0] best_fitness = fitness_scores[0] print(f"Generation: {generation + 1}, Best Individual: {best_individual}, Fitness: {best_fitness}")
Leave a Comment