-
Notifications
You must be signed in to change notification settings - Fork 1
/
rate.go
73 lines (57 loc) · 1.82 KB
/
rate.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
package openskill
import (
"math"
"sort"
"github.com/samber/lo"
)
// Rate rates a group of teams with the provided optional parameters for classification
func Rate(teams []Team, options Options) []Team {
var model Model
var processedTeams = make([]Team, len(teams))
if options.Model != nil {
model = *options.Model
} else {
model = PlackettLuce
}
if options.Tau != nil {
tauSquared := math.Pow(*options.Tau, 2)
processedTeams = lo.Map(teams, func(item Team, index int) Team {
return lo.Map([]*Rating(item), func(item *Rating, index int) *Rating {
item.SkillUncertaintyDegree = math.Sqrt(math.Pow(item.SkillUncertaintyDegree, 2) + tauSquared)
return item
})
})
} else {
copy(processedTeams, teams)
}
var rank []int64
if len(options.Rankings) > 0 {
rank = lo.Map(options.Rankings, func(item int64, index int) int64 {
return item
})
} else if len(options.Scores) > 0 {
rank = lo.Map(options.Scores, func(item int64, index int) int64 {
return -item
})
} else {
rank = lo.RangeFrom[int64](1, len(teams))
}
orderedTeams, tenet := unwind(rank, processedTeams)
_newRanks := make([]int64, len(rank))
copy(_newRanks, rank)
sort.Slice(_newRanks, func(i, j int) bool {
return _newRanks[i] < _newRanks[j]
})
options.Rankings = _newRanks
newRatings := model(orderedTeams, &options)
reorderedTeams, _ := unwind(tenet, newRatings)
if options.Tau != nil && options.PreventUncertaintyIncrease != nil && *options.PreventUncertaintyIncrease {
reorderedTeams = lo.Map(reorderedTeams, func(item Team, index int) Team {
return lo.Map([]*Rating(item), func(localItem *Rating, localIndex int) *Rating {
localItem.SkillUncertaintyDegree = math.Min(localItem.SkillUncertaintyDegree, teams[index][localIndex].SkillUncertaintyDegree)
return localItem
})
})
}
return reorderedTeams
}