[3570545] | 1 | """ |
---|
| 2 | """ |
---|
| 3 | from __future__ import division |
---|
| 4 | import thread |
---|
| 5 | import numpy |
---|
| 6 | |
---|
| 7 | import diffev |
---|
| 8 | |
---|
| 9 | import fit,fitresult |
---|
| 10 | |
---|
| 11 | __all__ = ['DiffEv'] |
---|
| 12 | |
---|
| 13 | class DiffEv(fit.Fitter): |
---|
| 14 | """ |
---|
| 15 | Differential evolution optimizer |
---|
| 16 | |
---|
| 17 | This implements `park.fit.Fitter`. |
---|
| 18 | """ |
---|
| 19 | |
---|
| 20 | maxiter=1000 |
---|
| 21 | """Maximum number of iterations""" |
---|
| 22 | pop_scale=4 |
---|
| 23 | """Number of active points per dimension""" |
---|
| 24 | crossover_rate=0.9 |
---|
| 25 | """Amount of mixing in population""" |
---|
| 26 | Fscale=0.5 |
---|
| 27 | """Step size along difference vector""" |
---|
| 28 | tolerance=1e-5 |
---|
| 29 | """Fit tolerance""" |
---|
| 30 | |
---|
| 31 | def progress(self, k, n): |
---|
| 32 | self.handler.progress(k,n) |
---|
| 33 | def improvement(self, x, fx, ncalls=-1): |
---|
| 34 | self.handler.result.update(x,fx,-1) |
---|
| 35 | self.handler.improvement() |
---|
| 36 | |
---|
| 37 | def _call(self, objective, x0, bounds): |
---|
| 38 | x,fx,calls = diffev.diffev(objective, bounds, |
---|
| 39 | maxiter=self.maxiter, |
---|
| 40 | pop_scale=self.pop_scale, |
---|
| 41 | crossover_rate=self.crossover_rate, |
---|
| 42 | Fscale=self.Fscale, |
---|
| 43 | tolerance=self.tolerance, |
---|
| 44 | x0=x0, monitor=self) |
---|
| 45 | |
---|
| 46 | # Post the result |
---|
| 47 | self.handler.result.update(x, fx, calls) |
---|
| 48 | self.handler.result.calc_cov(objective) |
---|
| 49 | self.handler.done = True |
---|
| 50 | self.handler.finalize() |
---|
| 51 | |
---|
| 52 | def _fit(self, objective, x0, bounds): |
---|
| 53 | self._threaded(self._call, objective, x0, bounds) |
---|
| 54 | |
---|
| 55 | if __name__ == "__main__": |
---|
| 56 | fit.demo2(fitter=DiffEv()) |
---|