# NLOPT forced stop with two constraints

**URL:** https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371
**Category:** Optimization (Mathematical)
**Tags:** nlopt
**Created:** [June 8, 2024, 9:09pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371 "2024-06-08T21:09:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [June 8, 2024, 9:09pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371/1 "2024-06-08T21:09:50Z")

</div>

Hi  
I am using NLOPT with two inequality constraints. It can do the optmization when I suppress the second constraint but with two constraints it stops after 1st iteration with forced stop. I also run objective function and constraints out of the Nlopt and they are fine. do you have any idea on this issue? NLopt can handle two constraints or not?

```julia
function gf_p_optimize(p_init; r, β, η, TOL=1e-4, MAX_ITER, fem_params)
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 1e-5
    opt.upper_bounds = 1
    opt.xtol_rel = TOL
    opt.maxeval = MAX_ITER
    iteration_counter = 0 
    iteration_solutions = Any[] 
    function objective_fn(p0, grad)
        iteration_counter += 1 
        push!(iteration_solutions, p0) 
        pf_vec = pf_p0(iteration_solutions[iteration_counter]; r, fem_params)
        return gf_p(p0, grad;iteration_counter, r, β, η, fem_params)
    end
    opt.min_objective = (p0, grad) -> objective_fn(p0, grad)
    inequality_constraint!(opt, (p0, gradc) -> cf_p(p0, gradc; r, β, η, fem_params), 1e-8)
    inequality_constraint!(opt, (p0, gradw) -> wf_p(p0, gradw; r, β, η, fem_params), 1e-8)
    (g_opt, p_opt, ret) = optimize(opt, p_init)
    @show numevals = opt.numevals # the number of function evaluations
    println("got $g_opt at $p_opt after $numevals iterations (returned $ret)")
    return g_opt, p_opt

end

```

---

<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: [June 8, 2024, 11:56pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371/2 "2024-06-08T23:56:11Z")

</div>

Yes, NLopt can handle multiple constraints. There must be a error in your `wf_p` function.

Do you have a reproducible example?

---

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [June 9, 2024, 12:03am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371/3 "2024-06-09T00:03:07Z")

</div>

wf\_p can show the output out of the NLopt.

```julia
function wf_p(p0::Vector, gradw::Vector;iteration_counter, r, β, η, fem_params)    
    if length(gradw) > 0
       #grad calculation
            gradw[:] = assemble_vector(grad_temp,fem_params.Pf)
    end
    wset = 80
    cons_temp(dp)=∫(( ((p->Em(p))∘pfh) * (jfuntN∘(ε(uh),sh,ε(uhp))) )*dp)fem_params.dΩ
    cons=sum(assemble_vector(cons_temp,fem_params.Pf))
    wo = wset - cons
    open("constraintwork.txt", "a") do io
        write(io, "$wo \n")
    end
    return wo
end

```

this is the function I have used.

---

<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: [June 9, 2024, 12:06am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-with-two-constraints/115371/4 "2024-06-09T00:06:51Z")

</div>

Do you have the full stack trace of the error? Have you updated to the latest version of NLopt.jl?

To be able to help, we really need a reproducible example of your code that someone can copy-paste into their REPL and reproduce the error. It’s hard to spot something wrong from just this snippet.
