using Cbc,JuMP,LinearAlgebra, Juniper,Ipopt,HiGHS;
optimizer = Juniper.Optimizer;
nl_solver = optimizer_with_attributes(Ipopt.Optimizer);
mip_solver = optimizer_with_attributes(HiGHS.Optimizer);
mod2 = Model(optimizer_with_attributes(optimizer, "nl_solver"=>nl_solver,
"mip_solver"=>mip_solver));
@variable(mod2,Tnk[h in 1:3,
i in 1:14])
@variable(mod2, 0.0 <= x[h in 1:5,
i in 1:3,
j in 1:2,
k in 1:1,
l in 1:2] <=19999);
mctc=[718.3492, 0.0237787, -6.845167e-7, 1.028082e-11, -6.545553000000001e-17, 1.472553e-22, 0.0, 0.0, 0.0];
trimhc=[196.3769 3.780828e-5 3.682656e-11 0.0 0.0 0.0 0.0 0.0; 161.5506 -4.104959e-5 1.164278e-10 0.0 0.0 0.0 0.0 0.0; 124.5838 -1.5003e-6 8.643112e-12 0.0 0.0 0.0 0.0 0.0; 87.61682 4.037087e-5 -1.147064e-10 0.0 0.0 0.0 0.0 0.0; 53.35888 -7.157266e-5 -2.043487e-10 0.0 0.0 0.0 0.0 0.0]
lcbc=[125.7708, -6.765074e-5, 1.182517e-9, -1.564492e-14, 5.572839e-20, 0.0, 0.0, 0.0];
zgrade=[1.1273957158962795 1.1547344110854503];
Tnkcn=[8.880501 -0.01480475 2.180284e-5 1.4783610000000008e-8 3.5880200000000023e-12 0.0 0.0 0.0;
219.3388 0.004594099 3.936225e-6 1.4076890000000013e-9 1.7606220000000012e-13 0.0 0.0 0.0;
195.0587 0.008311252 -2.186105e-5 2.339001e-9 -1.022953e-12 1.550898e-16 0.0 0.0;
195.0587 0.008311252 2.186105e-5 2.3390010000000013e-8 1.0229530000000006e-11 1.5508980000000014e-15 0.0 0.0;
161.5083 0.0001058785 4.817396000000002e-8 7.101192000000004e-12 0.0 0.0 0.0 0.0;
161.5083 0.0001058785 4.817396000000002e-8 7.101192000000004e-12 0.0 0.0 0.0 0.0;
124.582 -6.062317e-5 1.4651240000000006e-7 7.352221000000004e-11 1.0783470000000007e-14 0.0 0.0 0.0;
124.582 -6.062317e-5 1.4651240000000006e-7 7.352221000000004e-11 1.0783470000000007e-14 0.0 0.0 0.0;
87.87159 -0.0001834451 8.704129000000005e-8 1.755314000000001e-11 0.0 0.0 0.0 0.0; 87.87159 -0.0001834451 8.704129000000005e-8 1.755314000000001e-11 0.0 0.0 0.0 0.0; 53.56956 -0.001285654 1.11241e-6 5.177961000000003e-10 0.0 0.0 0.0 0.0; 53.56956 -0.001285654 1.11241e-6 5.177961000000003e-10 0.0 0.0 0.0 0.0; 44.81982 0.2890156 0.003176682 1.393608e-5 2.679035e-8 1.877435e-11 0.0 0.0; 44.81982 0.2890156 0.003176682 1.393608e-5 2.679035e-8 1.877435e-11 0.0 0.0]
ztrm=[0.97,0.97,0.97,0.97,0.97]
for p in 1:3
DL=@expression(mod2,sum(x[h,i,j,k,l] for h in 1:5, i in 1:p, j in 1:2,k in 1:1, l in 1:2));
mctcexp=@NLexpression(mod2,
mctc[1] + mctc[2]*(DL*ztrm[p])
+ mctc[3]*(DL*ztrm[p])^2 + mctc[4]*(DL*ztrm[p])^3
+ mctc[5]*(DL*ztrm[p])^4 + mctc[6]*(DL*ztrm[p])^5
+ mctc[7]*(DL*ztrm[p])^6 + mctc[8]*(DL*ztrm[p])^7)
lcbexp=@NLexpression(mod2, lcbc[1] + lcbc[2]*(DL*ztrm[p])
+ lcbc[3]*(DL*ztrm[p])^2
+ lcbc[4]*(DL*ztrm[p])^3 + lcbc[5]*(DL*ztrm[p])^4
+ lcbc[6]*(DL*ztrm[p])^5 + lcbc[7]*(DL*ztrm[p])^6
+ lcbc[8]*(DL*ztrm[p])^7
)
Ntnknumber=10
zz=1/1.025
WBMOM=@variable(mod2,[z in 1:Ntnknumber])
for t in 1:Ntnknumber
@NLconstraint(mod2,WBMOM[t]==Tnk[p,t]*(Tnkcn[t,1] + Tnkcn[t,2]*zz*Tnk[p,t]
+ Tnkcn[t,3]*(zz*Tnk[p,t])^2 +Tnkcn[t,4]*(zz*Tnk[p,t])^3 +Tnkcn[t,5]*(zz*Tnk[p,t])^4
+Tnkcn[t,6]*(zz*Tnk[p,t])^5 +Tnkcn[t,7]*(zz*Tnk[p,t])^6 ))
end
ziter=0
Ndimen=5*p*3*1*2
@variable(mod2,HoldLMOM[z in 1:Ndimen])
for h in 1:5
for i in 1:p
for j in 1:2
for k=1:1
for l in 1: 2
ziter+=1
@constraint(mod2,HoldLMOM[ziter] == x[h,i,j,k,l]*
(trimhc[h,1]+trimhc[h,2]*zgrade[k,l]*x[h,i,j,k,l]
+trimhc[h,3]*(zgrade[k,l]*x[h,i,j,k,l])^2
+trimhc[h,4]*(zgrade[k,l]*x[h,i,j,k,l])^3
+trimhc[h,5]*(zgrade[k,l]*x[h,i,j,k,l])^4
+trimhc[h,6]*(zgrade[k,l]*x[h,i,j,k,l])^5
+trimhc[h,7]*(zgrade[k,l]*x[h,i,j,k,l])^6
+trimhc[h,8]*(zgrade[k,l]*x[h,i,j,k,l])^7))
end
end
end
end
end
println("LightshipMOM",LightshipMOM[p])
println("WCMOM",WCMOM[p])
println("HoldLMOM=",HoldLMOM[1])
println("HoldLMOM=",HoldLMOM[2])
println("HoldLMOM=",HoldLMOM[1])
println("HoldLMOM=",HoldLMOM[2])
@NLconstraint(mod2,LightshipMOM[p]+ sum(HoldLMOM[yy] for yy in 1:Ndimen) +
sum(WBMOM[yy] for yy in 1:Ntnknumber)+
WCMOM[p] + WB3BHMOM ==
-100*TRIM*mctcexp + lcbexp*DL)
end
oadError: Cannot multiply a quadratic expression by an aff. expression
in expression starting at D:\BCAP_Phase_2\BCAP_phase_4\bcap_Dynamic_expressions\dynamicexp.jl:29
error(s::String) at error.jl:33
*(q::QuadExpr, a::AffExpr) at operators.jl:305
promote_operation_fallback(op::typeof(*), #unused#::Type{QuadExpr}, #unused#::Type{AffExpr}) at interface.jl:37
promote_operation(::typeof(*), ::Type, ::Type) at interface.jl:99
promote_operation_fallback(::typeof(*), ::Type{AffExpr}, ::Type{AffExpr}, ::Type{AffExpr}) at interface.jl:56
promote_operation(::typeof(*), ::Type, ::Type, ::Type) at interface.jl:99
promote_operation_fallback(#unused#::typeof(*), #unused#::Type{VariableRef}, #unused#::Type{Float64}, #unused#::Type{AffExpr}, args::Type) at interface.jl:56
promote_operation(::typeof(*), ::Type, ::Type, ::Type, ::Type) at interface.jl:99
promote_operation_fallback(::typeof(MutableArithmetics.sub_mul), ::Type, ::Type, ::Type, ::Type, ::Type) at interface.jl:85
promote_operation(::typeof(MutableArithmetics.sub_mul), ::Type, ::Type, ::Type, ::Type, ::Type) at interface.jl:99
mutability(::Type, ::Function, ::Type, ::Type, ::Type, ::Type, ::Type) at interface.jl:232
mutability at interface.jl:240 [inlined]
operate!!(::typeof(MutableArithmetics.sub_mul), ::QuadExpr, ::VariableRef, ::Float64, ::AffExpr, ::AffExpr) at rewrite.jl:88
macro expansion at rewrite.jl:289 [inlined]
macro expansion at macros.jl:815 [inlined]
top-level scope at dynamicexp.jl:61
eval at boot.jl:360 [inlined]
include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String) at loading.jl:1116
Yes, you cannot multiply a quadratic expression by an affine expression in the normal macros. Use @NL
instead:
julia> model = Model();
julia> @variable(model, x)
x
julia> quad = @expression(model, x^2)
x²
julia> aff = @expression(model, x + 1)
x + 1
julia> @expression(model, quad * aff)
ERROR: Cannot multiply a quadratic expression by an aff. expression
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:33
[2] *(q::QuadExpr, a::AffExpr)
@ JuMP ~/.julia/packages/JuMP/Psd1J/src/operators.jl:305
[3] operate(::typeof(*), ::QuadExpr, ::AffExpr)
@ MutableArithmetics ~/.julia/packages/MutableArithmetics/L8iac/src/interface.jl:186
[4] operate(::typeof(MutableArithmetics.add_mul), ::MutableArithmetics.Zero, ::QuadExpr, ::AffExpr)
@ MutableArithmetics ~/.julia/packages/MutableArithmetics/L8iac/src/rewrite.jl:40
[5] operate_fallback!!(::MutableArithmetics.IsNotMutable, ::typeof(MutableArithmetics.add_mul), ::MutableArithmetics.Zero, ::QuadExpr, ::AffExpr)
@ MutableArithmetics ~/.julia/packages/MutableArithmetics/L8iac/src/interface.jl:555
[6] operate!!(::typeof(MutableArithmetics.add_mul), ::MutableArithmetics.Zero, ::QuadExpr, ::AffExpr)
@ MutableArithmetics ~/.julia/packages/MutableArithmetics/L8iac/src/rewrite.jl:94
[7] macro expansion
@ ~/.julia/packages/MutableArithmetics/L8iac/src/rewrite.jl:294 [inlined]
[8] macro expansion
@ ~/.julia/packages/JuMP/Psd1J/src/macros.jl:1352 [inlined]
[9] top-level scope
@ REPL[87]:1
julia> @NLexpression(model, quad * aff)
subexpression[1]: (0 + x * x) * (1.0 + x)
1 Like
oh i missed using @NL macro