L-BFGS-B  3.0
Large-scale Bound-constrained Optimization
driver1.f90

This simple driver demonstrates how to call the L-BFGS-B code to solve a sample problem (the extended Rosenbrock function subject to bounds on the variables). The dimension n of this problem is variable. (Fortran-90 version)

1 !> \file driver1.f90
2 
3 !
4 ! L-BFGS-B is released under the “New BSD License” (aka “Modified BSD License”
5 ! or “3-clause license”)
6 ! Please read attached file License.txt
7 !
8 !
9 ! DRIVER1 in Fortran 90
10 ! --------------------------------------------------------------
11 !
12 ! L-BFGS-B is a code for solving large nonlinear optimization
13 ! problems with simple bounds on the variables.
14 !
15 ! The code can also be used for unconstrained problems and is
16 ! as efficient for these problems as the earlier limited memory
17 ! code L-BFGS.
18 !
19 ! This is the simplest driver in the package. It uses all the
20 ! default settings of the code.
21 !
22 !
23 ! References:
24 !
25 ! [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited
26 ! memory algorithm for bound constrained optimization'',
27 ! SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208.
28 !
29 ! [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN
30 ! Subroutines for Large Scale Bound Constrained Optimization''
31 ! Tech. Report, NAM-11, EECS Department, Northwestern University,
32 ! 1994.
33 !
34 !
35 ! (Postscript files of these papers are available via anonymous
36 ! ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.)
37 !
38 ! * * *
39 !
40 ! March 2011 (latest revision)
41 ! Optimization Center at Northwestern University
42 ! Instituto Tecnologico Autonomo de Mexico
43 !
44 ! Jorge Nocedal and Jose Luis Morales
45 !
46 ! --------------------------------------------------------------
47 ! DESCRIPTION OF THE VARIABLES IN L-BFGS-B
48 ! --------------------------------------------------------------
49 !
50 ! n is an INTEGER variable that must be set by the user to the
51 ! number of variables. It is not altered by the routine.
52 !
53 ! m is an INTEGER variable that must be set by the user to the
54 ! number of corrections used in the limited memory matrix.
55 ! It is not altered by the routine. Values of m < 3 are
56 ! not recommended, and large values of m can result in excessive
57 ! computing time. The range 3 <= m <= 20 is recommended.
58 !
59 ! x is a DOUBLE PRECISION array of length n. On initial entry
60 ! it must be set by the user to the values of the initial
61 ! estimate of the solution vector. Upon successful exit, it
62 ! contains the values of the variables at the best point
63 ! found (usually an approximate solution).
64 !
65 ! l is a DOUBLE PRECISION array of length n that must be set by
66 ! the user to the values of the lower bounds on the variables. If
67 ! the i-th variable has no lower bound, l(i) need not be defined.
68 !
69 ! u is a DOUBLE PRECISION array of length n that must be set by
70 ! the user to the values of the upper bounds on the variables. If
71 ! the i-th variable has no upper bound, u(i) need not be defined.
72 !
73 ! nbd is an INTEGER array of dimension n that must be set by the
74 ! user to the type of bounds imposed on the variables:
75 ! nbd(i)=0 if x(i) is unbounded,
76 ! 1 if x(i) has only a lower bound,
77 ! 2 if x(i) has both lower and upper bounds,
78 ! 3 if x(i) has only an upper bound.
79 !
80 ! f is a DOUBLE PRECISION variable. If the routine setulb returns
81 ! with task(1:2)= 'FG', then f must be set by the user to
82 ! contain the value of the function at the point x.
83 !
84 ! g is a DOUBLE PRECISION array of length n. If the routine setulb
85 ! returns with taskb(1:2)= 'FG', then g must be set by the user to
86 ! contain the components of the gradient at the point x.
87 !
88 ! factr is a DOUBLE PRECISION variable that must be set by the user.
89 ! It is a tolerance in the termination test for the algorithm.
90 ! The iteration will stop when
91 !
92 ! (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr*epsmch
93 !
94 ! where epsmch is the machine precision which is automatically
95 ! generated by the code. Typical values for factr on a computer
96 ! with 15 digits of accuracy in double precision are:
97 ! factr=1.d+12 for low accuracy;
98 ! 1.d+7 for moderate accuracy;
99 ! 1.d+1 for extremely high accuracy.
100 ! The user can suppress this termination test by setting factr=0.
101 !
102 ! pgtol is a double precision variable.
103 ! On entry pgtol >= 0 is specified by the user. The iteration
104 ! will stop when
105 !
106 ! max{|proj g_i | i = 1, ..., n} <= pgtol
107 !
108 ! where pg_i is the ith component of the projected gradient.
109 ! The user can suppress this termination test by setting pgtol=0.
110 !
111 ! wa is a DOUBLE PRECISION array of length
112 ! (2mmax + 5)nmax + 11mmax^2 + 8mmax used as workspace.
113 ! This array must not be altered by the user.
114 !
115 ! iwa is an INTEGER array of length 3nmax used as
116 ! workspace. This array must not be altered by the user.
117 !
118 ! task is a CHARACTER string of length 60.
119 ! On first entry, it must be set to 'START'.
120 ! On a return with task(1:2)='FG', the user must evaluate the
121 ! function f and gradient g at the returned value of x.
122 ! On a return with task(1:5)='NEW_X', an iteration of the
123 ! algorithm has concluded, and f and g contain f(x) and g(x)
124 ! respectively. The user can decide whether to continue or stop
125 ! the iteration.
126 ! When
127 ! task(1:4)='CONV', the termination test in L-BFGS-B has been
128 ! satisfied;
129 ! task(1:4)='ABNO', the routine has terminated abnormally
130 ! without being able to satisfy the termination conditions,
131 ! x contains the best approximation found,
132 ! f and g contain f(x) and g(x) respectively;
133 ! task(1:5)='ERROR', the routine has detected an error in the
134 ! input parameters;
135 ! On exit with task = 'CONV', 'ABNO' or 'ERROR', the variable task
136 ! contains additional information that the user can print.
137 ! This array should not be altered unless the user wants to
138 ! stop the run for some reason. See driver2 or driver3
139 ! for a detailed explanation on how to stop the run
140 ! by assigning task(1:4)='STOP' in the driver.
141 !
142 ! iprint is an INTEGER variable that must be set by the user.
143 ! It controls the frequency and type of output generated:
144 ! iprint<0 no output is generated;
145 ! iprint=0 print only one line at the last iteration;
146 ! 0<iprint<99 print also f and |proj g| every iprint iterations;
147 ! iprint=99 print details of every iteration except n-vectors;
148 ! iprint=100 print also the changes of active set and final x;
149 ! iprint>100 print details of every iteration including x and g;
150 ! When iprint > 0, the file iterate.dat will be created to
151 ! summarize the iteration.
152 !
153 ! csave is a CHARACTER working array of length 60.
154 !
155 ! lsave is a LOGICAL working array of dimension 4.
156 ! On exit with task = 'NEW_X', the following information is
157 ! available:
158 ! lsave(1) = .true. the initial x did not satisfy the bounds;
159 ! lsave(2) = .true. the problem contains bounds;
160 ! lsave(3) = .true. each variable has upper and lower bounds.
161 !
162 ! isave is an INTEGER working array of dimension 44.
163 ! On exit with task = 'NEW_X', it contains information that
164 ! the user may want to access:
165 ! isave(30) = the current iteration number;
166 ! isave(34) = the total number of function and gradient
167 ! evaluations;
168 ! isave(36) = the number of function value or gradient
169 ! evaluations in the current iteration;
170 ! isave(38) = the number of free variables in the current
171 ! iteration;
172 ! isave(39) = the number of active constraints at the current
173 ! iteration;
174 !
175 ! see the subroutine setulb.f for a description of other
176 ! information contained in isave
177 !
178 ! dsave is a DOUBLE PRECISION working array of dimension 29.
179 ! On exit with task = 'NEW_X', it contains information that
180 ! the user may want to access:
181 ! dsave(2) = the value of f at the previous iteration;
182 ! dsave(5) = the machine precision epsmch generated by the code;
183 ! dsave(13) = the infinity norm of the projected gradient;
184 !
185 ! see the subroutine setulb.f for a description of other
186 ! information contained in dsave
187 !
188 ! --------------------------------------------------------------
189 ! END OF THE DESCRIPTION OF THE VARIABLES IN L-BFGS-B
190 ! --------------------------------------------------------------
191 !
192  program driver
193 !
194 ! This simple driver demonstrates how to call the L-BFGS-B code to
195 ! solve a sample problem (the extended Rosenbrock function
196 ! subject to bounds on the variables). The dimension n of this
197 ! problem is variable.
198 
199  implicit none
200 !
201 ! Declare variables and parameters needed by the code.
202 ! Note thar we wish to have output at every iteration.
203 ! iprint=1
204 !
205 ! We also specify the tolerances in the stopping criteria.
206 ! factr = 1.0d+7, pgtol = 1.0d-5
207 !
208 ! A description of all these variables is given at the beginning
209 ! of the driver
210 !
211  integer, parameter :: n = 25, m = 5, iprint = 1
212  integer, parameter :: dp = kind(1.0d0)
213  real(dp), parameter :: factr = 1.0d+7, pgtol = 1.0d-5
214 !
215  character(len=60) :: task, csave
216  logical :: lsave(4)
217  integer :: isave(44)
218  real(dp) :: f
219  real(dp) :: dsave(29)
220  integer, allocatable :: nbd(:), iwa(:)
221  real(dp), allocatable :: x(:), l(:), u(:), g(:), wa(:)
222 
223 ! Declare a few additional variables for this sample problem
224 
225  real(dp) :: t1, t2
226  integer :: i
227 
228 ! Allocate dynamic arrays
229 
230  allocate ( nbd(n), x(n), l(n), u(n), g(n) )
231  allocate ( iwa(3*n) )
232  allocate ( wa(2*m*n + 11*m*m + 5*n + 8*m) )
233 !
234  do 10 i=1, n, 2
235  nbd(i) = 2
236  l(i) = 1.0d0
237  u(i) = 1.0d2
238  10 continue
239 
240 ! Next set bounds on the even-numbered variables.
241 
242  do 12 i=2, n, 2
243  nbd(i) = 2
244  l(i) = -1.0d2
245  u(i) = 1.0d2
246  12 continue
247 
248 ! We now define the starting point.
249 
250  do 14 i=1, n
251  x(i) = 3.0d0
252  14 continue
253 
254  write (6,16)
255  16 format(/,5x, 'Solving sample problem.', &
256  /,5x, ' (f = 0.0 at the optimal solution.)',/)
257 
258 ! We start the iteration by initializing task.
259 
260  task = 'START'
261 
262 ! The beginning of the loop
263 
264  do while(task(1:2).eq.'FG'.or.task.eq.'NEW_X'.or. &
265  task.eq.'START')
266 
267 ! This is the call to the L-BFGS-B code.
268 
269  call setulb ( n, m, x, l, u, nbd, f, g, factr, pgtol, &
270  wa, iwa, task, iprint,&
271  csave, lsave, isave, dsave )
272 
273  if (task(1:2) .eq. 'FG') then
274 
275  f=.25d0*( x(1)-1.d0 )**2
276  do 20 i=2, n
277  f = f + ( x(i)-x(i-1 )**2 )**2
278  20 continue
279  f = 4.d0*f
280 
281 ! Compute gradient g for the sample problem.
282 
283  t1 = x(2) - x(1)**2
284  g(1) = 2.d0*(x(1) - 1.d0) - 1.6d1*x(1)*t1
285  do 22 i=2, n-1
286  t2 = t1
287  t1 = x(i+1) - x(i)**2
288  g(i) = 8.d0*t2 - 1.6d1*x(i)*t1
289  22 continue
290  g(n) = 8.d0*t1
291 
292  end if
293  end do
294 
295 ! end of loop do while
296 
297 
298  end program driver
299 
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