简体   繁体   English

如何使用 scipy.optimize.minimize() 指定目标 function 最小化的参数?

[英]How to specify the parameter an objective function is minimized with respect to, using scipy.optimize.minimize()?

Suppose I have an objective function f(a,b,c) .假设我有一个目标 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) .我想找到最小化它的b值,保持ac不变,并尝试ac的不同组合,我不喜欢将f(a,b,c)写为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?那么我如何指定bf(a,b,c)应该最小化的参数? Does that parameter have to be expressed as x ?该参数是否必须表示为x Or should I make b the first argument of f ?或者我应该将b作为f的第一个参数?

As the documentation states, the signature of the function should be fun(x, *args) where x is the parameter that is minimized for.正如文档所述,function 的签名应该是fun(x, *args)其中x是最小化的参数。 So you can just use a small wrapper around your original function:因此,您可以在原始 function 周围使用一个小包装器:

res = minimize(lambda b, a, c: f(a, b, c), x0=1, args=(a, c))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scipy.optimize.minimize目标函数ValueError - Scipy.optimize.minimize objective function ValueError 哪个变量通过scipy.optimize.minimize最小化/如何工作? - Which variable is minimized by scipy.optimize.minimize/How does it work? 将约束应用于scipy.optimize.minimize?中的目标函数? 如0 &lt;=目标&lt;= 1 - Apply constraints to the objective function in scipy.optimize.minimize? Such as 0 <= objective <= 1 Scipy.optimize.minimize 目标 function 必须返回一个标量 - Scipy.optimize.minimize Objective function must return a scalar scipy.optimize.minimize 跟踪目标函数 - scipy.optimize.minimize keep track of objective function 使用 SciPy.optimize.minimize 从 function 中提取非最小化值 - Pull non-minimized values out of function being minimized with SciPy.optimize.minimize 当你想要计算梯度和目标函数时,如何使用scipy.optimize.minimize函数? - How to use scipy.optimize.minimize function when you want to compute gradient along with the objective function? 除了自变量外,如何为scipy.optimize.minimize的目标函数提供附加输入 - How to give additional input to objective function of scipy.optimize.minimize other than independent variables 如何编写scipy.optimize.minimize()的参数 - how to write the scipy.optimize.minimize()'s parameter 如何将 scipy.optimize.minimize 用于具有 3 个变量的 function? - How to use scipy.optimize.minimize for function with 3 variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM