# NLopt :FORCED\_STOP after one iteration Gradient based optimization

**URL:** <https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383>\
**Category:** Optimization (Mathematical)\
**Created:** [February 9, 2023, 11:28pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383 "2023-02-09T23:28:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamerGerges](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SamerGerges](https://discourse.julialang.org/u/SamerGerges)\
**Post date:** [February 9, 2023, 11:28pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/1 "2023-02-09T23:28:02Z")

</div>

Hello,  
I am using NLopt to solve an optimization problem with non-linear constraints. I provide the gradients for both the optimization function and the gradient function. Both functions provide the correct values of the gradients as I compared them to previous Matlab results. The optimization stops after one iteration providing the initial point and the corresponding value of the optimization function and giving the message :FORCED\_STOP. I tried the examples in the Documentation and they worked finely. Here is the code that I am using.  
After running the code I get:  
got -0.6640272512608545 after 1 iterations (returned FORCED\_STOP)

```julia
begin
# M, k, xf, psi, a and R2 are all previously defined Float64 except xf::Vector[1,2]
    al=minimum(a)* ones(M,1)         
    xu=R2* ones(M,1)                     
    yu=R2* ones(M,1)
    au=maximum(a)* ones(M,1)      
    lb =reshape([xl yl al]',M*3,1)
    ub =reshape([xu yu au]',M*3,1)
	
function f(x, grad)
	
	Pres=FP_Multi_Freq_Multi_IncAngle_Radius_Position(x,M,k,xf,psi) # The optimization fn that returns two values: Float64 and a vector of gradients [1x3M] (number of variables to be optimized)
	if length(grad) > 0
		grad[:]=vec(Pres[2])
	end
return Pres[1]
end
	
function myConst(x, grad)
	 C=nlcon_grad_Radius_Position(x, R2, M, delta)
	if length(grad) > 0
	        grad[:]=C[2]
	         
       end
	return C[1]
end

	opt = Opt(:LD_SLSQP, M*3)
	
opt.lower_bounds = lb[:,1]
opt.upper_bounds=ub[:,1]

opt.xtol_abs = 1e-11

inequality_constraint!(opt, myConst)
opt.min_objective = f
opt.maxtime=10

(minf,minx,ret) = optimize(opt, vec(xx)) #xx is a previously defined initial vector
	
numevals = opt.numevals # the number of function evaluations
println("got $minf at $minx after $numevals iterations (returned $ret)")
end

```

Thank you in advance.

---

<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:** [February 9, 2023, 11:44pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/2 "2023-02-09T23:44:36Z")

</div>

I can’t run your code because I don’t have the data or the functions. But typically the `FORCED_STOP` is because there is a different error in your code. What happens if you call

```julia
g = zeros(length(xx))
myConst(vec(xx), g)

```

See also: [Rethrow errors instead of returning FORCED\_STOP by odow · Pull Request #194 · JuliaOpt/NLopt.jl · GitHub](https://github.com/JuliaOpt/NLopt.jl/pull/194)

---

<div class="post-metadata">

**Author:** ![SamerGerges](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SamerGerges](https://discourse.julialang.org/u/SamerGerges)\
**Post date:** [February 9, 2023, 11:58pm UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/3 "2023-02-09T23:58:25Z")

</div>

I get this when I call the code.  
1×1275 adjoint(::Vector{Float64}) with eltype Float64:  
-0.00763178 -0.00462144 -0.00469838 … -0.000935341 -0.00198357 -0.00633272  
These values are the constraint values that I get in Matlab.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 10, 2023, 12:07am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/4 "2023-02-10T00:07:46Z")

</div>

Is `xx` a feasible solution? Some optimization algorithms (with barrier functions) would not like an initial points which is not feasible.

---

<div class="post-metadata">

**Author:** ![SamerGerges](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SamerGerges](https://discourse.julialang.org/u/SamerGerges)\
**Post date:** [February 10, 2023, 12:14am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/5 "2023-02-10T00:14:53Z")

</div>

xx is a feasible solution that satisfies all the constraints.

---

<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:** [February 10, 2023, 12:15am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/6 "2023-02-10T00:15:13Z")

</div>

> I get this when I call the code.  
> 1×1275 adjoint(::Vector{Float64}) with eltype Float64:  
> -0.00763178 -0.00462144 -0.00469838 … -0.000935341 -0.00198357 -0.00633272  
> These values are the constraint values that I get in Matlab.

So your constraint returns a vector? If so, you need to use different syntax: [GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language](https://github.com/JuliaOpt/NLopt.jl#vector-valued-constraints)

The function you used assumed the constraint returned a scalar.

---

<div class="post-metadata">

**Author:** ![SamerGerges](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SamerGerges](https://discourse.julialang.org/u/SamerGerges)\
**Post date:** [February 10, 2023, 12:31am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/7 "2023-02-10T00:31:50Z")

</div>

```julia
function myConst(result, x, grad)
	
	
	 C=nlcon_grad_Radius_Position(x, R2, M, delta)
	
	
	 println(length(grad))
	println(1)
	if length(grad) > 0
		println(2)
        grad=C[2]
		println(size(grad))
		println(grad)
		println(3)
    end
	result=C[1]
  	
end

```

I used` printlp()` to track the flow of the code and in this way the code did not even call the constraint function and I got the exact same result as previous.

---

<div class="post-metadata">

**Author:** ![SamerGerges](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@SamerGerges](https://discourse.julialang.org/u/SamerGerges)\
**Post date:** [February 10, 2023, 2:33am UTC](https://discourse.julialang.org/t/nlopt-forced-stop-after-one-iteration-gradient-based-optimization/94383/8 "2023-02-10T02:33:19Z")

</div>

It worked. I needed to add the value of `tol` in  
`inequality_constraint!(opt::Opt, c, tol::AbstractVector)` like this:

` inequality_constraint!(opt, myConst,vec(1e-8*ones(sum(1:M),1)))`  
The key was in @odow 's suggestion of using the constraint function syntax for vector output.

Thank you very much.
