-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.go
195 lines (172 loc) · 5.2 KB
/
calc.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
194
195
package tws
import (
"errors"
log "github.com/sirupsen/logrus"
"math"
"slices"
)
func init() {
log.SetLevel(log.ErrorLevel)
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
})
}
const (
minDeep int64 = 1
)
func Calc(nodes *[]Node) (map[id]float64, error) {
if nodes == nil || len(*nodes) == 0 {
return nil, errors.New("空节点,无法计算")
}
maxDeep := slices.MaxFunc(*nodes, func(a, b Node) int {
if a.Deep > b.Deep {
return 1
} else if a.Deep < b.Deep {
return -1
}
return 0
}).Deep
m := make(map[id]float64)
// 评分离散,越接近0代表所有评分集中,反之代表争议很大
m2 := make(map[id]float64)
// 评分者个人对集体的离散,越接近0代表此人评分越从众,反之代表与他人有很大不同
m3 := make(map[id]float64)
upCalc(nodes, maxDeep, m2, m3)
downCalc(nodes, maxDeep, m, m2, m3)
return m, nil
}
func groupByUserIdWhereDeep(nodes *[]Node, deep int64) map[id][]Node {
groups := make(map[id][]Node)
for _, node := range *nodes {
if deep == node.Deep {
groups[node.TargetId] = append(groups[node.TargetId], node)
}
}
return groups
}
// 计算节点权重
func calcNodeWeight(node Node, nodeScoreMap *map[id]float64) float64 {
scoreMap := *nodeScoreMap
raterScore := scoreMap[node.RaterId]
if raterScore > 0 {
return raterScore
} else {
// 顶节点的默认分
return 5
}
}
// 计算来自上级节点传递的能量分
func calcNodeIncrScore(node Node, nodeScoreMap *map[id]float64, nodeWeight float64) float64 {
scoreMap := *nodeScoreMap
raterScore := scoreMap[node.RaterId]
if raterScore == 0 {
// 顶节点给予默认分5
raterScore = 5
}
log.Debug("raterScore=", raterScore)
incrScore := raterScore * nodeWeight / 10
log.Debug("incrScore=", incrScore)
return incrScore
}
// 往下计算
func downCalc(nodes *[]Node, maxDeep int64, nodeScoreMap map[id]float64, nodeDMap map[id]float64, dMap map[id]float64) {
for currDeep := maxDeep; currDeep > 0; currDeep-- {
log.Debug("-----------DOWN-DOWN-DOWN-DOWN-------------")
log.Debug("currDeep=", currDeep)
nodesGroup := groupByUserIdWhereDeep(nodes, currDeep)
log.Debug("len(nodesGroup)=", len(nodesGroup))
for userId, nodes := range nodesGroup {
log.Debug("userId=", userId)
log.Debug("nodes.count=", len(nodes))
/**
* 形如 a / (a + b) 中的 (a+b),用以取权重总和,方便计算动态权重
*/
var weightSum float64 = 0
for _, node := range nodes {
log.Debugf("node=%+v", node)
weightSum += calcNodeWeight(node, &nodeScoreMap)
}
log.Debug("总权重=", weightSum)
totalScore := 0.0
// 确保 for nodes 是顺序的
for _, node := range nodes {
nodeWeight := calcNodeWeight(node, &nodeScoreMap) / weightSum
log.Debug("node.RaterId=", node.RaterId)
log.Debug("nodeWeight=", nodeWeight)
nodeScore := float64(node.Score) * nodeWeight
totalScore += nodeScore
log.Debug("nodeScore=", nodeScore)
incrScore := calcNodeIncrScore(node, &nodeScoreMap, nodeWeight)
log.Debug("incrScore=", incrScore)
totalScore += incrScore
// 识人之能
if nodeDMap[node.TargetId] > 0 {
coeff2 := 1.0 / nodeDMap[node.TargetId]
log.Debug("coeff2=", coeff2)
totalScore += 0.8 * coeff2
}
}
// dMap越接近0,代表争议越小
if dMap[userId] > 0 {
coeff1 := 1.0 / dMap[userId]
log.Debug("coeff1=", coeff1)
// 争议附加分
totalScore += 0.5 * coeff1
}
nodeScoreMap[userId] = totalScore
log.Debug("totalScore=", totalScore)
log.Debugf("map=%+v", nodeScoreMap)
}
}
}
// 往上计算
func upCalc(nodes *[]Node, maxDeep int64, nodeDMap map[id]float64, dMap map[id]float64) {
for currDeep := minDeep; currDeep <= maxDeep; currDeep++ {
log.Debug("-----------UP-UP-UP-UP-------------")
log.Debug("currDeep=", currDeep)
nodesGroup := groupByUserIdWhereDeep(nodes, currDeep)
log.Debug("len(nodesGroup)=", len(nodesGroup))
for userId, nodes := range nodesGroup {
log.Debug("userId=", userId)
log.Debug("nodes.count=", len(nodes))
if len(nodes) <= 1 {
log.Debug("节点无多人评分,Skip...")
continue
}
var scores []int64
for _, node := range nodes {
scores = append(scores, node.Score)
}
log.Debugln("scores=", scores)
arrD := calcArrDiscrepancy(&scores)
dMap[userId] = arrD
log.Debugln("评分离散=", arrD)
// 根据被评者的评分离散,为评分者附加额外离散,表达 评分者 被 被评人 影响
var appendD float64 = 0
if nodeDMap[userId] > 0 {
appendD = math.Sqrt(nodeDMap[userId] / 2)
}
log.Debugln("追加离散", appendD)
scoresSum := int64(0)
for _, v := range scores {
scoresSum += v
}
scoresLen := len(scores)
// 确保 for nodes 是顺序的
for _, node := range nodes {
// 计算此人评分从众程度(离散程度)
d, err := calcDiscrepancy3(float64(node.Score), arrD, scoresLen, scoresSum)
if err != nil {
// 即使错误也不能影响后续计算
log.Fatal(err)
}
log.Debug("node.RaterId=", node.RaterId)
log.Debug("原始d=", d)
log.Debug("最终d=", d+appendD)
nodeDMap[node.RaterId] = d + appendD
log.Debugln("评分者个人对集体的离散", nodeDMap)
}
}
}
}