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

**URL:** <https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [September 7, 2022, 2:40pm UTC](https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896 "2022-09-07T14:40:23Z")\
**Posts on this page:** 4\
**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:** [September 7, 2022, 2:40pm UTC](https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896/1 "2022-09-07T14:40:23Z")

</div>

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

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

```

---

<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:** [September 8, 2022, 8:27am UTC](https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896/2 "2022-09-08T08:27:51Z")

</div>

> [@Eric.O](#):
>
> `#model = Model(Mosek.Optimizer)`

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.

---

<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:** [September 8, 2022, 1:06pm UTC](https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896/3 "2022-09-08T13:06:36Z")

</div>

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?

---

<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:** [September 8, 2022, 7:33pm UTC](https://discourse.julialang.org/t/an-object-of-name-x-is-already-attached-to-this-model-if-this-is-intended-consider-using-the-anonymous-construction-syntax/86896/4 "2022-09-08T19:33:22Z")

</div>

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.
