more sim
This commit is contained in:
parent
6807ddd96c
commit
562a8b3e26
123
simulate.py
123
simulate.py
|
|
@ -15,20 +15,38 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import simpy
|
import simpy, sys
|
||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize Pygame
|
||||||
|
pygame.init()
|
||||||
|
screen = pygame.display.set_mode((1100, 750))
|
||||||
|
pygame.display.set_caption('School Simulation')
|
||||||
|
|
||||||
# Define the simulation environment
|
# Define the simulation environment
|
||||||
env = simpy.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)
|
# Define the resources (rooms and teachers)
|
||||||
rooms = [simpy.Resource(env, capacity=20) for i in range(10)]
|
rooms = [simpy.Resource(env, capacity=20) for i in range(num_rooms)]
|
||||||
teachers = [simpy.Resource(env, capacity=1) for i in range(30)]
|
teachers = [simpy.Resource(env, capacity=1) for i in range(num_teachers)]
|
||||||
|
|
||||||
# Define the courses and majors
|
# Define the courses and majors
|
||||||
majors = ['major1', 'major2', 'major3', 'major4', 'major5']
|
majors = ['major1', 'major2', 'major3', 'major4', 'major5']
|
||||||
courses = {'course' + str(i): {'credits': 3} for i in range(50)}
|
courses = {'course' + str(i): {'credits': 3} for i in range(num_courses)}
|
||||||
|
current_semester = ''
|
||||||
|
students = []
|
||||||
|
|
||||||
|
|
||||||
# Define the student process
|
# Define the student process
|
||||||
class Student:
|
class Student:
|
||||||
|
|
@ -37,51 +55,122 @@ class Student:
|
||||||
self.id = id
|
self.id = id
|
||||||
self.major = major
|
self.major = major
|
||||||
self.completed_courses = []
|
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):
|
def choose_courses(self):
|
||||||
# Implement a custom function to determine which courses the student chooses each semester
|
# Implement a custom function to determine which courses the student chooses each semester
|
||||||
pass
|
# 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):
|
def attend_courses(self):
|
||||||
# Choose the courses to attend
|
# Choose the courses to attend
|
||||||
chosen_courses = self.choose_courses()
|
chosen_courses = self.choose_courses()
|
||||||
# Request a room and teacher for each course
|
# Request a room and teacher for each course
|
||||||
for course in chosen_courses:
|
for course in chosen_courses:
|
||||||
with rooms[random.randint(0, 9)].request() as room_request:
|
with rooms[random.randint(0, num_rooms-1)].request() as room_request:
|
||||||
with teachers[random.randint(0, 29)].request() as teacher_request:
|
with teachers[random.randint(0, num_teachers-1)].request() as teacher_request:
|
||||||
# Simulate attending the course
|
# Simulate attending the course
|
||||||
yield self.env.timeout(1)
|
yield self.env.timeout(1)
|
||||||
# Update the completed courses list
|
# Update the completed courses list
|
||||||
self.completed_courses.append(course)
|
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
|
# Define the main simulation process
|
||||||
def run_simulation(env, num_students):
|
def run_simulation(env, num_students):
|
||||||
|
global current_semester, students
|
||||||
# Create the students
|
# Create the students
|
||||||
students = [Student(env, i, random.choice(majors)) for i in range(num_students)]
|
students = [Student(env, i, random.choice(majors)) for i in range(num_students)]
|
||||||
# Simulate for 4 years (8 semesters)
|
# Simulate for 4 years (8 semesters)
|
||||||
for semester in range(8):
|
for semester in range(num_semesters):
|
||||||
|
current_semester = semester
|
||||||
for student in students:
|
for student in students:
|
||||||
env.process(student.attend_courses())
|
env.process(student.attend_courses())
|
||||||
yield env.timeout(16) # each semester is 16 weeks long
|
#student.overview()
|
||||||
|
|
||||||
# Initialize Pygame
|
# Trigger a redraw of the game screen at the end of each semester
|
||||||
pygame.init()
|
# Create a custom event and post it to the pygame event queue
|
||||||
screen = pygame.display.set_mode((800, 600))
|
custom_event = pygame.event.Event(SEMESTER_END, {'some_key': 'a', 'other_key': 'b'})
|
||||||
pygame.display.set_caption("School Simulation")
|
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
|
# Start the simulation
|
||||||
env.process(run_simulation(env, 1000))
|
env.process(run_simulation(env, num_students))
|
||||||
|
env.run()
|
||||||
|
|
||||||
|
|
||||||
# Define a Pygame loop to visualize the simulation
|
# Define a Pygame loop to visualize the simulation
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
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:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
screen.fill((255, 255, 255))
|
#screen.fill((255, 255, 255))
|
||||||
# Draw the simulation elements (e.g., students, rooms, teachers, courses)
|
# Draw the simulation elements (e.g., students, rooms, teachers, courses)
|
||||||
# ...
|
# ...
|
||||||
pygame.display.update()
|
#draw_game()
|
||||||
|
#pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue