NLOPT constraint violation

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

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; 

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?

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

Do you have a reproducible example?

i tried to make one but i couldnt. i understand that it is hard to figure it out without example

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

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

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

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