Changeset 772


Ignore:
Timestamp:
11/21/14 14:33:33 (18 months ago)
Author:
mmckerns
Message:

fix: remove stray random.seed causing seeding of random number generation;
add random_state; convert rng to use random_state, seed to use random_seed;
seed faster run for examples/mpmap_desolve

Location:
mystic
Files:
35 edited

Legend:

Unmodified
Added
Removed
  • mystic/_math/grid.py

    r770 r772  
    5353    if N == 0: return [0] if ndim else [0]*ndim 
    5454    from itertools import chain 
    55     from random import random 
     55    from mystic.tools import random_state 
     56    random = random_state().random 
    5657    def factors(n): 
    5758        result = list() 
  • mystic/_math/measures.py

    r770 r772  
    2525""" 
    2626  from numpy import sum, array 
    27   from numpy.random import rand 
     27  from mystic.tools import random_state 
     28  rand = random_state().random 
    2829  # generate a list representing the weighted distribution 
    2930  wts = normalize(weights, mass) 
  • mystic/_math/samples.py

    r713 r772  
    2323    npts  --  number of sample points [default = 10000] 
    2424""" 
    25   from numpy.random import random 
     25  from mystic.tools import random_state 
    2626  dim = len(lb) 
    27   pts = random((dim,npts)) 
     27  pts = random_state(module='numpy.random').rand(dim,npts) 
    2828  for i in range(dim): 
    2929    pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] 
     
    4242    npts -- the number of points to sample [Default is npts=10000] 
    4343""" 
    44   from numpy.random import random 
    4544  from numpy import transpose 
    4645  pts = random_samples(lb, ub, npts) 
     
    189188####################################################################### 
    190189 
    191 def __test1(): 
    192   # From branches/UQ/math/samples.py 
    193   num_sample_points = 5 
    194   lower = [10.0, 0.0, 2.1] 
    195   upper = [100.0, 30.0, 2.8] 
    196  
    197   pts = random_samples(lower,upper,num_sample_points) 
    198   print "randomly sampled points\nbetween %s and %s" % (lower, upper) 
    199   print pts 
    200  
    201 def __test2(): 
    202   # From branches/UQ/math/cut.py 
    203   from mystic.tools import random_seed 
    204   random_seed(123) 
    205   lower = [-60.0, -10.0, -50.0] 
    206   upper = [105.0, 30.0, 75.0] 
    207  
    208   def model(x): 
    209     x1,x2,x3 = x 
    210     if x1 > (x2 + x3): return x1*x2 - x3 
    211     return 0.0 
    212  
    213   failure,success = sample(model,lower,upper) 
    214   pof = float(failure) / float(failure + success) 
    215   print "PoF using method 1: %s" % pof 
    216   random_seed(123) 
    217   print "PoF using method 2: %s" % sampled_pof(model,lower,upper) 
    218190 
    219191if __name__ == '__main__': 
     192 
     193  def __test1(): 
     194    # From branches/UQ/math/samples.py 
     195    num_sample_points = 5 
     196    lower = [10.0, 0.0, 2.1] 
     197    upper = [100.0, 30.0, 2.8] 
     198 
     199    pts = random_samples(lower,upper,num_sample_points) 
     200    print "randomly sampled points\nbetween %s and %s" % (lower, upper) 
     201    print pts 
     202 
     203  def __test2(): 
     204    # From branches/UQ/math/cut.py 
     205    from mystic.tools import random_seed 
     206    random_seed(123) 
     207    lower = [-60.0, -10.0, -50.0] 
     208    upper = [105.0, 30.0, 75.0] 
     209 
     210    def model(x): 
     211      x1,x2,x3 = x 
     212      if x1 > (x2 + x3): return x1*x2 - x3 
     213      return 0.0 
     214 
     215    failure,success = sample(model,lower,upper) 
     216    pof = float(failure) / float(failure + success) 
     217    print "PoF using method 1: %s" % pof 
     218    random_seed(123) 
     219    print "PoF using method 2: %s" % sampled_pof(model,lower,upper) 
     220 
     221    # run the tests 
    220222    __test1() 
    221223    __test2() 
  • mystic/examples/TEST_ffitPP2.py

    r723 r772  
    1818 
    1919if __name__ == '__main__': 
     20    import random 
    2021    from mystic.solvers import fmin 
    2122   #from mystic._scipyoptimize import fmin 
    22     import random 
    23     random.seed(123) 
     23    from mystic.tools import random_seed 
     24    random_seed(123) 
    2425 
    2526    import pp 
  • mystic/examples/TEST_ffitPP2_b.py

    r723 r772  
    1919 
    2020if __name__ == '__main__': 
     21    import random 
    2122    from mystic.solvers import fmin 
    2223   #from mystic._scipyoptimize import fmin 
    23     import random 
    24     random.seed(123) 
     24    from mystic.tools import random_seed 
     25    random_seed(123) 
    2526 
    2627    import pp 
  • mystic/examples/buckshot_example06.py

    r771 r772  
    2828try: 
    2929    from pathos.multiprocessing import ProcessingPool as Pool 
     30   #from pathos.pp import ParallelPythonPool as Pool 
    3031except ImportError: 
    3132    from mystic.python import PythonSerial as Pool 
     
    7273 
    7374    # dimensional information 
    74     import random 
    75     random.seed(123) 
     75    from mystic.tools import random_seed 
     76    random_seed(123) 
    7677    ndim = 9 
    7778    npts = 8 
  • mystic/examples/example06.py

    r722 r772  
    6262    # initial guess 
    6363    import random 
    64     random.seed(123) 
     64    from mystic.tools import random_seed 
     65    random_seed(123) 
    6566    ndim = 9 
    6667    x0 = [random.uniform(-100,100) for i in range(ndim)] 
  • mystic/examples/example11.py

    r722 r772  
    3030from mystic.termination import CandidateRelativeTolerance as CRT 
    3131from mystic.monitors import VerboseMonitor, Monitor 
    32 from mystic.tools import getch, random_seed 
     32from mystic.tools import getch 
    3333from mystic.math import poly1d 
    3434import pylab 
     
    8686    # initial guess 
    8787    import random 
    88     random.seed(123) 
     88    from mystic.tools import random_seed 
     89    random_seed(123) 
    8990    ndim = 9 
    9091    x0 = [random.uniform(-5,5) + chebyshev8coeffs[i] for i in range(ndim)] 
  • mystic/examples/lattice_example06.py

    r771 r772  
    2828try: 
    2929    from pathos.multiprocessing import ProcessingPool as Pool 
     30   #from pathos.pp import ParallelPythonPool as Pool 
    3031except ImportError: 
    3132    from mystic.python import PythonSerial as Pool 
     
    7273 
    7374    # dimensional information 
    74     import random 
    75     random.seed(123) 
     75    from mystic.tools import random_seed 
     76    random_seed(123) 
    7677    ndim = 9 
    77     nbins = [2,1,2,1,2,1,2,1,1] 
     78    nbins = 8 #[2,1,2,1,2,1,2,1,1] 
    7879 
    7980    # draw frame and exact coefficients 
  • mystic/examples/mpmap_desolve.py

    r721 r772  
    4141 
    4242ND = 9 
    43 NP = 40 
     43NP = 80 
    4444MAX_GENERATIONS = NP*NP 
    4545NNODES = NP/5 
     
    5858    random_seed(seed) 
    5959    print "first sequential..." 
    60     solver = DifferentialEvolutionSolver2(ND,NP)  #XXX: sequential 
     60    solver = DifferentialEvolutionSolver2(ND,NP) 
    6161    solver.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND) 
    6262    solver.SetEvaluationLimits(generations=MAX_GENERATIONS) 
     
    6767    print_solution( solver.bestSolution ) 
    6868 
    69     #''' 
    7069    random_seed(seed) 
    7170    print "\n and now parallel..." 
    72     solver2 = DifferentialEvolutionSolver2(ND,NP)  #XXX: parallel 
    73     solver2.SetMapper(Pool(NNODES).map) 
     71    solver2 = DifferentialEvolutionSolver2(ND,NP) 
     72    solver2.SetMapper(Pool(NNODES).map) # parallel 
    7473    solver2.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND) 
    7574    solver2.SetEvaluationLimits(generations=MAX_GENERATIONS) 
     
    7978    print "" 
    8079    print_solution( solver2.bestSolution ) 
    81     #''' 
    8280 
    8381# end of file 
  • mystic/examples/test_corana.py

    r766 r772  
    2222from mystic.strategy import Best1Exp, Rand1Exp 
    2323 
    24 import random 
    25 random.seed(123) 
     24from mystic.tools import random_seed 
     25random_seed(123) 
    2626 
    2727from mystic.models import corana 
  • mystic/examples/test_dejong3.py

    r713 r772  
    2525from mystic.models.dejong import step as DeJong3 
    2626 
    27 import random 
    28 random.seed(123) 
     27from mystic.tools import random_seed 
     28random_seed(123) 
    2929 
    3030ND = 5 
  • mystic/examples/test_dejong4.py

    r713 r772  
    2525from mystic.models.dejong import quartic as DeJong4 
    2626 
    27 import random 
    28 random.seed(123) 
     27from mystic.tools import random_seed 
     28random_seed(123) 
    2929 
    3030ND = 30 
  • mystic/examples/test_dejong5.py

    r713 r772  
    2323from mystic.models.dejong import shekel as DeJong5 
    2424 
    25 import random 
    26 random.seed(123) 
     25from mystic.tools import random_seed 
     26random_seed(123) 
    2727 
    2828ND = 2 
  • mystic/examples/test_ffit.py

    r726 r772  
    2727from mystic.tools import getch 
    2828 
    29 import random 
    30 random.seed(123) 
     29from mystic.tools import random_seed 
     30random_seed(123) 
    3131 
    3232# get the target coefficients and cost function 
  • mystic/examples/test_ffit2.py

    r713 r772  
    2323from mystic.models.poly import chebyshev16cost 
    2424 
    25 import random 
    26 random.seed(123) 
     25from mystic.tools import random_seed 
     26random_seed(123) 
    2727 
    2828# get the target coefficients 
  • mystic/examples/test_ffitC.py

    r723 r772  
    1818 
    1919if __name__ == '__main__': 
     20    import random 
    2021    from mystic.solvers import fmin 
    2122   #from mystic._scipyoptimize import fmin 
    22     import random 
    23     random.seed(123) 
     23    from mystic.tools import random_seed 
     24    random_seed(123) 
    2425    x = [random.uniform(-100,100) + Chebyshev8[i] for i in range(9)] 
    2526    solution = fmin(ChebyshevCost, x) 
  • mystic/examples/test_ffitD.py

    r713 r772  
    1919from test_ffit import plot_solution, print_solution, Chebyshev8, ChebyshevCost 
    2020 
    21 import random 
    22 random.seed(123) 
     21from mystic.tools import random_seed 
     22random_seed(123) 
    2323 
    2424ND = 9 
  • mystic/examples/test_fosc3d.py

    r723 r772  
    1616from mystic.strategy import Best1Exp, Best1Bin, Rand1Exp 
    1717 
    18 import random 
    19 random.seed(123) 
     18from mystic.tools import random_seed 
     19random_seed(123) 
    2020 
    2121from mystic.models import fosc3d as fOsc3D 
  • mystic/examples/test_griewangk.py

    r713 r772  
    2222from mystic.strategy import Best1Exp, Rand1Exp 
    2323 
    24 import random 
    25 random.seed(123) 
     24from mystic.tools import random_seed 
     25random_seed(123) 
    2626 
    2727# The costfunction for Griewangk's Function, Eq. (23) of [1]. 
  • mystic/examples/test_lorentzian2.py

    r721 r772  
    1919from mystic.strategy import Best1Exp, Rand1Exp, Best2Exp, Best2Exp 
    2020from mystic.monitors import Monitor 
    21 from mystic.tools import getch, random_seed 
     21from mystic.tools import getch 
    2222 
    23 random.seed(123) 
     23from mystic.tools import random_seed 
     24random_seed(123) 
    2425 
    2526#matplotlib.interactive(True) 
  • mystic/examples/test_rosenbrock.py

    r713 r772  
    2323from mystic.monitors import Monitor, VerboseMonitor 
    2424 
    25 import random 
    26 random.seed(123) 
     25from mystic.tools import random_seed 
     26random_seed(123) 
    2727 
    2828ND = 3 
     
    7171   #ssow= VerboseMonitor(1) 
    7272 
     73  # import random 
    7374 #  xinit = [random.random() for j in range(ND)] 
    7475    xinit = [0.8,1.2,0.7] 
  • mystic/examples/test_wavy.py

    r737 r772  
    1919#from mystic._scipyoptimize import fmin 
    2020 
    21 import random 
    22 random.seed(123) 
     21from mystic.tools import random_seed 
     22random_seed(123) 
    2323 
    2424from mystic.models import wavy1, wavy2 
  • mystic/examples/test_zimmermann.py

    r713 r772  
    2525from mystic.strategy import Best1Exp, Rand1Exp 
    2626 
    27 import random 
    28 random.seed(123) 
     27from mystic.tools import random_seed 
     28random_seed(123) 
    2929 
    3030# Eq. (24-26) of [2]. 
     
    6060    from mystic.termination import CandidateRelativeTolerance as CRT 
    6161 
     62    import random 
    6263    simplex = Monitor() 
    6364    esow = Monitor() 
  • mystic/examples_other/chebyshevinputs.py

    r733 r772  
    2020from numpy import inf 
    2121import random 
    22 random.seed(123) 
     22from mystic.tools import random_seed 
     23random_seed(123) 
    2324 
    2425x0 = [random.uniform(-5,5) + chebyshev8coeffs[i] for i in range(ND)] 
  • mystic/examples_other/roseninputs.py

    r733 r772  
    1919 
    2020from numpy import inf 
    21 import random 
    22 random.seed(123) 
     21from mystic.tools import random_seed 
     22random_seed(123) 
    2323 
    2424x0 = [0.8, 1.2, 0.5] 
  • mystic/examples_other/test_svr1.py

    r713 r772  
    1414from mystic.svmtools import * 
    1515 
    16 #random.seed(123) 
     16from mystic.tools import random_seed 
     17#random_seed(123) 
    1718 
    1819x = arange(-5, 5.001) 
  • mystic/examples_other/testsolvers_pyre.py

    r733 r772  
    6666        termination = self.mod.termination 
    6767 
    68         import random 
    69         random.seed(123) 
     68        from mystic.tools import random_seed 
     69        random_seed(123) 
    7070 
    7171        # set initial points 
  • mystic/models/circle.py

    r713 r772  
    1616 
    1717from numpy import array, pi, arange 
    18 from numpy import random, sin, cos 
     18from numpy import sin, cos 
    1919from math import floor, sqrt 
     20import random 
    2021 
    21 random.seed(123) 
     22# random.seed(123) 
    2223 
    2324class Circle(AbstractModel): 
  • mystic/models/lorentzian.py

    r721 r772  
    1616from numpy import sum as numpysum 
    1717from numpy import array, pi, asarray, arange 
    18 from numpy import random 
     18import random 
    1919 
    2020class Lorentzian(AbstractModel): 
     
    5353    F = lorentzian.ForwardFactory 
    5454    def gensample(F, xmin, xmax): 
    55         from numpy import arange, random 
     55        from numpy import arange 
     56        import random 
    5657        a = arange(xmin, xmax, (xmax-xmin)/200.) 
    5758        ymin = 0 
  • mystic/mystic/abstract_solver.py

    r763 r772  
    406406        matrix: -> the variance matrix. must be the right size! 
    407407        """ 
    408         from numpy.random import multivariate_normal 
     408        from mystic.tools import random_state 
     409        prng = random_state(module='numpy.random') 
    409410        assert(len(mean) == self.nDim) 
    410411        if var is None: 
     
    418419                var = var * numpy.eye(self.nDim) 
    419420        for i in range(len(self.population)): 
    420             self.population[i] = multivariate_normal(mean, var).tolist() 
     421            self.population[i] = prng.multivariate_normal(mean, var).tolist() 
    421422        return 
    422423 
  • mystic/mystic/forward_model.py

    r753 r772  
    6565from inspect import getargspec 
    6666from numpy import pi, sqrt, array, mgrid, random, real, conjugate, arange, sum 
    67 #from numpy.random import rand 
    6867 
    6968class CostFactory(object): 
  • mystic/mystic/metropolis.py

    r713 r772  
    1616(not just a way to draw from the proposal) 
    1717    """ 
    18     from numpy import random 
     18    import random 
    1919    Yt = proposal(x) 
    2020    r = min(target(Yt)/(target(x) + 1e-100), 1) 
  • mystic/mystic/scemtools.py

    r733 r772  
    201201    """ 
    202202    import numpy 
     203    from mystic.tools import random_state 
     204    prng = random_state(module=numpy.random) 
     205 
    203206    # function level constants 
    204207    T = 100000. # predefined likelihood ratio. Paragraph 45 of [1.] 
     
    236239 
    237240    # TODO should take a proposal instead ! 
    238     Yt = numpy.random.multivariate_normal(basept, cn*cn * Sigma) 
     241    Yt = prng.multivariate_normal(basept, cn*cn * Sigma) 
    239242    cY = target(Yt)     
    240243 
     
    243246    r = min( cY / (Sak[-1]+TINY), 1) 
    244247 
    245     if numpy.random.random() <= r: 
     248    if prng.rand() <= r: 
    246249        Sk.append(Yt) 
    247250        Sak.append(cY) 
  • mystic/mystic/tools.py

    r760 r772  
    2020    - getch: provides "press any key to quit" 
    2121    - random_seed: sets the seed for calls to 'random()' 
     22    - random_state: build a localized random generator 
    2223    - wrap_nested: nest a function call within a function object 
    2324    - wrap_penalty: append a function call to a function object 
     
    115116       return raw_input() 
    116117 
    117 def random_seed(s): 
     118def random_seed(s=None): 
    118119    "sets the seed for calls to 'random()'" 
    119120    import random 
     
    125126        pass 
    126127    return 
     128 
     129def random_state(module='random', new=False, seed='!'): 
     130    """return a (optionally manually seeded) random generator 
     131 
     132For a given module, return an object that has random number generation (RNG) 
     133methods available.  If new=False, use the global copy of the RNG object. 
     134If seed='!', do not reseed the RNG (using seed=None 'removes' any seeding). 
     135If seed='*', use a seed that depends on the process id (PID); this is useful 
     136for building RNGs that are different across multiple threads or processes. 
     137    """ 
     138    import random 
     139    if module == 'random': 
     140        rng = random 
     141    elif not isinstance(module, type(random)): 
     142        # convienence for passing in 'numpy' 
     143        if module == 'numpy': module = 'numpy.random' 
     144        import importlib 
     145        rng = importlib.import_module(module) 
     146    elif module.__name__ == 'numpy': # convienence for passing in numpy 
     147        from numpy import random as rng 
     148    else: rng = module 
     149 
     150    _rng = getattr(rng, 'RandomState', None) or \ 
     151           getattr(rng, 'Random') # throw error if no rng found 
     152    if new: 
     153        rng = _rng() 
     154 
     155    if seed == '!': # special case: don't reset the seed 
     156        return rng 
     157    if seed == '*': # special case: random seeding for multiprocessing 
     158        try: 
     159            try: 
     160                import multiprocessing as mp 
     161            except ImportError: 
     162                import processing as mp 
     163            try: 
     164                seed = mp.current_process().pid 
     165            except AttributeError: 
     166                seed = mp.currentProcess().getPid() 
     167        except:    
     168            seed = 0 
     169        import time 
     170        seed += int(time.time()*1e6) 
     171 
     172    # set the random seed (or 'reset' with None) 
     173    rng.seed(seed) 
     174    return rng 
    127175 
    128176def wrap_nested(outer_function, inner_function): 
Note: See TracChangeset for help on using the changeset viewer.