Skip to content

Commit

Permalink
Fixed some issues with Brent (#163)
Browse files Browse the repository at this point in the history
* Fixed some issues with Brent

The minimum value in Anki is 0.7 now. And 'funccalls' was off by 1

* fix unit tests

* fix ci

---------

Co-authored-by: Jarrett Ye <[email protected]>
  • Loading branch information
Expertium and L-M-Sherlock authored Jan 31, 2025
1 parent f4d518c commit 82e4b5b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
19 changes: 14 additions & 5 deletions src/fsrs_optimizer/fsrs_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def mean_reversion(init, current):
10,
)

need_learn = card_table[col["due"]] == learn_span
need_learn = card_table[col["stability"]] == 1e-10
card_table[col["cost"]][need_learn] = np.choose(
card_table[col["rating"]][need_learn].astype(int) - 1,
learn_costs,
Expand Down Expand Up @@ -282,6 +282,15 @@ def mean_reversion(init, current):
"rating": card_table[col["rating"]][true_review | true_learn],
}

has_learned = card_table[col["stability"]] > 1e-10
card_table[col["delta_t"]][has_learned] = (
today - card_table[col["last_date"]][has_learned]
)
card_table[col["retrievability"]][has_learned] = power_forgetting_curve(
card_table[col["delta_t"]][has_learned],
card_table[col["stability"]][has_learned],
)

review_cnt_per_day[today] = np.sum(true_review)
learn_cnt_per_day[today] = np.sum(true_learn)
memorized_cnt_per_day[today] = card_table[col["retrievability"]].sum()
Expand Down Expand Up @@ -367,16 +376,16 @@ def brent(tol=0.01, maxiter=20, **kwargs):
mintol = 1.0e-11
cg = 0.3819660

funccalls = 0
xb = 0.75
xb = 0.70
fb = sample(xb, **kwargs)
funccalls = 1

#################################
# BEGIN
#################################
x = w = v = xb
fw = fv = fx = fb
a = 0.75
a = 0.70
b = 0.95
deltax = 0.0
iter = 0
Expand Down Expand Up @@ -471,7 +480,7 @@ def brent(tol=0.01, maxiter=20, **kwargs):
success = (
iter < maxiter
and not (np.isnan(fval) or np.isnan(xmin))
and (0.75 <= xmin <= 0.95)
and (0.70 <= xmin <= 0.95)
)

if success:
Expand Down
4 changes: 2 additions & 2 deletions tests/simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_simulate(self):
cost_per_day,
revlogs,
) = simulate(w=DEFAULT_PARAMETER, request_retention=0.9)
assert memorized_cnt_per_day[-1] == 5880.482440745369
assert memorized_cnt_per_day[-1] == 5960.836176338407

def test_optimal_retention(self):
default_params = {
Expand All @@ -25,4 +25,4 @@ def test_optimal_retention(self):
"loss_aversion": 2.5,
}
r = optimal_retention(**default_params)
assert r == 0.8263932
assert r == 0.8254596913507394

0 comments on commit 82e4b5b

Please sign in to comment.