This driver shows how to replace the default stopping test by other termination criteria. It also illustrates how to print the values of several parameters during the course of the iteration. The sample problem used here is the same as in DRIVER1 (the extended Rosenbrock function with bounds on the variables). (Fortran-90 version)
49 use json_writer,
only: json_write_aggregate
66 integer,
parameter :: n = 25, m = 5, iprint = -1
67 integer,
parameter :: dp = kind(1.0d0)
68 real(dp),
parameter :: factr = 0.0d0, pgtol = 0.0d0
70 character(len=60) :: task, csave
75 integer,
allocatable :: nbd(:), iwa(:)
76 real(dp),
allocatable :: x(:), l(:), u(:), g(:), wa(:)
84 character(len=512) :: lbfgsb_json
85 character(len=64) :: env_nfg_limit
86 logical :: json_active
89 allocate ( nbd(n), x(n), l(n), u(n), g(n) )
91 allocate ( wa(2*m*n + 5*n + 11*m*m + 8*m) )
128 16
format(/,5x,
'Solving sample problem.', &
129 /,5x,
' (f = 0.0 at the optimal solution.)',/)
137 call get_environment_variable(
'LBFGSB_JSON_OUTPUT', lbfgsb_json)
138 json_active = (len_trim(lbfgsb_json) > 0)
140 call get_environment_variable(
'LBFGSB_NFG_LIMIT', env_nfg_limit)
141 if (len_trim(env_nfg_limit) > 0)
read(env_nfg_limit, *) nfg_limit
145 do while( task(1:2).eq.
'FG'.or.task.eq.
'NEW_X'.or. &
150 call setulb(n,m,x,l,u,nbd,f,g,factr,pgtol,wa,iwa,task,iprint, &
151 csave,lsave,isave,dsave)
153 if (task(1:2) .eq.
'FG')
then
160 f =.25d0*(x(1) - 1.d0)**2
162 f = f + (x(i) - x(i-1)**2)**2
169 g(1) = 2.d0*(x(1) - 1.d0) - 1.6d1*x(1)*t1
172 t1 = x(i+1) - x(i)**2
173 g(i) = 8.d0*t2 - 1.6d1*x(i)*t1
179 if (task(1:5) .eq.
'NEW_X')
then
195 if (isave(34) .ge. nfg_limit) &
196 task=
'STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT'
201 if (dsave(13) .le. 1.d-10*(1.0d0 + abs(f))) &
202 task=
'STOP: THE PROJECTED GRADIENT IS SUFFICIENTLY SMALL'
215 write (6,
'(2(a,i5,4x),a,1p,d12.5,4x,a,1p,d12.5)')
'Iterate'
216 , isave(30),
'nfg =',isave(34),
'f =',f,
'|proj g| =',dsave(
221 if (task(1:4) .eq.
'STOP')
then
223 write (6,*)
'Final X='
224 write (6,
'((1x,1p, 6(1x,d11.4)))') (x(i),i = 1,n)
236 call json_write_aggregate(trim(lbfgsb_json), task, f, dsave(13)
subroutine setulb(n, m, x, l, u, nbd, f, g, factr, pgtol, wa, iwa, task, iprint, csave, lsave, isave, dsave)
This subroutine partitions the working arrays wa and iwa, and then uses the limited memory BFGS metho...