Re: Scipy differential_evolution initial guess x0 values?
Warren Weckesser <[email protected]> Tue, 9 Feb 2021 11:02:46 -0500
| Newsgroups | gmane.comp.python.scientific.user |
|---|---|
| Message-ID | <CAGzF1udfvZ5cWmS5tpACfvTMy2VRQEoexG495DYq2on5_ijDSA@mail.gmail.com> |
On 2/8/21, Haapa Mik <[email protected]> wrote: > How to pass some good x0 values to differential_evolution so that it does > not have to start from "beginning" which is something 1.0435e+16 and then > slowly decreasing ... > > My simplified example. Scoring function func() returns the sum of (yestimate > - y)^2 ... sum of error squares and DE is going to minimize it. > > def func(parameters, *data): > k1,k2,k3,v0 = parameters > c,j,afff = data > result = 0 > for i in range(len(c)): > result += ( k1*c[i] + k2*j[i] + k3*(j[i]/c[i]) + v0 - (afff[i]) )**2 > return result > > ... > > result = differential_evolution(func, bounds, args=(args), > updating='immediate', workers=1, disp=True, tol=0) > > ... > > $ python3 test.py > > differential_evolution step 5: f(x)= 8.68165e+13 > differential_evolution step 6: f(x)= 3.0159e+13 > differential_evolution step 7: f(x)= 5.72267e+11 > differential_evolution step 8: f(x)= 5.72267e+11 > differential_evolution step 9: f(x)= 5.72267e+11 > differential_evolution step 10: f(x)= 5.72267e+11 > printing result.x > [-9.96712308e-04 1.31194421e-03 -9.99999813e+01 1.63032881e+02] > printing result.fun > 229654.91015705158 > > How to pass these x0 values [-9.96712308e-04 1.31194421e-03 > -9.99999813e+01 1.63032881e+02] to differential_evolution? > The `init` parameter allows you to specify the initial population that is used by the algorithm. The parameter is expected to be an array containing at least five guesses. If you have a pretty good guess for the solution, you could create a cluster of candidates around that guess. Here's a simple example. The objective function has a minimum at [1, 1]. ----- In [104]: from scipy.optimize import differential_evolution In [105]: def objective(x): ...: x = np.array(x) ...: return np.sum((x - 1)**2) ...: In [106]: differential_evolution(objective, [[-25, 25], [-25, 25]], tol=1e-9) Out[106]: fun: 1.5777218104420236e-30 message: 'Optimization terminated successfully.' nfev: 2793 nit: 92 success: True x: array([1., 1.]) ----- Create a cluster of 15 points around [1, 1], and use them as the init parameter. ----- In [130]: x0 = np.random.multivariate_normal([1, 1], 0.02*np.eye(2), size=15) In [131]: x0 Out[131]: array([[0.99181517, 1.24919039], [1.10938275, 0.97639551], [0.6991003 , 1.05940167], [1.18903841, 0.990736 ], [1.1369098 , 0.94895271], [0.64620631, 1.19959699], [0.86884416, 1.07479914], [0.93342624, 1.15701549], [0.93504748, 1.16624753], [0.92176135, 1.05270535], [1.07944635, 1.03205849], [0.94648726, 0.99061289], [1.14088686, 0.89811341], [1.23903334, 1.09125584], [1.12380905, 0.88606074]]) In [132]: differential_evolution(objective, [[-25, 25], [-25, 25]], tol=1e-9, init=x0) Out[132]: fun: 1.5777218104420236e-30 message: 'Optimization terminated successfully.' nfev: 1173 nit: 77 success: True x: array([1., 1.]) ----- Warren > Br, MH > _______________________________________________ > SciPy-User mailing list > [email protected] > https://mail.python.org/mailman/listinfo/scipy-user >