The Elk Code
minf_nm.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2007 J. K. Dewhurst and D. W. H. Rankin.
3 ! This file is distributed under the terms of the GNU General Public License.
4 ! See the file COPYING for license details.
5 
6 subroutine minf_nm(id,rd,n,x,maxit,eps)
7 implicit none
8 ! arguments
9 integer, intent(in) :: id(*)
10 real(8), intent(in) :: rd(*)
11 integer, intent(in) :: n
12 real(8), intent(inout) :: x(n,n+1)
13 integer, intent(in) :: maxit
14 real(8), intent(in) :: eps
15 ! local variables
16 integer it,il,iu,i,j
17 ! Nelder-Mead parmeters
18 real(8), parameter :: alpha=1.d0,gamma=2.d0
19 real(8), parameter :: beta=0.5d0,sigma=0.5d0
20 real(8) fr,fe,fc,sm
21 ! automatic arrays
22 real(8) f(n+1),xm(n),xr(n),xe(n),xc(n)
23 ! external functions
24 real(8), external :: fmin_nm
25 if (n < 1) then
26  write(*,*)
27  write(*,'("Error(minf_nm): n < 1 : ",I0)') n
28  write(*,*)
29  stop
30 end if
31 ! evaluate the function at each vertex
32 do i=1,n+1
33  f(i)=fmin_nm(id,rd,x(:,i))
34 end do
35 ! begin iteration loop
36 do it=1,maxit
37 ! find the lowest and highest vertex
38  il=minloc(f(1:n+1),1)
39  iu=maxloc(f(1:n+1),1)
40 ! check for convergence
41  if ((f(iu)-f(il)) < eps) return
42 ! compute the mean of the n lowest vertices
43  do i=1,n
44  sm=sum(x(i,1:iu-1))+sum(x(i,iu+1:n+1))
45  xm(i)=sm/n
46  end do
47  xr(1:n)=xm(1:n)+alpha*(xm(1:n)-x(1:n,iu))
48  fr=fmin_nm(id,rd,xr)
49  if (f(il) < fr) then
50  if (fr < f(iu)) then
51 ! reflection
52  x(1:n,iu)=xr(1:n)
53  f(iu)=fr
54  else
55  xc(1:n)=xm(1:n)+beta*(x(1:n,iu)-xm(1:n))
56  fc=fmin_nm(id,rd,xc)
57  if (fc < f(iu)) then
58 ! contraction
59  x(1:n,iu)=xc(1:n)
60  f(iu)=fc
61  else
62 ! shrinkage
63  do j=1,n+1
64  if (j == il) cycle
65  x(1:n,j)=x(1:n,il)+sigma*(x(1:n,j)-x(1:n,il))
66  f(j)=fmin_nm(id,rd,x(1,j))
67  end do
68  end if
69  end if
70  else
71  xe(1:n)=xm(1:n)+gamma*(xr(1:n)-xm(1:n))
72  fe=fmin_nm(id,rd,xe)
73  if (fr > fe) then
74 ! expansion
75  x(1:n,iu)=xe(1:n)
76  f(iu)=fe
77  else
78 ! reflection
79  x(1:n,iu)=xr(1:n)
80  f(iu)=fr
81  end if
82  end if
83 end do
84 end subroutine
85 
subroutine minf_nm(id, rd, n, x, maxit, eps)
Definition: minf_nm.f90:7