added ws202425 courses
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
Times:
|
||||
|
||||
10 simulations: 0m0.021s
|
||||
100 simulations: 0m0.027s
|
||||
1000 simulations: 0m0.040s
|
||||
10000 simulations: 0m0.084s
|
||||
100000 simulations: 0m0.728s
|
||||
1000000 simulations: 0m6.556s
|
||||
|
||||
Questions:
|
||||
|
||||
Which predictions, if any, proved incorrect as you increased the number of simulations?:
|
||||
|
||||
With a small number of simulations.
|
||||
|
||||
Suppose you're charged a fee for each second of compute time your program uses.
|
||||
After how many simulations would you call the predictions "good enough"?: TODO
|
||||
|
||||
Predictions stabilized over 10000 simulations.
|
||||
@@ -0,0 +1,69 @@
|
||||
# Simulate a sports tournament
|
||||
|
||||
import csv
|
||||
import sys
|
||||
import random
|
||||
|
||||
# Number of simluations to run
|
||||
N = 1000
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
# Ensure correct usage
|
||||
if len(sys.argv) != 2:
|
||||
sys.exit("Usage: python tournament.py FILENAME")
|
||||
|
||||
teams = []
|
||||
counts = {}
|
||||
|
||||
# TODO: Read teams into memory from file
|
||||
with open(sys.argv[1], 'r') as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
for row in reader:
|
||||
teams.append({'team': row['team'], 'rating': int(row['rating'])})
|
||||
counts[row['team']] = 0
|
||||
|
||||
# TODO: Simulate N tournaments and keep track of win counts
|
||||
for _ in range(N):
|
||||
winner = simulate_tournament(teams.copy())
|
||||
counts[winner] += 1
|
||||
|
||||
# Print each team's chances of winning, according to simulation
|
||||
for team in sorted(counts, key=lambda team: counts[team], reverse=True):
|
||||
print(f"{team}: {counts[team] * 100 / N:.1f}% chance of winning")
|
||||
|
||||
|
||||
def simulate_game(team1, team2):
|
||||
"""Simulate a game. Return True if team1 wins, False otherwise."""
|
||||
rating1 = team1["rating"]
|
||||
rating2 = team2["rating"]
|
||||
probability = 1 / (1 + 10 ** ((rating2 - rating1) / 600))
|
||||
return random.random() < probability
|
||||
|
||||
|
||||
def simulate_round(teams):
|
||||
"""Simulate a round. Return a list of winning teams."""
|
||||
winners = []
|
||||
|
||||
# Simulate games for all pairs of teams
|
||||
for i in range(0, len(teams), 2):
|
||||
if simulate_game(teams[i], teams[i + 1]):
|
||||
winners.append(teams[i])
|
||||
else:
|
||||
winners.append(teams[i + 1])
|
||||
|
||||
return winners
|
||||
|
||||
|
||||
def simulate_tournament(teams):
|
||||
"""Simulate a tournament. Return name of winning team."""
|
||||
# TODO
|
||||
|
||||
while len(teams) > 1:
|
||||
teams = simulate_round(teams)
|
||||
return teams[0]['team']
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user