# An object of name x is already attached to this model. If this is intended, consider using the anonymous construction syntax

I am using Ipopt to solve the non linear optimization problem. My code is running but gives an error when the final solution is to be dispalyed as " An object of name x is already attached to this model. If this is intended, consider using the anonymous construction syntax. " How do I solve this please?

My code is below

``````using JuMP
import Juniper
import Ipopt
NumVar = 5
a = [46.891299081399524, 18.358839924481327, 24.81156595151281, 14.307918586681046, 45.809245326064875] # x-cordinates of selected circle
b = [34.20824038054229, 34.209275827684294, 15.03051769314379, 5.0321444549166845, 17.229495614433407] # y-cordinates of selected circle
r = [20, 20, 20, 20, 20 ] # Radius of selected circles
R = 0.0
model = Model(
optimizer_with_attributes(
Juniper.Optimizer,
"nl_solver" => optimizer_with_attributes(
Ipopt.Optimizer,
MOI.Silent() => true,
),
),
)

function inner_loop(x, y,R)

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)

@NLobjective(model, Min, x^2 + y^2 - z)
@NLconstraint(
model,
[i in 1:NumVar],
-2 * x * a[i] - 2 * b[i] * y + z <= (R - r[i])^2 - a[i]^2 - b[i]^2, )

@show model
print(model)
optimize!(model)

return value(x), value(y), value(z), value(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,R,)
end

print("The abs value is: abs(x^2+y^2-z) = " , abs(x^2+y^2-z))

return x, y, z, R
end

main()

``````

You construct a single model outside the function, and then inside the function, this line is commented out. So it’s trying to build add variables to the existing problem, instead of constructing a new one. Just move your current model definition inside the `inner_loop` function.

If I move the model definition inside the inner loop it keeps running and printing just the model continuously without giving the final solution. What will have caused this please?

Because your solution never satisfies your termination condition for the loop.

I suggest you take another look at the formulation, on paper, and think about your objective.