# NLOPT While loop problem

**URL:** <https://discourse.julialang.org/t/nlopt-while-loop-problem/104882>\
**Category:** Optimization (Mathematical)\
**Tags:** optimization, gridap, nlopt\
**Created:** [October 12, 2023, 12:17am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882 "2023-10-12T00:17:56Z")\
**Posts on this page:** 15\
**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:** [October 12, 2023, 12:17am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/1 "2023-10-12T00:17:57Z")

</div>

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

```julia
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.

```julia
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

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [October 12, 2023, 6:08am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/2 "2023-10-12T06:08:41Z")

</div>

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

---

<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:** [October 13, 2023, 5:25am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/3 "2023-10-13T05:25:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [October 13, 2023, 5:51am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/4 "2023-10-13T05:51:07Z")

</div>

First try manually calling `gf_p` and `cf_p` with the last known`p_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.

---

<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:** [October 14, 2023, 8:15pm UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/5 "2023-10-14T20:15:54Z")

</div>

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.

---

<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:** [October 16, 2023, 12:38am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/6 "2023-10-16T00:38:43Z")

</div>

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

---

<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:** [October 16, 2023, 1:14am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/7 "2023-10-16T01:14:00Z")

</div>

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

---

<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:** [October 16, 2023, 1:20am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/8 "2023-10-16T01:20:57Z")

</div>

Thank you 🙏 🙏 🙏 🙏

---

<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:** [October 16, 2023, 6:03am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/9 "2023-10-16T06:03:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [October 16, 2023, 6:14am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/10 "2023-10-16T06:14:12Z")

</div>

> [@mary](#):
>
> `p0 = p_temp_opt`

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`.

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [October 16, 2023, 7:47am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/11 "2023-10-16T07:47:37Z")

</div>

> [@mary](#):
>
> ```julia
> p_opt = p_temp_opt
> #umpdate initial value
> p0 = p_temp_opt
> 
> ```

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

```julia
    p_opt .= p_temp_opt
    #umpdate initial value
    p0 .= p_temp_opt

```

---

<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:** [October 18, 2023, 12:10am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/12 "2023-10-18T00:10:34Z")

</div>

@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.

---

<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:** [October 22, 2023, 1:27am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/13 "2023-10-22T01:27:04Z")

</div>

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.

---

<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:** [October 22, 2023, 1:35am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/14 "2023-10-22T01:35:44Z")

</div>

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

---

<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:** [October 22, 2023, 1:41am UTC](https://discourse.julialang.org/t/nlopt-while-loop-problem/104882/15 "2023-10-22T01:41:23Z")

</div>

exactly 🤣 🤣 🤣
