EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
driver2.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 ! DRIVER 2 in Fortran 90
8 ! --------------------------------------------------------------
9 ! CUSTOMIZED DRIVER FOR L-BFGS-B
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 driver illustrates how to control the termination of the
20 ! run and how to design customized output.
21 !
22 ! References:
23 !
24 ! [1] R. H. Byrd, P. Lu, J. Nocedal and C. Zhu, ``A limited
25 ! memory algorithm for bound constrained optimization'',
26 ! SIAM J. Scientific Computing 16 (1995), no. 5, pp. 1190--1208.
27 !
28 ! [2] C. Zhu, R.H. Byrd, P. Lu, J. Nocedal, ``L-BFGS-B: FORTRAN
29 ! Subroutines for Large Scale Bound Constrained Optimization''
30 ! Tech. Report, NAM-11, EECS Department, Northwestern University,
31 ! 1994.
32 !
33 !
34 ! (Postscript files of these papers are available via anonymous
35 ! ftp to eecs.nwu.edu in the directory pub/lbfgs/lbfgs_bcm.)
36 !
37 ! * * *
38 !
39 ! February 2011 (latest revision)
40 ! Optimization Center at Northwestern University
41 ! Instituto Tecnologico Autonomo de Mexico
42 !
43 ! Jorge Nocedal and Jose Luis Morales
44 !
45 ! **************
46  program driver
47 
48 ! This driver shows how to replace the default stopping test
49 ! by other termination criteria. It also illustrates how to
50 ! print the values of several parameters during the course of
51 ! the iteration. The sample problem used here is the same as in
52 ! DRIVER1 (the extended Rosenbrock function with bounds on the
53 ! variables).
54 
55  implicit none
56 
57 ! Declare variables and parameters needed by the code.
58 !
59 ! Note that we suppress the default output (iprint = -1)
60 ! We suppress both code-supplied stopping tests because the
61 ! user is providing his/her own stopping criteria.
62 
63  integer, parameter :: n = 25, m = 5, iprint = -1
64  integer, parameter :: dp = kind(1.0d0)
65  real(dp), parameter :: factr = 0.0d0, pgtol = 0.0d0
66 
67  character(len=60) :: task, csave
68  logical :: lsave(4)
69  integer :: isave(44)
70  real(dp) :: f
71  real(dp) :: dsave(29)
72  integer, allocatable :: nbd(:), iwa(:)
73  real(dp), allocatable :: x(:), l(:), u(:), g(:), wa(:)
74 !
75  real(dp) :: t1, t2
76  integer :: i
77 
78  allocate ( nbd(n), x(n), l(n), u(n), g(n) )
79  allocate ( iwa(3*n) )
80  allocate ( wa(2*m*n + 5*n + 11*m*m + 8*m) )
81 !
82 ! This driver shows how to replace the default stopping test
83 ! by other termination criteria. It also illustrates how to
84 ! print the values of several parameters during the course of
85 ! the iteration. The sample problem used here is the same as in
86 ! DRIVER1 (the extended Rosenbrock function with bounds on the
87 ! variables).
88 ! We now specify nbd which defines the bounds on the variables:
89 ! l specifies the lower bounds,
90 ! u specifies the upper bounds.
91 
92 ! First set bounds on the odd numbered variables.
93 
94  do 10 i=1, n,2
95  nbd(i)=2
96  l(i)=1.0d0
97  u(i)=1.0d2
98  10 continue
99 
100 ! Next set bounds on the even numbered variables.
101 
102  do 12 i=2, n,2
103  nbd(i)=2
104  l(i)=-1.0d2
105  u(i)=1.0d2
106  12 continue
107 
108 ! We now define the starting point.
109 
110  do 14 i=1, n
111  x(i)=3.0d0
112  14 continue
113 
114 ! We now write the heading of the output.
115 
116  write (6,16)
117  16 format(/,5x, 'Solving sample problem.', &
118  /,5x, ' (f = 0.0 at the optimal solution.)',/)
119 
120 
121 ! We start the iteration by initializing task.
122 !
123  task = 'START'
124 
125 ! ------- the beginning of the loop ----------
126 
127  do while( task(1:2).eq.'FG'.or.task.eq.'NEW_X'.or. &
128  task.eq.'START')
129 
130 ! This is the call to the L-BFGS-B code.
131 
132  call setulb(n,m,x,l,u,nbd,f,g,factr,pgtol,wa,iwa,task,iprint, &
133  csave,lsave,isave,dsave)
134 
135  if (task(1:2) .eq. 'FG') then
136 
137 ! the minimization routine has returned to request the
138 ! function f and gradient g values at the current x.
139 
140 ! Compute function value f for the sample problem.
141 
142  f =.25d0*(x(1) - 1.d0)**2
143  do 20 i=2,n
144  f = f + (x(i) - x(i-1)**2)**2
145  20 continue
146  f = 4.d0*f
147 
148 ! Compute gradient g for the sample problem.
149 
150  t1 = x(2) - x(1)**2
151  g(1) = 2.d0*(x(1) - 1.d0) - 1.6d1*x(1)*t1
152  do 22 i= 2,n-1
153  t2 = t1
154  t1 = x(i+1) - x(i)**2
155  g(i) = 8.d0*t2 - 1.6d1*x(i)*t1
156  22 continue
157  g(n)=8.d0*t1
158 !
159  else
160 !
161  if (task(1:5) .eq. 'NEW_X') then
162 !
163 ! the minimization routine has returned with a new iterate.
164 ! At this point have the opportunity of stopping the iteration
165 ! or observing the values of certain parameters
166 !
167 ! First are two examples of stopping tests.
168 
169 ! Note: task(1:4) must be assigned the value 'STOP' to terminate
170 ! the iteration and ensure that the final results are
171 ! printed in the default format. The rest of the character
172 ! string TASK may be used to store other information.
173 
174 ! 1) Terminate if the total number of f and g evaluations
175 ! exceeds 99.
176 
177  if (isave(34) .ge. 99) &
178  task='STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT'
179 
180 ! 2) Terminate if |proj g|/(1+|f|) < 1.0d-10, where
181 ! "proj g" denoted the projected gradient
182 
183  if (dsave(13) .le. 1.d-10*(1.0d0 + abs(f))) &
184  task='STOP: THE PROJECTED GRADIENT IS SUFFICIENTLY SMALL'
185 
186 ! We now wish to print the following information at each
187 ! iteration:
188 !
189 ! 1) the current iteration number, isave(30),
190 ! 2) the total number of f and g evaluations, isave(34),
191 ! 3) the value of the objective function f,
192 ! 4) the norm of the projected gradient, dsve(13)
193 !
194 ! See the comments at the end of driver1 for a description
195 ! of the variables isave and dsave.
196 
197  write (6,'(2(a,i5,4x),a,1p,d12.5,4x,a,1p,d12.5)') 'Iterate' &
198  , isave(30),'nfg =',isave(34),'f =',f,'|proj g| =',dsave(13)
199 
200 ! If the run is to be terminated, we print also the information
201 ! contained in task as well as the final value of x.
202 
203  if (task(1:4) .eq. 'STOP') then
204  write (6,*) task
205  write (6,*) 'Final X='
206  write (6,'((1x,1p, 6(1x,d11.4)))') (x(i),i = 1,n)
207  end if
208 
209  end if
210  end if
211 
212  end do
213 ! ---------- the end of the loop -------------
214 
215 ! If task is neither FG nor NEW_X we terminate execution.
216 
217  end program driver
218 
219 !======================= The end of driver2 ============================
220 
program driver
Definition: driver1.f90:190