Remove comma after means in many docstrings
[rgr-org-mode.git] / contrib / lisp / org-learn.el
blob0d45380ab7132a774990bdc8b40f7bfab962dbd1
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)
40 (require 'calendar))
42 (defgroup org-learn nil
43 "Options concerning the learning code in Org-mode."
44 :tag "Org Learn"
45 :group 'org-progress)
47 (defcustom org-learn-always-reschedule nil
48 "If non-nil, always reschedule items, even if retention was \"perfect\"."
49 :type 'boolean
50 :group 'org-learn)
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)."
56 :type 'float
57 :group 'org-learn)
59 (defun initial-optimal-factor (n ef)
60 (if (= 1 n)
62 ef))
64 (defun get-optimal-factor (n ef of-matrix)
65 (let ((factors (assoc n of-matrix)))
66 (or (and factors
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)))
73 (if factors
74 (let ((ef-of (assoc ef (cdr factors))))
75 (if ef-of
76 (setcdr ef-of of)
77 (push (cons ef of) (cdr factors))))
78 (push (cons n (list (cons ef of))) of-matrix)))
79 of-matrix)
81 (defun inter-repetition-interval (n ef &optional of-matrix)
82 (let ((of (get-optimal-factor n ef of-matrix)))
83 (if (= 1 n)
85 (* of (inter-repetition-interval (1- n) ef of-matrix)))))
87 (defun modify-e-factor (ef quality)
88 (if (< ef 1.3)
89 1.3
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
97 old-of fraction)
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
107 of the OF matrix).
109 Returns the newly calculated value of the considered entry of the
110 OF matrix."
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
117 modifier)
118 (if (< mod5 1.05)
119 (setq mod5 1.05))
120 (if (< mod2 0.75)
121 (setq mod5 0.75))
122 (if (> quality 4)
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))
128 (if (> quality 4)
129 (if (< new-of old-of)
130 (setq new-of old-of)))
131 (if (< quality 4)
132 (if (> new-of old-of)
133 (setq new-of old-of)))
134 (setq new-of (+ (* new-of fraction) (* old-of (- 1 fraction))))
135 (if (< new-of 1.2)
136 (setq new-of 1.2)
137 new-of)))
139 (defvar initial-repetition-state '(-1 1 2.5 nil))
141 (defun determine-next-interval (n ef quality of-matrix)
142 (assert (> n 0))
143 (assert (and (>= quality 0) (<= quality 5)))
144 (if (< quality 3)
145 (list (inter-repetition-interval n ef) (1+ n) ef nil)
146 (let ((next-ef (modify-e-factor ef quality)))
147 (setq of-matrix
148 (set-optimal-factor n next-ef of-matrix
149 (modify-of (get-optimal-factor n ef of-matrix)
150 quality org-learn-fraction))
151 ef next-ef)
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)
157 ef of-matrix)))))
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
163 (read learn-str))
164 (copy-list initial-repetition-state)))
165 closed-dates)
166 (setq learn-data
167 (determine-next-interval (nth 1 learn-data)
168 (nth 2 learn-data)
169 quality
170 (nth 3 learn-data)))
171 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data))
172 (if (= 0 (nth 0 learn-data))
173 (org-schedule t)
174 (org-schedule nil (time-add (current-time)
175 (days-to-time (nth 0 learn-data)))))))
177 (provide 'org-learn)
179 ;; arch-tag: a46bb0e5-e4fb-4004-a9b8-63933c55af33
181 ;;; org-learn.el ends here