How to define the absolute value of all elements in the matrix?
using JuMP,Gurobi
model = Model(Gurobi.Optimizer);
model=Model(Gurobi.Optimizer)
T=24
@variable(model, 2 <= a[1:T,1:3]<= 2.5, Int)
@variable(model,-1<=PMT[1:T,1:3]<=20)
@variable(model,0<=WES_BT[1:T,1:3]<=20)
for i = 1:3
for t = 2:T
@constraint(model, WES_BT[t,i] == WES_BT[t-1,i]/0.98 )
end
end
@NLobjective(model,Min,sum(abs(0.3PMT+0.3a)))
@constraint(model,0 .<=PMT .<= 60)
optimize!(model)
term_status = JuMP.termination_status(model)
primal_status = JuMP.primal_status(model)
is_optimal = term_status == MOI.OPTIMAL
I try to use@NLobjectiveļ¼ @NLobjective(model,Min,sum(abs(0.3PMT[i,j]+0.3a[i,j] for i in 1:T j in 1:3)))
The following error still occurs, I donāt know how to change it
LoadError: sum() can appear in nonlinear expressions only if the argument is a generator statement, for example, sum(x[i] for i in 1:N).
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] top-level scope at C:\Users\61940\.juliapro\JuliaPro_v1.4.2-1\packages\JuMP\YXK4e\src\parse_nlp.jl:247
[3] top-level scope at C:\Users\61940\.juliapro\JuliaPro_v1.4.2-1\packages\JuMP\YXK4e\src\macros.jl:1328
in expression starting at E:\JuliaēØåŗ\lizi.jl:23
Have you tried sum(abs(0.3PMT[i,j]+0.3a[i,j]) for i in 1:T j in 1:3) ? I am not proficient in JuMP but the error suggests that sum should take generator instead of the abs.
@odow
Iām very sorry to ask you again!
In JuMP, how to add absolute value to the objective function, I only know that because of the absolute value, it becomes nonlinear and needs to be used @NLobjective
But how to realize the sum of matrix?
This transformation is so ingeniousļ¼
But I still want to ask one more question: is it not easy to realize my previous idea of using for loops in objective ļ¼
If it is a nonlinear objective function (for example, if there is a matrix summation of the square term in the objective function), then how to realize sum () I am more curious