# (L-)BFGS for a user-defined function

**URL:** https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309
**Category:** General Usage
**Tags:** optimization
**Created:** [July 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309 "2021-07-26T15:07:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)
#### Post date: [July 26, 2021, 3:07pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/1 "2021-07-26T15:07:25Z")

</div>

Hi,  
I have a complex nonlinear user-defined function (blackbox) that I am trying to minimize using BFGS or L-BFGS. I found [this](https://github.com/JuliaNLSolvers/Optim.jl) library, but I can’t use it. I think it works only for simple algebraic expressions, it that correct? and what would be the alternative?

---

<div class="post-metadata">

### Author: ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)
#### Post date: [July 26, 2021, 3:19pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/2 "2021-07-26T15:19:01Z")

</div>

[NLopt.jl](https://github.com/JuliaOpt/NLopt.jl) has an L-BFGS implementation, but you will need to supply gradients. Depending on the complexity of your objective, you might be able to derive the gradient via autodiff

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [July 26, 2021, 4:16pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/3 "2021-07-26T16:16:34Z")

</div>

> [@AhmedAlreweny](#):
>
> I think it works only for simple algebraic expressions

No, it works, in principle, on any function. If it is differentiable, as mentioned above, you might want to provide the manually computed gradient or use an automatic differentiation package. If your function is not differentiable, other methods exist that will probably perform better than L-BFGS.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [July 26, 2021, 6:09pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/4 "2021-07-26T18:09:11Z")

</div>

> [@AhmedAlreweny](#):
>
> I found [this](https://github.com/JuliaNLSolvers/Optim.jl) library, but I can’t use it. I think it works only for simple algebraic expressions, it that correct?

It should work for black box functions.

---

<div class="post-metadata">

### Author: ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)
#### Post date: [July 26, 2021, 6:27pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/5 "2021-07-26T18:27:00Z")

</div>

I get the following error massage when I try to use the library (I also provide a gradient):

```julia
MethodError: no method matching optimize(::typeof(AugmantedLagrangiannn), ::typeof(AugmantedLagrangianGradient_tryy), ::BFGS{LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},Nothing,Nothing,Flat})
Closest candidates are:
  optimize(::Any, ::Any, ::Any, !Matched::AbstractArray; inplace, autodiff, kwargs...) at C:\Users\u0132800\.juliapro\JuliaPro_v1.4.0-1\packages\Optim\onG5j\src\multivariate\optimize\interface.jl:104
  optimize(::Any, ::Any, ::Any, !Matched::AbstractArray{T,N} where N, !Matched::Optim.Options; inplace, autodiff) where T at C:\Users\u0132800\.juliapro\JuliaPro_v1.4.0-1\packages\Optim\onG5j\src\multivariate\optimize\interface.jl:131
  optimize(::Any, ::Any, ::Any, !Matched::AbstractArray{T,N} where N, !Matched::Optim.AbstractOptimizer) where T at C:\Users\u0132800\.juliapro\JuliaPro_v1.4.0-1\packages\Optim\onG5j\src\multivariate\optimize\interface.jl:160
  ...
top-level scope at untitled-0b01580f0f477249f7f9af02f60c1251:178

```

where `AugmantedLagrangiannn(X)` is my function and the gradient is `AugmantedLagrangianGradient_tryy(X)`

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [July 26, 2021, 8:17pm UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/6 "2021-07-26T20:17:00Z")

</div>

Can you provide a minimum working example (MWE) that reproduces your error? From what you provided above, it looks like you didn’t supply the optimizer with a starting value for `X`. The Optim.jl [documentation](https://julianlsolvers.github.io/Optim.jl/stable/#user/minimization/) has some basic examples to get you started.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [July 27, 2021, 6:28am UTC](https://discourse.julialang.org/t/l-bfgs-for-a-user-defined-function/65309/7 "2021-07-27T06:28:12Z")

</div>

I think you have forgotten the initial guess when you call `optimize`
