1 ;;; org-learn.el --- Implements SuperMemo's incremental learning algorithm
4 ;; Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw at gnu dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; The file implements the learning algorithm described at
30 ;; http://supermemo.com/english/ol/sm5.htm, which is a system for reading
31 ;; material according to "spaced repetition". See
32 ;; http://en.wikipedia.org/wiki/Spaced_repetition for more details.
34 ;; To use, turn on state logging and schedule some piece of information you
35 ;; want to read. Then in the agenda buffer type
42 (defgroup org-learn nil
43 "Options concerning the learning code in Org-mode."
47 (defcustom org-learn-always-reschedule nil
48 "If non-nil, always reschedule items, even if retention was \"perfect\"."
52 (defcustom org-learn-fraction
0.5
53 "Controls the rate at which EF is increased or decreased.
54 Must be a number between 0 and 1 (the greater it is the faster
55 the changes of the OF matrix)."
59 (defun initial-optimal-factor (n ef
)
64 (defun get-optimal-factor (n ef of-matrix
)
65 (let ((factors (assoc n of-matrix
)))
67 (let ((ef-of (assoc ef
(cdr factors
))))
68 (and ef-of
(cdr ef-of
))))
69 (initial-optimal-factor n ef
))))
71 (defun set-optimal-factor (n ef of-matrix of
)
72 (let ((factors (assoc n of-matrix
)))
74 (let ((ef-of (assoc ef
(cdr factors
))))
77 (push (cons ef of
) (cdr factors
))))
78 (push (cons n
(list (cons ef of
))) of-matrix
)))
81 (defun inter-repetition-interval (n ef
&optional of-matrix
)
82 (let ((of (get-optimal-factor n ef of-matrix
)))
85 (* of
(inter-repetition-interval (1- n
) ef of-matrix
)))))
87 (defun modify-e-factor (ef quality
)
90 (+ ef
(- 0.1 (* (- 5 quality
) (+ 0.08 (* (- 5 quality
) 0.02)))))))
92 (defun modify-of (of q fraction
)
93 (let ((temp (* of
(+ 0.72 (* q
0.07)))))
94 (+ (* (- 1 fraction
) of
) (* fraction temp
))))
96 (defun calculate-new-optimal-factor (interval-used quality used-of
98 "This implements the SM-5 learning algorithm in Lisp.
99 INTERVAL-USED is the last interval used for the item in question.
100 QUALITY is the quality of the repetition response.
101 USED-OF is the optimal factor used in calculation of the last
102 interval used for the item in question.
103 OLD-OF is the previous value of the OF entry corresponding to the
104 relevant repetition number and the E-Factor of the item.
105 FRACTION is a number belonging to the range (0,1) determining the
106 rate of modifications (the greater it is the faster the changes
109 Returns the newly calculated value of the considered entry of the
111 (let (;; the value proposed for the modifier in case of q=5
112 (mod5 (/ (1+ interval-used
) interval-used
))
113 ;; the value proposed for the modifier in case of q=2
114 (mod2 (/ (1- interval-used
) interval-used
))
115 ;; the number determining how many times the OF value will
116 ;; increase or decrease
123 (setq modifier
(1+ (* (- mod5
1) (- quality
4))))
124 (setq modifier
(- 1 (* (/ (- 1 mod2
) 2) (- 4 quality
)))))
125 (if (< modifier
0.05)
126 (setq modifier
0.05))
127 (setq new-of
(* used-of modifier
))
129 (if (< new-of old-of
)
130 (setq new-of old-of
)))
132 (if (> new-of old-of
)
133 (setq new-of old-of
)))
134 (setq new-of
(+ (* new-of fraction
) (* old-of
(- 1 fraction
))))
139 (defvar initial-repetition-state
'(-1 1 2.5 nil
))
141 (defun determine-next-interval (n ef quality of-matrix
)
143 (assert (and (>= quality
0) (<= quality
5)))
145 (list (inter-repetition-interval n ef
) (1+ n
) ef nil
)
146 (let ((next-ef (modify-e-factor ef quality
)))
148 (set-optimal-factor n next-ef of-matrix
149 (modify-of (get-optimal-factor n ef of-matrix
)
150 quality org-learn-fraction
))
152 ;; For a zero-based quality of 4 or 5, don't repeat
153 (if (and (>= quality
4)
154 (not org-learn-always-reschedule
))
155 (list 0 (1+ n
) ef of-matrix
)
156 (list (inter-repetition-interval n ef of-matrix
) (1+ n
)
159 (defun org-smart-reschedule (quality)
160 (interactive "nHow well did you remember the information (on a scale of 0-5)? ")
161 (let* ((learn-str (org-entry-get (point) "LEARN_DATA"))
162 (learn-data (or (and learn-str
164 (copy-list initial-repetition-state
)))
167 (determine-next-interval (nth 1 learn-data
)
171 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data
))
172 (if (= 0 (nth 0 learn-data
))
174 (org-schedule nil
(time-add (current-time)
175 (days-to-time (nth 0 learn-data
)))))))
179 ;; arch-tag: a46bb0e5-e4fb-4004-a9b8-63933c55af33
181 ;;; org-learn.el ends here