unimin.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;; 6/1/89 (mh) added EXTREMAL-ARG and EXTREMAL-VALUE
  21. ;;; modified by mh 12/6/87
  22. ;;; $Header: unimin.scm,v 1.4 88/01/26 07:45:38 GMT gjs Exp $
  23. ;;; 7/5/87 UNIMIN.SCM -- first compilation of routines for univariate
  24. ;;; optimization.
  25. #|
  26. (declare (usual-integrations = + - * /
  27. zero? 1+ -1+
  28. ;; truncate round floor ceiling
  29. sqrt exp log sin cos))
  30. |#
  31. (declare (usual-integrations))
  32. ;;; The following univariate optimization routines typically return
  33. ;;; a list (x fx ...) where x is the argmument at which the extremal
  34. ;;; value fx is achieved. The following helps destructure this list.
  35. (define extremal-arg car)
  36. (define extremal-value cadr)
  37. ;;; Golden Section search, with supplied convergence test procedure,
  38. ;;; GOOD-ENUF?, which is a predicate accepting seven arguments: a, minx,
  39. ;;; b, fa, fminx, fb, count.
  40. (define (golden-section-min f a b good-enuf?)
  41. (define g1 (/ (- (sqrt 5) 1) 2))
  42. (define g2 (- 1 g1))
  43. (define (new-left lo-x hi-x)
  44. (+ (* g2 hi-x) (* g1 lo-x)))
  45. (define (new-right lo-x hi-x)
  46. (+ (* g2 lo-x) (* g1 hi-x)))
  47. (define (best-of a b c fa fb fc)
  48. (if (< fa (min fb fc))
  49. (list a fa)
  50. (if (<= fb (min fa fc))
  51. (list b fb)
  52. (list c fc))))
  53. (define (lp x0 x1 x2 x3 f0 f1 f2 f3 count)
  54. (if (< f1 f2)
  55. (if (good-enuf? x0 x1 x2 f0 f1 f2 count)
  56. (append (best-of x0 x1 x2 f0 f1 f2) (list count))
  57. (let ((nx1 (new-left x0 x2)))
  58. (lp x0 nx1 x1 x2 f0 (f nx1) f1 f2 (1+ count))))
  59. (if (good-enuf? x1 x2 x3 f1 f2 f3 count)
  60. (append (best-of x1 x2 x3 f1 f2 f3) (list count))
  61. (let ((nx2 (new-right x1 x3)))
  62. (lp x1 x2 nx2 x3 f1 f2 (f nx2) f3 (1+ count))))))
  63. (let ((x1 (new-left a b)) (x2 (new-right a b)))
  64. (let ((fa (f a)) (fx1 (f x1)) (fx2 (f x2)) (fb (f b)))
  65. (lp a x1 x2 b fa fx1 fx2 fb 0))))
  66. ;;; For convenience, we also provide the sister-procedure for finding
  67. ;;; the maximum of a unimodal function.
  68. (define (golden-section-max f a b good-enuf?)
  69. (let* ((-f (lambda (x) (- (f x))))
  70. (result (golden-section-min -f a b good-enuf?))
  71. (x (car result)) (-fx (cadr result)) (count (caddr result)))
  72. (list x (- -fx) count)))
  73. ;;; The following procedure allows keyword specification of typical
  74. ;;; convergence criteria.
  75. (define (gsmin f a b . params)
  76. (let ((ok?
  77. (if (null? params)
  78. (lambda (a minx b fa fminx fb count)
  79. (close-enuf? (max fa fb) fminx (* 10 *machine-epsilon*)))
  80. (let ((type (car params))
  81. (value (cadr params)))
  82. (cond ((eq? type 'function-tol)
  83. (lambda (a minx b fa fminx fb count)
  84. (close-enuf? (max fa fb) fminx value)))
  85. ((eq? type 'arg-tol)
  86. (lambda (a minx b fa fminx fb count)
  87. (close-enuf? a b value)))
  88. ((eq? type 'count)
  89. (lambda (a minx b fa fminx fb count)
  90. (>= count value))))))))
  91. (golden-section-min f a b ok?)))
  92. (define (gsmax f a b . params)
  93. (let ((ok?
  94. (if (null? params)
  95. (lambda (a minx b fa fminx fb count)
  96. (close-enuf? (max fa fb) fminx (* 10 *machine-epsilon*)))
  97. (let ((type (car params))
  98. (value (cadr params)))
  99. (cond ((eq? type 'function-tol)
  100. (lambda (a minx b fa fminx fb count)
  101. (close-enuf? (max fa fb) fminx value)))
  102. ((eq? type 'arg-tol)
  103. (lambda (a minx b fa fminx fb count)
  104. (close-enuf? a b value)))
  105. ((eq? type 'count)
  106. (lambda (a minx b fa fminx fb count)
  107. (>= count value))))))))
  108. (golden-section-max f a b ok?)))
  109. ;;; Brent's algorithm for univariate minimization -- transcribed from
  110. ;;; pages 79-80 of his book "Algorithms for Minimization Without Derivatives"
  111. (define (brent-min f a b eps)
  112. (let ((a (min a b)) (b (max a b))
  113. (maxcount 100)
  114. (small-bugger-factor *sqrt-machine-epsilon*)
  115. (g (/ (- 3 (sqrt 5)) 2))
  116. (d 0) (e 0) (old-e 0) (p 0) (q 0) (u 0) (fu 0))
  117. (let* ((x (+ a (* g (- b a))))
  118. (fx (f x))
  119. (w x) (fw fx) (v x) (fv fx))
  120. (let loop ((count 0))
  121. (if (> count maxcount)
  122. (list 'maxcount x fx count) ;failed to converge
  123. (let* ((tol (+ (* eps (abs x)) small-bugger-factor))
  124. (2tol (* 2 tol))
  125. (m (/ (+ a b) 2)))
  126. ;; test for convergence
  127. (if (< (max (- x a) (- b x)) 2tol)
  128. (list x fx count)
  129. (begin
  130. (if (> (abs e) tol)
  131. (let* ((t1 (* (- x w) (- fx fv)))
  132. (t2 (* (- x v) (- fx fw)))
  133. (t3 (- (* (- x v) t2) (* (- x w) t1)))
  134. (t4 (* 2 (- t2 t1))))
  135. (set! p (if (positive? t4) (- t3) t3))
  136. (set! q (abs t4))
  137. (set! old-e e)
  138. (set! e d)))
  139. (if (and (< (abs p) (abs (* 0.5 q old-e)))
  140. (> p (* q (- a x)))
  141. (< p (* q (- b x))))
  142. ;; parabolic step
  143. (begin (set! d (/ p q))
  144. (set! u (+ x d))
  145. (if (< (min (- u a) (- b u)) 2tol)
  146. (set! d (if (< x m) tol (- tol)))))
  147. ;;else, golden section step
  148. (begin (set! e (if (< x m) (- b x) (- a x)))
  149. (set! d (* g e))))
  150. (set! u (+ x (if (> (abs d) tol)
  151. d
  152. (if (positive? d) tol (- tol)))))
  153. (set! fu (f u))
  154. (if (<= fu fx)
  155. (begin (if (< u x) (set! b x) (set! a x))
  156. (set! v w) (set! fv fw)
  157. (set! w x) (set! fw fx)
  158. (set! x u) (set! fx fu))
  159. (begin (if (< u x) (set! a u) (set! b u))
  160. (if (or (<= fu fw) (= w x))
  161. (begin (set! v w) (set! fv fw)
  162. (set! w u) (set! fw fu))
  163. (if (or (<= fu fv) (= v x) (= v w))
  164. (begin (set! v u) (set! fv fu))))))
  165. (loop (+ count 1))))))))))
  166. (define (brent-max f a b eps)
  167. (define (-f x) (- (f x)))
  168. (let ((result (brent-min -f a b eps)))
  169. (list (car result) (- (cadr result)) (caddr result))))
  170. ;;; Given a function f, a starting point and a step size, try to bracket
  171. ;;; a local extremum for f. Return a list (retcode a b c fa fb fc iter-count)
  172. ;;; where a < b < c, and fa, fb, fc are the function values at these
  173. ;;; points. In the case of a minimum, fb <= (min fa fc); the opposite
  174. ;;; inequality holds in the case of a maximum. iter-count is the number
  175. ;;; of function evaluations required. retcode is 'okay if the search
  176. ;;; succeeded, or 'maxcount if it was abandoned.
  177. (define (bracket-min f start step max-tries)
  178. (define (reorder a b c fa fb fc) ;return with a < c
  179. (if (< a c)
  180. (list a b c fa fb fc)
  181. (list c b a fc fb fa)))
  182. (define (test-it a b c fa fb fc count) ;assumes b between a and c
  183. (if (> count max-tries)
  184. `(maxcount ,@(reorder a b c fa fb fc) ,max-tries)
  185. (if (<= fb (min fa fc))
  186. `(okay ,@(reorder a b c fa fb fc) ,count)
  187. (let ((d (+ c (- c a))))
  188. (test-it b c d fb fc (f d) (+ count 1))))))
  189. (let ((a start) (b (+ start step)))
  190. (let ((fa (f a)) (fb (f b)))
  191. (if (> fa fb)
  192. (let ((c (+ b (- b a))))
  193. (test-it a b c fa fb (f c) 0))
  194. (let ((c (+ a (- a b))))
  195. (test-it b a c fb fa (f c) 0))))))
  196. (define (bracket-max f start step . max-tries)
  197. (define (-f x) (- (f x)))
  198. (apply bracket-min (append (list -f start step) max-tries)))
  199. ;;; Given a function f on [a, b] and N > 0, examine f at the endpoints
  200. ;;; a, b, and at N equally-separated interior points. From this form a
  201. ;;; list of brackets (p q) in each of which a local maximum is trapped.
  202. ;;; Then apply Golden Section to all these brackets and return a list of
  203. ;;; pairs (x fx) representing the local maxima.
  204. (define (local-maxima f a b n ftol)
  205. (let* ((h (/ (- b a) (+ n 1)))
  206. (xlist (generate-list
  207. (+ n 2)
  208. (lambda (i) (if (= i (+ n 1)) b (+ a (* i h))))))
  209. (flist (map f xlist))
  210. (xi (lambda(i) (list-ref xlist i)))
  211. (fi (lambda(i) (list-ref flist i)))
  212. (brack1 (if (> (fi 0) (fi 1))
  213. (list (list (xi 0) (xi 1)))
  214. '()))
  215. (brack2 (if (> (fi (+ n 1)) (fi n))
  216. (cons (list (xi n) (xi (+ n 1))) brack1)
  217. brack1))
  218. (bracketlist
  219. (let loop ((i 1) (b brack2))
  220. (if (> i n)
  221. b
  222. (if (and (<= (fi (- i 1)) (fi i))
  223. (>= (fi i) (fi (+ i 1))))
  224. (loop (+ i 1) (cons (list (xi (- i 1))
  225. (xi (+ i 1))) b))
  226. (loop (+ i 1) b)))))
  227. (locmax (lambda (int) (gsmax f (car int) (cadr int)
  228. 'function-tol ftol))))
  229. (map locmax bracketlist)))
  230. (define (local-minima f a b n ftol)
  231. (let* ((g (lambda (x) (- (f x))))
  232. (result (local-maxima g a b n ftol))
  233. (flip (lambda (r) (list (car r) (- (cadr r)) (caddr r)))))
  234. (map flip result)))
  235. (define (estimate-global-max f a b n ftol)
  236. (let ((local-maxs (local-maxima f a b n ftol)))
  237. (let loop ((best-so-far (car local-maxs))
  238. (unexamined (cdr local-maxs)))
  239. (if (null? unexamined)
  240. best-so-far
  241. (let ((next (car unexamined)))
  242. (if (> (cadr next) (cadr best-so-far))
  243. (loop next (cdr unexamined))
  244. (loop best-so-far (cdr unexamined))))))))
  245. (define (estimate-global-min f a b n ftol)
  246. (let ((local-mins (local-minima f a b n ftol)))
  247. (let loop ((best-so-far (car local-mins))
  248. (unexamined (cdr local-mins)))
  249. (if (null? unexamined)
  250. best-so-far
  251. (let ((next (car unexamined)))
  252. (if (< (cadr next) (cadr best-so-far))
  253. (loop next (cdr unexamined))
  254. (loop best-so-far (cdr unexamined))))))))