# Understanding why COBYLA doesn't find optimum here

**URL:** <https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542>\
**Category:** New to Julia\
**Tags:** optimization\
**Created:** [November 12, 2024, 12:54pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542 "2024-11-12T12:54:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [November 12, 2024, 12:54pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/1 "2024-11-12T12:54:38Z")

</div>

Hi, I’m trying to understand why the COBYLA algorithm doesn’t find the optimal value in the following MWE. By contrast, IPNewton does work. Am I violating some COBYLA assumption that I’m forgetting about? For the problem with non-binding constraints, uncomment the other definition for `prob` and comment the current one; this gives similar behaviour.

```julia
using Random, Optimization, OptimizationMOI, OptimizationNLopt, OptimizationOptimJL, OptimizationPRIMA, DifferentiationInterface, Ipopt
Random.seed!(42)

function RunTest()
    rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
    x0 = [0.5, 0.5]
    _p = [1.0, 100.0]
    
    f(x) = [x[1]^2 + x[2]^2, x[1] * x[2]]
    cons(res, x, p) = (res .= f(x))
    
    optprob = OptimizationFunction(rosenbrock, SecondOrder(AutoForwardDiff(), AutoForwardDiff()), cons = cons)
    #prob = OptimizationProblem(optprob, x0, _p, lb = [0., 0.], ub = [1., 1.], lcons = [-1., -1.0], ucons = [2., 2.0])
    prob = OptimizationProblem(optprob, x0, _p, lb = [0., 0.], ub = [1., 1.], lcons = [-1., -1.0], ucons = [0.8, 2.0])

    opt_vec = [COBYLA(), IPNewton()]
    opt_names = ["COBYLA", "IPNewton"]

    for (opt, opt_name) in zip(opt_vec, opt_names)
        sol = solve(prob, opt)
        print("====\n$opt_name\n$sol")
    end
end

RunTest()

```

I get for the problem with constraints:

```julia
====
COBYLA
retcode: Success
u: [1.58113883008419e-6, 4.743416490252569e-6]
Final objective value: 0.9999968399748375
====
IPNewton
retcode: Success
u: [0.7247018404470709, 0.5242206047577943]
Final objective value: 0.07588358475785834

```

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [November 12, 2024, 1:15pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/2 "2024-11-12T13:15:30Z")

</div>

I think it has to do with the way Optimization.jl wraps PRIMA, because at first sight using PRIMA.jl directly seems to lead to fine results (although the API is a bit different, of course…)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 12, 2024, 2:19pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/3 "2024-11-12T14:19:48Z")

</div>

Can you share the PRIMA script with a side-by-side of how you ran it? That would be good for debugging why it’s different.

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [November 12, 2024, 2:55pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/4 "2024-11-12T14:55:06Z")

</div>

@ChrisRackauckas can you specify what you mean precisely? In the MWE I wrote up the call I made to PRIMA (through Optimization.jl). I’ve included a version with the cobyla function from PRIMA.jl directly, which does give the right solution:

```julia
using Random, Optimization, OptimizationMOI, OptimizationNLopt, OptimizationOptimJL, OptimizationPRIMA, DifferentiationInterface, Ipopt, PRIMA
Random.seed!(42)

function RunTest()
    rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
    x0 = [0.5, 0.5]
    _p = [1.0, 100.0]
    
    f(x) = [x[1]^2 + x[2]^2, x[1] * x[2]]
    cons(res, x, p) = (res .= f(x))
    
    optprob = OptimizationFunction(rosenbrock, SecondOrder(AutoForwardDiff(), AutoForwardDiff()), cons = cons)
    #prob = OptimizationProblem(optprob, x0, _p, lb = [0., 0.], ub = [1., 1.], lcons = [-1., -1.0], ucons = [2., 2.0])
    prob = OptimizationProblem(optprob, x0, _p, lb = [0., 0.], ub = [1., 1.], lcons = [-.8, -2.0], ucons = [0.8, 2.0])

    opt_vec = [COBYLA(), IPNewton()]
    opt_names = ["COBYLA", "IPNewton"]

    for (opt, opt_name) in zip(opt_vec, opt_names)
        sol = solve(prob, opt)
        print("====\n$opt_name\n$(sol)")
    end

    x, info = cobyla(x -> rosenbrock(x, _p), x0; nonlinear_ineq = x -> f(x) .- [0.8, 2.])
    print("====\nusing cobyla directly: $x\n$info\n")
end

RunTest()

```

If I had to guess it may be that PRIMA.jl requires constraints to be of the “f \<= 0” type rather than of the “lc \<= f \<= uc” type, perhaps there is some error in translation here when Optimization.jl wraps COBYLA() and calls cobyla() in PRIMA.jl? I’m not too much aware of the exact implementation here.

```julia
====
COBYLA
retcode: Success
u: [1.58113883008419e-6, 4.743416490252569e-6]
Final objective value: 0.9999968399748375
====
IPNewton
retcode: Success
u: [0.7247018404470709, 0.5242206047577943]
Final objective value: 0.07588358475785834
====
using cobyla directly: [0.7247016181219436, 0.5242209192620982]
PRIMA.Info(0.07588358340753011, 365, PRIMA.SMALL_TR_RADIUS, 7.500562615447848e-9, Float64[], [7.500562615447848e-9, -1.6200962515573847])

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 12, 2024, 3:18pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/5 "2024-11-12T15:18:27Z")

</div>

Can you open an issue?

---

<div class="post-metadata">

**Author:** ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)\
**Post date:** [November 12, 2024, 3:27pm UTC](https://discourse.julialang.org/t/understanding-why-cobyla-doesnt-find-optimum-here/122542/6 "2024-11-12T15:27:56Z")

</div>

Sure, this is now [Wrapping cobyla from PRIMA.jl seems to handle constraints incorrectly · Issue #854 · SciML/Optimization.jl · GitHub](https://github.com/SciML/Optimization.jl/issues/854).
