Skip to content

Commit

Permalink
Fix/calculate deltaT correctly (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
L-M-Sherlock authored Oct 27, 2024
1 parent 785ac3e commit 9e56b3b
Showing 1 changed file with 123 additions and 20 deletions.
143 changes: 123 additions & 20 deletions examples/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,124 @@ function createReviewHistoriesForCards() {

const reviewHistories = [
[
[new Date('2023-01-01'), 3],
[new Date('2023-01-02'), 4],
[new Date('2023-01-05'), 3],
[new Date('2023-01-15'), 4],
[new Date('2023-02-01'), 3],
[new Date('2023-02-20'), 4],
[new Date("2023-01-01"), 3],
[new Date("2023-01-02"), 4],
[new Date("2023-01-05"), 3],
[new Date("2023-01-15"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
],
[
[new Date('2023-01-01'), 2],
[new Date('2023-01-02'), 3],
[new Date('2023-01-04'), 4],
[new Date('2023-01-12'), 3],
[new Date('2023-01-28'), 4],
[new Date('2023-02-15'), 3],
[new Date('2023-03-05'), 4],
[new Date("2023-01-01"), 2],
[new Date("2023-01-02"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-12"), 3],
[new Date("2023-01-28"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
],
[
[new Date('2023-01-01'), 4],
[new Date('2023-01-08'), 4],
[new Date('2023-01-24'), 3],
[new Date('2023-02-10'), 4],
[new Date('2023-03-01'), 3],
[new Date("2023-01-01"), 4],
[new Date("2023-01-08"), 4],
[new Date("2023-01-24"), 3],
[new Date("2023-02-10"), 4],
[new Date("2023-03-01"), 3],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-02"), 1],
[new Date("2023-01-03"), 3],
[new Date("2023-01-06"), 4],
[new Date("2023-01-16"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-03"), 3],
[new Date("2023-01-08"), 2],
[new Date("2023-01-10"), 4],
[new Date("2023-01-22"), 3],
[new Date("2023-02-05"), 4],
[new Date("2023-02-25"), 3],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-09"), 3],
[new Date("2023-01-19"), 4],
[new Date("2023-02-05"), 3],
[new Date("2023-02-25"), 4],
],
[
[new Date("2023-01-01"), 2],
[new Date("2023-01-02"), 3],
[new Date("2023-01-05"), 4],
[new Date("2023-01-15"), 3],
[new Date("2023-01-30"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-14"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-01"), 3],
[new Date("2023-01-02"), 1],
[new Date("2023-01-02"), 3],
[new Date("2023-01-03"), 3],
[new Date("2023-01-07"), 3],
[new Date("2023-01-15"), 4],
[new Date("2023-01-31"), 3],
[new Date("2023-02-15"), 4],
[new Date("2023-03-05"), 3],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-10"), 3],
[new Date("2023-01-20"), 4],
[new Date("2023-02-05"), 4],
[new Date("2023-02-25"), 3],
[new Date("2023-03-15"), 4],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-02"), 2],
[new Date("2023-01-03"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-10"), 3],
[new Date("2023-01-20"), 4],
[new Date("2023-02-05"), 3],
[new Date("2023-02-25"), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-05"), 4],
[new Date("2023-01-15"), 3],
[new Date("2023-01-30"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
],
[
[new Date("2023-01-01"), 2],
[new Date("2023-01-03"), 3],
[new Date("2023-01-07"), 4],
[new Date("2023-01-17"), 3],
[new Date("2023-02-01"), 4],
[new Date("2023-02-20"), 3],
[new Date("2023-03-10"), 4],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-12"), 3],
[new Date("2023-01-25"), 4],
[new Date("2023-02-10"), 3],
[new Date("2023-03-01"), 4],
],
// ... add more review history arrays
]
// Cycle and repeat the array to create a larger dataset of 100 cards
const res = []
Expand All @@ -85,7 +179,7 @@ function convertToFSRSItem(history) {


for (const [date, rating] of history) {
const deltaT = (date.getTime() - lastDate.getTime()) / (1000 * 60 * 60 * 24) // Calculate difference in days
const deltaT = dateDiffInDays(lastDate, date)
reviews.push(new FSRSReview(rating, deltaT))
items.push(new FSRSItem([...reviews]))
lastDate = date
Expand All @@ -94,4 +188,13 @@ function convertToFSRSItem(history) {
return items.filter((item) => item.longTermReviewCnt() > 0)
}

function dateDiffInDays(a, b) {
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

return Math.floor((utc2 - utc1) / _MS_PER_DAY)
}

main()

0 comments on commit 9e56b3b

Please sign in to comment.