Skip to content

Commit

Permalink
Fix/clamp stability in simulator (#250)
Browse files Browse the repository at this point in the history
* Fix/clamp stability in simulator

* clip parameters in simulator
  • Loading branch information
L-M-Sherlock authored Oct 29, 2024
1 parent 9d5d2f3 commit 3721cdf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) const DECAY: f64 = -0.5;
/// (9/10) ^ (1 / DECAY) - 1
pub(crate) const FACTOR: f64 = 19f64 / 81f64;
pub(crate) const S_MIN: f32 = 0.01;
pub(crate) const S_MAX: f32 = 36500.0;
/// This is a slice for efficiency, but should always be 17 in length.
pub type Parameters = [f32];
use itertools::izip;
Expand Down
4 changes: 2 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::{FSRSError, Result};
use crate::inference::{Parameters, DECAY, FACTOR, S_MIN};
use crate::inference::{Parameters, DECAY, FACTOR, S_MAX, S_MIN};
use crate::parameter_clipper::clip_parameters;
use crate::DEFAULT_PARAMETERS;
use burn::backend::ndarray::NdArrayDevice;
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<B: Backend> Model<B> {
)
};
MemoryStateTensors {
stability: new_s.clamp(S_MIN, 36500.0),
stability: new_s.clamp(S_MIN, S_MAX),
difficulty: new_d,
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/optimal_retention.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::{FSRSError, Result};
use crate::inference::{next_interval, ItemProgress, Parameters, DECAY, FACTOR, S_MIN};
use crate::inference::{next_interval, ItemProgress, Parameters, DECAY, FACTOR, S_MAX, S_MIN};
use crate::model::check_and_fill_parameters;
use crate::parameter_clipper::clip_parameters;
use crate::FSRS;
use burn::tensor::backend::Backend;
use itertools::{izip, Itertools};
Expand Down Expand Up @@ -71,12 +72,13 @@ impl Default for SimulatorConfig {
fn stability_after_success(w: &[f32], s: f32, r: f32, d: f32, rating: usize) -> f32 {
let hard_penalty = if rating == 2 { w[15] } else { 1.0 };
let easy_bonus = if rating == 4 { w[16] } else { 1.0 };
s * (f32::exp(w[8])
(s * (f32::exp(w[8])
* (11.0 - d)
* s.powf(-w[9])
* (f32::exp((1.0 - r) * w[10]) - 1.0)
* hard_penalty)
.mul_add(easy_bonus, 1.0)
.mul_add(easy_bonus, 1.0))
.clamp(S_MIN, S_MAX)
}

fn stability_after_failure(w: &[f32], s: f32, r: f32, d: f32) -> f32 {
Expand All @@ -85,7 +87,7 @@ fn stability_after_failure(w: &[f32], s: f32, r: f32, d: f32) -> f32 {
}

fn stability_short_term(w: &[f32], s: f32, rating_offset: f32, session_len: f32) -> f32 {
s * (w[17] * (rating_offset + session_len * w[18])).exp()
(s * (w[17] * (rating_offset + session_len * w[18])).exp()).clamp(S_MIN, S_MAX)
}

fn init_d(w: &[f32], rating: usize) -> f32 {
Expand Down Expand Up @@ -132,6 +134,7 @@ pub fn simulate(
existing_cards: Option<Vec<Card>>,
) -> Result<(Array1<f32>, Array1<usize>, Array1<usize>, Array1<f32>), FSRSError> {
let w = &check_and_fill_parameters(w)?;
let w = &clip_parameters(w);
let SimulatorConfig {
deck_size,
learn_span,
Expand Down

0 comments on commit 3721cdf

Please sign in to comment.