NLOPT forced stop with two constraints

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?

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

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

Do you have a reproducible example?

1 Like

wf_p can show the output out of the NLopt.

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.

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.