Changeset 769


Ignore:
Timestamp:
11/14/14 16:34:38 (18 months ago)
Author:
mmckerns
Message:

add ones and exact keyword options to math.grid.randomly_bin;
lattice solver now takes same nbins as LatticeSolver? (defaults to 2ndim)

Location:
mystic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • mystic/_math/grid.py

    r713 r769  
    2525def samplepts(lb,ub,npts): 
    2626    """ 
    27 takes upper and lower bounds (e.g. ub = [2,4], lb = [0,3]) 
     27takes lower and upper bounds (e.g. lb = [0,3], ub = [2,4]) 
    2828produces a list of sample points s = [[1,3],[1,4],[2,3],[2,4]] 
    2929 
    3030Inputs: 
    31     lower bounds  --  a list of the lower bounds 
    32     upper bounds  --  a list of the upper bounds 
     31    lb  --  a list of the lower bounds 
     32    ub  --  a list of the upper bounds 
    3333    npts  --  number of sample points 
    3434    """ 
     
    4040 
    4141 
    42 def randomly_bin(nbins, ndim): 
    43     """generate n bins randomly gridded across ndim dimensions""" 
     42def randomly_bin(N, ndim=None, ones=True, exact=True): 
     43    """ 
     44generate N bins randomly gridded across ndim dimensions 
     45 
     46Inputs: 
     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 
    4454    from itertools import chain 
    4555    from random import random 
     
    5464            if n == 1: 
    5565                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()) 
    5974    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 
    6183 
    6284 
  • mystic/mystic/abstract_ensemble_solver.py

    r760 r769  
    120120        if isinstance(nbins, int): 
    121121            from mystic.math.grid import randomly_bin 
    122             nbins = randomly_bin(nbins, dim) 
     122            nbins = randomly_bin(nbins, dim, ones=True, exact=True) 
    123123        self._nbins           = nbins 
    124124        npts = 1 
  • mystic/mystic/ensemble.py

    r764 r769  
    366366 
    367367 
    368 def lattice(cost,ndim,nbins=2,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
     368def lattice(cost,ndim,nbins=None,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
    369369            maxfun=None,full_output=0,disp=1,retall=0,callback=None,**kwds): 
    370370    """Minimize a function using the lattice ensemble solver. 
     
    381381    cost -- the Python function or method to be minimized. 
    382382    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] 
    384384 
    385385Additional Inputs: 
     
    448448        termination = VTRChangeOverGeneration(ftol) 
    449449 
    450     if isinstance(nbins, int): nbins = [nbins]*ndim 
     450    if nbins is None: nbins = 2**ndim 
    451451 
    452452    solver = LatticeSolver(ndim,nbins) 
Note: See TracChangeset for help on using the changeset viewer.