Skip to content

Instantly share code, notes, and snippets.

@hengzhe-zhang
Last active May 11, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengzhe-zhang/8e5992a63a70341978b5a26c51fb5ee5 to your computer and use it in GitHub Desktop.
Save hengzhe-zhang/8e5992a63a70341978b5a26c51fb5ee5 to your computer and use it in GitHub Desktop.
DiffEvol
class DiffEvol(object):
"""
Implements the differential evolution optimization method by Storn & Price
(Storn, R., Price, K., Journal of Global Optimization 11: 341--359, 1997)
:param fun:
the function to be minimized
:param bounds:
parameter bounds as [npar,2] array
:param npop:
the size of the population (5*D - 10*D)
:param f: (optional)
the difference amplification factor. Values of 0.5-0.8 are good in most cases.
:param c: (optional)
The cross-over probability. Use 0.9 to test for fast convergence, and smaller
values (~0.1) for a more elaborate search.
:param seed: (optional)
Random seed
:param maximize: (optional)
Switch setting whether to maximize or minimize the function. Defaults to minimization.
"""
def __init__(self, fun, bounds, npop, f=None, c=None, seed=None, maximize=False, vectorize=False, cbounds=(0.25, 1),
fbounds=(0.25, 0.75), pool=None, min_ptp=1e-2, args=[], kwargs={}):
if seed is not None:
rseed(seed)
self.minfun = _function_wrapper(fun, args, kwargs)
self.bounds = asarray(bounds)
self.n_pop = npop
self.n_par = self.bounds.shape[0]
self.bl = tile(self.bounds[:, 0], [npop, 1])
self.bw = tile(self.bounds[:, 1] - self.bounds[:, 0], [npop, 1])
self.m = -1 if maximize else 1
self.pool = pool
self.args = args
if self.pool is not None:
self.map = self.pool.map
else:
self.map = map
self.periodic = []
self.min_ptp = min_ptp
self.cmin = cbounds[0]
self.cmax = cbounds[1]
self.cbounds = cbounds
self.fbounds = fbounds
self.seed = seed
self.f = f
self.c = c
self._population = asarray(self.bl + random([self.n_pop, self.n_par]) * self.bw)
self._fitness = zeros(npop)
self._minidx = None
self._trial_pop = zeros_like(self._population)
self._trial_fit = zeros_like(self._fitness)
if vectorize:
self._eval = self._eval_vfun
else:
self._eval = self._eval_sfun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment