minimize a function with multiple argument in Julia

259 views Asked by At

I have a function which depends on a let say N input x=(x1,x2,x3,x4,...,xN) and I have a set of M possible vectors p = ( (p11,p12,p13,...,p1N),...,(pM1,pM2,...,pMN))

I would like to find the value of the minimum for which j in p[j] the minimum is realized in the Julia programming language.

I tried to use the minimum function (here I set N=5)

minimum([x1,x2,x3,x4,x5] -> fmin(x1,x2,x3,x4,x5), p)

but it does not work and also it does not give the value of j at which the minimum is realised. Any suggestion?

2

There are 2 answers

0
BoZenKhaa On

You can use the splat operator ... to expand the vector into the function arguments, e.g., for N=3:

function f(x,y,z)
    return x+y+z
end

p = [[1,1,1], [2,2,2], [3,3,3]]

minimum(vector -> f(vector...), p)
0
Bogumił Kamiński On

In order to find the minimum you can do:

julia> fmin(x1, x2, x3) = x1+x2+x3
fmin (generic function with 1 method)

julia> p = [(1,1,1), (1,2,3), (-1,-2,10), (2,3,0)];

julia> minp = minimum(v -> fmin(v...), p)
3

Finding a minimizer can be done e.g. by:

julia> findall(v -> fmin(v...) == minp, p)
1

(note that you get a vector as there can be multiple indices for which the expression is minimized)