# NLOPT constraint violation

**URL:** <https://discourse.julialang.org/t/nlopt-constraint-violation/128851>\
**Category:** Optimization (Mathematical)\
**Tags:** gridap, nlopt\
**Created:** [May 8, 2025, 8:14pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851 "2025-05-08T20:14:12Z")\
**Posts on this page:** 7\
**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:** [May 8, 2025, 8:14pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/1 "2025-05-08T20:14:13Z")

</div>

Hi  
I am using NLOP for topology optimization. First I did the deterministic topology optimization with constraint on volume, Now I am adding another constraint. As I checked the values for constraint it seems that the design is violating the second constraint and not trying to satisfy the second constraint. Do you have any comments on this or how can I fix the issue?  
Thank you

```julia
function gf_p_optimize(p_init;u0, r, β, η, TOL, MAX_ITER, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 0.001
    opt.upper_bounds = 1
    opt.xtol_rel = TOL
    opt.maxeval = MAX_ITER
    opt.min_objective = (p0, grad) -> gf_p(p0, grad;u0, r, β, η, fem_params)
    inequality_constraint!(opt, (p0, gradc) -> cf_p(p0, gradc; r, β, η, fem_params), 1e-8)
    inequality_constraint!(opt, (p0, gradlsf) -> lsf_pc(p0, gradlsf; 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 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:** [May 8, 2025, 9:06pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/2 "2025-05-08T21:06:32Z")

</div>

Hi @mary, I’ve moved this question to the optimization section.

A few questions to help us understand the issue:

- How did you check that the constraint was violated? Was it violated by a large value or a small value?
- Did you check the `ret` code from the optimizer? Did it claim to find a feasible point or did it encounter an error?

It’s a bit hard to tell only by looking at this function in isolation. Can you provide 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:** [May 24, 2025, 6:05pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/3 "2025-05-24T18:05:45Z")

</div>

Hi @odow  
it is violating by not a small value. it is around 4. ret shows me XTOL reached and not an error.

---

<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:** [May 24, 2025, 7:59pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/4 "2025-05-24T19:59:14Z")

</div>

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:** [May 24, 2025, 8:05pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/5 "2025-05-24T20:05:37Z")

</div>

i tried to make one but i couldnt. i understand that it is hard to figure it out without 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:** [May 24, 2025, 8:33pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/6 "2025-05-24T20:33:43Z")

</div>

is it ok to define two different functions for 2 constraints?or it should be in one function ? i did like this

```julia
function gf_p_optimize(p_init; r, β, η, TOL=1e-4, MAX_ITER, fem_params)
# function gf_p_optimize(p_init; r, β, η, MAX_ITER, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 0.001
    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)
        pfh = FEFunction(fem_params.Pf, pf_vec)
        pth = (pf -> Threshold(pf; β, η)) ∘ pfh
        writevtk(Ω,"ldtlm$iteration_counter",cellfields= ["p_opt"=>iteration_solutions[iteration_counter],"pfh"=>pfh,"pth"=>pth]) 
        println("called from iteration $iteration_counter")
        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, gradpf) -> pf_p(p0, gradpf; iteration_counter,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)")
    open("gopt.txt", "a") do io
        write(io, "$g_opt \n")
    end
    return g_opt, p_opt

```

but ii can see in nlopt tutorial it is define in one function

```julia
function f(result::Vector{Float64}, x::Vector{Float64}, grad::Matrix{Float64})
    if length(grad) > 0
        ...set grad to gradient, in-place...
    end
    result[1] = ...value of c1(x)...
    result[2] = ...value of c2(x)...
    return

```

---

<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:** [May 24, 2025, 9:11pm UTC](https://discourse.julialang.org/t/nlopt-constraint-violation/128851/7 "2025-05-24T21:11:49Z")

</div>

It’s okay to add multiple constraints. It’s pretty much impossible to say what is going on without a reproducible example, unfortunately.
