2

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?

raskolnikov
  • 235
  • 1
  • 6
  • 2
    Note that if you know more about function and inputs the search can be much more efficient - e.g. you could use tools such as JuMP if you could write it as mathematical programming problem such as MIP, LP, QP – Przemyslaw Szufel Apr 22 '21 at 23:26

2 Answers2

1

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)
BoZenKhaa
  • 782
  • 1
  • 6
  • 22
1

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)

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107