EMPIRE DA  v1.9.1
Data assimilation codes using EMPIRE communication
 All Classes Files Functions Variables Pages
lbfgsb_sub.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 ! --------------------------------------------------------------
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 !
210  subroutine lbfgsb_sub(n,factr_in,pgtol_in,x,nbd,l,u)
211 !
212 ! This simple driver demonstrates how to call the L-BFGS-B code to
213 ! solve a sample problem (the extended Rosenbrock function
214 ! subject to bounds on the variables). The dimension n of this
215 ! problem is variable.
216 
217  implicit none
218 !
219 ! Declare variables and parameters needed by the code.
220 ! Note thar we wish to have output at every iteration.
221 ! iprint=1
222 !
223 ! We also specify the tolerances in the stopping criteria.
224 ! factr = 1.0d+7, pgtol = 1.0d-5
225 !
226 ! A description of all these variables is given at the beginning
227 ! of the driver
228 !
229  integer, parameter :: dp = kind(1.0d0)
230  integer, intent(in) :: n
231  real(kind=dp), intent(in) :: factr_in
232  real(kind=dp), intent(in) :: pgtol_in
233  real(kind=dp), dimension(n), intent(inout) :: x
234  integer, dimension(n), intent(in) :: nbd
235  real(kind=dp), dimension(n), intent(in) :: l
236  real(kind=dp), dimension(n), intent(in) :: u
237 
238  integer, parameter :: m = 5, iprint = 1
239 
240  real(dp) :: factr = 1.0d+7, pgtol = 1.0d-5
241 !
242  character(len=60) :: task, csave
243  logical :: lsave(4)
244  integer :: isave(44)
245  real(dp) :: f
246  real(dp) :: dsave(29)
247  integer, allocatable :: iwa(:)
248  real(dp), allocatable :: g(:), wa(:)
249 
250 ! Set convergence parameters
251 
252  factr = factr_in
253  pgtol = pgtol_in
254 
255 
256 ! Allocate dynamic arrays
257 
258  allocate ( g(n) )
259  allocate ( iwa(3*n) )
260  allocate ( wa(2*m*n + 5*n + 11*m*m + 8*m) )
261 !
262 
263  write (6,16)
264  16 format(/,5x, 'Solving sample problem.', &
265  /,5x, ' (f = 0.0 at the optimal solution.)',/)
266 
267 ! We start the iteration by initializing task.
268 
269  task = 'START'
270 
271 ! The beginning of the loop
272 
273  do while(task(1:2).eq.'FG'.or.task.eq.'NEW_X'.or. &
274  task.eq.'START')
275 
276 ! This is the call to the L-BFGS-B code.
277 
278  call setulb( n, m, x, l, u, nbd, f, g, factr, pgtol, &
279  wa, iwa, task, iprint,&
280  csave, lsave, isave, dsave )
281 
282  if (task(1:2) .eq. 'FG') then
283  call fcn(n,x,f,g)
284 ! call objective_function(n,x,f)
285 ! Compute gradient g for the sample problem.
286 ! call objective_gradient(n,x,g)
287  end if
288  end do
289 
290 ! end of loop do while
291 
292 
293  end subroutine lbfgsb_sub
subroutine lbfgsb_sub(n, factr_in, pgtol_in, x, nbd, l, u)
Limited memory BFGS bound constrained optimization code as callable subroutine.
Definition: lbfgsb_sub.f90:210
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