简体   繁体   中英

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

From scipy tutorial I don't really get how optimize.minimize works. I want to minimize c3 in following set of equations:

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. If I apply fsolve on the function it finds a root for 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? Is there a more clever way using constrains eg?

You have three transcendent equations for the 3 variables b1 , b2 and c3 . What you need to do is to solve this 3 equations for your variables. As the equations are transcendent, it's possible that there is no solution, one solution or many solutions. Solving them with Mathematica gives:

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:

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 is not made for what you want. 'Minimize' does something like minimize find the x that minimizes f(x) = x**2 . The answer would obviously be 'x=0'.

You have to write 'x[0]' because of the interface of 'minimize'. the function simply expects that you give the parameters you want to minimize in vector form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM