Using scipy to minimize a function that also takes non variational parameters

陌路散爱 提交于 2019-12-01 00:18:21

Read about the args argument to fmin in its docstring, and use

a = 1
x0 = 1
xopt = fmin(f, x0, xtol=1e-8, args=(a,))

The args argument is probably the right approach here, but here's another approach that's useful sometimes. First you write a wrapper function for f which will take take a function and an a value as inputs and return a new function where a is fixed.

def fix_a(f, a):
    def f_with_fixed_a(x):
        return f(x, a)
return f_with_fixed_a

Then you can call fmin like this:

xopt = fmin(fix_a(f, a), x0, xtol=1e-8)

If all you need to do is pass in a fixed a, using the args keyword of fmin is this is probably too verbose, but this approach is more flexible and can handle more complex situations (for example if you wanted to make a some function of x).

For your case the canonical solution by Warren is probably the right choice.

It is, however, worth noting that you can also optimize member functions of classes. In this case you have access to the class variables which can be used as arguments. This is especially useful if your code is already object based and adding a member function to optimize makes sense.

In your example this (overkill) solution would be:

class FunctionHolder(object):
    def __init__(self,a):
        self.a=a
    def f(self, x):
        return x*self.a

f1=FunctionHolder(1)
scipy.optimize.minimize(f1.f, x0)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!