如何在循环中收集NLexpression



你能帮我了解如何在循环中收集一些NL表达式吗?我想保留所有I=1:10的k8和k9作为场景。这意味着在循环结束时,Q等于每个场景(i(下的k8和k9的集合。我无法定义一个矩阵,并将其中的每一对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

您的代码中有几个问题。

  • 为什么每个循环都要创建一个新模型?不能像使用Q=Q+k8+k9那样跨模型聚合表达式
  • 不能添加像Q=Q+k8+k9这样的非线性表达式。所有非线性表达式都必须出现在宏内部

通常,您不限于JuMP的特定语法。您可以使用任何Julia数据结构来提供帮助。在下面的代码中,我只是将表达式推送到vec 中

我还没有测试,所以可能会有打字错误等,但这应该会给你指明正确的方向:

using JuMP, Distributions
E, landa, T1, T2, gam1, gam2, a1, a2 = 0.05, 0.01, 2, 2, 1, 1, 0.5, 0.1
ALT = Model()
f(x) = cdf(Normal(0, 1), x)
JuMP.register(ALT, :f, 1, f; autodiff = true)
@variable(ALT, h >= 0.001)
@variable(ALT, L >= 0.000001)
@variable(ALT, n >= 2, Int)
k8, k9 = Any[], Any[]
for i in 1:4
C1 = rand(100:100:300)
sig = rand(0.5:0.5:2)
k1 = @NLexpression(ALT, h / (1 - f(L - sig * sqrt(n)) + f(-L - sig * sqrt(n))))
k2 = @NLexpression(ALT, (1 - (1 + landa * h) * exp(-landa * h)) / (landa * (1 - exp(-landa * h))))
k3 = @NLexpression(ALT, E * n + T1 * gam1 + T2 * gam2)
push!(k8, @NLexpression(ALT, C1 * (k1 - k2 + k3))
push!(k9, @NLexpression(ALT, k9, (a1 + a2 * n) / h * k1)
end
Q = @NLexpression(ALT, sum(k for k in k8)  + sum(k for k in k9))

相关内容

  • 没有找到相关文章

最新更新