Changeset 769
- Timestamp:
- 11/14/14 16:34:38 (18 months ago)
- Location:
- mystic
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/grid.py
r713 r769 25 25 def samplepts(lb,ub,npts): 26 26 """ 27 takes upper and lower bounds (e.g. ub = [2,4], lb = [0,3])27 takes lower and upper bounds (e.g. lb = [0,3], ub = [2,4]) 28 28 produces a list of sample points s = [[1,3],[1,4],[2,3],[2,4]] 29 29 30 30 Inputs: 31 l ower bounds-- a list of the lower bounds32 u pper bounds-- a list of the upper bounds31 lb -- a list of the lower bounds 32 ub -- a list of the upper bounds 33 33 npts -- number of sample points 34 34 """ … … 40 40 41 41 42 def randomly_bin(nbins, ndim): 43 """generate n bins randomly gridded across ndim dimensions""" 42 def randomly_bin(N, ndim=None, ones=True, exact=True): 43 """ 44 generate N bins randomly gridded across ndim dimensions 45 46 Inputs: 47 N -- integer number of bins, where N = prod(bins) 48 ndim -- integer length of bins, thus ndim = len(bins) 49 ones -- if False, prevent bins from containing "1s", wherever possible 50 exact -- if False, find N-1 bins for prime numbers 51 """ 52 if ndim == 0: return [] 53 if N == 0: return [0] if ndim else [0]*ndim 44 54 from itertools import chain 45 55 from random import random … … 54 64 if n == 1: 55 65 return result 56 result = factors(nbins) 57 result += [1] * (ndim - (len(result) / ndim)) 58 result = sorted(result, key=lambda v: random()) 66 result = factors(N) 67 dim = nfact = len(result) 68 prime = nfact == 1 69 if ndim: result += [1] * (ndim - (nfact / ndim)); dim = ndim 70 elif ones: result += [1] # add some 'randomness' by adding a "1" 71 # if ones, mix in the 1s; otherwise, only use 1s when ndim < len(result) 72 if ones: result = sorted(result, key=lambda v: random()) 73 else: result[:nfact] = sorted(result[:nfact], key=lambda v: random()) 59 74 from numpy import prod 60 return [prod(result[i::ndim]) for i in range(ndim)] 75 result = [prod(result[i::dim]) for i in range(dim)] 76 # if not ones, now needs a full sort to sort in the 1s 77 if not ones: result = sorted(result, key=lambda v: random()) 78 elif not ndim and 1 in result: result.remove(1) # remove the added "1" 79 # if it's a prime, then do N-1 if exact=False 80 if not exact and N > 3 and prime: 81 result = randomly_bin(N-1, ndim, ones) 82 return result 61 83 62 84 -
mystic/mystic/abstract_ensemble_solver.py
r760 r769 120 120 if isinstance(nbins, int): 121 121 from mystic.math.grid import randomly_bin 122 nbins = randomly_bin(nbins, dim )122 nbins = randomly_bin(nbins, dim, ones=True, exact=True) 123 123 self._nbins = nbins 124 124 npts = 1 -
mystic/mystic/ensemble.py
r764 r769 366 366 367 367 368 def lattice(cost,ndim,nbins= 2,args=(),bounds=None,ftol=1e-4,maxiter=None, \368 def lattice(cost,ndim,nbins=None,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 369 369 maxfun=None,full_output=0,disp=1,retall=0,callback=None,**kwds): 370 370 """Minimize a function using the lattice ensemble solver. … … 381 381 cost -- the Python function or method to be minimized. 382 382 ndim -- dimensionality of the problem. 383 nbins -- tuple of number of bins in each dimension. 383 nbins -- tuple of number of bins in each dimension. [default = (2,)*ndim] 384 384 385 385 Additional Inputs: … … 448 448 termination = VTRChangeOverGeneration(ftol) 449 449 450 if isinstance(nbins, int): nbins = [nbins]*ndim450 if nbins is None: nbins = 2**ndim 451 451 452 452 solver = LatticeSolver(ndim,nbins)
Note: See TracChangeset
for help on using the changeset viewer.