basti
basti

Reputation: 23

callback in scipy.optimize does not call first value

I've got this simple Problem with callbacks in scipy.

I'm using optimize.minimize with values (func, x0, callback=callbackFunc). The callback function does work, BUT only returns the values after step 1.

x0=(240.,220.)
Nfeval=0
interim_res
optimize.minimize(func, x0, callback=callbackFunc)
def callbackFunc(X):
    global Nfeval, interim_res
    print('{0:4d}   {1: 3.6f}   {2: 3.6f}'.format(Nfeval, X[0], X[1]))
    Nfeval += 1
    interim_res.append([X[0], X[1]])

So in my opinion it should start with

0    240    220
...

BUT first return is:

0    173.345    159.56

Is this the usual way minimize works or do I somehow interpret something wrong?

Upvotes: 2

Views: 2354

Answers (1)

FernAndr
FernAndr

Reputation: 1608

The minimize documentation says for callback (emphasis mine):

Called after each iteration, as callback(xk), where xk is the current parameter vector.

Hence, it should just be the way minimize works, that must be the result after the first iteration, not the value it starts with. If you want to be fully sure that it starts with the value you want, you can always print the argument that is passed from minimize to func on each call.

Upvotes: 4

Related Questions