1 ;;; org-learn.el --- Implements SuperMemo's incremental learning algorithm
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw at gnu dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is not part of GNU Emacs.
12 ;; This program is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;; The file implements the learning algorithm described at
29 ;; http://supermemo.com/english/ol/sm5.htm, which is a system for reading
30 ;; material according to "spaced repetition". See
31 ;; http://en.wikipedia.org/wiki/Spaced_repetition for more details.
33 ;; To use, turn on state logging and schedule some piece of information you
34 ;; want to read. Then in the agenda buffer type
40 (defgroup org-learn nil
41 "Options concerning the learning code in Org-mode."
45 (defcustom org-learn-always-reschedule nil
46 "If non-nil, always reschedule items, even if retention was \"perfect\"."
50 (defcustom org-learn-fraction
0.5
51 "Controls the rate at which EF is increased or decreased.
52 Must be a number between 0 and 1 (the greater it is the faster
53 the changes of the OF matrix)."
57 (defun initial-optimal-factor (n ef
)
62 (defun get-optimal-factor (n ef of-matrix
)
63 (let ((factors (assoc n of-matrix
)))
65 (let ((ef-of (assoc ef
(cdr factors
))))
66 (and ef-of
(cdr ef-of
))))
67 (initial-optimal-factor n ef
))))
69 (defun set-optimal-factor (n ef of-matrix of
)
70 (let ((factors (assoc n of-matrix
)))
72 (let ((ef-of (assoc ef
(cdr factors
))))
75 (push (cons ef of
) (cdr factors
))))
76 (push (cons n
(list (cons ef of
))) of-matrix
)))
79 (defun inter-repetition-interval (n ef
&optional of-matrix
)
80 (let ((of (get-optimal-factor n ef of-matrix
)))
83 (* of
(inter-repetition-interval (1- n
) ef of-matrix
)))))
85 (defun modify-e-factor (ef quality
)
88 (+ ef
(- 0.1 (* (- 5 quality
) (+ 0.08 (* (- 5 quality
) 0.02)))))))
90 (defun modify-of (of q fraction
)
91 (let ((temp (* of
(+ 0.72 (* q
0.07)))))
92 (+ (* (- 1 fraction
) of
) (* fraction temp
))))
94 (defun calculate-new-optimal-factor (interval-used quality used-of
96 "This implements the SM-5 learning algorithm in Lisp.
97 INTERVAL-USED is the last interval used for the item in question.
98 QUALITY is the quality of the repetition response.
99 USED-OF is the optimal factor used in calculation of the last
100 interval used for the item in question.
101 OLD-OF is the previous value of the OF entry corresponding to the
102 relevant repetition number and the E-Factor of the item.
103 FRACTION is a number belonging to the range (0,1) determining the
104 rate of modifications (the greater it is the faster the changes
107 Returns the newly calculated value of the considered entry of the
109 (let (;; the value proposed for the modifier in case of q=5
110 (mod5 (/ (1+ interval-used
) interval-used
))
111 ;; the value proposed for the modifier in case of q=2
112 (mod2 (/ (1- interval-used
) interval-used
))
113 ;; the number determining how many times the OF value will
114 ;; increase or decrease
121 (setq modifier
(1+ (* (- mod5
1) (- quality
4))))
122 (setq modifier
(- 1 (* (/ (- 1 mod2
) 2) (- 4 quality
)))))
123 (if (< modifier
0.05)
124 (setq modifier
0.05))
125 (setq new-of
(* used-of modifier
))
127 (if (< new-of old-of
)
128 (setq new-of old-of
)))
130 (if (> new-of old-of
)
131 (setq new-of old-of
)))
132 (setq new-of
(+ (* new-of fraction
) (* old-of
(- 1 fraction
))))
137 (defvar initial-repetition-state
'(-1 1 2.5 nil
))
139 (defun determine-next-interval (n ef quality of-matrix
)
141 (assert (and (>= quality
0) (<= quality
5)))
143 (list (inter-repetition-interval n ef
) (1+ n
) ef nil
)
144 (let ((next-ef (modify-e-factor ef quality
)))
146 (set-optimal-factor n next-ef of-matrix
147 (modify-of (get-optimal-factor n ef of-matrix
)
148 quality org-learn-fraction
))
150 ;; For a zero-based quality of 4 or 5, don't repeat
151 (if (and (>= quality
4)
152 (not org-learn-always-reschedule
))
153 (list 0 (1+ n
) ef of-matrix
)
154 (list (inter-repetition-interval n ef of-matrix
) (1+ n
)
157 (defun org-smart-reschedule (quality)
158 (interactive "nHow well did you remember the information (on a scale of 0-5)? ")
159 (let* ((learn-str (org-entry-get (point) "LEARN_DATA"))
160 (learn-data (or (and learn-str
162 (copy-list initial-repetition-state
)))
165 (determine-next-interval (nth 1 learn-data
)
169 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data
))
170 (if (= 0 (nth 0 learn-data
))
172 (org-schedule nil
(time-add (current-time)
173 (days-to-time (nth 0 learn-data
)))))))
177 ;;; org-learn.el ends here