gameServer/OfficialServer/updateRating.hs
changeset 11358 55360683db75
child 11359 e6a9528f02f7
equal deleted inserted replaced
11357:89fd907ccdec 11358:55360683db75
       
     1 {-
       
     2     Glicko2, as described in http://www.glicko.net/glicko/glicko2.pdf
       
     3 -}
       
     4 
       
     5 module Main where
       
     6 
       
     7 --import Data.Map as Map
       
     8 
       
     9 data RatingData = RatingData {
       
    10         ratingValue
       
    11         , rD
       
    12         , volatility :: Double
       
    13     }
       
    14 data GameData = GameData {
       
    15         rating :: RatingData,
       
    16         opponentRating :: RatingData,
       
    17         gameScore :: Double
       
    18     }
       
    19 
       
    20 τ, ε :: Double
       
    21 τ = 0.3
       
    22 ε = 0.000001
       
    23 
       
    24 g_φ :: Double -> Double
       
    25 g_φ φ = 1 / sqrt (1 + 3 * φ^2 / pi^2)
       
    26 
       
    27 calcE :: GameData -> (Double, Double)
       
    28 calcE (GameData oldRating oppRating s) = (
       
    29     1 / (1 + exp (g_φᵢ * (μᵢ - μ)))
       
    30     , g_φᵢ
       
    31     )
       
    32     where
       
    33         μ = (ratingValue oldRating - 1500) / 173.7178
       
    34         φ = rD oldRating / 173.7178
       
    35         μᵢ = (ratingValue oppRating - 1500) / 173.7178
       
    36         φᵢ = rD oppRating / 173.7178
       
    37         g_φᵢ = g_φ φᵢ
       
    38 
       
    39 
       
    40 calcNewRating :: [GameData] -> RatingData
       
    41 calcNewRating [] = undefined
       
    42 calcNewRating games@(GameData oldRating _ _ : _) = undefined
       
    43     where
       
    44         _Es = map calcE games
       
    45         υ = 1 / sum (map υ_p _Es)
       
    46         υ_p (_Eᵢ, g_φᵢ) = g_φᵢ ^ 2 * _Eᵢ * (1 - _Eᵢ)
       
    47         _Δ = υ * sum (map _Δ_p $ zip _Es (map gameScore games))
       
    48         _Δ_p ((_Eᵢ, g_φᵢ), sᵢ) = g_φᵢ * (sᵢ - _Eᵢ)
       
    49 
       
    50         μ = (ratingValue oldRating - 1500) / 173.7178
       
    51         φ = rD oldRating / 173.7178
       
    52         σ = volatility oldRating
       
    53 
       
    54         a = log (σ ^ 2)
       
    55         f :: Double -> Double
       
    56         f x = exp x * (_Δ ^ 2 - φ ^ 2 - υ - exp x) / 2 / (φ ^ 2 + υ + exp x) ^ 2 - (x - a) / τ ^ 2
       
    57 
       
    58         _A = a
       
    59         _B = if _Δ ^ 2 > φ ^ 2 + υ then log (_Δ ^ 2 - φ ^ 2 - υ) else head . dropWhile ((>) 0 . f) . map (\k -> a - k * τ) $ [1 ..]
       
    60         fA = f _A
       
    61         fB = f _B
       
    62         σ' = (\(_A, _, _, _) -> exp (_A / 2)) . head . dropWhile (\(_A, _, _B, _) -> abs (_B - _A) > ε) $ iterate step5 (_A, fA, _B, fB)
       
    63         step5 (_A, fA, _B, fB) = let _C = _A + (_A - _B) * fA / (fB - fA); fC = f _C in
       
    64                                      if fC * fB < 0 then (_B, fB, _C, fC) else (_A, fA / 2, _C, fC)
       
    65 
       
    66 
       
    67 main = undefined