- Timestamp:
- 04/18/16 14:14:14 (4 weeks ago)
- Location:
- mystic
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/__init__.py
r855 r862 29 29 samplepts -- generate a set of randomly sampled points 30 30 almostEqual -- test if equal within some absolute or relative tolerance 31 Distribution -- generate a sampling distribution instance 31 32 32 33 … … 43 44 import distance as paramtrans 44 45 46 47 # distribution object 48 class Distribution(object): 49 """ 50 Sampling distribution for mystic optimizers 51 """ 52 def __init__(self, generator=None, *args, **kwds): 53 """ 54 generate a sampling distribution with interface dist(size=None) 55 56 input:: 57 - generator: a 'distribution' method from scipy.stats or numpy.random 58 - args: positional arguments for the distribtution object 59 - kwds: keyword arguments for the distribution object 60 61 note:: 62 this method only accepts numpy.random methods with the keyword 'size' 63 """ 64 from mystic.tools import random_state 65 rng = random_state(module='numpy.random') 66 if generator is None: generator = rng.random 67 if getattr(generator, 'rvs', False): 68 d = generator(*args, **kwds) 69 self.rvs = lambda size=None: d.rvs(size=size, random_state=rng) 70 else: 71 d = getattr(rng, generator.__name__) 72 self.rvs = lambda size=None: d(size=size, *args, **kwds) 73 return 74 def __call__(self, size=None): 75 """generate a sample of given size (tuple) from the distribution""" 76 return self.rvs(size) 77 45 78 # end of file -
mystic/_math/grid.py
r855 r862 9 9 """ 10 10 11 def gridpts(q ):11 def gridpts(q, dist=None): 12 12 """ 13 13 takes a list of lists of arbitrary length q = [[1,2],[3,4]] 14 14 and produces a list of gridpoints g = [[1,3],[1,4],[2,3],[2,4]] 15 16 Note: 17 if a mystic.math.Distribution is provided, use it to inject randomness 15 18 """ 16 19 w = [[] for i in range(len(q[-1]))] … … 20 23 w[l].append(q[j][k]) 21 24 if j: w += [i[:] for i in w[:]*(len(q[j-1])-1)] 22 return [list(reversed(w[i])) for i in range(len(w))] 25 pts = [list(reversed(w[i])) for i in range(len(w))] 26 # inject some randomness 27 if dist is None: return pts 28 if not len(pts): return pts 29 pts += dist((len(pts),len(pts[0]))) 30 return pts.tolist() 23 31 24 32 25 def samplepts(lb,ub,npts ):33 def samplepts(lb,ub,npts,dist=None): 26 34 """ 27 35 takes lower and upper bounds (e.g. lb = [0,3], ub = [2,4]) … … 32 40 ub -- a list of the upper bounds 33 41 npts -- number of sample points 42 dist -- a mystic.math.Distribution instance 34 43 """ 35 44 from mystic.math.samples import random_samples 36 q = random_samples(lb,ub,npts) 37 q = [list(i) for i in q] 38 q = zip(*q) 39 return [list(i) for i in q] 45 q = random_samples(lb,ub,npts,dist) 46 return q.T.tolist() 47 #q = [list(i) for i in q] 48 #q = zip(*q) 49 #return [list(i) for i in q] 40 50 41 51 -
mystic/_math/samples.py
r855 r862 14 14 15 15 # SAMPLING # 16 def random_samples(lb,ub,npts=10000):16 def _random_samples(lb,ub,npts=10000): 17 17 """ 18 18 generate npts random samples between given lb & ub … … 32 32 33 33 34 def random_samples(lb,ub, npts=10000, dist=None, clip=False): 35 """ 36 generate npts samples from the given distribution between given lb & ub 37 38 Inputs: 39 dist -- a mystic.tools.Distribution instance 40 lower bounds -- a list of the lower bounds 41 upper bounds -- a list of the upper bounds 42 npts -- number of sample points [default = 10000] 43 clip -- if True, clip at bounds, else resample [default = False] 44 """ 45 if dist is None: 46 return _random_samples(lb,ub, npts) 47 import numpy as np 48 dim = len(lb) 49 pts = dist((npts,dim)) # transpose of desired shape 50 pts = np.clip(pts, lb, ub).T 51 if clip: return pts #XXX: returns a numpy.array 52 bad = ((pts.T == lb) + (pts.T == ub)).T 53 new = bad.sum() 54 _n, n = 1, 1000 #FIXME: fixed number of max tries 55 while new: 56 if _n == n: #XXX: slows the while loop... 57 raise RuntimeError('bounds could not be applied in %s iterations' % n) 58 pts[bad] = dist(new) 59 pts = np.clip(pts.T, lb, ub).T 60 bad = ((pts.T == lb) + (pts.T == ub)).T 61 new = bad.sum() 62 _n += 1 63 return pts #XXX: returns a numpy.array 64 65 34 66 def sample(f,lb,ub,npts=10000): 35 67 """ … … 43 75 """ 44 76 from numpy import transpose 45 pts = random_samples(lb, ub, npts)77 pts = _random_samples(lb, ub, npts) 46 78 47 79 failure = 0; success = 0 … … 68 100 from numpy import inf, transpose 69 101 from mystic.tools import wrap_bounds 70 pts = random_samples(lb, ub, npts)102 pts = _random_samples(lb, ub, npts) 71 103 f = wrap_bounds(f,lb,ub) 72 104 ave = 0; count = 0 … … 111 143 npts -- the number of points to sample [Default is npts=10000] 112 144 """ 113 pts = random_samples(lb, ub, npts)145 pts = _random_samples(lb, ub, npts) 114 146 return _pof_given_samples(f, pts) 115 147 … … 197 229 upper = [100.0, 30.0, 2.8] 198 230 199 pts = random_samples(lower,upper,num_sample_points)231 pts = _random_samples(lower,upper,num_sample_points) 200 232 print "randomly sampled points\nbetween %s and %s" % (lower, upper) 201 233 print pts -
mystic/mystic/abstract_ensemble_solver.py
r860 r862 118 118 # default settings for nested optimization 119 119 #XXX: move nbins and npts to _InitialPoints? 120 self._dist = None #kwds['dist'] if 'dist' in kwds else None 120 121 nbins = kwds['nbins'] if 'nbins' in kwds else [1]*dim 121 122 if isinstance(nbins, int): … … 206 207 raise NotImplementedError, "must be overwritten..." 207 208 208 def Set DistributionInitialPoints(self, dist):209 def SetSampledInitialPoints(self, dist=None): 209 210 """Generate Random Initial Points from Distribution (dist) 210 211 211 212 input:: 212 - dist: a scipy.stats distribution instance213 - dist: a mystic.math.Distribution instance 213 214 214 215 *** this method must be overwritten ***""" … … 272 273 return 273 274 275 def SetDistribution(self, dist=None): 276 """Set the distribution used for determining solver starting points 277 278 Inputs: 279 - dist: a mystic.math.Distribution instance 280 """ 281 from mystic.math import Distribution 282 if dist and Distribution not in dist.__class__.mro(): 283 dist = Distribution(dist) #XXX: or throw error? 284 self._dist = dist 285 return 286 274 287 def _InitialPoints(self): 275 288 """Generate a grid of starting points for the ensemble of optimizers -
mystic/mystic/abstract_solver.py
r861 r862 411 411 """ 412 412 from mystic.tools import random_state 413 prng = random_state(module='numpy.random')413 rng = random_state(module='numpy.random') 414 414 assert(len(mean) == self.nDim) 415 415 if var is None: … … 423 423 var = var * numpy.eye(self.nDim) 424 424 for i in range(len(self.population)): 425 self.population[i] = prng.multivariate_normal(mean, var).tolist()426 return 427 428 def Set DistributionInitialPoints(self, dist, *args):425 self.population[i] = rng.multivariate_normal(mean, var).tolist() 426 return 427 428 def SetSampledInitialPoints(self, dist=None): 429 429 """Generate Random Initial Points from Distribution (dist) 430 430 431 431 input:: 432 - dist: a scipy.stats distribution instance (or a numpy.random method) 433 434 note:: 435 this method only accepts numpy.random methods with the keyword 'size'""" 436 from mystic.tools import random_state 437 prng = random_state(module='numpy.random') 438 if not args and type(dist).__name__ is 'rv_frozen': #from scipy.stats 439 for i in range(self.nPop): 440 self.population[i] = dist.rvs(self.nDim, random_state=prng).tolist() 441 else: #from numpy.random with *args and size in kwds 442 for i in range(self.nPop): 443 self.population[i] = dist(*args, size=self.nDim).tolist() 432 - dist: a mystic.math.Distribution instance 433 """ 434 from mystic.math import Distribution 435 if dist is None: 436 dist = Distribution() 437 elif type(Distribution) not in dist.__class__.mro(): 438 dist = Distribution(dist) #XXX: or throw error? 439 for i in range(self.nPop): 440 self.population[i] = dist(self.nDim) 444 441 return 445 442 -
mystic/mystic/ensemble.py
r860 r862 38 38 class LatticeSolver(AbstractEnsembleSolver): 39 39 """ 40 parallel mapped optimization starting from the center of N grid points40 parallel mapped optimization starting from the centers of N grid points 41 41 """ 42 42 def __init__(self, dim, nbins=8): … … 72 72 # build a grid of starting points 73 73 from mystic.math import gridpts 74 return gridpts(bins )74 return gridpts(bins, self._dist) 75 75 76 76 77 77 class BuckshotSolver(AbstractEnsembleSolver): 78 78 """ 79 parallel mapped optimization starting from the N randompoints79 parallel mapped optimization starting from N uniform randomly sampled points 80 80 """ 81 81 def __init__(self, dim, npts=8): … … 104 104 # build a grid of starting points 105 105 from mystic.math import samplepts 106 return samplepts(lower,upper,npts )106 return samplepts(lower,upper,npts, self._dist) 107 107 108 108 … … 150 150 This function should return y', with y' == 0 when the encoded 151 151 constraints are satisfied, and y' > 0 otherwise. 152 dist -- an optional mystic.math.Distribution instance. If provided, 153 this distribution generates randomness in ensemble starting position. 152 154 153 155 Returns: (xopt, {fopt, iter, funcalls, warnflag, allfuncalls}, {allvecs}) … … 186 188 solver.SetEvaluationMonitor(evalmon) 187 189 solver.SetGenerationMonitor(stepmon) 190 if 'dist' in kwds: 191 solver.SetDistribution(kwds['dist']) 188 192 if 'penalty' in kwds: 189 193 solver.SetPenalty(kwds['penalty']) … … 271 275 This function should return y', with y' == 0 when the encoded 272 276 constraints are satisfied, and y' > 0 otherwise. 277 dist -- an optional mystic.math.Distribution instance. If provided, 278 this distribution generates randomness in ensemble starting position. 273 279 274 280 Returns: (xopt, {fopt, iter, funcalls, warnflag, allfuncalls}, {allvecs}) … … 307 313 solver.SetEvaluationMonitor(evalmon) 308 314 solver.SetGenerationMonitor(stepmon) 315 if 'dist' in kwds: 316 solver.SetDistribution(kwds['dist']) 309 317 if 'penalty' in kwds: 310 318 solver.SetPenalty(kwds['penalty'])
Note: See TracChangeset
for help on using the changeset viewer.