# Writing loops in optimization problems in Julia

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.

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

``````

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

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.

You don’t show us the problem you are hitting. Is it this?

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

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

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.