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.
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()