I need to revive a previously asked question. I have a problem that involves evaluating the exp
function over an array of variables and I want to avoid huge values by centering those variables before appplying exp
. I get a bounds error:
using JuMP
using Ipopt
m = Model(Ipopt.Optimizer)
fmax(y...) = max(y...)
JuMP.register(m, :fmax, N, fmax, autodiff=true)
@variable(m,x[i=1:2, j=1:3])
@NLexpression(m, xbar[i=1:2, j=1:3], x[i,j] - fmax(x...)) # normalize by largest value in x
@NLexpression(m, p[i=1:2, j=1:3], exp(xbar[i,j]) / sum(xbar[k,l] for k=1:2, l=1:3))
@NLobjective(m, Max, sum(p[i,j] for i=1:2, j=1:3))
optimize!(m)
ERROR: BoundsError: attempt to access 2-element Vector{Float64} at index [1:6]
it is strange because it seems the subexpression is built correctly:
julia> m[:xbar]
2×3 Matrix{NonlinearExpression}:
subexpression[1]: x[1,1] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3]) … subexpression[5]: x[1,3] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3])
subexpression[2]: x[2,1] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3]) subexpression[6]: x[2,3] - fmax(x[1,1], x[2,1], x[1,2], x[2,2], x[1,3], x[2,3])
or, at least to me that looks like exactly what I wanted to achieve. What 2-element vector are we talking abou here?
thanks!