简体   繁体   English

哪个变量通过scipy.optimize.minimize最小化/如何工作?

[英]Which variable is minimized by scipy.optimize.minimize/How does it work?

From scipy tutorial I don't really get how optimize.minimize works. 从scipy教程中,我并没有真正了解Optimiz.minimize的工作原理。 I want to minimize c3 in following set of equations: 我想在以下方程组中最小化c3:

0 = cos(b1)+ cos(b2)- 0.0166
0 = sin(b1)+ sin(b2)+ 0.3077*c3 - 0.6278
0 = cos(b1)- cos(b2)+ 5.4155*c3 - 4.3547

in intervals: 每隔一段时间:

c3[0,1]
b1,b2[0,2*pi]

Here is my code: 这是我的代码:

def fun(x):
    return 4.9992-5.7233*x[0]-2*np.cos(x[2])-np.sin(x[2])-np.sin(x[1])
bnds = ((0,1),(0,2*np.pi),(0,2*np.pi))
i =  optimize.minimize(fun, (0.05,np.pi*0.5,np.pi), method='SLSQP', bounds=bnds)

The Output is 输出是

  status: 0
 success: True
    njev: 6
    nfev: 30
     fun: -3.9601679766628886
       x: array([ 1.        ,  1.57079633,  0.46367497])
 message: 'Optimization terminated successfully.'
     jac: array([ -5.72330004e+00,   0.00000000e+00,   6.11841679e-05,
         0.00000000e+00])
     nit: 6

The result is the same in L-BFGS-B My understanding is that here c3 has become 1 which is still ok, but I wanted it to be lower. 结果在L-BFGS-B是相同的。我的理解是,此处c3已变为1,这仍然可以,但是我希望将其降低。 If I apply fsolve on the function it finds a root for c3=0.46. 如果我对函数应用fsolve ,它将找到c3 = 0.46的根。 btw why do I have to write x[0],x[1] and x[2] instead of c3,b1,b2 in the code? btw为什么我必须在代码中写x [0],x [1]和x [2]而不是c3,b1,b2? Is there a more clever way using constrains eg? 有没有更聪明的使用约束的方法?

You have three transcendent equations for the 3 variables b1 , b2 and c3 . 您具有3个变量b1b2c3的三个超越方程。 What you need to do is to solve this 3 equations for your variables. 您需要做的是为您的变量求解这3个方程。 As the equations are transcendent, it's possible that there is no solution, one solution or many solutions. 由于方程式是超验的,因此可能没有解决方案,一个解决方案或许多解决方案。 Solving them with Mathematica gives: Mathematica解决它们可以得到:

In[30]:= eq1 = 0 == Cos[b1] + Cos[b2] - 0.0166;
eq2 = 0 == Sin[b1] + Sin[b2] + 0.3077*c3 - 0.6278;
eq3 = 0 == Cos[b1] - Cos[b2] + 5.4155*c3 - 4.3547;

In[42]:= Reduce[{eq1, eq2, eq3, b1 >= 0, b1 <= 2*Pi, b2 >= 0, 
  b2 <= 2*Pi, c3 >= 0, c3 <= 1}, {b1, b2, c3}]

During evaluation of In[42]:= Reduce::ratnz: Reduce was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>

Out[42]= b1 == 0.214076 && b2 == 2.85985 && c3 == 0.446303

So there is in fact only one solution. 因此,实际上只有一种解决方案。 Now you could also use root to find this solution of your system of equations numerically: 现在,您还可以使用root在数字上找到方程组的此解决方案:

import scipy as sp
import scipy.optimize

def f(x):
    b1, b2, c3 = x
    e1 = sp.cos(b1) + sp.cos(b2) - 0.0166
    e2 = sp.sin(b1) + sp.sin(b2) + 0.3077*c3 - 0.6278
    e3 = sp.cos(b1) - sp.cos(b2) + 5.4155*c3 - 4.3547
    return e1, e2, e3

res = sp.optimize.root(f, [0.5, 1.0, 1.0])
print('b1 = {}, b2 = {}, c3 = {}'.format(*res.x))

gives: 给出:

b1 = 0.214076256767, b2 = 2.85985240432, c3 = 0.446302998585

What you did with minimize is to minimize the sum of the three equations which is not equivalent to "minimize c3 in (the) set of equations". 您执行的minimize是使三个方程式的总和最小化,这不等同于“使方程组中的c3最小化”。

minimize is not made for what you want. minimize不是针对您想要的。 'Minimize' does something like minimize find the x that minimizes f(x) = x**2 . “最小化”的作用类似于最小化找到使f(x) = x**2最小化的f(x) = x**2 The answer would obviously be 'x=0'. 答案显然是“ x = 0”。

You have to write 'x[0]' because of the interface of 'minimize'. 由于接口“最小化”,因此必须写“ x [0]”。 the function simply expects that you give the parameters you want to minimize in vector form. 该函数只是希望您以矢量形式给出要最小化的参数。

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

相关问题 为什么 scipy.optimize.minimize 不适用于约束和初始值 0 - Why scipy.optimize.minimize does not work with a constraint and initial value 0 如何使用 scipy.optimize.minimize() 指定目标 function 最小化的参数? - How to specify the parameter an objective function is minimized with respect to, using scipy.optimize.minimize()? 如何在Scipy.optimize.minimize中显示结果 - how to present results in Scipy.optimize.minimize 如何使用scipy.optimize.minimize - How to use scipy.optimize.minimize 使用 SciPy.optimize.minimize 从 function 中提取非最小化值 - Pull non-minimized values out of function being minimized with SciPy.optimize.minimize 为什么我的优化(scipy.optimize.minimize)不起作用并返回初始值? - Why does my optimization (scipy.optimize.minimize) not work and return the initial values instead? scipy.optimize.minimize()中的“ fun”应采用哪种格式? - Which format should the “fun” in scipy.optimize.minimize() have? 为什么 scipy.optimize.minimize 找不到最小值? - Why does scipy.optimize.minimize not find the minimum? scipy.optimize.minimize不更新重量参数 - scipy.optimize.minimize does not update weight parameters scipy.optimize.minimize 在多变量优化中不收敛 - scipy.optimize.minimize does not converge in multivariable optimization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM