# Conditional inside a function to be optimized

**URL:** <https://discourse.julialang.org/t/conditional-inside-a-function-to-be-optimized/48563>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [October 17, 2020, 10:41pm UTC](https://discourse.julialang.org/t/conditional-inside-a-function-to-be-optimized/48563 "2020-10-17T22:41:00Z")\
**Posts on this page:** 1\
**Showing post:** 16

<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, 2020, 8:32pm UTC](https://discourse.julialang.org/t/conditional-inside-a-function-to-be-optimized/48563/16 "2020-10-18T20:32:19Z")

</div>

There are a few comments leading you astray in this thread, so let me summarize.

- Your problem is nonlinear. You need to use the nonlinear interface for JuMP. In particular, you need to use a user-defined function: [https://jump.dev/JuMP.jl/stable/nlp/#User-defined-Functions-1](https://jump.dev/JuMP.jl/stable/nlp/#User-defined-Functions-1).
- The nonlinear interface requires functions with scalar inputs, so `s` can’t be a vector input. [Read this suggested work-around.](https://jump.dev/JuMP.jl/stable/nlp/#User-defined-functions-with-vector-inputs-1)
- @Henrique_Becker is correct that `@objective(model, Max, C_opt(s)` evaluates this function with the arguments as JuMP variables _before_ passing it to the solver. This is a little confusing, but is very helpful in some cases.
- I don’t understand the need for `OffSetArray`, but it looks like your nonlinearity is just the single `max(0, C)`? Due to nondifferentiability and the large flat area, Ipopt tends to struggle with this. You might get a local optima.
- There is nothing wrong with how you have defined `@variable`.
- Your issue with `Model(GLPK.Optimizer)` is because you are using an old version of JuMP, although I think you’ve fixed this in [another issue](https://discourse.julialang.org/t/getting-methoderror-no-method-matching-model-when-initializing-models-on-jump/43647/7).

---

_[View the full topic](https://discourse.julialang.org/t/conditional-inside-a-function-to-be-optimized/48563)._
