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
41 (defgroup org-learn nil
42 "Options concerning the learning code in Org-mode."
46 (defcustom org-learn-always-reschedule nil
47 "If non-nil, always reschedule items, even if retention was \"perfect\"."
51 (defcustom org-learn-fraction
0.5
52 "Controls the rate at which EF is increased or decreased.
53 Must be a number between 0 and 1 (the greater it is the faster
54 the changes of the OF matrix)."
58 (defun initial-optimal-factor (n ef
)
63 (defun get-optimal-factor (n ef of-matrix
)
64 (let ((factors (assoc n of-matrix
)))
66 (let ((ef-of (assoc ef
(cdr factors
))))
67 (and ef-of
(cdr ef-of
))))
68 (initial-optimal-factor n ef
))))
70 (defun set-optimal-factor (n ef of-matrix of
)
71 (let ((factors (assoc n of-matrix
)))
73 (let ((ef-of (assoc ef
(cdr factors
))))
76 (push (cons ef of
) (cdr factors
))))
77 (push (cons n
(list (cons ef of
))) of-matrix
)))
80 (defun inter-repetition-interval (n ef
&optional of-matrix
)
81 (let ((of (get-optimal-factor n ef of-matrix
)))
84 (* of
(inter-repetition-interval (1- n
) ef of-matrix
)))))
86 (defun modify-e-factor (ef quality
)
89 (+ ef
(- 0.1 (* (- 5 quality
) (+ 0.08 (* (- 5 quality
) 0.02)))))))
91 (defun modify-of (of q fraction
)
92 (let ((temp (* of
(+ 0.72 (* q
0.07)))))
93 (+ (* (- 1 fraction
) of
) (* fraction temp
))))
95 (defun calculate-new-optimal-factor (interval-used quality used-of
97 "This implements the SM-5 learning algorithm in Lisp.
98 INTERVAL-USED is the last interval used for the item in question.
99 QUALITY is the quality of the repetition response.
100 USED-OF is the optimal factor used in calculation of the last
101 interval used for the item in question.
102 OLD-OF is the previous value of the OF entry corresponding to the
103 relevant repetition number and the E-Factor of the item.
104 FRACTION is a number belonging to the range (0,1) determining the
105 rate of modifications (the greater it is the faster the changes
108 Returns the newly calculated value of the considered entry of the
110 (let (;; the value proposed for the modifier in case of q=5
111 (mod5 (/ (1+ interval-used
) interval-used
))
112 ;; the value proposed for the modifier in case of q=2
113 (mod2 (/ (1- interval-used
) interval-used
))
114 ;; the number determining how many times the OF value will
115 ;; increase or decrease
122 (setq modifier
(1+ (* (- mod5
1) (- quality
4))))
123 (setq modifier
(- 1 (* (/ (- 1 mod2
) 2) (- 4 quality
)))))
124 (if (< modifier
0.05)
125 (setq modifier
0.05))
126 (setq new-of
(* used-of modifier
))
128 (if (< new-of old-of
)
129 (setq new-of old-of
)))
131 (if (> new-of old-of
)
132 (setq new-of old-of
)))
133 (setq new-of
(+ (* new-of fraction
) (* old-of
(- 1 fraction
))))
138 (defvar initial-repetition-state
'(-1 1 2.5 nil
))
140 (defun determine-next-interval (n ef quality of-matrix
)
142 (assert (and (>= quality
0) (<= quality
5)))
144 (list (inter-repetition-interval n ef
) (1+ n
) ef nil
)
145 (let ((next-ef (modify-e-factor ef quality
)))
147 (set-optimal-factor n next-ef of-matrix
148 (modify-of (get-optimal-factor n ef of-matrix
)
149 quality org-learn-fraction
))
151 ;; For a zero-based quality of 4 or 5, don't repeat
152 (if (and (>= quality
4)
153 (not org-learn-always-reschedule
))
154 (list 0 (1+ n
) ef of-matrix
)
155 (list (inter-repetition-interval n ef of-matrix
) (1+ n
)
158 (defun org-smart-reschedule (quality)
159 (interactive "nHow well did you remember the information (on a scale of 0-5)? ")
160 (let* ((learn-str (org-entry-get (point) "LEARN_DATA"))
161 (learn-data (or (and learn-str
163 (copy-list initial-repetition-state
)))
166 (determine-next-interval (nth 1 learn-data
)
170 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data
))
171 (if (= 0 (nth 0 learn-data
))
173 (org-schedule nil
(time-add (current-time)
174 (days-to-time (nth 0 learn-data
)))))))
178 ;; arch-tag: a46bb0e5-e4fb-4004-a9b8-63933c55af33
180 ;;; org-learn.el ends here