org-learn.el 5.9 KB

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