Skip to content

Commit

Permalink
Fix/Stability is not restricted to maximum or minimum values.
Browse files Browse the repository at this point in the history
  • Loading branch information
ishiko732 committed Sep 18, 2024
1 parent ba9fa37 commit 092c5d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion __tests__/FSRSV5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('get retrievability', () => {
let card = createEmptyCard()
let now = new Date()
let i = 0
while (i < 10 ** 3) {
while (i < 5) {
card = fsrs.next(card, now, Rating.Again).card
now = card.due
i++
Expand Down
36 changes: 21 additions & 15 deletions src/fsrs/algorithm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generatorParameters } from './default'
import { FSRSParameters, Grade, Rating } from './models'
import type { int } from './types'
import { get_fuzz_range } from './help'
import { clamp, get_fuzz_range } from './help'
import { alea } from './alea'

/**
Expand Down Expand Up @@ -213,15 +213,17 @@ export class FSRSAlgorithm {
next_recall_stability(d: number, s: number, r: number, g: Grade): number {
const hard_penalty = Rating.Hard === g ? this.param.w[15] : 1
const easy_bound = Rating.Easy === g ? this.param.w[16] : 1
return +(
return +clamp(
s *
(1 +
Math.exp(this.param.w[8]) *
(11 - d) *
Math.pow(s, -this.param.w[9]) *
(Math.exp((1 - r) * this.param.w[10]) - 1) *
hard_penalty *
easy_bound)
(1 +
Math.exp(this.param.w[8]) *
(11 - d) *
Math.pow(s, -this.param.w[9]) *
(Math.exp((1 - r) * this.param.w[10]) - 1) *
hard_penalty *
easy_bound),
0.01,
this.param.maximum_interval
).toFixed(8)
}

Expand All @@ -234,11 +236,13 @@ export class FSRSAlgorithm {
* @return {number} S^\prime_f new stability after forgetting
*/
next_forget_stability(d: number, s: number, r: number): number {
return +(
return +clamp(
this.param.w[11] *
Math.pow(d, -this.param.w[12]) *
(Math.pow(s + 1, this.param.w[13]) - 1) *
Math.exp((1 - r) * this.param.w[14])
Math.pow(d, -this.param.w[12]) *
(Math.pow(s + 1, this.param.w[13]) - 1) *
Math.exp((1 - r) * this.param.w[14]),
0.01,
this.param.maximum_interval
).toFixed(8)
}

Expand All @@ -249,8 +253,10 @@ export class FSRSAlgorithm {
* @param {Grade} g Grade (Rating[0.again,1.hard,2.good,3.easy])
*/
next_short_term_stability(s: number, g: Grade): number {
return +(
s * Math.exp(this.param.w[17] * (g - 3 + this.param.w[18]))
return +clamp(
s * Math.exp(this.param.w[17] * (g - 3 + this.param.w[18])),
0.01,
this.param.maximum_interval
).toFixed(8)
}

Expand Down
4 changes: 4 additions & 0 deletions src/fsrs/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,7 @@ export function get_fuzz_range(
min_ivl = Math.min(min_ivl, max_ivl)
return { min_ivl, max_ivl }
}

export function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max)
}

0 comments on commit 092c5d7

Please sign in to comment.