# Error : no method matching getindex

**URL:** <https://discourse.julialang.org/t/error-no-method-matching-getindex/86537>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [August 30, 2022, 6:47am UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537 "2022-08-30T06:47:04Z")\
**Posts on this page:** 5\
**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:** [August 30, 2022, 6:47am UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537/1 "2022-08-30T06:47:04Z")

</div>

I have my code displaying the objective function with the constraints but the final answer is not reflecting. I always get the error " No method matching getindex".  
I look through the work severally but am not able to rectify the error. Any help will be greatly appreciated please. Attached is the code please.

```julia
using JuMP
import Juniper
import Ipopt

m=15 # num customers
NumVar=5 # num hospitals
a=[0 2.5 -2.5 2 -2.5] # x cordinates of the circle
 b=[0 0.3 0.25 -2.5 -2.25] # y cordinates of the circle
xs=[0 2.5 -2.5 2 -2.5] # x cordinates of the circle
 ys=[0 0.3 0.25 -2.5 -2.25] # y cordinates of the circle

r= [2, 2 , 2 , 2 ,2] # Radiuus of circle
 R = 0.0

coverage=[1 0 0 0 1;0 0 0 1 1;1 0 0 0 1;0 0 1 1 1;0 0 0 1 1; 0 1 0 0 1;0 0 1 1 0;0 0 0 1 0;1 0 0 0 0;1 1 0 0 0;0 1 0 0 0;0 0 1 0 0;0 0 1 0 0;1 0 0 0 0;0 0 0 1 0] # here is the coverage matrix for five hospitals and two customers

   
function inner_loop(x, y,R,coverage)
   

    for i in 1:length(a)
        R = max(R, sqrt((a[i] - x)^2 + (b[i] - y)^2) + r[i])
    end
    
    model = Model(
    optimizer_with_attributes(
        Juniper.Optimizer,
        "nl_solver" => optimizer_with_attributes(Ipopt.Optimizer, MOI.Silent() => true),
    ),
)
    
    ###Declare the variables###
@variable(model, x[i=1:NumVar], Bin)
@variable(model, R>=0)
@variable(model, X>=0)
@variable(model, y>=0)
@variable(model, z >= 0)
    

##Define the objective function###

@objective(model, Min, R)

###Define your constraints###
@constraint(model, constr[i=1:m], sum(coverage[i,j]*x[j] for j=1:NumVar) >= 1)
for i in 1:NumVar
    
    
   @NLconstraint(model, (sqrt((xs[i] -X)^2 + (ys[i] -y)^2 ) + r[i])*x[i] <= R )
    
end
    
    
    @show model
print(model)
    optimize!(model)
    for i in 1:NumVar
   println("X[$i] = ", value(X[i]))
   
    #println("r[$i] = ", value(r[i]))
end
    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,coverage)
    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:** [August 30, 2022, 7:34am UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537/2 "2022-08-30T07:34:35Z")

</div>

Your problem is `println("X[$i] = ", value(X[i]))`. The capital `X` is a single variable, not a vector.

I’d refactored your code a little, but you still have a problem: it doesn’t converge because `z` isn’t part of the optimization model (and so it can pick any value).

```julia
using JuMP
import Juniper
import Ipopt

function inner_loop(old_x, old_y, R)
    m = 15 # num customers
    NumVar = 5 # num hospitals
    a = [0, 2.5, -2.5, 2, -2.5] # x cordinates of the circle
    b = [0, 0.3, 0.25, -2.5, -2.25] # y cordinates of the circle
    xs = [0, 2.5, -2.5, 2, -2.5] # x cordinates of the circle
    ys = [0, 0.3, 0.25, -2.5, -2.25] # y cordinates of the circle
    r = [2, 2, 2, 2, 2] # Radiuus of circle
    R = 0.0
    coverage = [
        1 0 0 0 1;
        0 0 0 1 1;
        1 0 0 0 1;
        0 0 1 1 1;
        0 0 0 1 1;
        0 1 0 0 1;
        0 0 1 1 0;
        0 0 0 1 0;
        1 0 0 0 0;
        1 1 0 0 0;
        0 1 0 0 0;
        0 0 1 0 0;
        0 0 1 0 0;
        1 0 0 0 0;
        0 0 0 1 0
    ]
    for i in 1:length(a)
        R = max(R, sqrt((a[i] - old_x)^2 + (b[i] - old_y)^2) + r[i])
    end    
    model = Model(
        optimizer_with_attributes(
            Juniper.Optimizer,
            "nl_solver" => optimizer_with_attributes(
                Ipopt.Optimizer, 
                MOI.Silent() => true,
            ),
        ),
    )
    @variable(model, x[1:NumVar], Bin)
    @variable(model, R >= 0)
    @variable(model, X >= 0)
    @variable(model, Y >= 0)
    @variable(model, z >= 0)
    @objective(model, Min, R)
    @constraint(model, [i=1:m], coverage[i, :]' * x >= 1)
    @NLconstraint(
        model, 
        [i=1:NumVar],
        (sqrt((xs[i] - X)^2 + (ys[i] - Y)^2) + r[i]) * x[i] <= R
    )
    optimize!(model)
    for i in 1:NumVar
        println("x[$i] = ", value(x[i]))
    end
    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:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [August 30, 2022, 8:02am UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537/3 "2022-08-30T08:02:37Z")

</div>

Thank you sir. I will fix the issue with z. It seems the code keeps running without stopping. How do I get that fixed 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:** [August 30, 2022, 9:44pm UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537/4 "2022-08-30T21:44:06Z")

</div>

> It seems the code keeps running without stopping

`z` can take any value. There’s nothing constraining it to be close to `x^2 + y^2`.

---

<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:** [August 31, 2022, 10:33am UTC](https://discourse.julialang.org/t/error-no-method-matching-getindex/86537/5 "2022-08-31T10:33:44Z")

</div>

Corrected. Everything looks perfect now. Thank you for the explanation and help
