[英]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个变量b1 , b2和c3的三个超越方程。 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.