问题描述
在 Fortran 中尝试使用 ifort
编译时遇到一些奇怪的错误。我的代码使用 nlopt
库,并且我是从 MacOS Big Sur 编写的。
已经尝试使用 gfortran
代替 ifort
,但在 GNU 编译中安装 nlopt
也有困难。
代码很长,但我使用nlopt
库的模块和子程序如下:
subroutine nlopt_stat_fcn(ff,nv,x,gradient,need_gradient,param)
use globvar,only: dp
use model
implicit none
integer :: nv,need_gradient
real(dp) :: x(nv),FF,gradient(nv)
real(dp) :: param(2) ! bounds
ff = statopt(x(1)) ! negative of payoff
if (need_gradient.ne.0) then
print*,'nlopt algorithm expects gradient,mistake'
endif
end subroutine nlopt_stat_fcn
subroutine nlopt_stat(n,x0,x1,res,ires,step,tol,algo,param)
! n is dimensionality of problem (number of unkNowns x)
! x0 is guess IN
! x1 is solution OUT
! res is the minimized function value OUT
use globvar,only: dp
implicit none
include 'nlopt.f'
external nlopt_stat_fcn
integer ires,n,algo
real(dp),intent(in) :: x0(n)
real(dp),intent(out) :: x1(n),res
real(dp) :: x(n),abstol(n),reltol(n),step(n),tol
real(dp) :: param(2),xlo(n),xhi(n)
integer opt,local_opt
xlo = param(1)
xhi = param(2)
abstol = tol !/2.0_dp
reltol = tol
! algorithm and stopping
if (algo==1) call nlo_create(opt,nlopt_ld_slsqp,n)
if (algo==2) call nlo_create(opt,nlopt_ln_cobyla,n)
if (algo==3) call nlo_create(opt,nlopt_ld_auglag,n)
if (algo==4) call nlo_create(opt,nlopt_gn_isres,n)
if (algo==5) call nlo_create(opt,nlopt_ld_lbfgs,n)
if (algo==6) call nlo_create(opt,nlopt_ln_bobyqa,n)
if (algo==7) call nlo_create(opt,nlopt_ln_neldermead,n)
if (algo==8) call nlo_create(opt,nlopt_ln_sbplx,n)
call nlo_set_xtol_rel(ires,opt,reltol)
call nlo_set_xtol_abs(ires,abstol)
call nlo_set_ftol_rel(ires,reltol)
call nlo_set_ftol_abs(ires,abstol)
call nlo_set_maxeval(ires,10000)
! objective function
! parameters must be passed as declared variables
call nlo_set_min_objective(ires,nlopt_stat_fcn,param)
! bounds
! *never* violated during optimization (constraints can be)
call nlo_set_lower_bounds(ires,xlo)
call nlo_set_upper_bounds(ires,xhi)
call nlo_set_initial_step(ires,step)
! call solver
x = x0
call nlo_optimize(ires,res)
x1 = x
call nlo_destroy(opt)
if (algo==3) call nlo_destroy(local_opt)
end subroutine nlopt_stat
! ------------------------------------------------------------------
! nlopt dynamic problem
! ------------------------------------------------------------------
subroutine nlopt_fcn(ff,gradient(nv)
real(dp) :: param(2) ! bounds
ff = dynopt(x(1)) ! negative of payoff
if (need_gradient.ne.0) then
print*,mistake'
endif
end subroutine nlopt_fcn
subroutine nlopt(n,only: dp
implicit none
include 'nlopt.f'
external nlopt_fcn
integer ires,nlopt_fcn,res)
x1 = x
call nlo_destroy(opt)
if (algo==3) call nlo_destroy(local_opt)
end subroutine nlopt
这给了我以下错误:
victoralexandrino@MacBook-Pro-de-Victor-3:~/Google Drive/PhD Insper/Thesis/Paper 1 - Sovereign Default Holdings/Read/Sunder-Plassmann (2020) - Paper,Codes and Data/research_data$ ifort *.f90 -L/Library/Developer/CommandLinetools/SDKs/MacOSX.sdk/usr/lib
Undefined symbols for architecture x86_64:
"_nlo_create_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_destroy_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_optimize_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_ftol_abs_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_ftol_rel_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_initial_step_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_lower_bounds_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_maxeval_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_min_objective_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_upper_bounds_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_xtol_abs_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
"_nlo_set_xtol_rel_",referenced from:
_model_mp_solve_dynamic_problem_ in ifortTeFs8c.o
_model_mp_solve_static_problem_ in ifortTeFs8c.o
_nlopt_stat_ in ifortTeFs8c.o
_nlopt_ in ifortTeFs8c.o
ld: symbol(s) not found for architecture x86_64
非常感谢您的帮助:)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)