MethodError: no method matching ^(::VariableRef, ::Vector{VariableRef})

Please I need help to get this code running. I am using mosek to solve this optimization problem but I keep getting errors. I tried correcting it severally but I keep getting the error " no method matching ^ ". I will be grateful if anyone can help me get this code to work. Thank you. The code is below

using JuMP
using Mosek
using MosekTools
model= Model(Mosek.Optimizer)
# Let parameter 
m=15 # num customers
NumVar=5 # num hospitals

###Declare the variables###
@variable(model, x[i=1:NumVar], Bin)
@variable(model, R>=0)
@variable(model, X>=0)
@variable(model, y>=0)

###Define your constraints###
for i in 1:NumVar
    @constraint(model,  x[i]>=1)
    @constraint(model,  ( sqrt((x[i]^x -X)^2 + (x[i]^y -y)^2 ) + r[i] )x[i] <= R  )

###Define the objective function###

@objective(model, Min, R)

##Showing and solving the model##

@show model
@show termination_status(model)
@show primal_status(model)
@show dual_status(model)
@show objective_value(model)
for i in 1:NumVar
   println("x[$i] = ", value(x[i]))
    println("r[$i] = ", value(r[i]))

@show value(y)

The problem is this constraint. The error is saying you can’t write x[i]^x, because the power is a vector, not a number. Did you mean X instead?

You also have r[i], but that isn’t defined, and I’m not sure why you have the constraint x[i] >= 1 if x[I] is binary, because that just forces x[i] to be 1.

However, even if you fix all that, you still can’t write this constraint and pass it to Mosek because it is nonlinear.

You’ll need to write it as a nonlinear constraint using @NLconstraint, and pass it to a MINLP solver like Juniper.jl. But you’ll likely still run into some issues because you allow x[i]^0 and a sqrt(0) terms.

Is this the full problem you’re trying to solve? What is the real formulation?


Attached is the problem am trying to solve. Is my formulation correct?

What is r_i and J_i?

r is to be defined as a variable where i starts from 1 to 5. j also starts from 1 to 5

You need to take another look at the subscripts in your math formulation. You have r_i in the big constraint, as well as i in I and i in J_i.

You should also think a lot more about your formulation.

  • What does it mean for (x_j)^x if x_j is either 0 or 1? The power has no effect, so you can just remove it.
  • Is r_i really a variable or is it data? Can’t r_i just take arbitrarily negative values to reduce R? Is r_i >= 0?
  • Does it make sense for x and y to have upper bounds of 1? I don’t think they’d ever be something outside those values if the left-hand side of their inner square terms are 0 or 1`?
  • Why do you even need for all j in J? There’s no data, so every constraint is identical.

Ignoring the formulation issues, here’s how you could write it in JuMP, but note that you likely won’t get the answer you’re expecting because the formulation doesn’t make sense:

using JuMP
import Juniper
import Ipopt
n = 5
model = Model(
        "nl_solver" => optimizer_with_attributes(Ipopt.Optimizer, MOI.Silent() => true),
@variable(model, 0 <= x <= 1)
@variable(model, 0 <= y <= 1)
@variable(model, R)
@variable(model, r[1:n] >= 0)
@variable(model, x_j[j=1:n], Bin, start = j == 1 ? 1.0 : 0.0)
@objective(model, Min, R)
@constraint(model, sum(x_j[j] for j in 1:n) >= 1)
    (sqrt((x_j[j] - x)^2 + (x_j[j] - y)^2) + r[j]) * x_j[j] <= R,
1 Like

Thank you for the guidance and suggestions. I will recheck the formulation to make the necessary corrections accordingly. Highly appreciated please.

1 Like