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