Is there a better way to write this?
@pipe [(1,2),(2,3)] .|> (f(x) = (x-_)^2 + (x-_)^2 ) .|> optimize(_, -10,10).minimizer
I’d like to minimize a function over a given variable for different values of the other two variables.
Can the optimize function be directly vectorised ? It didn’t seem to work for me.
v = [(1, 2), (2, 3)]
[optimize(x -> sum(abs2, x .- y), -10, 10).minimizer for y in v]
Or equivalently with map-do
map(v) do y
optimize(x -> sum(abs2, x .- y), -10, 10).minimizer
Thanks tomerarnon. Much appreciated
realised there is also this
( (a,b) -> optimize( x -> (x-a)^2 + (x-b)^2, -10,10).minimizer ).( [1,2], [2,3] )
Realize that you are not actually “vectorizing” the optimization in any meaningful way with the above solutions: you are just calling
optimize twice in sequence.
(That’s okay! Julia is not a language where you need to “vectorize” everything to get good performance. And in general it is tricky to benefit from sharing computations between optimizations with different parameters, although there are algorithms to compute the Pareto front that attempt this. I suppose you could try numerical continuation of the KKT conditions to track a local optimum as you vary parameters continuously.)
Thanks Steven. Perhaps Vectorize was the wrong word here. in this case its not about performance. I’m just trying to find the most concise code. Often the function. “dot” notation allows this but it doesn’t seem to work in the case of optimize so this is the next best i think in terms of readability
Have you considered a
for loop? I suspect that an ordinary loop is probably clearer than any of the suggestions above.
I’ve always hated
for loops. but i guess they have their place:)