When learning steps are missing, start from the SM-2 state

Closes https://github.com/open-spaced-repetition/fsrs-rs/issues/87
This commit is contained in:
Damien Elmes 2023-10-03 12:20:10 +10:00
parent 257d7bbbbc
commit 72b0c81761
2 changed files with 118 additions and 26 deletions

View File

@ -361,7 +361,7 @@ impl Collection {
// and will need its initial memory state to be calculated based on review
// history.
let revlog = self.revlog_for_srs(SearchNode::CardIds(card.id.to_string()))?;
let item = single_card_revlog_to_item(revlog, timing.next_day_at);
let item = single_card_revlog_to_item(&fsrs, revlog, timing.next_day_at);
card.set_memory_state(&fsrs, item);
}
let days_elapsed = self

View File

@ -5,12 +5,14 @@ use std::collections::HashMap;
use anki_proto::scheduler::ComputeMemoryStateResponse;
use fsrs::FSRSItem;
use fsrs::MemoryState;
use fsrs::FSRS;
use itertools::Itertools;
use crate::card::CardType;
use crate::prelude::*;
use crate::revlog::RevlogEntry;
use crate::revlog::RevlogReviewKind;
use crate::scheduler::fsrs::weights::single_card_revlog_to_items;
use crate::scheduler::fsrs::weights::Weights;
use crate::scheduler::states::fuzz::with_review_fuzz;
@ -55,9 +57,9 @@ impl Collection {
} else {
None
};
let items = fsrs_items_for_memory_state(revlog, timing.next_day_at);
let desired_retention = req.as_ref().map(|w| w.desired_retention);
let fsrs = FSRS::new(req.as_ref().map(|w| &w.weights[..]))?;
let items = fsrs_items_for_memory_state(&fsrs, revlog, timing.next_day_at);
let desired_retention = req.as_ref().map(|w| w.desired_retention);
let mut progress = self.new_progress_handler::<ComputeMemoryProgress>();
progress.update(false, |s| s.total_cards = items.len() as u32)?;
for (idx, (card_id, item)) in items.into_iter().enumerate() {
@ -65,7 +67,7 @@ impl Collection {
let mut card = self.storage.get_card(card_id)?.or_not_found(card_id)?;
let original = card.clone();
if let Some(req) = &req {
card.set_memory_state(&fsrs, item.clone());
card.set_memory_state(&fsrs, item);
card.desired_retention = desired_retention;
// if rescheduling
if let Some(reviews) = &last_reviews {
@ -128,7 +130,7 @@ impl Collection {
let desired_retention = config.inner.desired_retention;
let fsrs = FSRS::new(Some(&config.inner.fsrs_weights))?;
let revlog = self.revlog_for_srs(SearchNode::CardIds(card.id.to_string()))?;
let item = single_card_revlog_to_item(revlog, self.timing_today()?.next_day_at);
let item = single_card_revlog_to_item(&fsrs, revlog, self.timing_today()?.next_day_at);
card.set_memory_state(&fsrs, item);
Ok(ComputeMemoryStateResponse {
state: card.memory_state.map(Into::into),
@ -138,13 +140,18 @@ impl Collection {
}
impl Card {
pub(crate) fn set_memory_state(&mut self, fsrs: &FSRS, item: Option<FSRSItem>) {
pub(crate) fn set_memory_state(
&mut self,
fsrs: &FSRS,
item: Option<FsrsItemWithStartingState>,
) {
self.memory_state = item
.map(|i| fsrs.memory_state(i, None))
.map(|i| fsrs.memory_state(i.item, i.starting_state))
.or_else(|| {
if self.ctype == CardType::New {
None
} else {
// no valid revlog entries; infer state from current card state
Some(fsrs.memory_state_from_sm2(self.ease_factor(), self.interval as f32))
}
})
@ -152,12 +159,21 @@ impl Card {
}
}
#[derive(Debug)]
pub(crate) struct FsrsItemWithStartingState {
pub item: FSRSItem,
/// When revlogs have been truncated, this stores the initial state at first
/// review
pub starting_state: Option<MemoryState>,
}
/// When updating memory state, FSRS only requires the last FSRSItem that
/// contains the full history.
pub(crate) fn fsrs_items_for_memory_state(
fsrs: &FSRS,
revlogs: Vec<RevlogEntry>,
next_day_at: TimestampSecs,
) -> Vec<(CardId, Option<FSRSItem>)> {
) -> Vec<(CardId, Option<FsrsItemWithStartingState>)> {
revlogs
.into_iter()
.group_by(|r| r.cid)
@ -165,7 +181,7 @@ pub(crate) fn fsrs_items_for_memory_state(
.map(|(card_id, group)| {
(
card_id,
single_card_revlog_to_item(group.collect(), next_day_at),
single_card_revlog_to_item(fsrs, group.collect(), next_day_at),
)
})
.collect()
@ -190,42 +206,118 @@ fn get_last_reviews(revlogs: &[RevlogEntry]) -> HashMap<CardId, TimestampSecs> {
out
}
/// When calculating memory state, only the last FSRSItem is required.
/// When calculating memory state, only the last FSRSItem is required. If the
/// revlog is non-empty and no learning steps have been detected (indicative of
/// a truncated revlog), we return the starting state inferred from the first
/// revlog entry, so that the first review is not treated as if started from
/// scratch.
pub(crate) fn single_card_revlog_to_item(
fsrs: &FSRS,
entries: Vec<RevlogEntry>,
next_day_at: TimestampSecs,
) -> Option<FSRSItem> {
let items = single_card_revlog_to_items(entries, next_day_at, false);
items.and_then(|mut i| i.pop())
) -> Option<FsrsItemWithStartingState> {
let have_learning = entries
.iter()
.any(|e| e.review_kind == RevlogReviewKind::Learning);
if have_learning {
let items = single_card_revlog_to_items(entries, next_day_at, false);
Some(FsrsItemWithStartingState {
item: items.unwrap().pop().unwrap(),
starting_state: None,
})
} else if let Some(first_review) = entries.iter().find(|e| e.button_chosen > 0) {
let ease_factor = if first_review.ease_factor == 0 {
2500
} else {
first_review.ease_factor
};
let interval = first_review.interval.max(1);
let starting_state =
fsrs.memory_state_from_sm2(ease_factor as f32 / 1000.0, interval as f32);
let items = single_card_revlog_to_items(entries, next_day_at, false);
items.and_then(|mut items| {
let mut item = items.pop().unwrap();
item.reviews.remove(0);
if item.reviews.is_empty() {
None
} else {
Some(FsrsItemWithStartingState {
item,
starting_state: Some(starting_state),
})
}
})
} else {
None
}
}
#[cfg(test)]
mod tests {
use fsrs::MemoryState;
use super::super::weights::tests::fsrs_items;
use super::*;
use crate::card::FsrsMemoryState;
use crate::revlog::RevlogReviewKind;
use crate::scheduler::fsrs::weights::tests::convert;
use crate::scheduler::fsrs::weights::tests::review;
use crate::scheduler::fsrs::weights::tests::revlog;
#[test]
fn bypassed_learning_is_handled() {
// cards without any learning steps due to truncated history still have memory
// state calculated
let fsrs = FSRS::new(Some(&[])).unwrap();
let item = single_card_revlog_to_item(
&fsrs,
vec![
RevlogEntry {
ease_factor: 2500,
interval: 100,
..revlog(RevlogReviewKind::Review, 100)
},
revlog(RevlogReviewKind::Review, 1),
],
TimestampSecs::now(),
)
.unwrap();
assert_eq!(
convert(
&[
RevlogEntry {
ease_factor: 2500,
..revlog(RevlogReviewKind::Manual, 7)
},
revlog(RevlogReviewKind::Review, 6),
],
false,
),
fsrs_items!([review(0)])
item.starting_state,
Some(MemoryState {
stability: 100.,
difficulty: 4.4642878
})
);
let mut card = Card::default();
card.set_memory_state(&fsrs, Some(item));
assert_eq!(
card.memory_state,
Some(FsrsMemoryState {
stability: 248.47879,
difficulty: 4.468945
})
);
// but if there's only a single revlog entry, we'll fall back on current card
// state
let item = single_card_revlog_to_item(
&fsrs,
vec![RevlogEntry {
ease_factor: 2500,
interval: 100,
..revlog(RevlogReviewKind::Review, 100)
}],
TimestampSecs::now(),
);
assert!(item.is_none());
card.interval = 123;
card.ease_factor = 2000;
card.ctype = CardType::Review;
card.set_memory_state(&fsrs, item);
assert_eq!(
card.memory_state,
Some(FsrsMemoryState {
stability: 123.0,
difficulty: 6.5147324,
})
);
}