I’m not sure I understand correctly what the specific setting is for you but I’ll try.
You can do the following:
# I'm using a buffer array here so I don't have to allocate a new
# array when going from length 3 to 4.
function wrapper(optim_array, buffer_array, index, value)
found = 0
for i in 1:length(buffer_array)
if i == index
buffer_array[i] = value
found = 1
else
buffer_array[i] = optim_array[i - found]
end
end
@show buffer_array
cost_function(buffer_array)
end
buffer_array = zeros(4)
optimize(x -> wrapper(x, buffer_array, 3, 5), ...)
It hasn’t zero overhead but pretty close to it.
cost_function(x) = sum(x)
@btime wrapper($(ones(3)), $(zeros(4)), 3, 5)
11.918 ns (0 allocations: 0 bytes)
8.0