t.sh
1
I am the new in JuMP package, and I am trying to solve the optimization with a piecewise function
My maximum working example looks as follows:
using JuMP, Ipopt
function sum_rate(β::T ...) where {T <: Real}
rate = zeros(T, length(β))
for i = 1:length(β)-1
rate[i] = 0.5 * log2(1 + β[i]/(sum(β[i]) + 2))
end
rate[length(β)] = 0.5 * log2(1 + β[length(β)]/2)
return sum(rate)
end
model = Model(Ipopt.Optimizer)
@variable(model, β[i = 1:3] >= 0)
@constraint(model, sum(β) <= 1)
@NLobjective(model, Max, sum_rate)
However, I can not pass the compile with error:
Unexpected object sum_rate of type typeof(sum_rate) in nonlinear expression.
Can anyone help me to solve this issue? Thank you!
1 Like
odow
2
Hi there! Since it seems that you’re new to JuMP, you should take a read through the tutorials:
You can’t create a nonlinear objective by passing it a Julia function like that.
Do instead
@NLobjective(model, Max, sum_rate(β...)
You’ll also get a warning, so you’ll need
register(model, :sum_rate, 3, sum_rate; autodiff = true)
You should also consider writing out the expression directly. It lets JuMP compute more efficient derivatives that might improve performance.
model = Model(Ipopt.Optimizer)
@variable(model, β[i = 1:3] >= 0)
@constraint(model, sum(β) <= 1)
@NLobjective(
model,
Max,
sum(0.5 * log2(1 + β[i]/(β[i] + 2)) for i in 1:length(β)-1) +
0.5 * log2(1 + β[3] / 2)
)
optimize!(model)
2 Likes
t.sh
3
Thank you very much for the help!
1 Like