Hi, I am using Nlopt to do the optimization. in this case i have defined a function for optimization. inside this function the optimizer reads another functions to calculate the objective and gradients. I also want to generate a random number in each iteration. In this case I need the iteration counter in my objective and gradients function to be used. can you please help me how I can return the iteration number in each iteration and use it in other functions?

so in a nutshell I need the optimizer iteration to : 1- generate identified random number in each iteration,

2- save the forward analysis results in each iteration.

3-save the optimized design in each iteration.

Thank you!

this is the optimization function

```
using NLopt
function gf_p_optimize(p_init; r, β, η, TOL=1e-4, MAX_ITER, fem_params)
##################### Optimize #################
opt = Opt(:LD_MMA, fem_params.np)
opt.lower_bounds = 0
opt.upper_bounds = 1
opt.xtol_rel = TOL
opt.maxeval = MAX_ITER
opt.max_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, fem_params)
(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
```

and gf_p is the function for objective and gradients calculation based on rrule

```
function gf_pf(pf_vec; β, η, fem_params)
#generate random number for iteration m
sample = 2
Random.seed!(1234);
diste = LogNormal(log(E_mat),(log(E_mat*0.1))^2)
Es = rand(diste,sample)
#forward analysis to solve the equations based on the random value
#save the result for this iteration
writevtk(Ω,"resultscellelasti $m",cellfields=["uh"=>uh, "sh"=>sh])
return obj
function rrule(::typeof(gf_pf), pf_vec; β, η, fem_params)
function U_Disp_pullback(dgdg)
NO_FIELDS, dgdg * Dgfdpf(pf_vec; β, η, fem_params)
end
gf_pf(pf_vec; β, η, fem_params), U_Disp_pullback
end
function Dgfdpf(pf_vec; β, η, fem_params)
#solve forward problem again with define random value at iteration m
# solve backward for adjoint
#calculate the gradient
return dgfdpfl
end
```