Suppose I have an objective function f(a,b,c). I want to find the value of b that minimizes it, holding a and c constant, and to experiment with different combinations of a and c, I prefer not to write f(a,b,c) as g(b).
from scipy.optimize import minimize
def f(a,b,c):
return((a+1)**2 + b + c/2)
res = minimize(f, x0=1, args=(a,c,),)
print(res.x)
Then how do I specify that b is the parameter that f(a,b,c) should be minimized with respect to? Does that parameter have to be expressed as x? Or should I make b the first argument of f?
As the documentation states, the signature of the function should be fun(x, *args)
where x
is the parameter that is minimized for. So you can just use a small wrapper around your original function:
res = minimize(lambda b, a, c: f(a, b, c), x0=1, args=(a, c))