-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
scheduler_basic.go
183 lines (154 loc) · 5.56 KB
/
scheduler_basic.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
package fsrs
import (
"math"
"time"
)
type basicScheduler struct {
*Scheduler
}
var _ implScheduler = basicScheduler{}
func (p *Parameters) NewBasicScheduler(card Card, now time.Time) *Scheduler {
return p.newScheduler(card, now, func(s *Scheduler) implScheduler {
return basicScheduler{s}
})
}
func (bs basicScheduler) newState(grade Rating) SchedulingInfo {
exist, ok := bs.next[grade]
if ok {
return exist
}
next := bs.current
next.Difficulty = bs.parameters.initDifficulty(grade)
next.Stability = bs.parameters.initStability(grade)
switch grade {
case Again:
next.ScheduledDays = 0
next.Due = bs.now.Add(1 * time.Minute)
next.State = Learning
case Hard:
next.ScheduledDays = 0
next.Due = bs.now.Add(5 * time.Minute)
next.State = Learning
case Good:
next.ScheduledDays = 0
next.Due = bs.now.Add(10 * time.Minute)
next.State = Learning
case Easy:
easyInterval := bs.parameters.nextInterval(
next.Stability,
float64(next.ElapsedDays),
)
next.ScheduledDays = uint64(easyInterval)
next.Due = bs.now.Add(time.Duration(easyInterval) * 24 * time.Hour)
next.State = Review
}
item := SchedulingInfo{
Card: next,
ReviewLog: bs.buildLog(grade),
}
bs.next[grade] = item
return item
}
func (bs basicScheduler) learningState(grade Rating) SchedulingInfo {
exist, ok := bs.next[grade]
if ok {
return exist
}
next := bs.current
interval := float64(bs.current.ElapsedDays)
next.Difficulty = bs.parameters.nextDifficulty(bs.last.Difficulty, grade)
next.Stability = bs.parameters.shortTermStability(bs.last.Stability, grade)
switch grade {
case Again:
next.ScheduledDays = 0
next.Due = bs.now.Add(5 * time.Minute)
next.State = bs.last.State
case Hard:
next.ScheduledDays = 0
next.Due = bs.now.Add(10 * time.Minute)
next.State = bs.last.State
case Good:
goodInterval := bs.parameters.nextInterval(next.Stability, interval)
next.ScheduledDays = uint64(goodInterval)
next.Due = bs.now.Add(time.Duration(goodInterval) * 24 * time.Hour)
next.State = Review
case Easy:
goodStability := bs.parameters.shortTermStability(bs.last.Stability, Good)
goodInterval := bs.parameters.nextInterval(goodStability, interval)
easyInterval := math.Max(
bs.parameters.nextInterval(next.Stability, interval),
float64(goodInterval)+1,
)
next.ScheduledDays = uint64(easyInterval)
next.Due = bs.now.Add(time.Duration(easyInterval) * 24 * time.Hour)
next.State = Review
}
item := SchedulingInfo{
Card: next,
ReviewLog: bs.buildLog(grade),
}
bs.next[grade] = item
return item
}
func (bs basicScheduler) reviewState(grade Rating) SchedulingInfo {
exist, ok := bs.next[grade]
if ok {
return exist
}
interval := float64(bs.current.ElapsedDays)
difficulty := bs.last.Difficulty
stability := bs.last.Stability
retrievability := bs.parameters.forgettingCurve(interval, stability)
nextAgain := bs.current
nextHard := bs.current
nextGood := bs.current
nextEasy := bs.current
bs.nextDs(&nextAgain, &nextHard, &nextGood, &nextEasy, difficulty, stability, retrievability)
bs.nextInterval(&nextAgain, &nextHard, &nextGood, &nextEasy, interval)
bs.nextState(&nextAgain, &nextHard, &nextGood, &nextEasy)
nextAgain.Lapses++
itemAgain := SchedulingInfo{Card: nextAgain, ReviewLog: bs.buildLog(Again)}
itemHard := SchedulingInfo{Card: nextHard, ReviewLog: bs.buildLog(Hard)}
itemGood := SchedulingInfo{Card: nextGood, ReviewLog: bs.buildLog(Good)}
itemEasy := SchedulingInfo{Card: nextEasy, ReviewLog: bs.buildLog(Easy)}
bs.next[Again] = itemAgain
bs.next[Hard] = itemHard
bs.next[Good] = itemGood
bs.next[Easy] = itemEasy
return bs.next[grade]
}
func (bs basicScheduler) nextDs(nextAgain, nextHard, nextGood, nextEasy *Card, difficulty, stability, retrievability float64) {
nextAgain.Difficulty = bs.parameters.nextDifficulty(difficulty, Again)
nextSMin := stability / math.Exp(bs.parameters.W[17]*bs.parameters.W[18])
nextAgain.Stability = math.Min(nextSMin, bs.parameters.nextForgetStability(difficulty, stability, retrievability))
nextHard.Difficulty = bs.parameters.nextDifficulty(difficulty, Hard)
nextHard.Stability = bs.parameters.nextRecallStability(difficulty, stability, retrievability, Hard)
nextGood.Difficulty = bs.parameters.nextDifficulty(difficulty, Good)
nextGood.Stability = bs.parameters.nextRecallStability(difficulty, stability, retrievability, Good)
nextEasy.Difficulty = bs.parameters.nextDifficulty(difficulty, Easy)
nextEasy.Stability = bs.parameters.nextRecallStability(difficulty, stability, retrievability, Easy)
}
func (bs basicScheduler) nextInterval(nextAgain, nextHard, nextGood, nextEasy *Card, elapsedDays float64) {
hardInterval := bs.parameters.nextInterval(nextHard.Stability, elapsedDays)
goodInterval := bs.parameters.nextInterval(nextGood.Stability, elapsedDays)
hardInterval = math.Min(hardInterval, goodInterval)
goodInterval = math.Max(goodInterval, hardInterval+1)
easyInterval := math.Max(
bs.parameters.nextInterval(nextEasy.Stability, elapsedDays),
goodInterval+1,
)
nextAgain.ScheduledDays = 0
nextAgain.Due = bs.now.Add(5 * time.Minute)
nextHard.ScheduledDays = uint64(hardInterval)
nextHard.Due = bs.now.Add(time.Duration(hardInterval) * 24 * time.Hour)
nextGood.ScheduledDays = uint64(goodInterval)
nextGood.Due = bs.now.Add(time.Duration(goodInterval) * 24 * time.Hour)
nextEasy.ScheduledDays = uint64(easyInterval)
nextEasy.Due = bs.now.Add(time.Duration(easyInterval) * 24 * time.Hour)
}
func (bs basicScheduler) nextState(nextAgain, nextHard, nextGood, nextEasy *Card) {
nextAgain.State = Relearning
nextHard.State = Review
nextGood.State = Review
nextEasy.State = Review
}