Last active
February 21, 2021 12:07
-
-
Save xiaodaigh/f9d5838a07e481a473b8b3a24f33ceb0 to your computer and use it in GitHub Desktop.
Nongshim Cup simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Revise | |
includet("utils.jl") | |
function remove_player!(team) | |
if length(team) == 1 | |
team.players = [] | |
else | |
team.players = team.players[2:end] | |
end | |
team | |
end | |
function get_player(team) | |
team.players[1] | |
end | |
function win_and_continue!(teams; bye = length(teams), verbose=false) | |
if length(teams) == 1 | |
return teams[1] | |
end | |
if length(teams) == 2 | |
teams_to_play = [1,2] | |
else | |
teams_to_play = setdiff(1:length(teams), bye) | |
end | |
player1 = get_player(teams[teams_to_play[1]]) | |
player2 = get_player(teams[teams_to_play[2]]) | |
if verbose | |
print("$(player1.name) vs $(player2.name)") | |
end | |
win_rate = player1_predicted_win_pct(player1, player2) | |
if verbose | |
print(": $(round(win_rate*100))% ") | |
end | |
if rand() < win_rate | |
if verbose | |
println(": $(player1.name) wins") | |
end | |
remove_player!(teams[teams_to_play[2]]) | |
bye = teams_to_play[2] | |
else | |
if verbose | |
println(": $(player2.name) wins") | |
end | |
remove_player!(teams[teams_to_play[1]]) | |
bye = teams_to_play[1] | |
end | |
# remove dead teams | |
if any(length.(teams) .== 0) | |
bye = -1 | |
teams = teams[length.(teams) .!= 0] | |
end | |
# println("") | |
# for team in teams | |
# println(team) | |
# end | |
# println("") | |
win_and_continue!(teams; bye=bye, verbose=verbose) | |
end | |
# simulation 1 | |
china = Team("China", [Player("Yang Dingxin", 3598), Player("Ke Jie", 3709)]) | |
korea = Team("Korea", [Player("Shin Jinseo", 3808), Player("Park Junghwan", 3674)]) | |
japan = Team("Japan", [Player("Iyama Yuta", 3519), Player("Ichiriki Ryo", 3574)]) | |
teams = [korea, japan, china] | |
win_and_continue!(deepcopy(teams)) | |
@time team_win = [win_and_continue!(deepcopy(teams)) for _ in 1:100_000]; | |
team_won = [team.name for team in team_win]; | |
using StatsBase | |
countmap(team_won) | |
team_won2 = [(team.name, length(team)) for team in team_win] | |
countmap(team_won2) | |
# simulation 2 - In the unlikely event that Shin Jin Seo loses | |
china = Team("China", [Player("Yang Dingxin", 3598), Player("Ke Jie", 3709)]) | |
korea = Team("Korea", [Player("Park Junghwan", 3674)]) | |
japan = Team("Japan", [Player("Iyama Yuta", 3519), Player("Ichiriki Ryo", 3574)]) | |
teams = [korea, japan, china] | |
win_and_continue!(deepcopy(teams), bye=1, verbose=true) | |
@time team_win = [win_and_continue!(deepcopy(teams)) for _ in 1:100_000]; | |
team_won = [team.name for team in team_win]; | |
using StatsBase | |
countmap(team_won) | |
team_won2 = [(team.name, length(team)) for team in team_win]; | |
countmap(team_won2) | |
# simulation 3 in the likely event that SJS beats ichiriki | |
china = Team("China", [Player("Yang Dingxin", 3598), Player("Ke Jie", 3709)]) | |
korea = Team("Korea", [Player("Shin Jinseo", 3808), Player("Park Junghwan", 3674)]) | |
japan = Team("Japan", [Player("Ichiriki Ryo", 3574)]) | |
teams = [korea, japan, china] | |
win_and_continue!(deepcopy(teams), bye=2, verbose=true) | |
@time team_win = [win_and_continue!(deepcopy(teams)) for _ in 1:100_000]; | |
team_won = [team.name for team in team_win]; | |
using StatsBase | |
countmap(team_won) | |
team_won2 = [(team.name, length(team)) for team in team_win]; | |
countmap(team_won2) | |
using GraphRecipes | |
using Plots | |
using LightGraphs | |
pyplot() | |
# You had WheelGraph here, but I don't think that is what you intended? | |
g = DiGraph(3) | |
add_edge!(g, 1, 2) | |
add_edge!(g, 1, 3) | |
names = ["TWX", "SJS", "KJ"] | |
edgelabels = Dict((1,2)=>"SJS Win", (2,3)=>"KJ Win") | |
x = [0, 0, 1] | |
y = [1, 2, 2] | |
graphplot(g, x=x, y=y, curvature_scalar=0.0, nodesize=0.5, | |
node_weights=[0.5,0.5,0.5], | |
names=names, nodecolor=:lightgray, color=:black, | |
nodeshape=:rect, self_edge_size=0.0, | |
edgelabel = edgelabels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Player | |
name::String | |
rating::Int | |
end | |
struct Match | |
best_of::Int | |
end | |
mutable struct Team | |
name::String | |
players::Vector{Player} | |
end | |
import Base: length | |
length(t::Team) = length(t.players) | |
function player1_predicted_win_pct(player1, player2) | |
power = (player2.rating-player1.rating)/400 | |
denom = 1 + 10^power | |
1/denom | |
end | |
function simulate_match(player1, player2, match) | |
p1_win_pct = player1_predicted_win_pct(player1, player2) | |
# println(p1_win_pct) | |
sum(rand(match.best_of) .< p1_win_pct) > match.best_of/2 ? player1 : player2 | |
end | |
function player1_predicted_win_pct(player1, player2) | |
power = (player2.rating-player1.rating)/400 | |
denom = 1 + 10^power | |
1/denom | |
end | |
function play1(player1, player2) | |
simulate_match(player1, player2, Match(1)) | |
end | |
function simulate_match(player1, player2, match) | |
p1_win_pct = player1_predicted_win_pct(player1, player2) | |
# println(p1_win_pct) | |
sum(rand(match.best_of) .< p1_win_pct) > match.best_of/2 ? player1 : player2 | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment