Reputation: 11602
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)
Upvotes: 2
Views: 775
Reputation: 953
You're not obligated to use JuMP's macros to create containers for expressions. You should be able to conditionally create the expressions as follows:
model = JuMP.Model()
@variable(model, 0 <= x[1:2])
y = Dict() # Or Array, if you prefer.
for j=1:2, k=1:2
if j == k
y[j,k] = 1
else
y[j,k] = @NLexpression(model, 1/(p[j] - p[k]))
end
end
Upvotes: 3