Fix tiny spelling mistake.
[org-mode.git] / contrib / lisp / org-learn.el
blob10780018e4176f37c37cdd8795d7cc8112ed6c25
1 ;;; org-learn.el --- Implements SuperMemo's incremental learning algorithm
3 ;; Copyright (C) 2009
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
9 ;; Version: 6.32trans
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
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
37 (require 'org)
38 (eval-when-compile
39 (require 'cl))
41 (defgroup org-learn nil
42 "Options concerning the learning code in Org-mode."
43 :tag "Org Learn"
44 :group 'org-progress)
46 (defcustom org-learn-always-reschedule nil
47 "If non-nil, always reschedule items, even if retention was \"perfect\"."
48 :type 'boolean
49 :group 'org-learn)
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)."
55 :type 'float
56 :group 'org-learn)
58 (defun initial-optimal-factor (n ef)
59 (if (= 1 n)
61 ef))
63 (defun get-optimal-factor (n ef of-matrix)
64 (let ((factors (assoc n of-matrix)))
65 (or (and factors
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)))
72 (if factors
73 (let ((ef-of (assoc ef (cdr factors))))
74 (if ef-of
75 (setcdr ef-of of)
76 (push (cons ef of) (cdr factors))))
77 (push (cons n (list (cons ef of))) of-matrix)))
78 of-matrix)
80 (defun inter-repetition-interval (n ef &optional of-matrix)
81 (let ((of (get-optimal-factor n ef of-matrix)))
82 (if (= 1 n)
84 (* of (inter-repetition-interval (1- n) ef of-matrix)))))
86 (defun modify-e-factor (ef quality)
87 (if (< ef 1.3)
88 1.3
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
96 old-of fraction)
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
106 of the OF matrix).
108 Returns the newly calculated value of the considered entry of the
109 OF matrix."
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
116 modifier)
117 (if (< mod5 1.05)
118 (setq mod5 1.05))
119 (if (< mod2 0.75)
120 (setq mod5 0.75))
121 (if (> quality 4)
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))
127 (if (> quality 4)
128 (if (< new-of old-of)
129 (setq new-of old-of)))
130 (if (< quality 4)
131 (if (> new-of old-of)
132 (setq new-of old-of)))
133 (setq new-of (+ (* new-of fraction) (* old-of (- 1 fraction))))
134 (if (< new-of 1.2)
135 (setq new-of 1.2)
136 new-of)))
138 (defvar initial-repetition-state '(-1 1 2.5 nil))
140 (defun determine-next-interval (n ef quality of-matrix)
141 (assert (> n 0))
142 (assert (and (>= quality 0) (<= quality 5)))
143 (if (< quality 3)
144 (list (inter-repetition-interval n ef) (1+ n) ef nil)
145 (let ((next-ef (modify-e-factor ef quality)))
146 (setq of-matrix
147 (set-optimal-factor n next-ef of-matrix
148 (modify-of (get-optimal-factor n ef of-matrix)
149 quality org-learn-fraction))
150 ef next-ef)
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)
156 ef of-matrix)))))
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
162 (read learn-str))
163 (copy-list initial-repetition-state)))
164 closed-dates)
165 (setq learn-data
166 (determine-next-interval (nth 1 learn-data)
167 (nth 2 learn-data)
168 quality
169 (nth 3 learn-data)))
170 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data))
171 (if (= 0 (nth 0 learn-data))
172 (org-schedule t)
173 (org-schedule nil (time-add (current-time)
174 (days-to-time (nth 0 learn-data)))))))
176 (provide 'org-learn)
178 ;; arch-tag: a46bb0e5-e4fb-4004-a9b8-63933c55af33
180 ;;; org-learn.el ends here