I am trying to define a @NLexpression
in JuMP
that has different specifications at different indices. In the below example, I want the expression y[j,k]
to be defined as 1/(x[j] - x[k])
when j != k
and take some other value when j == k
. I can simulate this behavior by defining an auxiliary variable z
and adding constraints conditional on index values. Is there a similar way to define expression conditional on index values?
using JuMP, Ipopt
model = JuMP.Model(with_optimizer(Ipopt.Optimizer))
@variable(model, 0 <= x[1:2])
@NLexpression(model, y[j=1:2,k=1:2], 1/(x[j] - x[k])) # <- problematic line
@variable(model, z[1:2,1:2])
for j=1:2, k=1:2
if j == k
@constraint(model, z[j,k] == 1)
else
@NLconstraint(model, z[j,k] == 1/(p[j] - p[k]))
end
end
display(model)