World championship magnus simulator

 avatar
unknown
python
6 months ago
2.5 kB
30
Indexable

import random

# Function to calculate win probability based on Elo ratings
def calculate_win_probability(rating_a, rating_b):
    return 1 / (1 + 10 ** ((rating_b - rating_a) / 400))

# Simulating a match with alternating white advantage between two players
def simulate_match_with_alternating_white_advantage(player_a, player_b, players_elo, white_advantage, draw_rate=0.75, games=14):
    wins_a = 0
    draws = 0
    for game in range(games):
        # Alternate white advantage between Player A and Player B
        if game % 2 == 0:  # Player A (Magnus) has White in even games
            elo_a = players_elo[player_a] + white_advantage
            elo_b = players_elo[player_b]
        else:  # Player B (Opponent) has White in odd games
            elo_a = players_elo[player_a]
            elo_b = players_elo[player_b] + white_advantage

        # Calculate expected score based on Elo ratings
        expected_score_a = calculate_win_probability(elo_a, elo_b)
        
        # Adjust win probability based on draw rate
        win_probability_a = expected_score_a - draw_rate / 2
        
        # Simulate match outcome
        outcome_roll = random.random()
        if outcome_roll < win_probability_a:
            wins_a += 1  # Player A wins
        elif outcome_roll < win_probability_a + draw_rate:
            draws += 1  # Draw
        # else: Player B wins

    return wins_a, draws

# Set Elo ratings for players and white advantage
players_elo = {"Magnus": 2850, "Opponent": 2800}
white_advantage = 30  # White advantage (30 Elo points)
draw_rate = 0.75  # Fixed draw rate

# Initialize counters for wins and draws
magnus_wins_alternating = 0
opponent_wins_alternating = 0
draws_alternating = 0

# Number of simulations
simulations = 100000

# Run simulation for the specified number of matches
for _ in range(simulations):
    wins, draws = simulate_match_with_alternating_white_advantage("Magnus", "Opponent", players_elo, white_advantage, draw_rate)
    total_score_magnus = wins + 0.5 * draws
    if total_score_magnus > 7:  # Magnus wins if he scores more than 7 points
        magnus_wins_alternating += 1
    elif total_score_magnus < 7:  # Opponent wins if they score more than 7 points
        opponent_wins_alternating += 1
    else:
        draws_alternating += 1  # If exactly 7 points each, it's a tie

# Print results
print(f"Magnus wins: {magnus_wins_alternating}")
print(f"Opponent wins: {opponent_wins_alternating}")
print(f"Draws: {draws_alternating}")
Editor is loading...
Leave a Comment