# Optim constrained optimization issues

**URL:** <https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [August 8, 2019, 9:15pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333 "2019-08-08T21:15:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ys3636](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ys3636/32/9701_2.png) [@ys3636](https://discourse.julialang.org/u/ys3636)\
**Post date:** [August 8, 2019, 9:15pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/1 "2019-08-08T21:15:34Z")

</div>

Hi,

Apologies, Julia newcomer. I’m trying to use Optim.jl to fit the parameters of a particular custom noise model by minimizing negative log-likelihood, but I’m running into some issues where methods that exist in previous examples don’t seem to exist anymore.

In particular, I’m trying to use the following code:

```julia
function fit_model(data_x, data_y; x0 = [0.3, 0.1, 0.05])
    lower = [0, 0, 0]
    upper = [1, 1, 1]
  
    result = optimize(x -> session_neg_log_likelihood(x, data_x, data_y), lower, upper, x0, Fminbox(LBFGS()))
end

```

However, it’s producing an error:

```julia
MethodError: no method matching optimize(::getfield(Main, Symbol("##52#53")){Array{Int64,3},Array{Int64,1}}, ::Array{Int64,1}, ::Array{Int64,1}, ::Array{Float64,1}, ::Fminbox{LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},getfield(Optim, Symbol("##19#21"))},Float64,getfield(Optim, Symbol("##43#45"))})

```

I’m not really sure how to go about debugging this, since I (think) I’ve seen examples that look like the above around.

Any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 9, 2019, 5:56am UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/2 "2019-08-09T05:56:43Z")

</div>

Please provide a self-contained example and the version of Optim that you are using.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 9, 2019, 6:47am UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/3 "2019-08-09T06:47:34Z")

</div>

There is a fair chance that changing to

```julia
lower = [0, 0, 0.0]
upper = [1, 1, 1.0]

```

will solve the problem. (Yes, the bounds should be floats.)

---

<div class="post-metadata">

**Author:** ![ys3636](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ys3636/32/9701_2.png) [@ys3636](https://discourse.julialang.org/u/ys3636)\
**Post date:** [August 9, 2019, 7:09am UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/4 "2019-08-09T07:09:20Z")

</div>

This did it. Thank you so much.

This might be a dumb question but I’ve been having a little difficulty finding documentation regarding things like this (e.g. bounds arguments to `optimize` should be floats, types of function outputs, etc). In the spirit of learning to fish, where could I have found this information?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 9, 2019, 7:10am UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/5 "2019-08-09T07:10:58Z")

</div>

> [@ys3636](#):
>
> In the spirit of learning to fish, where could I have found this information?

I am not sure this is documented.

And not dealing with all kinds of `AbstractArray`s (as long as they contain numbers, which may or may not be reflected by their element type) is probably something that could be improved in Optim’s interface, so perhaps open an issue.

---

<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:** [August 9, 2019, 2:51pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/6 "2019-08-09T14:51:35Z")

</div>

This is a known issue (arguably an API design issue common in Julia) that I’ve ranted about: [Common error: providing an Integer when a Float is expected - #3 by miles.lubin](https://discourse.julialang.org/t/common-error-providing-an-integer-when-a-float-is-expected/17786/3).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 9, 2019, 2:57pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/7 "2019-08-09T14:57:30Z")

</div>

I agree with you about preferring an informative error message to a non-informative one, but I think that in general just accepting generic valid inputs (and potentially converting them if necessary) is the best solution.

---

<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:** [August 9, 2019, 4:23pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/8 "2019-08-09T16:23:22Z")

</div>

Agreed. Either providing informative error messages or performing the appropriate conversions are good solutions.

---

<div class="post-metadata">

**Author:** ![ys3636](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ys3636/32/9701_2.png) [@ys3636](https://discourse.julialang.org/u/ys3636)\
**Post date:** [August 9, 2019, 5:43pm UTC](https://discourse.julialang.org/t/optim-constrained-optimization-issues/27333/9 "2019-08-09T17:43:09Z")

</div>

I see, thanks for discussion everyone!

In principle, I would have no issues as a complete Julia-novice if this point was properly documented somewhere. Given that Julia has these strong typing constraints, I find it a little odd that type information is not freely disseminated.

Related question: Is there anywhere I could look to find function signatures? One of the other issues I was having was not being aware how to call the `optimize` function correctly (e.g., should it go `optimize(f, lower, upper, x0, method)` or `optimize(f, x0, lower, upper, method)`, etc. I solved this by looking through tutorials, so it’s not a total barrier, but is there anywhere such information is listed directly?
