EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
lbfgs_sub.f90
Go to the documentation of this file.
1 !
2 ! L-BFGS-B is released under the “New BSD License” (aka “Modified
3 ! BSD License”
4 ! or “3-clause license”)
5 ! Please read attached file License.txt
6 !
7 !
8 ! DRIVER1 in Fortran 90
9 ! --------------------------------------------------------------
47 ! --------------------------------------------------------------
48 ! DESCRIPTION OF THE VARIABLES IN L-BFGS-B
49 ! --------------------------------------------------------------
50 !
51 ! n is an INTEGER variable that must be set by the user to the
52 ! number of variables. It is not altered by the routine.
53 !
54 ! m is an INTEGER variable that must be set by the user to the
55 ! number of corrections used in the limited memory matrix.
56 ! It is not altered by the routine. Values of m < 3 are
57 ! not recommended, and large values of m can result in excessive
58 ! computing time. The range 3 <= m <= 20 is recommended.
59 !
60 ! x is a DOUBLE PRECISION array of length n. On initial entry
61 ! it must be set by the user to the values of the initial
62 ! estimate of the solution vector. Upon successful exit, it
63 ! contains the values of the variables at the best point
64 ! found (usually an approximate solution).
65 !
66 ! l is a DOUBLE PRECISION array of length n that must be set by
67 ! the user to the values of the lower bounds on the variables. If
68 ! the i-th variable has no lower bound, l(i) need not be defined.
69 !
70 ! u is a DOUBLE PRECISION array of length n that must be set by
71 ! the user to the values of the upper bounds on the variables. If
72 ! the i-th variable has no upper bound, u(i) need not be defined.
73 !
74 ! nbd is an INTEGER array of dimension n that must be set by the
75 ! user to the type of bounds imposed on the variables:
76 ! nbd(i)=0 if x(i) is unbounded,
77 ! 1 if x(i) has only a lower bound,
78 ! 2 if x(i) has both lower and upper bounds,
79 ! 3 if x(i) has only an upper bound.
80 !
81 ! f is a DOUBLE PRECISION variable. If the routine setulb returns
82 ! with task(1:2)= 'FG', then f must be set by the user to
83 ! contain the value of the function at the point x.
84 !
85 ! g is a DOUBLE PRECISION array of length n. If the routine setulb
86 ! returns with taskb(1:2)= 'FG', then g must be set by the user to
87 ! contain the components of the gradient at the point x.
88 !
89 ! factr is a DOUBLE PRECISION variable that must be set by the user.
90 ! It is a tolerance in the termination test for the algorithm.
91 ! The iteration will stop when
92 !
93 ! (f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr*epsmch
94 !
95 ! where epsmch is the machine precision which is automatically
96 ! generated by the code. Typical values for factr on a computer
97 ! with 15 digits of accuracy in double precision are:
98 ! factr=1.d+12 for low accuracy;
99 ! 1.d+7 for moderate accuracy;
100 ! 1.d+1 for extremely high accuracy.
101 ! The user can suppress this termination test by setting factr=0.
102 !
103 ! pgtol is a double precision variable.
104 ! On entry pgtol >= 0 is specified by the user. The iteration
105 ! will stop when
106 !
107 ! max{|proj g_i | i = 1, ..., n} <= pgtol
108 !
109 ! where pg_i is the ith component of the projected gradient.
110 ! The user can suppress this termination test by setting pgtol=0.
111 !
112 ! wa is a DOUBLE PRECISION array of length
113 ! (2mmax + 5)nmax + 11mmax^2 + 8mmax used as workspace.
114 ! This array must not be altered by the user.
115 !
116 ! iwa is an INTEGER array of length 3nmax used as
117 ! workspace. This array must not be altered by the user.
118 !
119 ! task is a CHARACTER string of length 60.
120 ! On first entry, it must be set to 'START'.
121 ! On a return with task(1:2)='FG', the user must evaluate the
122 ! function f and gradient g at the returned value of x.
123 ! On a return with task(1:5)='NEW_X', an iteration of the
124 ! algorithm has concluded, and f and g contain f(x) and g(x)
125 ! respectively. The user can decide whether to continue or stop
126 ! the iteration.
127 ! When
128 ! task(1:4)='CONV', the termination test in L-BFGS-B has been
129 ! satisfied;
130 ! task(1:4)='ABNO', the routine has terminated abnormally
131 ! without being able to satisfy the termination conditions,
132 ! x contains the best approximation found,
133 ! f and g contain f(x) and g(x) respectively;
134 ! task(1:5)='ERROR', the routine has detected an error in the
135 ! input parameters;
136 ! On exit with task = 'CONV', 'ABNO' or 'ERROR', the variable task
137 ! contains additional information that the user can print.
138 ! This array should not be altered unless the user wants to
139 ! stop the run for some reason. See driver2 or driver3
140 ! for a detailed explanation on how to stop the run
141 ! by assigning task(1:4)='STOP' in the driver.
142 !
143 ! iprint is an INTEGER variable that must be set by the user.
144 ! It controls the frequency and type of output generated:
145 ! iprint<0 no output is generated;
146 ! iprint=0 print only one line at the last iteration;
147 ! 0<iprint<99 print also f and |proj g| every iprint iterations;
148 ! iprint=99 print details of every iteration except n-vectors;
149 ! iprint=100 print also the changes of active set and final x;
150 ! iprint>100 print details of every iteration including x and g;
151 ! When iprint > 0, the file iterate.dat will be created to
152 ! summarize the iteration.
153 !
154 ! csave is a CHARACTER working array of length 60.
155 !
156 ! lsave is a LOGICAL working array of dimension 4.
157 ! On exit with task = 'NEW_X', the following information is
158 ! available:
159 ! lsave(1) = .true. the initial x did not satisfy the bounds;
160 ! lsave(2) = .true. the problem contains bounds;
161 ! lsave(3) = .true. each variable has upper and lower bounds.
162 !
163 ! isave is an INTEGER working array of dimension 44.
164 ! On exit with task = 'NEW_X', it contains information that
165 ! the user may want to access:
166 ! isave(30) = the current iteration number;
167 ! isave(34) = the total number of function and gradient
168 ! evaluations;
169 ! isave(36) = the number of function value or gradient
170 ! evaluations in the current iteration;
171 ! isave(38) = the number of free variables in the current
172 ! iteration;
173 ! isave(39) = the number of active constraints at the current
174 ! iteration;
175 !
176 ! see the subroutine setulb.f for a description of other
177 ! information contained in isave
178 !
179 ! dsave is a DOUBLE PRECISION working array of dimension 29.
180 ! On exit with task = 'NEW_X', it contains information that
181 ! the user may want to access:
182 ! dsave(2) = the value of f at the previous iteration;
183 ! dsave(5) = the machine precision epsmch generated by the code;
184 ! dsave(13) = the infinity norm of the projected gradient;
185 !
186 ! see the subroutine setulb.f for a description of other
187 ! information contained in dsave
188 !
189 ! --------------------------------------------------------------
190 ! END OF THE DESCRIPTION OF THE VARIABLES IN L-BFGS-B
191 ! --------------------------------------------------------------
192 !
198  subroutine lbfgs_sub(n,factr_in,pgtol_in,x)
199 !
200 ! This simple driver demonstrates how to call the L-BFGS-B code to
201 ! solve a sample problem (the extended Rosenbrock function
202 ! subject to bounds on the variables). The dimension n of this
203 ! problem is variable.
204 
205  implicit none
206 !
207 ! Declare variables and parameters needed by the code.
208 ! Note thar we wish to have output at every iteration.
209 ! iprint=1
210 !
211 ! We also specify the tolerances in the stopping criteria.
212 ! factr = 1.0d+7, pgtol = 1.0d-5
213 !
214 ! A description of all these variables is given at the beginning
215 ! of the driver
216 !
217  integer, parameter :: dp = kind(1.0d0)
218  integer, intent(in) :: n
219  real(kind=dp), intent(in) :: factr_in
220  real(kind=dp), intent(in) :: pgtol_in
221  real(kind=dp), dimension(n), intent(inout) :: x
222 
223  integer, parameter :: m = 5, iprint = 1
224 
225  real(dp) :: factr = 1.0d+7, pgtol = 1.0d-5
226 !
227  character(len=60) :: task, csave
228  logical :: lsave(4)
229  integer :: isave(44)
230  real(dp) :: f
231  real(dp) :: dsave(29)
232  integer, allocatable :: nbd(:), iwa(:)
233  real(dp), allocatable :: l(:), u(:), g(:), wa(:)
234 
235 ! Set convergence parameters
236 
237  factr = factr_in
238  pgtol = pgtol_in
239 
240 
241 ! Allocate dynamic arrays
242 
243  allocate ( nbd(n), l(n), u(n), g(n) )
244  allocate ( iwa(3*n) )
245  allocate ( wa(2*m*n + 5*n + 11*m*m + 8*m) )
246 !
247 
248  !there are no constraints so set all entries of nbd = 0
249  nbd = 0
250 
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 
266 ! This is the call to the L-BFGS-B code.
267 
268  call setulb( n, m, x, l, u, nbd, f, g, factr, pgtol, &
269  wa, iwa, task, iprint,&
270  csave, lsave, isave, dsave )
271 
272  if (task(1:2) .eq. 'FG') then
273  call fcn(n,x,f,g)
274 ! call objective_function(n,x,f)
275 ! Compute gradient g for the sample problem.
276 ! call objective_gradient(n,x,g)
277  end if
278  end do
279 
280 ! end of loop do while
281 
282 
283  end subroutine lbfgs_sub
284 
subroutine fcn(n, x, f, g)
This is the subroutine which the optimization routines call to get the objective function value and i...
Definition: 4denvar_fcn.f90:31
subroutine lbfgs_sub(n, factr_in, pgtol_in, x)
Limited memory BFGS unconstrained optimization code as callable subroutine.
Definition: lbfgs_sub.f90:198