From d399fc4f11c49605c8daac5f844c2171f2c8e05c Mon Sep 17 00:00:00 2001 From: Pawan Garia Date: Thu, 21 Nov 2024 16:13:05 +0530 Subject: [PATCH 1/3] added crossover file and updated mutation file --- Samples/Mutation_Files/crossover.py | 79 ++++++++++++ Samples/Mutation_Files/mutation.py | 186 ++++++++++++++-------------- 2 files changed, 173 insertions(+), 92 deletions(-) diff --git a/Samples/Mutation_Files/crossover.py b/Samples/Mutation_Files/crossover.py index e69de29..967f3ac 100644 --- a/Samples/Mutation_Files/crossover.py +++ b/Samples/Mutation_Files/crossover.py @@ -0,0 +1,79 @@ +class ScheduleCrossover: + def __init__(self): + # schedules for week1 and week2 + self.week1 = { + "Monday": { + "A": [{"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "9:55 - 10:50"}], + "B": [{"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}] + }, + "Tuesday": { + "A": [{"teacher_id": "AK24", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "9:00 - 9:55"}], + "B": [{"teacher_id": "BJ11", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "9:55 - 10:50"}] + }, + "Wednesday": { + "A": [{"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}] + }, + "Thursday": { + "A": [{"teacher_id": "SJ11", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}] + }, + "Friday": { + "A": [{"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}] + }, + "Saturday": { + "A": [{"teacher_id": "SJ16", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}] + } + } + + self.week2 = { + "Monday": { + "A": [{"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "9:55 - 10:50"}], + "B": [{"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}] + }, + "Tuesday": { + "A": [{"teacher_id": "AK22", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "9:00 - 9:55"}], + "B": [{"teacher_id": "BJ16", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "9:55 - 10:50"}] + }, + "Wednesday": { + "A": [{"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}] + }, + "Thursday": { + "A": [{"teacher_id": "SJ12", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}] + }, + "Friday": { + "A": [{"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}] + }, + "Saturday": { + "A": [{"teacher_id": "SJ14", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}] + } + } + + self.new_offspring = None # Will hold the result after crossover + + def perform_crossover(self): + # Days for crossover + crossover_days = ["Tuesday", "Friday", "Saturday"] + + # Perform the crossover between week1 and week2 + for day in crossover_days: + self.week1[day], self.week2[day] = self.week2[day], self.week1[day] + + # Create a new chromosome structure with the updated schedules + self.new_offspring = { + "week1": self.week1, + "week2": self.week2 + + } + + def print_schedule(self): + # Print the new chromosome after crossover + for week, schedule in self.new_offspring.items(): + print(f"{week}:") + for day, details in schedule.items(): + print(f" {day}: {details}") + print() # Empty line between weeks + + +# Instantiate the class and run the crossover +schedule_crossover = ScheduleCrossover() +schedule_crossover.perform_crossover() +schedule_crossover.print_schedule() diff --git a/Samples/Mutation_Files/mutation.py b/Samples/Mutation_Files/mutation.py index 80437cb..55badae 100644 --- a/Samples/Mutation_Files/mutation.py +++ b/Samples/Mutation_Files/mutation.py @@ -1,112 +1,114 @@ import random -def mutate_time_slots_in_section(schedule, section): - """ - Mutates the time slots within a particular section by shuffling them. - - Args: - schedule (dict): The schedule containing sections and their data. - section (str): The section in which mutation should occur. - - Returns: - bool: True if mutation was performed, False otherwise. - """ - # Check if the section exists in the schedule - if section in schedule: - section_list = schedule[section] - - # Ensure there are enough time slots to shuffle - if len(section_list) < 2: - print(f"Not enough time slots to mutate in section '{section}'.") +class Mutation: + def mutate_time_slots_in_section(self, schedule, section): + """ + Mutates the time slots within a particular section by shuffling them. + + Args: + schedule (dict): The schedule containing sections and their data. + section (str): The section in which mutation should occur. + + Returns: + bool: True if mutation was performed, False otherwise. + """ + # Check if the section exists in the schedule + if section in schedule: + section_list = schedule[section] + + # Ensure there are enough time slots to shuffle + if len(section_list) < 2: + print(f"Not enough time slots to mutate in section '{section}'.") + return False + + # Extract and shuffle the time slots + time_slots = [entry["time_slot"] for entry in section_list] + random.shuffle(time_slots) + + # Assign the shuffled time slots back to the section + for i, entry in enumerate(section_list): + entry["time_slot"] = time_slots[i] + + return True + else: return False - # Extract and shuffle the time slots - time_slots = [entry["time_slot"] for entry in section_list] - random.shuffle(time_slots) + def mutate_all_sections(self, schedule): + """ + Mutates time slots for all sections in the schedule. - # Assign the shuffled time slots back to the section - for i, entry in enumerate(section_list): - entry["time_slot"] = time_slots[i] + Args: + schedule (dict): The schedule containing sections and their data. - return True - else: - return False + Returns: + list: List of sections where mutations occurred. + """ + mutated_sections = [] + for section in schedule.keys(): + if self.mutate_time_slots_in_section(schedule, section): + mutated_sections.append(section) -def mutate_all_sections(schedule): - """ - Mutates time slots for all sections in the schedule. + return mutated_sections - Args: - schedule (dict): The schedule containing sections and their data. + def print_schedule(self, schedule): + """ + Prints the schedule in an organized format. - Returns: - list: List of sections where mutations occurred. - """ - mutated_sections = [] - - for section in schedule.keys(): - if mutate_time_slots_in_section(schedule, section): - mutated_sections.append(section) - - return mutated_sections - - -def print_schedule(schedule): - """ - Prints the schedule in an organized format. - - Args: - schedule (dict): The schedule to print. - """ - print("\nMutated Schedule:") - print("=" * 50) - for section, entries in schedule.items(): - print(f"Section {section}:") - print("-" * 30) - for entry in entries: - print(f" Teacher: {entry['teacher_id']}, Subject: {entry['subject_id']}, " - f"Classroom: {entry['classroom_id']}, Time Slot: {entry['time_slot']}") - print("-" * 30) - print("=" * 50) + Args: + schedule (dict): The schedule to print. + """ + print("\nMutated Schedule:") + print("=" * 50) + for section, entries in schedule.items(): + print(f"Section {section}:") + print("-" * 30) + for entry in entries: + print(f" Teacher: {entry['teacher_id']}, Subject: {entry['subject_id']}, " + f"Classroom: {entry['classroom_id']}, Time Slot: {entry['time_slot']}") + print("-" * 30) + print("=" * 50) # Example schedule schedule = { "A": [ - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AP24", "subject_id": "SCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "AK23", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}, - {"teacher_id": "SS03", "subject_id": "TCS-502", "classroom_id": "R1", "time_slot": "1:20 - 2:15"}, - {"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}, - {"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "3:30 - 4:25"}, - ], - "B": [ - {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "BJ10", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "PA21", "subject_id": "XCS-501", "classroom_id": "R2", "time_slot": "12:05 - 1:00"}, - ], - "C": [ - {"teacher_id": "RS11", "subject_id": "TMA-502", "classroom_id": "R3", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AA04", "subject_id": "TCS-502", "classroom_id": "R3", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "12:05 - 1:00"}, - {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "DP07", "subject_id": "TCS-503", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}, - {"teacher_id": "SJ16", "subject_id": "TCS-509", "classroom_id": "R3", "time_slot": "2:15 - 3:10"}, - {"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "3:30 - 4:25"}, - ], - "D": [ - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AC05", "subject_id": "TCS-502", "classroom_id": "R4", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "PK02", "subject_id": "TCS-531", "classroom_id": "R4", "time_slot": "12:05 - 1:00"}, - ] + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AP24", "subject_id": "SCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "AK23", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}, + {"teacher_id": "SS03", "subject_id": "TCS-502", "classroom_id": "R1", "time_slot": "1:20 - 2:15"}, + {"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}, + {"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "3:30 - 4:25"}, + ], + "B": [ + {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "BJ10", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "PA21", "subject_id": "XCS-501", "classroom_id": "R2", "time_slot": "12:05 - 1:00"}, + ], + "C": [ + {"teacher_id": "RS11", "subject_id": "TMA-502", "classroom_id": "R3", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AA04", "subject_id": "TCS-502", "classroom_id": "R3", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "12:05 - 1:00"}, + {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "DP07", "subject_id": "TCS-503", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}, + {"teacher_id": "SJ16", "subject_id": "TCS-509", "classroom_id": "R3", "time_slot": "2:15 - 3:10"}, + {"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "3:30 - 4:25"}, + ], + "D": [ + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AC05", "subject_id": "TCS-502", "classroom_id": "R4", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "PK02", "subject_id": "TCS-531", "classroom_id": "R4", "time_slot": "12:05 - 1:00"}, + ] } +# Create an instance of the Mutation class +mutator = Mutation() + # Perform mutations on all sections -mutate_all_sections(schedule) +mutator.mutate_all_sections(schedule) # Print the mutated schedule -print_schedule(schedule) +mutator.print_schedule(schedule) From e8f68d69686515a167c5de3e91744f69dc663c71 Mon Sep 17 00:00:00 2001 From: Pawan Garia Date: Fri, 22 Nov 2024 16:59:58 +0530 Subject: [PATCH 2/3] updated mutation funtion --- Samples/Mutation_Files/mutation.py | 29 +++- Samples/Mutation_Files/mutation2.py | 220 +++++++++++++++++----------- 2 files changed, 161 insertions(+), 88 deletions(-) diff --git a/Samples/Mutation_Files/mutation.py b/Samples/Mutation_Files/mutation.py index 55badae..5292939 100644 --- a/Samples/Mutation_Files/mutation.py +++ b/Samples/Mutation_Files/mutation.py @@ -1,6 +1,15 @@ import random class Mutation: + def __init__(self, mutation_rate=0.5): + """ + Initializes the Mutation class with a specified mutation rate. + + Args: + mutation_rate (float): Fraction (0 to 1) of sections to mutate. + """ + self.mutation_rate = mutation_rate + def mutate_time_slots_in_section(self, schedule, section): """ Mutates the time slots within a particular section by shuffling them. @@ -35,7 +44,7 @@ def mutate_time_slots_in_section(self, schedule, section): def mutate_all_sections(self, schedule): """ - Mutates time slots for all sections in the schedule. + Mutates time slots for a fixed fraction of sections in the schedule. Args: schedule (dict): The schedule containing sections and their data. @@ -43,9 +52,15 @@ def mutate_all_sections(self, schedule): Returns: list: List of sections where mutations occurred. """ + # Calculate the number of sections to mutate + total_sections = list(schedule.keys()) + num_to_mutate = max(1, int(self.mutation_rate * len(total_sections))) + + # Randomly select the sections to mutate + sections_to_mutate = random.sample(total_sections, num_to_mutate) mutated_sections = [] - for section in schedule.keys(): + for section in sections_to_mutate: if self.mutate_time_slots_in_section(schedule, section): mutated_sections.append(section) @@ -104,11 +119,15 @@ def print_schedule(self, schedule): ] } -# Create an instance of the Mutation class -mutator = Mutation() +# Create an instance of the Mutation class with a fixed mutation rate (e.g., 50%) +mutation_rate = 0.5 +mutator = Mutation(mutation_rate=mutation_rate) # Perform mutations on all sections -mutator.mutate_all_sections(schedule) +mutated_sections = mutator.mutate_all_sections(schedule) # Print the mutated schedule mutator.print_schedule(schedule) + +# Output the sections where mutation occurred +print(f"Mutated Sections: {mutated_sections}") diff --git a/Samples/Mutation_Files/mutation2.py b/Samples/Mutation_Files/mutation2.py index 3295a22..f341b0e 100644 --- a/Samples/Mutation_Files/mutation2.py +++ b/Samples/Mutation_Files/mutation2.py @@ -1,97 +1,151 @@ import random +import copy -def mutate_time_slots_in_section(schedule, section): - """ - Mutates the time slots within a particular section by shuffling them. - - Args: - schedule (dict): The schedule containing sections and their data. - section (str): The section in which mutation should occur. - - Returns: - bool: True if mutation was performed, False otherwise. - """ - # Check if the section exists in the schedule - if section in schedule: - section_list = schedule[section] - - # Ensure there are enough time slots to shuffle - if len(section_list) < 2: - print(f"Not enough time slots to mutate in section '{section}'.") - return False - - # Extract and shuffle the time slots - time_slots = [entry["time_slot"] for entry in section_list] - random.shuffle(time_slots) - - # Assign the shuffled time slots back to the section - for i, entry in enumerate(section_list): - entry["time_slot"] = time_slots[i] - - print(f"Mutated section '{section}': Time slots randomized.") - return True - else: - print(f"Section '{section}' not found in the schedule.") - return False +class Mutation: + def __init__(self, mutation_rate=1.0): + """ + Initializes the Mutation class with a specified mutation rate. + + Args: + mutation_rate (float): Fraction (0 to 1) of sections to mutate. + """ + self.mutation_rate = mutation_rate + def mutate_time_slots_in_section(self, schedule, section): + """ + Mutates the time slots within a particular section by shuffling them. -def mutate_all_sections(schedule): - """ - Mutates time slots for all sections in the schedule. + Args: + schedule (dict): The schedule containing sections and their data. + section (str): The section in which mutation should occur. - Args: - schedule (dict): The schedule containing sections and their data. + Returns: + bool: True if mutation was performed, False otherwise. + """ + if section in schedule: + section_list = schedule[section] - Returns: - list: List of sections where mutations occurred. - """ - mutated_sections = [] + if len(section_list) < 2: # Not enough time slots to shuffle + return False - for section in schedule.keys(): - if mutate_time_slots_in_section(schedule, section): - mutated_sections.append(section) + time_slots = [entry["time_slot"] for entry in section_list] + random.shuffle(time_slots) - return mutated_sections + for i, entry in enumerate(section_list): + entry["time_slot"] = time_slots[i] + return True + return False -# Example schedule (reuse the previously defined schedule) -schedule = { + def mutate_schedule_for_week(self, weekly_schedule): + """ + Mutates the time slots for all weekdays in the weekly schedule. + + Args: + weekly_schedule (dict): The weekly schedule containing days, sections, and data. + + Returns: + dict: The mutated weekly schedule. + """ + # Create a deep copy to keep the original intact + mutated_weekly_schedule = copy.deepcopy(weekly_schedule) + + for day, day_schedule in mutated_weekly_schedule.items(): + # Calculate the number of sections to mutate + total_sections = list(day_schedule.keys()) + num_to_mutate = max(1, int(self.mutation_rate * len(total_sections))) + + # Randomly select sections to mutate + sections_to_mutate = random.sample(total_sections, num_to_mutate) + + for section in sections_to_mutate: + self.mutate_time_slots_in_section(day_schedule, section) + + return mutated_weekly_schedule + + def print_weekly_schedule(self, weekly_schedule): + """ + Prints the weekly schedule in an organized format. + + Args: + weekly_schedule (dict): The schedule to print. + """ + print("\nWeekly Schedule:") + print("=" * 50) + for day, day_schedule in weekly_schedule.items(): + print(f"Day: {day}") + print("-" * 30) + for section, entries in day_schedule.items(): + print(f" Section {section}:") + for entry in entries: + print(f" Teacher: {entry['teacher_id']}, Subject: {entry['subject_id']}, " + f"Classroom: {entry['classroom_id']}, Time Slot: {entry['time_slot']}") + print("-" * 30) + print("=" * 50) + + +# Example weekly schedule +weekly_schedule = { + "Monday": { "A": [ - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AP24", "subject_id": "SCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "AK23", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}, - {"teacher_id": "SS03", "subject_id": "TCS-502", "classroom_id": "R1", "time_slot": "1:20 - 2:15"}, - {"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}, - {"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "3:30 - 4:25"}, - ], - "B": [ - {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "BJ10", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "PA21", "subject_id": "XCS-501", "classroom_id": "R2", "time_slot": "12:05 - 1:00"}, - ], - "C": [ - {"teacher_id": "RS11", "subject_id": "TMA-502", "classroom_id": "R3", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AA04", "subject_id": "TCS-502", "classroom_id": "R3", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "12:05 - 1:00"}, - {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "DP07", "subject_id": "TCS-503", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}, - {"teacher_id": "SJ16", "subject_id": "TCS-509", "classroom_id": "R3", "time_slot": "2:15 - 3:10"}, - {"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "3:30 - 4:25"}, - ], - "D": [ - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, - {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, - {"teacher_id": "AC05", "subject_id": "TCS-502", "classroom_id": "R4", "time_slot": "11:10 - 12:05"}, - {"teacher_id": "PK02", "subject_id": "TCS-531", "classroom_id": "R4", "time_slot": "12:05 - 1:00"}, - ], + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AP24", "subject_id": "SCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "AK23", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}, + {"teacher_id": "SS03", "subject_id": "TCS-502", "classroom_id": "R1", "time_slot": "1:20 - 2:15"}, + {"teacher_id": "SP06", "subject_id": "TCS-503", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}, + {"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "3:30 - 4:25"}, + ], + "B": [ + {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "PM14", "subject_id": "PMA-502", "classroom_id": "L2", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "BJ10", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "PA21", "subject_id": "XCS-501", "classroom_id": "R2", "time_slot": "12:05 - 1:00"}, + ], + "C": [ + {"teacher_id": "RS11", "subject_id": "TMA-502", "classroom_id": "R3", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AA04", "subject_id": "TCS-502", "classroom_id": "R3", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "12:05 - 1:00"}, + {"teacher_id": "RD09", "subject_id": "PCS-506", "classroom_id": "L3", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "DP07", "subject_id": "TCS-503", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}, + {"teacher_id": "SJ16", "subject_id": "TCS-509", "classroom_id": "R3", "time_slot": "2:15 - 3:10"}, + {"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "3:30 - 4:25"}, + ], + "D": [ + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, + {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, + {"teacher_id": "AC05", "subject_id": "TCS-502", "classroom_id": "R4", "time_slot": "11:10 - 12:05"}, + {"teacher_id": "PK02", "subject_id": "TCS-531", "classroom_id": "R4", "time_slot": "12:05 - 1:00"}, + ] + }, + "Tuesday": { + "A": [{"teacher_id": "AK22", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "9:00 - 9:55"}], + "B": [{"teacher_id": "BJ16", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "9:55 - 10:50"}] + }, + "Wednesday": { + "A": [{"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}] + }, + "Thursday": { + "A": [{"teacher_id": "SJ12", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}] + }, + "Friday": { + "A": [{"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}] + }, + "Saturday": { + "A": [{"teacher_id": "SJ14", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}] + } } -# Perform mutations on all sections -mutated_sections = mutate_all_sections(schedule) +# Create an instance of the Mutation class with a mutation rate of 100% +mutation_rate = 1.0 +mutator = Mutation(mutation_rate=mutation_rate) + +# Perform mutations for the entire week +mutated_weekly_schedule = mutator.mutate_schedule_for_week(weekly_schedule) + +# Print the original and mutated schedules +print("Original Weekly Schedule:") +mutator.print_weekly_schedule(weekly_schedule) -# Print all mutated sections -print("\nAll mutated sections:") -for section in mutated_sections: - print(f"Section '{section}':", schedule[section]) +print("\nMutated Weekly Schedule:") +mutator.print_weekly_schedule(mutated_weekly_schedule) From 10249cea6fb271b99f2b56b3bdf4357e364104a2 Mon Sep 17 00:00:00 2001 From: Pawan Garia Date: Fri, 22 Nov 2024 19:03:03 +0530 Subject: [PATCH 3/3] mutation rate is random chosen --- Samples/Mutation_Files/mutation.py | 104 +++++++++++++++++------------ 1 file changed, 61 insertions(+), 43 deletions(-) diff --git a/Samples/Mutation_Files/mutation.py b/Samples/Mutation_Files/mutation.py index 5292939..66a8e4b 100644 --- a/Samples/Mutation_Files/mutation.py +++ b/Samples/Mutation_Files/mutation.py @@ -1,7 +1,8 @@ import random +import copy class Mutation: - def __init__(self, mutation_rate=0.5): + def __init__(self, mutation_rate= random.random()): """ Initializes the Mutation class with a specified mutation rate. @@ -21,73 +22,72 @@ def mutate_time_slots_in_section(self, schedule, section): Returns: bool: True if mutation was performed, False otherwise. """ - # Check if the section exists in the schedule if section in schedule: section_list = schedule[section] - # Ensure there are enough time slots to shuffle - if len(section_list) < 2: - print(f"Not enough time slots to mutate in section '{section}'.") + if len(section_list) < 2: # Not enough time slots to shuffle return False - # Extract and shuffle the time slots time_slots = [entry["time_slot"] for entry in section_list] random.shuffle(time_slots) - # Assign the shuffled time slots back to the section for i, entry in enumerate(section_list): entry["time_slot"] = time_slots[i] return True - else: - return False + return False - def mutate_all_sections(self, schedule): + def mutate_schedule_for_week(self, weekly_schedule): """ - Mutates time slots for a fixed fraction of sections in the schedule. + Mutates the time slots for all weekdays in the weekly schedule. Args: - schedule (dict): The schedule containing sections and their data. + weekly_schedule (dict): The weekly schedule containing days, sections, and data. Returns: - list: List of sections where mutations occurred. + dict: The mutated weekly schedule. """ - # Calculate the number of sections to mutate - total_sections = list(schedule.keys()) - num_to_mutate = max(1, int(self.mutation_rate * len(total_sections))) + # Create a deep copy to keep the original intact + mutated_weekly_schedule = copy.deepcopy(weekly_schedule) + + for day, day_schedule in mutated_weekly_schedule.items(): + # Calculate the number of sections to mutate + total_sections = list(day_schedule.keys()) + num_to_mutate = max(1, int(self.mutation_rate * len(total_sections))) - # Randomly select the sections to mutate - sections_to_mutate = random.sample(total_sections, num_to_mutate) - mutated_sections = [] + # Randomly select sections to mutate + sections_to_mutate = random.sample(total_sections, num_to_mutate) - for section in sections_to_mutate: - if self.mutate_time_slots_in_section(schedule, section): - mutated_sections.append(section) + for section in sections_to_mutate: + self.mutate_time_slots_in_section(day_schedule, section) - return mutated_sections + return mutated_weekly_schedule - def print_schedule(self, schedule): + def print_weekly_schedule(self, weekly_schedule): """ - Prints the schedule in an organized format. + Prints the weekly schedule in an organized format. Args: - schedule (dict): The schedule to print. + weekly_schedule (dict): The schedule to print. """ - print("\nMutated Schedule:") + print("\nWeekly Schedule:") print("=" * 50) - for section, entries in schedule.items(): - print(f"Section {section}:") + for day, day_schedule in weekly_schedule.items(): + print(f"Day: {day}") print("-" * 30) - for entry in entries: - print(f" Teacher: {entry['teacher_id']}, Subject: {entry['subject_id']}, " - f"Classroom: {entry['classroom_id']}, Time Slot: {entry['time_slot']}") + for section, entries in day_schedule.items(): + print(f" Section {section}:") + for entry in entries: + print(f" Teacher: {entry['teacher_id']}, Subject: {entry['subject_id']}, " + f"Classroom: {entry['classroom_id']}, Time Slot: {entry['time_slot']}") print("-" * 30) print("=" * 50) -# Example schedule -schedule = { - "A": [ +# Example weekly schedule +weekly_schedule = { + "Monday": { + "A": [ {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:55 - 10:50"}, {"teacher_id": "AD08", "subject_id": "PCS-506", "classroom_id": "L5", "time_slot": "9:00 - 9:55"}, {"teacher_id": "AP24", "subject_id": "SCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}, @@ -117,17 +117,35 @@ def print_schedule(self, schedule): {"teacher_id": "AC05", "subject_id": "TCS-502", "classroom_id": "R4", "time_slot": "11:10 - 12:05"}, {"teacher_id": "PK02", "subject_id": "TCS-531", "classroom_id": "R4", "time_slot": "12:05 - 1:00"}, ] + }, + "Tuesday": { + "A": [{"teacher_id": "AK22", "subject_id": "CSP-501", "classroom_id": "R1", "time_slot": "9:00 - 9:55"}], + "B": [{"teacher_id": "BJ16", "subject_id": "TMA-502", "classroom_id": "R2", "time_slot": "9:55 - 10:50"}] + }, + "Wednesday": { + "A": [{"teacher_id": "DT20", "subject_id": "XCS-501", "classroom_id": "R1", "time_slot": "11:10 - 12:05"}] + }, + "Thursday": { + "A": [{"teacher_id": "SJ12", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "12:05 - 1:00"}] + }, + "Friday": { + "A": [{"teacher_id": "AB01", "subject_id": "TCS-531", "classroom_id": "R3", "time_slot": "1:20 - 2:15"}] + }, + "Saturday": { + "A": [{"teacher_id": "SJ14", "subject_id": "TCS-509", "classroom_id": "R1", "time_slot": "2:15 - 3:10"}] + } } -# Create an instance of the Mutation class with a fixed mutation rate (e.g., 50%) -mutation_rate = 0.5 +# Create an instance of the Mutation class with a mutation rate randomly chosen +mutation_rate = random.random() mutator = Mutation(mutation_rate=mutation_rate) -# Perform mutations on all sections -mutated_sections = mutator.mutate_all_sections(schedule) +# Perform mutations for the entire week +mutated_weekly_schedule = mutator.mutate_schedule_for_week(weekly_schedule) -# Print the mutated schedule -mutator.print_schedule(schedule) +# Print the original and mutated schedules +print("Original Weekly Schedule:") +mutator.print_weekly_schedule(weekly_schedule) -# Output the sections where mutation occurred -print(f"Mutated Sections: {mutated_sections}") +print("\nMutated Weekly Schedule:") +mutator.print_weekly_schedule(mutated_weekly_schedule)