L-BFGS-B  3.0
Large-scale Bound-constrained Optimization
driver3.f

This time-controlled driver shows that it is possible to terminate a run by elapsed CPU time, and yet be able to print all desired information. This driver also illustrates the use of two stopping criteria that may be used in conjunction with a limit on execution time. The sample problem used here is the same as in driver1 and driver2 (the extended Rosenbrock function with bounds on the variables). (Fortran-77 version)

1 c> \file driver3.f
2 
3 c
4 c L-BFGS-B is released under the “New BSD License” (aka “Modified BSD License”
5 c or “3-clause license”)
6 c Please read attached file License.txt
7 c
8 c DRIVER 3 in Fortran 77
9 c --------------------------------------------------------------
10 c TIME-CONTROLLED DRIVER FOR L-BFGS-B (version 3.0)
11 c --------------------------------------------------------------
12 c
13 c L-BFGS-B is a code for solving large nonlinear optimization
14 c problems with simple bounds on the variables.
15 c
16 c The code can also be used for unconstrained problems and is
17 c as efficient for these problems as the earlier limited memory
18 c code L-BFGS.
19 c
20 c This driver shows how to terminate a run after some prescribed
21 c CPU time has elapsed, and how to print the desired information
22 c before exiting.
23 c
24 c References:
25 c
26 c [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited
27 c memory algorithm for bound constrained optimization'',
28 c SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208.
29 c
30 c [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN
31 c Subroutines for Large Scale Bound Constrained Optimization''
32 c Tech. Report, NAM-11, EECS Department, Northwestern University,
33 c 1994.
34 c
35 c
36 c (Postscript files of these papers are available via anonymous
37 c ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.)
38 c
39 c * * *
40 c
41 c February 2011 (latest revision)
42 c Optimization Center at Northwestern University
43 c Instituto Tecnologico Autonomo de Mexico
44 c
45 c Jorge Nocedal and Jose Luis Morales, Remark on "Algorithm 778:
46 c L-BFGS-B: Fortran Subroutines for Large-Scale Bound Constrained
47 c Optimization" (2011). To appear in ACM Transactions on
48 c Mathematical Software,
49 c
50 c
51 c **************
52 
53  program driver
54 
55 c This time-controlled driver shows that it is possible to terminate
56 c a run by elapsed CPU time, and yet be able to print all desired
57 c information. This driver also illustrates the use of two
58 c stopping criteria that may be used in conjunction with a limit
59 c on execution time. The sample problem used here is the same as in
60 c driver1 and driver2 (the extended Rosenbrock function with bounds
61 c on the variables).
62 
63  integer nmax, mmax
64  parameter(nmax=1024,mmax=17)
65 c nmax is the dimension of the largest problem to be solved.
66 c mmax is the maximum number of limited memory corrections.
67 
68 c Declare the variables needed by the code.
69 c A description of all these variables is given at the end of
70 c driver1.
71 
72  character*60 task, csave
73  logical lsave(4)
74  integer n, m, iprint,
75  + nbd(nmax), iwa(3*nmax), isave(44)
76  double precision f, factr, pgtol,
77  + x(nmax), l(nmax), u(nmax), g(nmax), dsave(29),
78  + wa(2*mmax*nmax+5*nmax+11*mmax*mmax+8*mmax)
79 
80 c Declare a few additional variables for the sample problem
81 c and for keeping track of time.
82 
83  double precision t1, t2, time1, time2, tlimit
84  integer i, j
85 
86 c We specify a limite on the CPU time (in seconds).
87 
88  tlimit = 0.2
89 
90 c We suppress the default output. (The user could also elect to
91 c use the default output by choosing iprint >= 0.)
92 
93  iprint = -1
94 
95 c We suppress the code-supplied stopping tests because we will
96 c provide our own termination conditions
97 
98  factr=0.0d0
99  pgtol=0.0d0
100 
101 c We specify the dimension n of the sample problem and the number
102 c m of limited memory corrections stored. (n and m should not
103 c exceed the limits nmax and mmax respectively.)
104 
105  n=1000
106  m=10
107 
108 c We now specify nbd which defines the bounds on the variables:
109 c l specifies the lower bounds,
110 c u specifies the upper bounds.
111 
112 c First set bounds on the odd-numbered variables.
113 
114  do 10 i=1,n,2
115  nbd(i)=2
116  l(i)=1.0d0
117  u(i)=1.0d2
118  10 continue
119 
120 c Next set bounds on the even-numbered variables.
121 
122  do 12 i=2,n,2
123  nbd(i)=2
124  l(i)=-1.0d2
125  u(i)=1.0d2
126  12 continue
127 
128 c We now define the starting point.
129 
130  do 14 i=1,n
131  x(i)=3.0d0
132  14 continue
133 
134 c We now write the heading of the output.
135 
136  write (6,16)
137  16 format(/,5x, 'Solving sample problem.',
138  + /,5x, ' (f = 0.0 at the optimal solution.)',/)
139 
140 c We start the iteration by initializing task.
141 c
142  task = 'START'
143 
144 c ------- the beginning of the loop ----------
145 
146 c We begin counting the CPU time.
147 
148  call timer(time1)
149 
150  111 continue
151 
152 c This is the call to the L-BFGS-B code.
153 
154  call setulb(n,m,x,l,u,nbd,f,g,factr,pgtol,wa,iwa,task,iprint,
155  + csave,lsave,isave,dsave)
156 
157  if (task(1:2) .eq. 'FG') then
158 c the minimization routine has returned to request the
159 c function f and gradient g values at the current x.
160 c Before evaluating f and g we check the CPU time spent.
161 
162  call timer(time2)
163  if (time2-time1 .gt. tlimit) then
164  task='STOP: CPU EXCEEDING THE TIME LIMIT.'
165 
166 c Note: Assigning task(1:4)='STOP' will terminate the run;
167 c setting task(7:9)='CPU' will restore the information at
168 c the latest iterate generated by the code so that it can
169 c be correctly printed by the driver.
170 
171 c In this driver we have chosen to disable the
172 c printing options of the code (we set iprint=-1);
173 c instead we are using customized output: we print the
174 c latest value of x, the corresponding function value f and
175 c the norm of the projected gradient |proj g|.
176 
177 c We print out the information contained in task.
178 
179  write (6,*) task
180 
181 c We print the latest iterate contained in wa(j+1:j+n), where
182 c
183  j = 3*n+2*m*n+11*m**2
184  write (6,*) 'Latest iterate X ='
185  write (6,'((1x,1p, 6(1x,d11.4)))') (wa(i),i = j+1,j+n)
186 
187 c We print the function value f and the norm of the projected
188 c gradient |proj g| at the last iterate; they are stored in
189 c dsave(2) and dsave(13) respectively.
190 
191  write (6,'(a,1p,d12.5,4x,a,1p,d12.5)')
192  + 'At latest iterate f =',dsave(2),'|proj g| =',dsave(13)
193 
194  else
195 
196 c The time limit has not been reached and we compute
197 c the function value f for the sample problem.
198 
199  f=.25d0*(x(1)-1.d0)**2
200  do 20 i=2,n
201  f=f+(x(i)-x(i-1)**2)**2
202  20 continue
203  f=4.d0*f
204 
205 c Compute gradient g for the sample problem.
206 
207  t1=x(2)-x(1)**2
208  g(1)=2.d0*(x(1)-1.d0)-1.6d1*x(1)*t1
209  do 22 i=2,n-1
210  t2=t1
211  t1=x(i+1)-x(i)**2
212  g(i)=8.d0*t2-1.6d1*x(i)*t1
213  22 continue
214  g(n)=8.d0*t1
215 
216  endif
217 
218 c go back to the minimization routine.
219  goto 111
220  endif
221 c
222  if (task(1:5) .eq. 'NEW_X') then
223 c the minimization routine has returned with a new iterate.
224 c The time limit has not been reached, and we test whether
225 c the following two stopping tests are satisfied:
226 
227 c 1) Terminate if the total number of f and g evaluations
228 c exceeds 900.
229 
230  if (isave(34) .ge. 900)
231  + task='STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT'
232 
233 c 2) Terminate if |proj g|/(1+|f|) < 1.0d-10.
234 
235  if (dsave(13) .le. 1.d-10*(1.0d0 + abs(f)))
236  + task='STOP: THE PROJECTED GRADIENT IS SUFFICIENTLY SMALL'
237 
238 c We wish to print the following information at each iteration:
239 c 1) the current iteration number, isave(30),
240 c 2) the total number of f and g evaluations, isave(34),
241 c 3) the value of the objective function f,
242 c 4) the norm of the projected gradient, dsve(13)
243 c
244 c See the comments at the end of driver1 for a description
245 c of the variables isave and dsave.
246 
247 
248  write (6,'(2(a,i5,4x),a,1p,d12.5,4x,a,1p,d12.5)') 'Iterate'
249  + ,isave(30),'nfg =',isave(34),'f =',f,'|proj g| =',dsave(13)
250 
251 c If the run is to be terminated, we print also the information
252 c contained in task as well as the final value of x.
253 
254 
255  if (task(1:4) .eq. 'STOP') then
256  write (6,*) task
257  write (6,*) 'Final X='
258  write (6,'((1x,1p, 6(1x,d11.4)))') (x(i),i = 1,n)
259  endif
260 
261 c go back to the minimization routine.
262  goto 111
263 
264  endif
265 
266 c ---------- the end of the loop -------------
267 
268 c If task is neither FG nor NEW_X we terminate execution.
269 
270  stop
271 
272  end
273 
274 c======================= The end of driver3 ============================
275 
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...
Definition: setulb.f:190
subroutine timer(ttime)
This routine computes cpu time in double precision.
Definition: timer.f:11