是否可以在 julia 中使用非线性函数 ifor objective?
is it possible use non-linear function ifor objective in julia?
如何为 objective 和约束使用非线性函数。例如发生了一些函数错误
#-----Model parameters--------------------------------------------------------
sig=0.86;
landa=50;
E=T0=T1=.0833;
T2=0.75;
gam2=1; gam1=0;
a1=5; a2=4.22; a3=977.4; ap=977.4;
C1=949.2; c0=114.24;
#---------------------------------------------------------------------------
ALT=Model(solver=IpoptSolver());
# variables-----------------------------------------------------------------
f(x) = cdf(Normal(0, 1), x);
JuMP.register(ALT, :f, 1, f; autodiff = true);
@variable(ALT, h >= 0);
@variable(ALT, L >= 0);
@variable(ALT, n, Int);
#-----------------------------------------------------------------------------
k1(x)=h/(1-f(L-sig*sqrt(n))+f(-L - sig*sqrt(n)));
k2(x)=(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)));
k3(x)=E*n+T1*gam1+T2*gam2;
k4(x)=1/landa+h/(1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n)));
k5(x)=-(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))+E*n+T1*gam1+T2*gam2;
k6(x)=(exp(-landa*h)/1-exp(-landa*h))*(a3/(2*f(-L)))+ap;
k7(x)=1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n));
F(x)=c0/landa+C1*(k1(x)-k2(x)+k3(x))+((a1+a2*n)/h)*(k4(x)+k5(x)+k3(x))+k6(x);
FF(x)=k4(x)-k2(x)+E*n+T1+T2+(1-gam1)*((exp(-landa*h)/1-exp(-landa*h)*T0)/(2*f(-L)));
#objective function---------------------------------------------------------
f1(x)=F(x)/FF(x);
f2(x)=1/k7(x);
#-------------------------------------------------------------------------------------------------------
@NLconstraint(ALT,rf1,f1(x)<=1000000000000);
@NLconstraint(ALT,lf1,f1(x)>=-1000000000000);
#------------------------------------------------------------------------------------------------------
@NLobjective(ALT, Min, f1(x));
status=solve(ALT);
#-------------------------------------------------------------------------------------------------------
但是这个错误发生了
ERROR: Unrecognized function "f1" used in nonlinear expression.
如何消除这个错误?你能帮帮我吗?非常感谢。
您需要 register
@NLconstraint
和 @NLobjective
中使用的所有函数。
如何为 objective 和约束使用非线性函数。例如发生了一些函数错误
#-----Model parameters--------------------------------------------------------
sig=0.86;
landa=50;
E=T0=T1=.0833;
T2=0.75;
gam2=1; gam1=0;
a1=5; a2=4.22; a3=977.4; ap=977.4;
C1=949.2; c0=114.24;
#---------------------------------------------------------------------------
ALT=Model(solver=IpoptSolver());
# variables-----------------------------------------------------------------
f(x) = cdf(Normal(0, 1), x);
JuMP.register(ALT, :f, 1, f; autodiff = true);
@variable(ALT, h >= 0);
@variable(ALT, L >= 0);
@variable(ALT, n, Int);
#-----------------------------------------------------------------------------
k1(x)=h/(1-f(L-sig*sqrt(n))+f(-L - sig*sqrt(n)));
k2(x)=(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)));
k3(x)=E*n+T1*gam1+T2*gam2;
k4(x)=1/landa+h/(1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n)));
k5(x)=-(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))+E*n+T1*gam1+T2*gam2;
k6(x)=(exp(-landa*h)/1-exp(-landa*h))*(a3/(2*f(-L)))+ap;
k7(x)=1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n));
F(x)=c0/landa+C1*(k1(x)-k2(x)+k3(x))+((a1+a2*n)/h)*(k4(x)+k5(x)+k3(x))+k6(x);
FF(x)=k4(x)-k2(x)+E*n+T1+T2+(1-gam1)*((exp(-landa*h)/1-exp(-landa*h)*T0)/(2*f(-L)));
#objective function---------------------------------------------------------
f1(x)=F(x)/FF(x);
f2(x)=1/k7(x);
#-------------------------------------------------------------------------------------------------------
@NLconstraint(ALT,rf1,f1(x)<=1000000000000);
@NLconstraint(ALT,lf1,f1(x)>=-1000000000000);
#------------------------------------------------------------------------------------------------------
@NLobjective(ALT, Min, f1(x));
status=solve(ALT);
#-------------------------------------------------------------------------------------------------------
但是这个错误发生了
ERROR: Unrecognized function "f1" used in nonlinear expression.
如何消除这个错误?你能帮帮我吗?非常感谢。
您需要 register
@NLconstraint
和 @NLobjective
中使用的所有函数。