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

Feat/float next interval #213

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.2.4"
version = "1.3.0"
authors = ["Open Spaced Repetition"]
categories = ["algorithms", "science"]
edition = "2021"
Expand Down
22 changes: 10 additions & 12 deletions src/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ impl<B: Backend> From<MemoryState> for MemoryStateTensors<B> {
}
}

pub fn next_interval(stability: f32, desired_retention: f32) -> u32 {
(stability / FACTOR as f32 * (desired_retention.powf(1.0 / DECAY as f32) - 1.0))
.round()
.max(1.0) as u32
pub fn next_interval(stability: f32, desired_retention: f32) -> f32 {
stability / FACTOR as f32 * (desired_retention.powf(1.0 / DECAY as f32) - 1.0)
}

impl<B: Backend> FSRS<B> {
Expand Down Expand Up @@ -144,7 +142,7 @@ impl<B: Backend> FSRS<B> {
stability: Option<f32>,
desired_retention: f32,
rating: u32,
) -> u32 {
) -> f32 {
let stability = stability.unwrap_or_else(|| {
// get initial stability for new card
let rating = Tensor::from_data(
Expand Down Expand Up @@ -333,7 +331,7 @@ pub struct NextStates {
#[derive(Debug, PartialEq, Clone)]
pub struct ItemState {
pub memory: MemoryState,
pub interval: u32,
pub interval: f32,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -475,7 +473,7 @@ mod tests {
let desired_retentions = (1..=10).map(|i| i as f32 / 10.0).collect::<Vec<_>>();
let intervals = desired_retentions
.iter()
.map(|r| next_interval(1.0, *r))
.map(|r| next_interval(1.0, *r).round().max(1.0) as i32)
.collect::<Vec<_>>();
assert_eq!(intervals, [422, 102, 43, 22, 13, 8, 4, 2, 1, 1]);
}
Expand Down Expand Up @@ -548,32 +546,32 @@ mod tests {
stability: 2.969144,
difficulty: 9.520562
},
interval: 3
interval: 2.9691453
},
hard: ItemState {
memory: MemoryState {
stability: 17.091442,
difficulty: 8.4513445
},
interval: 17
interval: 17.09145
},
good: ItemState {
memory: MemoryState {
stability: 31.722975,
difficulty: 7.382128
},
interval: 32
interval: 31.722988
},
easy: ItemState {
memory: MemoryState {
stability: 71.75015,
difficulty: 6.3129106
},
interval: 72
interval: 71.75018
}
}
);
assert_eq!(fsrs.next_interval(Some(121.01552), 0.9, 1), 121);
assert_eq!(fsrs.next_interval(Some(121.01552), 0.9, 1), 121.01557);
Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion src/optimal_retention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ pub fn simulate(
izip!(&mut new_interval, &new_stability, &true_review, &true_learn)
.filter(|(.., &true_review_flag, &true_learn_flag)| true_review_flag || true_learn_flag)
.for_each(|(new_ivl, &new_stab, ..)| {
*new_ivl = (next_interval(new_stab, desired_retention) as f32).clamp(1.0, max_ivl);
*new_ivl = next_interval(new_stab, desired_retention)
.round()
.clamp(1.0, max_ivl);
});

let old_due = card_table.slice(s![Column::Due, ..]);
Expand Down
Loading