Julia: Minimise a function with multiple arguments

2019-05-30 04:38发布

I am trying to minimise a function with multiple arguments with the Optim.jl library, using a BFGS algorithm.

In the GitHub website of the Optim library, I found the following working example:

using Optim
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
result        = optimize(rosenbrock, zeros(2), BFGS())

Let's say that my objective function is:

fmin(x, a) = (1.0 - x[1])^a + 100.0 * (x[2] - x[1]^2)^(1-a)

How can I pass the additional - constant - argument a using optimize?

1条回答
【Aperson】
2楼-- · 2019-05-30 05:03

The simplest way is to pass an anonymous function of one variable which calls your original function with the parameters set. For example, using a variant of your fmin:

julia> fmin(x, a) = (1.0 - x[1])^a + 100.0 * (x[2] - x[1]^2)^(a)
fmin (generic function with 1 method)

julia> r = optimize(x->fmin(x, 2), zeros(2), BFGS());

julia> r.minimizer, r.minimum
([1.0,1.0],5.471432684244042e-17)

Alternatively you can make a separate named function of one variable which closes over whatever parameters you like. There's no equivalent to the args in scipy.optimize.minimize in Python where you pass the non-varying arguments separately as a tuple, AFAIK.

查看更多
登录 后发表回答