# Writing loops in optimization problems in Julia

**URL:** <https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927>\
**Category:** Optimization (Mathematical)\
**Created:** [April 24, 2022, 2:39am UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927 "2022-04-24T02:39:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 24, 2022, 2:39am UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/1 "2022-04-24T02:39:12Z")

</div>

I am trying to use Mosek to solve this optimization problem. I have my code which displays the optimal solution of the values of x,y,z and the radius R. I want to insert a loop in my code to test if the absolute of the optimal solutions thus absolute(x^2 + y^2 -z) \<= 0.01 then it should stop and maintain the answer for x,y, z and R but if the absolute value of (X^2 + y^2-z) is not less than or equal to 0.01 it should again find R with the same formula R= max(R,(((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] )) using the current optimal values for x,y and calculate the new optimal value of x,y,z until the condition that the absolute of (x^2 + y^2 -z) \<= 0.01 is met. It should stop and display the optimal results for x,y,z and R immediately the absolute(x^2 + y^2 -z) \<= 0.01.

Any guidance to get this done will be highly appreciated please. Attached is my code.

```julia
using JuMP
using Mosek
using MosekTools
model= Model(Mosek.Optimizer)

 
m=15 
NumVar=5

a=[0,2.5,-2.5,2,-2.5]
b=[0,0.3,0.25,-2.5,-2.25]
r=[4, 8, 9, 8, 8]

(x,y)=(0,0)
for (i,j) in enumerate(a)
   R= max(R,(((a[i]-x)^2 + (b[i]-y)^2)^0.5 + r[i] )) 
   
end

@variable(model, x>=0)
@variable(model, y>=0)
@variable(model, z>=0)

@objective(model, Min, x^2 + y^2-z )

#######Declare the constraints
for i in 1:NumVar
    @constraint(model, -2*x*a[i]-2*b[i]*y + z <=(R-r[i])^2-a[i]^2-b[i]^2)
end

@show model
print(model)
optimize!(model)
@show termination_status(model)
@show primal_status(model)
@show dual_status(model)
@show objective_value(model)

@show value(x)
@show value(y)
@show value(R)
@show value(z)

```

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [April 24, 2022, 10:15am UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/2 "2022-04-24T10:15:51Z")

</div>

Can’t you use -0.01 \le x^2 + y^2 -z \le 0.01 as a constraint of your optimization problem?

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 24, 2022, 1:32pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/3 "2022-04-24T13:32:11Z")

</div>

That is not working please. I want to write a loop to achieve that but I am having challenges. Your guidance will be appreciated please.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 24, 2022, 3:14pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/4 "2022-04-24T15:14:56Z")

</div>

You don’t show us the problem you are hitting. Is it [this](https://jump.dev/JuMP.jl/stable/manual/constraints/#Anonymous-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:** [April 24, 2022, 10:30pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/5 "2022-04-24T22:30:03Z")

</div>

Perhaps something like this? (I didn’t test, so there might be typos, etc).

```nohighlight
using JuMP
import Mosek
import MosekTools

function inner_loop(x, y)
    NumVar = 5
    a = [0,2.5,-2.5,2,-2.5]
    b = [0,0.3,0.25,-2.5,-2.25]
    r = [4, 8, 9, 8, 8]
    R = 0.0
    for i in 1:length(a)
        R = max(R, sqrt((a[i] - x)^2 + (b[i] - y)^2) + r[i])
    end
    model = Model(Mosek.Optimizer)
    @variable(model, x >= 0)
    @variable(model, y >= 0)
    @variable(model, z >= 0)
    @objective(model, Min, x^2 + y^2 - z)
    @constraint(
        model, 
        [i in 1:NumVar],
        -2 * x * a[i] - 2 * b[i] * y + z <= (R - r[i])^2 - a[i]^2 - b[i]^2,
    )
    optimize!(model)
    return value(x), value(y), value(z), R
end

function main()
    x, y, z, R = 0.0, 0.0, Inf, 0.0
    while abs(x^2 + y^2 - z) >= 0.01
        x, y, z, R = inner_loop(x, y)
    end
    return x, y, z, R
end

```

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 24, 2022, 11:37pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/6 "2022-04-24T23:37:43Z")

</div>

1. This worked after I call the function at the end of the code. Am grateful for the help. Thank you very much.  
I kindly want to ask some questions.  
Why did you set R=0.0 from the beginning.

2. and how did you get this values of x,y,z,R given below.  
x, y, z, R = 0.0, 0.0, Inf, 0.0

Your kind response please. Thank you

---

<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:** [April 24, 2022, 11:40pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/7 "2022-04-24T23:40:00Z")

</div>

1. It needed to have a default value, otherwise when `i` is `1`, `R` doesn’t exist yet.
2. I just picked a random point that would fail the `abs` check.

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 24, 2022, 11:44pm UTC](https://discourse.julialang.org/t/writing-loops-in-optimization-problems-in-julia/79927/8 "2022-04-24T23:44:55Z")

</div>

Thank you very much for the guidance.

I appreciate your help so much.

Thank you once more.
