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)