Hello,
I am trying to run the basic tutorial from the web page, works fine in
C. My workhorse code is f90, so I rebuilt this in f90 as below. But the
result is not as expected:
found minimum at f(********************) = +Infinity
The debugger (gdb) seems to indicate that the args passed into
nlopt_minimize_constrained are indeed the same for C and f90, bar for
fc_datum_size which it finds 8, but should be 16. This seems to stem
from line 60 in F77, where sizeof(f77_func_data) is passed as the datum
difference, while we should see sth evaluating to fc_second_datum - fc_data.
However, this does not seem to be the issue, as my func and constr
routines are never called. Unfortunately, the gdb debugger seems
confused about the mixed languages and does not allow me to trace the
f90 version into the case statement in nlopt_minimize_.
Any pointers what to look at? Should this work?
Many thanks, Jens.
program test_f
include 'nlopt.f'
integer :: algorithm, n
real(8) :: x(2)
real(8) :: f_data(4) ! void, for now.
real(8) :: fc_data(4)
real(8) :: lb(2), ub(2)
real(8) :: fMin
real(8) :: xTol_abs(2)
integer :: have_xTol_abs
external func, constr
algorithm = NLOPT_LD_MMA
n = 2
lb(1) = -HUGE_VAL
lb(2) = 0
lb(1) = HUGE_VAL
lb(2) = HUGE_VAL
have_xTol_abs = 0
fc_data(1) = 2
fc_data(2) = 0
fc_data(3) = 2
fc_data(4) = 1
x(1) = 1.234
x(2) = 5.678
call nloptc(info, algorithm, n, func, f_data, 2, constr, fc_data(1), &
fc_data(3), lb, ub, x, fMin, -HUGE_VAL, 0.d0, 0.d0, 1.d-4, &
xtol_abs, have_xtol_abs, 0, 0.d0)
write (*,'(A,2E10.6,A,E10.6)'), "found minimum at f(",x(1),x(2) &
,") = ",fMin
stop
end program test_f
subroutine func (val, n, x, grad, need_gradient, f_data)
implicit none
real(8), intent(out) :: val
integer, intent(in) :: n
real(8), dimension(n), intent(in) ::x
real(8), dimension(n), intent(out) :: grad
integer, intent(in) :: need_gradient
real(8), dimension(:), intent(in) :: f_data
if ( need_gradient .eq. 1 ) then
grad(1) = 0.d0
grad(2) = 0.5d0 / sqrt(x(2));
end if
val = sqrt( x(2) )
write (*,'(A,2F7.4,A,F7.4,A,2F7.4)') &
" eval func for x=(",x(1),", ",x(2),", val=",val, &
", grad=(",grad(1),", ",grad(2)
return
end subroutine func
! nonlinear constraints
subroutine constr(val, n, x, grad, need_gradient, fc_datum)
real(8), intent(out) :: val
integer, intent(in) :: n
real(8), dimension(n), intent(in) ::x
real(8), dimension(n), intent(out) :: grad
integer, intent(in) :: need_gradient
real(8), dimension(:), intent(in) :: fc_datum
real(8) :: a,b
if ( need_gradient .eq. 1 ) then
a = fc_datum(1)
b = fc_datum(2)
grad(1) = 3 * a * (a*x(1) + b)**2
grad(2) = -1.0
end if
val = (a*x(1) + b)**3 - x(2)
write (*,'(A,2F7.4,A,F7.4,A,2F7.4)') &
" eval constr for x=(",x(1),", ",x(2),", val=",val, &
", grad=(",grad(1),", ",grad(2)
return
end subroutine constr
-----------------------------------------------------
Dr. Jens-Dominik Müller
School of Engineering and Materials Science
Mile End Road
Queen Mary, University of London
London, E1 4NS
tel: (+44) (0) 20 7882 5421, fax: (+44) (0) 20 7882 5532
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss