Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. )
  7.  
  8. // Seed each player 0 through 15
  9. // Play round 1 and advance winners to round 2
  10. // Repeat until you have a winner in round 4
  11. // Count the number of times each player ended in each top 2 spots.
  12. // How to seed?
  13. // First tournament is seeded in no particular order.
  14. // Next tournament is seeded based on results of last tournament, ties broken by coin flip.
  15. // Finals are best of 5, the rest best of 3
  16.  
  17. func main() {
  18. ratings := getRatings()
  19. names := getNames()
  20. initSeed := make([]string, 0)
  21. for player := range ratings {
  22. initSeed = append(initSeed, player)
  23. }
  24. wins := make(map[string][]int64) // 1st and 2nd place finishes
  25. for player := range ratings {
  26. wins[player] = []int64{0, 0}
  27. }
  28. seeds := getTournamentResults(ratings, initSeed)
  29.  
  30. // Simulation
  31. for i := 1; true; i++ {
  32. results := getTournamentResults(ratings, seeds)
  33. wins[results[0]][0]++
  34. wins[results[1]][1]++
  35. if i%100000 == 0 {
  36. fmt.Println("Iteration ", i)
  37. for _, name := range names {
  38. fmt.Printf("%.6f %.6f %v\n", float64(wins[name][0])/float64(i), float64(wins[name][1])/float64(i), name)
  39. }
  40. }
  41. seeds = results
  42. }
  43. }
  44.  
  45. func getTournamentResults(ratings map[string]float64, seeds []string) []string {
  46. results := make([]string, len(ratings))
  47. rankIndex := len(results) - 1
  48. // Round 1
  49. round2Contestants := make([]string, 0)
  50. playAndPlace(0, 15, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  51. playAndPlace(1, 14, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  52. playAndPlace(2, 13, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  53. playAndPlace(3, 12, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  54. playAndPlace(4, 11, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  55. playAndPlace(5, 10, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  56. playAndPlace(6, 9, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  57. playAndPlace(7, 8, 3, ratings, seeds, &rankIndex, &round2Contestants, &results)
  58. // Round 2
  59. round3Contestants := make([]string, 0)
  60. playAndPlace(0, 1, 5, ratings, round2Contestants, &rankIndex, &round3Contestants, &results)
  61. playAndPlace(2, 3, 5, ratings, round2Contestants, &rankIndex, &round3Contestants, &results)
  62. playAndPlace(4, 5, 5, ratings, round2Contestants, &rankIndex, &round3Contestants, &results)
  63. playAndPlace(6, 7, 5, ratings, round2Contestants, &rankIndex, &round3Contestants, &results)
  64. // Round 3
  65. round4Contestants := make([]string, 0)
  66. playAndPlace(0, 1, 5, ratings, round3Contestants, &rankIndex, &round4Contestants, &results)
  67. playAndPlace(2, 3, 5, ratings, round3Contestants, &rankIndex, &round4Contestants, &results)
  68. // Round 4
  69. winner := make([]string, 0)
  70. playAndPlace(0, 1, 5, ratings, round4Contestants, &rankIndex, &winner, &results)
  71. results[0] = winner[0]
  72. return results
  73. }
  74.  
  75. func playAndPlace(seedA, seedB, numGames int, ratings map[string]float64, seeds []string, rankIndex *int, nextRoundContestants *[]string, results *[]string) {
  76. if wonMatch(numGames, ratings[seeds[seedA]], ratings[seeds[seedB]]) {
  77. // fmt.Printf("%v beat %v\n", seeds[seedA], seeds[seedB])
  78. *nextRoundContestants = append(*nextRoundContestants, seeds[seedA])
  79. (*results)[*rankIndex] = seeds[seedB]
  80. } else {
  81. *nextRoundContestants = append(*nextRoundContestants, seeds[seedB])
  82. (*results)[*rankIndex] = seeds[seedA]
  83. // fmt.Printf("%v beat %v\n", seeds[seedB], seeds[seedA])
  84. }
  85. *rankIndex--
  86. }
  87.  
  88. // Returns true if player a beats player b in a match of n games
  89. func wonMatch(n int, a, b float64) bool {
  90. var wins int
  91. for i := 0; i < n; i++ {
  92. if getWin(a, b) {
  93. wins++
  94. }
  95. }
  96. return wins > n/2
  97. }
  98.  
  99. // The true if a defeats b, based on their elo difference
  100. func getWin(a, b float64) bool {
  101. chance := a / (a + b)
  102. return chance >= rand.Float64()
  103. }
  104.  
  105. func getRatings() map[string]float64 {
  106. return map[string]float64{
  107. "Joseph_S": 583.6006,
  108. "Jonas_N": 506.0191,
  109. "Green_T": 413.6078,
  110. "Harry_H": 396.8535,
  111. "Koryan": 387.0532,
  112. "DanQZ": 357.6342,
  113. "Alex_K": 314.4253,
  114. "Quaid": 302.3414,
  115. "Svavar_G": 272.5940,
  116. "Eli_M": 263.8913,
  117. "Noztrom": 249.9731,
  118. "Jeff_M": 232.0740,
  119. "Matt_B": 229.9798,
  120. "Bo_S": 192.5092,
  121. "Beastinshen": 188.4295,
  122. "Shakil_A": 180.4603,
  123. }
  124. }
  125.  
  126. func getNames() []string {
  127. return []string{
  128. "Joseph_S",
  129. "Jonas_N",
  130. "Green_T",
  131. "Harry_H",
  132. "Koryan",
  133. "DanQZ",
  134. "Alex_K",
  135. "Quaid",
  136. "Svavar_G",
  137. "Eli_M",
  138. "Noztrom",
  139. "Jeff_M",
  140. "Matt_B",
  141. "Bo_S",
  142. "Beastinshen",
  143. "Shakil_A",
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement