org-learn.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; org-learn.el --- Implements SuperMemo's incremental learning algorithm
  2. ;; Copyright (C) 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: John Wiegley <johnw at gnu dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.32trans
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; The file implements the learning algorithm described at
  25. ;; http://supermemo.com/english/ol/sm5.htm, which is a system for reading
  26. ;; material according to "spaced repetition". See
  27. ;; http://en.wikipedia.org/wiki/Spaced_repetition for more details.
  28. ;;
  29. ;; To use, turn on state logging and schedule some piece of information you
  30. ;; want to read. Then in the agenda buffer type
  31. (require 'org)
  32. (eval-when-compile
  33. (require 'cl))
  34. (defgroup org-learn nil
  35. "Options concerning the learning code in Org-mode."
  36. :tag "Org Learn"
  37. :group 'org-progress)
  38. (defcustom org-learn-always-reschedule nil
  39. "If non-nil, always reschedule items, even if retention was \"perfect\"."
  40. :type 'boolean
  41. :group 'org-learn)
  42. (defcustom org-learn-fraction 0.5
  43. "Controls the rate at which EF is increased or decreased.
  44. Must be a number between 0 and 1 (the greater it is the faster
  45. the changes of the OF matrix)."
  46. :type 'float
  47. :group 'org-learn)
  48. (defun initial-optimal-factor (n ef)
  49. (if (= 1 n)
  50. 4
  51. ef))
  52. (defun get-optimal-factor (n ef of-matrix)
  53. (let ((factors (assoc n of-matrix)))
  54. (or (and factors
  55. (let ((ef-of (assoc ef (cdr factors))))
  56. (and ef-of (cdr ef-of))))
  57. (initial-optimal-factor n ef))))
  58. (defun set-optimal-factor (n ef of-matrix of)
  59. (let ((factors (assoc n of-matrix)))
  60. (if factors
  61. (let ((ef-of (assoc ef (cdr factors))))
  62. (if ef-of
  63. (setcdr ef-of of)
  64. (push (cons ef of) (cdr factors))))
  65. (push (cons n (list (cons ef of))) of-matrix)))
  66. of-matrix)
  67. (defun inter-repetition-interval (n ef &optional of-matrix)
  68. (let ((of (get-optimal-factor n ef of-matrix)))
  69. (if (= 1 n)
  70. of
  71. (* of (inter-repetition-interval (1- n) ef of-matrix)))))
  72. (defun modify-e-factor (ef quality)
  73. (if (< ef 1.3)
  74. 1.3
  75. (+ ef (- 0.1 (* (- 5 quality) (+ 0.08 (* (- 5 quality) 0.02)))))))
  76. (defun modify-of (of q fraction)
  77. (let ((temp (* of (+ 0.72 (* q 0.07)))))
  78. (+ (* (- 1 fraction) of) (* fraction temp))))
  79. (defun calculate-new-optimal-factor (interval-used quality used-of
  80. old-of fraction)
  81. "This implements the SM-5 learning algorithm in Lisp.
  82. INTERVAL-USED is the last interval used for the item in question.
  83. QUALITY is the quality of the repetition response.
  84. USED-OF is the optimal factor used in calculation of the last
  85. interval used for the item in question.
  86. OLD-OF is the previous value of the OF entry corresponding to the
  87. relevant repetition number and the E-Factor of the item.
  88. FRACTION is a number belonging to the range (0,1) determining the
  89. rate of modifications (the greater it is the faster the changes
  90. of the OF matrix).
  91. Returns the newly calculated value of the considered entry of the
  92. OF matrix."
  93. (let (;; the value proposed for the modifier in case of q=5
  94. (mod5 (/ (1+ interval-used) interval-used))
  95. ;; the value proposed for the modifier in case of q=2
  96. (mod2 (/ (1- interval-used) interval-used))
  97. ;; the number determining how many times the OF value will
  98. ;; increase or decrease
  99. modifier)
  100. (if (< mod5 1.05)
  101. (setq mod5 1.05))
  102. (if (< mod2 0.75)
  103. (setq mod5 0.75))
  104. (if (> quality 4)
  105. (setq modifier (1+ (* (- mod5 1) (- quality 4))))
  106. (setq modifier (- 1 (* (/ (- 1 mod2) 2) (- 4 quality)))))
  107. (if (< modifier 0.05)
  108. (setq modifier 0.05))
  109. (setq new-of (* used-of modifier))
  110. (if (> quality 4)
  111. (if (< new-of old-of)
  112. (setq new-of old-of)))
  113. (if (< quality 4)
  114. (if (> new-of old-of)
  115. (setq new-of old-of)))
  116. (setq new-of (+ (* new-of fraction) (* old-of (- 1 fraction))))
  117. (if (< new-of 1.2)
  118. (setq new-of 1.2)
  119. new-of)))
  120. (defvar initial-repetition-state '(-1 1 2.5 nil))
  121. (defun determine-next-interval (n ef quality of-matrix)
  122. (assert (> n 0))
  123. (assert (and (>= quality 0) (<= quality 5)))
  124. (if (< quality 3)
  125. (list (inter-repetition-interval n ef) (1+ n) ef nil)
  126. (let ((next-ef (modify-e-factor ef quality)))
  127. (setq of-matrix
  128. (set-optimal-factor n next-ef of-matrix
  129. (modify-of (get-optimal-factor n ef of-matrix)
  130. quality org-learn-fraction))
  131. ef next-ef)
  132. ;; For a zero-based quality of 4 or 5, don't repeat
  133. (if (and (>= quality 4)
  134. (not org-learn-always-reschedule))
  135. (list 0 (1+ n) ef of-matrix)
  136. (list (inter-repetition-interval n ef of-matrix) (1+ n)
  137. ef of-matrix)))))
  138. (defun org-smart-reschedule (quality)
  139. (interactive "nHow well did you remember the information (on a scale of 0-5)? ")
  140. (let* ((learn-str (org-entry-get (point) "LEARN_DATA"))
  141. (learn-data (or (and learn-str
  142. (read learn-str))
  143. (copy-list initial-repetition-state)))
  144. closed-dates)
  145. (setq learn-data
  146. (determine-next-interval (nth 1 learn-data)
  147. (nth 2 learn-data)
  148. quality
  149. (nth 3 learn-data)))
  150. (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data))
  151. (if (= 0 (nth 0 learn-data))
  152. (org-schedule t)
  153. (org-schedule nil (time-add (current-time)
  154. (days-to-time (nth 0 learn-data)))))))
  155. (provide 'org-learn)
  156. ;; arch-tag: a46bb0e5-e4fb-4004-a9b8-63933c55af33
  157. ;;; org-learn.el ends here