NLOPT While loop problem

Hi All, I am using gridap for topology optimization. I have defined function of optimization using MMA as below

using NLopt

function gf_p_optimize(p_init; r, β, η, TOL = 1e-4, fem_params)
    ##################### Optimize #################
    opt = Opt(:LD_MMA, fem_params.np)
    opt.lower_bounds = 0
    opt.upper_bounds = 1
    opt.ftol_rel = TOL
    opt.max_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, fem_params)
    inequality_constraint!(opt, (p0, gradc) -> cf_p(p0, gradc; r, β, η, fem_params), 1e-8)
    (g_opt, p_opt, ret) = optimize(opt, p_init)
    @show numevals = opt.numevals # the number of function evaluations
    return g_opt, p_opt

end

and i have defined a while loop which do the calculation including forward,backward, and topology optimization but it seems that it just run the optimization function once and do not care about tolerance. I have checked the code without while and it seems that it is working correctly and it take around 68 internal iteration for mma to get result but within the while loop it just do 1 internal iteration for mma.

mmax = 230
m=0
p_opt = fill(0.4, fem_params.np)   # Initial guess
TOL = 1e-8
while m < mmax
    m += 1
    #forward solution
    #backward solution
    #topology optimization
    g_opt = 0
    g_opt, p_temp_opt = gf_p_optimize(p_opt; r, β, η, TOL, fem_params)
    p_opt = p_temp_opt
    #umpdate initial value
    p0 = p_temp_opt
end

can you tell me how can i solve this issue? It also doesn’t work by setting m as 1. it just can do optimization when i delete external while loop.
thank you

Have you checked stopping reason(ret value) of the optimization? There may be another reason like hitting the bounds.

1 Like

I check that it returns “force stop” . i cant understand why it can solve the problem without a loop but in the loop even in first iteration it is not working.
I also try to solve manually for iterations which is working but adding while loop makes it wrong.

First try manually calling gf_p and cf_p with the last knownp_init and check if there is an error thrown. If there is one try fixing it. If not, read nlopt’s LD_MMA reference for possible forced stops because some methods may need p_init in an interior point namely constraints should be satisfied with it. Last but not least, there is a chance you are missing a “null” pointer check on grad argument when the algorithm just wants to evaluate objective function but not its gradient. Finally, if all not worked, try logging arguments to gf_p and cf_p and run them manually with logged arguments for which forced stop occur.

1 Like

I call functions manually, even i did while loop manually for 5 iteration and it works but as i put in while loop it returns force stop. also after 5 iterations of doing manually the while loop then i run while and nlopt can do optimization. i cant understand why it cant do optimization in while for first iteration while it can do it outside the loop with same inputs.
about your comment on p_init i am searching to find anything but i couldnt find yet. however it is confusing, if there should be satisfied constraint on p_init it should have problem outside the loop but it doesnt have any problem.
thank you for your suggestions.

Hi @odow
I see that you have solved some similar issues. could you please take a look at my problem. I really appreciate it.

1 Like

Ive moved this to the optimization section. That should get some more eyes on it. Im away from my computer right now but will take a look when i get a chance

Thank you :pray: :pray: :pray: :pray:

i found some issue with my code. i have solved forward problem in my while loop and saved the result of i iteration of forward in a matrix but as they are in a loop they are not accessible outside. i used those values in defined function of objective and gradients. do you know how can i solve this issue? i found this when i try to call gf_p manually. it works outside the loop as the variables are saving correctly in matrix but in side the loop they are not accessible for other functions.

May be make this mutate the vector p0 by p0 .= p_temp_opt. You can make g_opt global too if you needed.

Since I don’t have all the code and data needed to run the code myself, I’ve just listed general failure points I’ve encountered in using nlopt.

1 Like

I meant this p_opt but it is ok if both done I guess

    p_opt .= p_temp_opt
    #umpdate initial value
    p0 .= p_temp_opt
1 Like

@mary do you have a reproducible example of the code that I can copy-and-paste and get running? It’s hard to tell what is wrong without it.

thank you odow i tried to make a simple code that you can run but it was hard. I found the problem i had some values which calculated inside the while loop and was not accessible outside.

1 Like

Glad to hear. Often the process of creating a simple reproducible example is enough to find the issue :smile:

1 Like

exactly :rofl: :rofl: :rofl:

1 Like