Scipy differential_evolution initial guess x0 values?
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? Br, MH
On 2/8/21, Haapa Mik <haapamik@yahoo.com> 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 SciPy-User@python.org https://mail.python.org/mailman/listinfo/scipy-user
Scipy 1.7.0 will have an `x0` keyword that can specify an initial guess instead of having to specify an initial population.
This is great news! I am going to wait for Scipy 1.7 version release. The init= way is too difficult for me. Thank you both! -MH On Tuesday, February 9, 2021, 07:52:38 PM GMT+2, Andrew Nelson <andyfaff@gmail.com> wrote: Scipy 1.7.0 will have an `x0` keyword that can specify an initial guess instead of having to specify an initial population.
participants (3)
-
Andrew Nelson -
Haapa Mik -
Warren Weckesser