你能帮我看看如何在一个循环中收集一些 NL 表达式吗?我想为所有 i=1:10 保留 k8 和 k9 作为场景。这意味着在循环结束时,Q 等于每个场景下的 k8 和 k9 的集合( i )。我无法定义一个矩阵并将每对 k8 和 k9 作为一个元素放入其中。考虑到 Q 代码也不能正常工作。非常感谢您的热心帮助。
using JuMP,CPUTime, Distributions, Ipopt,Juniper,Cplex
n1=1; #the least of scenarios
N=4; #number of scenarios
M=20; #number of sampling
landa=0.01;
E=0.05
T0=0;
T1=2;
T2=2;
gam2=1; gam1=1;
a1=0.5; a2=0.1; a3=50; ap=25;
c0=10;
Zn=zeros(N, 4)
Q=0;
for i in n1:N
C1=rand(100:100:300);
sig=rand(0.5:0.5:2);
f(x) = cdf(Normal(0, 1), x);
#---------------------------------------------------------------------------
ALT= Model(optimizer_with_attributes(Juniper.Optimizer, "nl_solver"=>optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0),
"mip_solver"=>optimizer_with_attributes(Cplex.Optimizer, "logLevel" => 0),"registered_functions" =>[Juniper.register( :f, 1, f; autodiff = true)])
);
# variables-----------------------------------------------------------------
JuMP.register(ALT, :f, 1, f; autodiff = true);
@variable(ALT, h >= 0.001);
@variable(ALT, L >= 0.000001);
@variable(ALT, n>=2, Int);
#---------------------------------------------------------------------------
@NLexpression(ALT,k1,h/(1-f(L-sig*sqrt(n))+f(-L - sig*sqrt(n)))); # HARL1
@NLexpression(ALT,k2,(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))); #to
@NLexpression(ALT,k3,E*n+T1*gam1+T2*gam2);
@NLexpression(ALT,k8,(C1*(k1-k2+k3)));# depend on scenario
@NLexpression(ALT,k9,(((a1+a2*n)/h)*(k1)));#depend on scenario
Q=Q+k8+k9;
#-----------------------------------------------------------------------
end