-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
193 lines (157 loc) · 3.98 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package openskill
import (
"math"
"sort"
"github.com/samber/lo"
"golang.org/x/exp/constraints"
)
type teamRating struct {
TeamMu float64
TeamSigmaSq float64
Team *Team
Rank int64
}
type sums struct {
omegaSum float64
deltaSum float64
}
func rankings(teams []Team, ranks []int64) []int64 {
teamScores := lo.Map(teams, func(item Team, index int) int64 {
if index < len(ranks) {
return ranks[index]
}
return int64(index)
})
outrank := make([]int64, len(teams))
var s int64 = 0
for i := 0; i < len(teamScores); i++ {
if (i > 0) && teamScores[i-1] < teamScores[i] {
s = int64(i)
}
outrank[i] = s
}
return outrank
}
func teamRatings(options *Options) func(game []Team) []*teamRating {
return func(game []Team) []*teamRating {
var rank []int64
if options != nil && options.Rankings != nil {
rank = rankings(game, options.Rankings)
} else {
rank = rankings(game, []int64{})
}
return lo.Map(game, func(item Team, index int) *teamRating {
mu := lo.Sum(lo.Map([]*Rating(item), func(item *Rating, index int) float64 {
return item.AveragePlayerSkill
}))
sigma := lo.Sum(lo.Map([]*Rating(item), func(item *Rating, index int) float64 {
return math.Pow(item.SkillUncertaintyDegree, 2)
}))
return &teamRating{
Team: &item,
TeamMu: mu,
TeamSigmaSq: sigma,
Rank: rank[index],
}
})
}
}
func ladderPairs[T any](slc []*T) [][]*T {
size := len(slc)
var left, right []*T = make([]*T, 0), make([]*T, 0)
// bail earlier
if size == 1 {
return [][]*T{}
}
left = append(left, nil)
left = append(left, slc[0:size-1]...)
right = append(right, slc[1:]...)
right = append(right, nil)
zip := lo.Zip2(left, right)
return lo.Map(zip, func(item lo.Tuple2[*T, *T], index int) []*T {
l, r := item.Unpack()
if l != nil && r != nil {
return []*T{l, r}
}
if l != nil && r == nil {
return []*T{l}
}
if l == nil && r != nil {
return []*T{r}
}
return []*T{} // this should really only happen when size == 1
})
}
func utilC(options *Options) func(teamRatings []*teamRating) float64 {
betasq := betaSq(options)
return func(teamRatings []*teamRating) float64 {
return math.Sqrt(
lo.Sum(lo.Map(teamRatings, func(item *teamRating, index int) float64 {
return item.TeamSigmaSq + betasq
})),
)
}
}
func utilSumQ(teamRatings []*teamRating, c float64) []float64 {
return lo.Map(teamRatings, func(item *teamRating, index int) float64 {
filteredRatings := lo.Filter(teamRatings, func(localItem *teamRating, index int) bool {
return localItem.Rank >= item.Rank
})
mappedFilteredRatings := lo.Map(filteredRatings, func(localItem *teamRating, index int) float64 {
return math.Exp(localItem.TeamMu / c)
})
return lo.Sum(mappedFilteredRatings)
})
}
func utilA(teamRatings []*teamRating) []int64 {
return lo.Map(teamRatings, func(item *teamRating, index int) int64 {
filteredRatings := lo.Filter(teamRatings, func(localItem *teamRating, index int) bool {
return item.Rank == localItem.Rank
})
return int64(len(filteredRatings))
})
}
func gamma(options *Options) Gamma {
if options.GammaFunction != nil {
return *options.GammaFunction
}
return func(c float64, k int64, mu, sigmaSq float64, team *Team, qRank int64) float64 {
return math.Sqrt(sigmaSq) / c
}
}
func score(q, i int64) float64 {
if q < i {
return 0.0
}
if q > i {
return 1.0
}
return 0.5
}
func unwind[T constraints.Integer, R any](order []T, collection []R) (sortedCollection []R, stochasticTenet []T) {
if len(collection) <= 0 {
sortedCollection = make([]R, 0)
stochasticTenet = make([]T, 0)
return
}
zipped := []struct {
x T
y T
z R
}{}
for i, v := range collection {
zipped = append(zipped, struct {
x T
y T
z R
}{x: order[i], y: T(i), z: v})
}
sort.Slice(zipped, func(i, j int) bool {
return zipped[i].x < zipped[j].x
})
for _, v := range zipped {
sortedCollection = append(sortedCollection, v.z)
stochasticTenet = append(stochasticTenet, v.y)
}
return
}