# Optimize many non linear functions

**URL:** <https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761>\
**Category:** Optimization (Mathematical)\
**Created:** [December 29, 2021, 3:02pm UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761 "2021-12-29T15:02:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmmshn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmmshn/32/32255_2.png) [@jmmshn](https://discourse.julialang.org/u/jmmshn)\
**Post date:** [December 29, 2021, 3:02pm UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761/1 "2021-12-29T15:02:04Z")

</div>

I’m tying optimize the sum of series objective functions in `funcs` using the Ipot optimizer

A minimal example shown below show result in an optimal objective value of 5.  
However it current returns `objective_value(model) = 2.0`

```julia
using JuMP, Ipopt
f1(δ) = -(δ - 2) ^ 2 + 1
f2(δ) = -(δ - 7) ^ 2 + 4
funcs = [f1, f2]
model = Model(Ipopt.Optimizer)
@variable(model, x[1:length(funcs)] >= 0)
@constraint(model, sum(x) <= 10)
@NLobjective(model, Max, sum(f(x) for (f,x) in zip(funcs, x)))
optimize!(model)
@show objective_value(model);

```

My understanding is that the `@NLobjectives` is parsing the actual code so the `f(x)` in the sum gets interpreted as repeats of the same function. so it is just optimizing `f1 + f1` instead of `f1 + f2`.  
Is it possible to make `@NLobjectives` parse the correct target function without doing a lot of metaprogramming?  
Thanks in advance for the help!

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [December 29, 2021, 9:17pm UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761/2 "2021-12-29T21:17:31Z")

</div>

No need for metaprogramming, but after [registering the functions](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions) you’ll likely need to construct the objective expression data structure programmatically: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#Raw-expression-input).

---

<div class="post-metadata">

**Author:** ![jmmshn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmmshn/32/32255_2.png) [@jmmshn](https://discourse.julialang.org/u/jmmshn)\
**Post date:** [December 30, 2021, 1:24pm UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761/3 "2021-12-30T13:24:11Z")

</div>

@miles.lubin thanks so much for the quick reply!!  
The code below now now works as intended.

```julia
using JuMP, Ipopt
f1(δ) = -(δ - 2) ^ 2 + 1
f2(δ) = -(δ - 7) ^ 2 + 4
funcs = [f1, f2]
model = Model(Ipopt.Optimizer)
@variable(model, x[1:length(funcs)] >= 0)
@constraint(model, sum(x) <= 10)
expr = Expr(:call, :+)
for (i, (f, x)) in enumerate(zip(funcs, x))
    f_sym = Symbol("f_$(i)")
    register(model, f_sym, 1, f; autodiff = true)
    push!(expr.args, :($(f_sym)($(x))))
end
set_NL_objective(model, MAX_SENSE, :($(expr))) 

optimize!(model)
@show objective_value(model);

```

Thanks again!

* * *

One minor bug to report: once you start optimizing many functions, there is visual bug in `latex_formulation` where `f_10` is rendered as `f_10` and not `f_{10}` in latex.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/5/358f2e44c1bed80c54ca80896c6b2e2a36034a89.png)

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 2, 2022, 6:59am UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761/4 "2022-01-02T06:59:20Z")

</div>

Just on the minor bug: are you not defining the Symbol `f_10` yourself with the line

```julia
f_sym = Symbol("f_$(i)")

```

or am I missing something?

---

<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:** [January 2, 2022, 8:44pm UTC](https://discourse.julialang.org/t/optimize-many-non-linear-functions/73761/5 "2022-01-02T20:44:06Z")

</div>

We should probably escape this as `f\_10`, so yes, this is a bug in the latex printing.
