Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/optimize card priority calculation and fix learn span behavior #263

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fsrs"
version = "1.4.7"
version = "1.4.8"
authors = ["Open Spaced Repetition"]
categories = ["algorithms", "science"]
edition = "2021"
Expand Down
57 changes: 45 additions & 12 deletions src/optimal_retention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ pub fn simulate(

let mut card_priorities = PriorityQueue::new();

fn card_priority(card: &Card, learn: bool) -> (usize, bool, usize) {
// High difficulty priority as example
(-card.due as usize, !learn, -card.difficulty as usize)
fn card_priority(card: &Card, learn: bool) -> (i32, bool, i32) {
// high priority for early due, review, low difficulty card
(-card.due as i32, !learn, -(card.difficulty * 100.0) as i32)
}

for (i, card) in cards.iter().enumerate() {
if card.due >= learn_span as f32 {
continue;
}
L-M-Sherlock marked this conversation as resolved.
Show resolved Hide resolved
card_priorities.push(i, card_priority(card, card.last_date == f32::NEG_INFINITY));
}

Expand Down Expand Up @@ -333,9 +336,7 @@ pub fn simulate(
card.last_date = card.due;
card.due += ivl;

if (card.due as usize) <= learn_span {
card_priorities.change_priority(&card_index, card_priority(card, false));
}
card_priorities.change_priority(&card_index, card_priority(card, false));
}

/*dbg!((
Expand Down Expand Up @@ -915,11 +916,43 @@ mod tests {
simulate(&config, &DEFAULT_PARAMETERS, 0.9, None, None)?;
assert_eq!(
memorized_cnt_per_day[memorized_cnt_per_day.len() - 1],
7004.319
6781.4946
);
Ok(())
}

#[test]
fn changing_learn_span_should_get_same_review_cnt_per_day() -> Result<()> {
const LOWER: usize = 365;
const DECK_SIZE: usize = 1000;
const LEARN_LIMIT: usize = 10;
let config = SimulatorConfig {
learn_span: LOWER,
learn_limit: LEARN_LIMIT,
deck_size: DECK_SIZE,
..Default::default()
};
let (_, review_cnt_per_day_lower, _, _) =
simulate(&config, &DEFAULT_PARAMETERS, 0.9, None, None)?;
let config = SimulatorConfig {
learn_span: LOWER + 10,
learn_limit: LEARN_LIMIT,
deck_size: DECK_SIZE,
..Default::default()
};
let (_, review_cnt_per_day_higher, _, _) =
simulate(&config, &DEFAULT_PARAMETERS, 0.9, None, None)?;
// Compare first LOWER items of review_cnt_per_day arrays
for i in 0..LOWER {
assert_eq!(
review_cnt_per_day_lower[i], review_cnt_per_day_higher[i],
"at index {}",
i
);
}
Ok(())
}

#[test]
fn simulate_with_existing_cards() -> Result<()> {
let config = SimulatorConfig {
Expand Down Expand Up @@ -1032,8 +1065,8 @@ mod tests {
assert_eq!(
results.1.to_vec(),
vec![
0, 16, 25, 34, 60, 65, 76, 85, 91, 92, 100, 103, 119, 107, 103, 113, 122, 143, 149,
151, 148, 172, 154, 175, 156, 169, 155, 191, 185, 170
0, 15, 18, 38, 64, 64, 80, 89, 95, 95, 100, 96, 107, 118, 120, 114, 126, 123, 139,
167, 158, 156, 167, 161, 154, 178, 163, 151, 160, 151
]
);
assert_eq!(
Expand All @@ -1050,7 +1083,7 @@ mod tests {
..Default::default()
};
let results = simulate(&config, &DEFAULT_PARAMETERS, 0.9, None, None)?;
assert_eq!(results.0[results.0.len() - 1], 6619.07);
assert_eq!(results.0[results.0.len() - 1], 6484.7144);
Ok(())
}

Expand Down Expand Up @@ -1103,7 +1136,7 @@ mod tests {
..Default::default()
};
let optimal_retention = fsrs.optimal_retention(&config, &[], |_v| true).unwrap();
assert_eq!(optimal_retention, 0.8372485);
assert_eq!(optimal_retention, 0.85450846);
assert!(fsrs.optimal_retention(&config, &[1.], |_v| true).is_err());
Ok(())
}
Expand All @@ -1123,7 +1156,7 @@ mod tests {
let mut param = DEFAULT_PARAMETERS[..17].to_vec();
param.extend_from_slice(&[0.0, 0.0]);
let optimal_retention = fsrs.optimal_retention(&config, &param, |_v| true).unwrap();
assert_eq!(optimal_retention, 0.85450846);
assert_eq!(optimal_retention, 0.83750373);
Ok(())
}

Expand Down
Loading