-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
parameters.go
141 lines (117 loc) · 3.71 KB
/
parameters.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
package fsrs
import (
"math"
)
type Parameters struct {
RequestRetention float64 `json:"RequestRetention"`
MaximumInterval float64 `json:"MaximumInterval"`
W Weights `json:"Weights"`
Decay float64 `json:"Decay"`
Factor float64 `json:"Factor"`
EnableShortTerm bool `json:"EnableShortTerm"`
EnableFuzz bool `json:"EnableFuzz"`
seed string
}
func DefaultParam() Parameters {
var Decay = -0.5
var Factor = math.Pow(0.9, 1/Decay) - 1
return Parameters{
RequestRetention: 0.9,
MaximumInterval: 36500,
W: DefaultWeights(),
Decay: Decay,
Factor: Factor,
EnableShortTerm: true,
EnableFuzz: false,
}
}
func (p *Parameters) forgettingCurve(elapsedDays float64, stability float64) float64 {
return math.Pow(1+p.Factor*elapsedDays/stability, p.Decay)
}
func (p *Parameters) initStability(r Rating) float64 {
return math.Max(p.W[r-1], 0.1)
}
func (p *Parameters) initDifficulty(r Rating) float64 {
return constrainDifficulty(p.W[4] - math.Exp(p.W[5]*float64(r-1)) + 1)
}
func (p *Parameters) ApplyFuzz(ivl float64, elapsedDays float64, enableFuzz bool) float64 {
if !enableFuzz || ivl < 2.5 {
return ivl
}
generator := Alea(p.seed)
fuzzFactor := generator.Double()
minIvl, maxIvl := getFuzzRange(ivl, elapsedDays, p.MaximumInterval)
return math.Floor(fuzzFactor*float64(maxIvl-minIvl+1)) + float64(minIvl)
}
func constrainDifficulty(d float64) float64 {
return math.Min(math.Max(d, 1), 10)
}
func linearDamping(deltaD float64, oldD float64) float64 {
return (10.0 - oldD) * deltaD / 9.0
}
func (p *Parameters) nextInterval(s, elapsedDays float64) float64 {
newInterval := s / p.Factor * (math.Pow(p.RequestRetention, 1/p.Decay) - 1)
return p.ApplyFuzz(math.Max(math.Min(math.Round(newInterval), p.MaximumInterval), 1), elapsedDays, p.EnableFuzz)
}
func (p *Parameters) nextDifficulty(d float64, r Rating) float64 {
deltaD := -p.W[6] * float64(r-3)
nextD := d + linearDamping(deltaD, d)
return constrainDifficulty(p.meanReversion(p.initDifficulty(Easy), nextD))
}
func (p *Parameters) shortTermStability(s float64, r Rating) float64 {
return s * math.Exp(p.W[17]*(float64(r-3)+p.W[18]))
}
func (p *Parameters) meanReversion(init float64, current float64) float64 {
return p.W[7]*init + (1-p.W[7])*current
}
func (p *Parameters) nextRecallStability(d float64, s float64, r float64, rating Rating) float64 {
var hardPenalty, easyBonus float64
if rating == Hard {
hardPenalty = p.W[15]
} else {
hardPenalty = 1
}
if rating == Easy {
easyBonus = p.W[16]
} else {
easyBonus = 1
}
return s * (1 + math.Exp(p.W[8])*
(11-d)*
math.Pow(s, -p.W[9])*
(math.Exp((1-r)*p.W[10])-1)*
hardPenalty*
easyBonus)
}
func (p *Parameters) nextForgetStability(d float64, s float64, r float64) float64 {
return p.W[11] *
math.Pow(d, -p.W[12]) *
(math.Pow(s+1, p.W[13]) - 1) *
math.Exp((1-r)*p.W[14])
}
type FuzzRange struct {
Start float64
End float64
Factor float64
}
var FUZZ_RANGES = []FuzzRange{
{Start: 2.5, End: 7.0, Factor: 0.15},
{Start: 7.0, End: 20.0, Factor: 0.1},
{Start: 20.0, End: math.Inf(1), Factor: 0.05},
}
func getFuzzRange(interval, elapsedDays, maximumInterval float64) (minIvl, maxIvl int) {
delta := 1.0
for _, r := range FUZZ_RANGES {
delta += r.Factor * math.Max(math.Min(interval, r.End)-r.Start, 0.0)
}
interval = math.Min(interval, maximumInterval)
minIvlFloat := math.Max(2, math.Round(interval-delta))
maxIvlFloat := math.Min(math.Round(interval+delta), maximumInterval)
if interval > elapsedDays {
minIvlFloat = math.Max(minIvlFloat, elapsedDays+1)
}
minIvlFloat = math.Min(minIvlFloat, maxIvlFloat)
minIvl = int(minIvlFloat)
maxIvl = int(maxIvlFloat)
return minIvl, maxIvl
}