forked from derekjgibbs/CourseCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontecarlo.py
69 lines (57 loc) · 2.44 KB
/
montecarlo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from coursematch_solver import CourseMatchSolver
import pandas as pd
from collections import Counter
import json
class MonteCarloSimulator:
def __init__(self, source_xlsx):
self.source_xlsx = source_xlsx
def run_simulation(self, base_input, num_simulations, callback=None):
"""
Runs Monte Carlo simulation multiple times with different seeds
Args:
base_input (dict): Base input with budget, max_credits, and courses
num_simulations (int): Number of simulations to run
callback (function): Optional callback function for progress updates
Returns:
dict: Course probabilities, schedule probabilities, and raw results
"""
simulation_results = []
schedule_counter = Counter()
for i in range(num_simulations):
# Update seed for this iteration
current_input = base_input.copy()
current_input["seed"] = i + 1
# Run solver
cms = CourseMatchSolver(self.source_xlsx, current_input)
selected = cms.solve()
simulation_results.append(selected)
# Create a frozen set of course IDs for this schedule
schedule = frozenset(course['uniqueid'] for course in selected)
schedule_counter[schedule] += 1
# Update progress if callback provided
if callback:
callback(i + 1, num_simulations)
# Calculate individual course probabilities
course_counts = {}
for result in simulation_results:
for course in result:
uniqueid = course['uniqueid']
course_counts[uniqueid] = course_counts.get(uniqueid, 0) + 1
course_probabilities = {
uniqueid: count/num_simulations
for uniqueid, count in course_counts.items()
}
# Calculate schedule probabilities
schedule_probabilities = [
{
'courses': list(schedule),
'probability': count/num_simulations,
'count': count
}
for schedule, count in schedule_counter.most_common()
]
return {
'course_probabilities': course_probabilities,
'schedule_probabilities': schedule_probabilities,
'raw_results': simulation_results
}