8

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

ehfaafzv
  • 181
  • 1
  • 2
  • 7
  • What errors do you get? And why is `x0` a vector of length 3 when you try to do `abs(x-v_3)` in `obj`? – user2357112 May 09 '15 at 03:22
  • I think the simplified code here could not generate the errors I got. So I guess I can change the way I ask: is it the right way to use minimize()? I don't think I told minimize() which variable should be "x" and which variables are acutally static. – ehfaafzv May 09 '15 at 03:33
  • @user2357112 Somehow I guess I solved the problem partially. I think I may interprete the minimize() in this way. It always take the first argument (if multiple) as the independent variable. And I need to claim other variables using args=(other variables). And they should be in the same order. Is this correct? – ehfaafzv May 09 '15 at 04:04

2 Answers2

7

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
mynameisvinn
  • 341
  • 4
  • 10
  • what is the purpose of the initial guess so? – keramat Jan 03 '20 at 15:49
  • 3
    imagine youre searching through some mathematical space, one that contains the optimal value (ie a global minimum in the form of a valley). the initial guess is just a starting point - you have to start somewhere. ultimately it doesnt matter where you start; what matters is where you end up. – mynameisvinn Jan 05 '20 at 00:38
4

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."

Community
  • 1
  • 1
ehfaafzv
  • 181
  • 1
  • 2
  • 7