# This code defines the simulation environment, the resources (rooms and teachers), the courses and majors, and the student # process. The choose_courses() method of the Student class is left as a placeholder for you to implement your custom # function that determines which courses the student chooses each semester. The attend_courses() method of the Student # class requests a room and a teacher for each chosen course and simulates attending the course. The main simulation # process run_simulation() creates the students and simulates for 8 semesters, each lasting 16 weeks. # We initialize Pygame and create a display window to visualize the simulation. In the Pygame loop, we update # the display with the simulation elements. You can use Pygame's drawing functions (e.g., pygame.draw.circle(), # pygame.draw.rect()) to draw the simulation elements on the screen. # # To make the visualization more interactive, you could also add user input (e.g., mouse clicks, keyboard input) to # modify the simulation parameters in real time. For example, you could add a button that toggles the display of # certain simulation elements or changes the number of students in the simulation. import simpy, sys import pygame import random # Initialize Pygame pygame.init() screen = pygame.display.set_mode((1100, 750)) pygame.display.set_caption('School Simulation') # Define the simulation environment env = simpy.Environment() num_students=50 num_teachers=3 num_rooms=4 num_courses=60 num_semesters = 20 # Define a custom event type SEMESTER_END = pygame.USEREVENT + 1 # Define the resources (rooms and teachers) rooms = [simpy.Resource(env, capacity=20) for i in range(num_rooms)] teachers = [simpy.Resource(env, capacity=1) for i in range(num_teachers)] # Define the courses and majors majors = ['major1', 'major2', 'major3', 'major4', 'major5'] courses = {'course' + str(i): {'credits': 3} for i in range(num_courses)} current_semester = '' students = [] # Define the student process class Student: def __init__(self, env, id, major): self.env = env self.id = id self.major = major self.completed_courses = [] def overview(self): print("Student", self.id, "is majoring in", self.major) print("Completed courses:", self.completed_courses, "\n") def choose_courses(self): # Implement a custom function to determine which courses the student chooses each semester # Choose a random number of courses (between 3 and 5) num_courses = random.randint(3, 5) # Randomly select the courses to take chosen_courses = random.sample(list(courses.keys()), num_courses) return chosen_courses def attend_courses(self): # Choose the courses to attend chosen_courses = self.choose_courses() # Request a room and teacher for each course for course in chosen_courses: with rooms[random.randint(0, num_rooms-1)].request() as room_request: with teachers[random.randint(0, num_teachers-1)].request() as teacher_request: # Simulate attending the course yield self.env.timeout(1) # Update the completed courses list self.completed_courses.append(course) # Define the game drawing function def draw_game(): # Clear the screen screen.fill((255, 255, 255)) # Draw the current semester number font = pygame.font.Font(None, 36) font2 = pygame.font.Font(None, 12) text = font.render('Semester: ' + str(current_semester+1), True, (0, 0, 0)) screen.blit(text, (10, 10)) # Draw the number of completed courses for each student box_width = 10 box_height = 10 y = 50 for student in students: text = font2.render('Student ' + str(student.id) + ': ' + str(len(student.completed_courses)) + ' courses', True, (0, 0, 0)) screen.blit(text, (10, y)) # Draw the boxes for each course x = 150 for course in courses: if course in student.completed_courses: box_color = (128, 128, 128) # Grey for completed courses else: box_color = (200, 200, 200) # White for incomplete courses pygame.draw.rect(screen, box_color, (x, y, box_width, box_height)) x += box_width + 2 # Add some spacing between boxes y += box_height + 2 # Add some spacing between rows # Update the screen pygame.display.update() # Define the main simulation process def run_simulation(env, num_students): global current_semester, students # Create the students students = [Student(env, i, random.choice(majors)) for i in range(num_students)] # Simulate for 4 years (8 semesters) for semester in range(num_semesters): current_semester = semester for student in students: env.process(student.attend_courses()) #student.overview() # Trigger a redraw of the game screen at the end of each semester # Create a custom event and post it to the pygame event queue custom_event = pygame.event.Event(SEMESTER_END, {'some_key': 'a', 'other_key': 'b'}) pygame.event.post(custom_event) yield env.timeout(16) # each semester is 16 weeks long print("Semester " + str(semester+1) + " is over") # could update game state in separate fxn # Draw the updated game state draw_game() pygame.display.update() # Delay between semesters pygame.time.wait(100) # Start the simulation env.process(run_simulation(env, num_students)) env.run() # Define a Pygame loop to visualize the simulation while True: for event in pygame.event.get(): if event.type == SEMESTER_END: # Extract data from the event some_value = event.some_key other_value = event.other_key # Update game state with the event data #update_game_state(some_value, other_value) draw_game() if event.type == pygame.QUIT: pygame.quit() sys.exit() #screen.fill((255, 255, 255)) # Draw the simulation elements (e.g., students, rooms, teachers, courses) # ... #draw_game() #pygame.display.update() """ import simpy import random # Define the simulation environment env = simpy.Environment() # Define the resources (rooms and teachers) rooms = [simpy.Resource(env, capacity=20) for i in range(10)] teachers = [simpy.Resource(env, capacity=1) for i in range(30)] # Define the courses and majors majors = ['major1', 'major2', 'major3', 'major4', 'major5'] courses = {'course' + str(i): {'credits': 3} for i in range(50)} # Define the student process class Student: def __init__(self, env, id, major): self.env = env self.id = id self.major = major self.completed_courses = [] def choose_courses(self): # Implement a custom function to determine which courses the student chooses each semester pass def attend_courses(self): # Choose the courses to attend chosen_courses = self.choose_courses() # Request a room and teacher for each course for course in chosen_courses: with rooms[random.randint(0, 9)].request() as room_request: with teachers[random.randint(0, 29)].request() as teacher_request: # Simulate attending the course yield self.env.timeout(1) # Update the completed courses list self.completed_courses.append(course) # Define the main simulation process def run_simulation(env, num_students): # Create the students students = [Student(env, i, random.choice(majors)) for i in range(num_students)] # Simulate for 4 years (8 semesters) for semester in range(8): for student in students: env.process(student.attend_courses()) yield env.timeout(16) # each semester is 16 weeks long # Start the simulation env.process(run_simulation(env, 1000)) env.run() """