Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use scipy.optimize.minimize

I have a objective function, say obj(x, arg_1, arg_2) within f(), I have variable_3 = f(x, arg_1, arg_2) obj() will return abs(x-variable_3)

I need to minimize the returned value of obj() using scipy.optimize.minimize

I guess I need to do it like this:

def obj(x, arg_1, arg_2)
    v_3 = f(x, arg_1, arg_2)
    return abs(x-v_3)
x0 = 1
result = minimize(obj, x0, args = (arg_1, arg_2))

Is this correct? Why I alwasy get errors?

Alternatively, actually I can do it in this way:

def obj(x, v_3)
    return abs(x-v_3)
def myfun(arg_1, arg_2)
    x0 = 1
    v_3 = f(x0, arg_1, arg_2)
    result = minimize(obj, x0, args = v_3)
    return result

But obviously, this is what I want. So could somebody tell me how to do this minimization? Thanks

like image 605
ehfaafzv Avatar asked May 09 '15 03:05

ehfaafzv


2 Answers

for scipy.optimize.minimize, multiple arguments should be packed into a tuple, which will then be unpacked by the objective function during numerical optimization.

it should look something like this:

def obj(arguments)
    """objective function, to be solved."""
    x, arg_1, arg_2 = arguments[0], arguments[1], arguments[2] 
    v_3 = f(x, arg_1, arg_2)
    return abs(x-v_3)

x0 = 1
initial_guess = [1,1,1]  # initial guess can be anything
result = minimize(obj, initial_guess)
print result.x
like image 192
mynameisvinn Avatar answered Sep 22 '22 05:09

mynameisvinn


Hope it will not cause some IP problem, quoted the essential part of the answer here: from @lmjohns3, at Structure of inputs to scipy minimize function "By default, scipy.optimize.minimize takes a function fun(x) that accepts one argument x (which might be an array or the like) and returns a scalar. scipy.optimize.minimize then finds an argument value xp such that fun(xp) is less than fun(x) for other values of x. The optimizer is responsible for creating values of x and passing them to fun for evaluation.

But what if you happen to have a function fun(x, y) that has some additional parameter y that needs to be passed in separately (but is considered a constant for the purposes of the optimization)? This is what the args tuple is for. The documentation tries to explain how the args tuple is used Effectively, scipy.optimize.minimize will pass whatever is in args as the remainder of the arguments to fun, using the asterisk arguments notation: the function is then called as fun(x, *args) during optimization. The x portion is passed in by the optimizer, and the args tuple is given as the remaining arguments."

like image 20
ehfaafzv Avatar answered Sep 26 '22 05:09

ehfaafzv