# Maximize sum of the piecewise function

**URL:** <https://discourse.julialang.org/t/maximize-sum-of-the-piecewise-function/88882>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 18, 2022, 4:17am UTC](https://discourse.julialang.org/t/maximize-sum-of-the-piecewise-function/88882 "2022-10-18T04:17:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![t.sh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t.sh/32/43644_2.png) [@t.sh](https://discourse.julialang.org/u/t.sh)\
**Post date:** [October 18, 2022, 4:17am UTC](https://discourse.julialang.org/t/maximize-sum-of-the-piecewise-function/88882/1 "2022-10-18T04:17:50Z")

</div>

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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 18, 2022, 4:56am UTC](https://discourse.julialang.org/t/maximize-sum-of-the-piecewise-function/88882/2 "2022-10-18T04:56:31Z")

</div>

Hi there! Since it seems that you’re new to JuMP, you should take a read through the tutorials:

- [Introduction · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/introduction/)

You can’t create a nonlinear objective by passing it a Julia function like that.

Do instead

```julia
@NLobjective(model, Max, sum_rate(β...)

```

You’ll also get a warning, so you’ll need

```julia
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.

```nohighlight
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)

```

---

<div class="post-metadata">

**Author:** ![t.sh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t.sh/32/43644_2.png) [@t.sh](https://discourse.julialang.org/u/t.sh)\
**Post date:** [October 18, 2022, 5:00am UTC](https://discourse.julialang.org/t/maximize-sum-of-the-piecewise-function/88882/3 "2022-10-18T05:00:03Z")

</div>

> [@odow](#):
>
> ```julia
> sum(0.5 * log2(1 + β[i]/(β[i] + 2)) for i in 1:length(β)-1) +
> 0.5 * log2(1 + β[3] / 2)
> 
> ```

Thank you very much for the help!
