Permalink
Browse files

Use rosenbrock function to test lbfgs().

  • Loading branch information...
1 parent 354e307 commit 9e80f01f5a787a2aac70ca7f876397e9c19d4340 @arnsholt committed Aug 19, 2011
Showing with 17 additions and 9 deletions.
  1. +17 −9 t/02-lbfgs.t
View
@@ -5,16 +5,24 @@
(in-package :clcrf)
-; A simple function to optimize: x^2 - 3x + y^2 - 4 y + 7
-; Fits the required properties for L-BFGS, minimal value is 7, at (3, 4).
-(defun gradient (args)
- (let ((x (aref args 0))
- (y (aref args 1)))
- (values (+ (* (- x 3) (- x 3)) (* (- y 4) (- y 4)) 7) ; f(x)
- (make-array 2 :initial-contents (list (- (* 2 x) 6) (- (* 2 y) 8)))))) ; g(x)
+; In "Numerical optimization" (Springer, 2006), Nocedal and Wright suggest the
+; Rosenbrock function as a test for an implementation of L-BFGS. Let's do
+; that. The function and its derivatives are:
+; f(x,y) = 100(y - x^2)^2 + (1-x)^2
+; d/dx f = -(400 x(y-x^2) + 2(1-x))
+; d/dy f = 200(y-x^2)
+(defun rosenbrock (x)
+ (let ((x (aref x 0))
+ (y (aref x 1)))
+ (values (+ (* 100 (sq (- y (sq x)))) (sq (- 1 x)))
+ (make-array 2 :initial-contents (list (- (+ (* 400 x (- y (sq x))) (* 2 (- 1 x))))
+ (* 200 (- y (sq x))))))))
+
+(defun sq (x)
+ (* x x))
(test 1
- (lbfgs #'gradient :dimen 2)
+ (lbfgs #'rosenbrock :dimen 2 :initial-x (make-array 2 :initial-contents '(-1.0 -1.0)))
(ok t "stub"))
-; vim: ts=2:sw=2:sts=2:syntax=lisp
+; vim: ts=2:sw=2:sts=2:ft=lisp

0 comments on commit 9e80f01

Please sign in to comment.