Changeset 772
- Timestamp:
- 11/21/14 14:33:33 (18 months ago)
- Location:
- mystic
- Files:
-
- 35 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/_math/grid.py
r770 r772 53 53 if N == 0: return [0] if ndim else [0]*ndim 54 54 from itertools import chain 55 from random import random 55 from mystic.tools import random_state 56 random = random_state().random 56 57 def factors(n): 57 58 result = list() -
mystic/_math/measures.py
r770 r772 25 25 """ 26 26 from numpy import sum, array 27 from numpy.random import rand 27 from mystic.tools import random_state 28 rand = random_state().random 28 29 # generate a list representing the weighted distribution 29 30 wts = normalize(weights, mass) -
mystic/_math/samples.py
r713 r772 23 23 npts -- number of sample points [default = 10000] 24 24 """ 25 from numpy.random import random25 from mystic.tools import random_state 26 26 dim = len(lb) 27 pts = random ((dim,npts))27 pts = random_state(module='numpy.random').rand(dim,npts) 28 28 for i in range(dim): 29 29 pts[i] = (pts[i] * abs(ub[i] - lb[i])) + lb[i] … … 42 42 npts -- the number of points to sample [Default is npts=10000] 43 43 """ 44 from numpy.random import random45 44 from numpy import transpose 46 45 pts = random_samples(lb, ub, npts) … … 189 188 ####################################################################### 190 189 191 def __test1():192 # From branches/UQ/math/samples.py193 num_sample_points = 5194 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 pts200 201 def __test2():202 # From branches/UQ/math/cut.py203 from mystic.tools import random_seed204 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 = x210 if x1 > (x2 + x3): return x1*x2 - x3211 return 0.0212 213 failure,success = sample(model,lower,upper)214 pof = float(failure) / float(failure + success)215 print "PoF using method 1: %s" % pof216 random_seed(123)217 print "PoF using method 2: %s" % sampled_pof(model,lower,upper)218 190 219 191 if __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 220 222 __test1() 221 223 __test2() -
mystic/examples/TEST_ffitPP2.py
r723 r772 18 18 19 19 if __name__ == '__main__': 20 import random 20 21 from mystic.solvers import fmin 21 22 #from mystic._scipyoptimize import fmin 22 import random23 random .seed(123)23 from mystic.tools import random_seed 24 random_seed(123) 24 25 25 26 import pp -
mystic/examples/TEST_ffitPP2_b.py
r723 r772 19 19 20 20 if __name__ == '__main__': 21 import random 21 22 from mystic.solvers import fmin 22 23 #from mystic._scipyoptimize import fmin 23 import random24 random .seed(123)24 from mystic.tools import random_seed 25 random_seed(123) 25 26 26 27 import pp -
mystic/examples/buckshot_example06.py
r771 r772 28 28 try: 29 29 from pathos.multiprocessing import ProcessingPool as Pool 30 #from pathos.pp import ParallelPythonPool as Pool 30 31 except ImportError: 31 32 from mystic.python import PythonSerial as Pool … … 72 73 73 74 # dimensional information 74 import random75 random .seed(123)75 from mystic.tools import random_seed 76 random_seed(123) 76 77 ndim = 9 77 78 npts = 8 -
mystic/examples/example06.py
r722 r772 62 62 # initial guess 63 63 import random 64 random.seed(123) 64 from mystic.tools import random_seed 65 random_seed(123) 65 66 ndim = 9 66 67 x0 = [random.uniform(-100,100) for i in range(ndim)] -
mystic/examples/example11.py
r722 r772 30 30 from mystic.termination import CandidateRelativeTolerance as CRT 31 31 from mystic.monitors import VerboseMonitor, Monitor 32 from mystic.tools import getch , random_seed32 from mystic.tools import getch 33 33 from mystic.math import poly1d 34 34 import pylab … … 86 86 # initial guess 87 87 import random 88 random.seed(123) 88 from mystic.tools import random_seed 89 random_seed(123) 89 90 ndim = 9 90 91 x0 = [random.uniform(-5,5) + chebyshev8coeffs[i] for i in range(ndim)] -
mystic/examples/lattice_example06.py
r771 r772 28 28 try: 29 29 from pathos.multiprocessing import ProcessingPool as Pool 30 #from pathos.pp import ParallelPythonPool as Pool 30 31 except ImportError: 31 32 from mystic.python import PythonSerial as Pool … … 72 73 73 74 # dimensional information 74 import random75 random .seed(123)75 from mystic.tools import random_seed 76 random_seed(123) 76 77 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] 78 79 79 80 # draw frame and exact coefficients -
mystic/examples/mpmap_desolve.py
r721 r772 41 41 42 42 ND = 9 43 NP = 4043 NP = 80 44 44 MAX_GENERATIONS = NP*NP 45 45 NNODES = NP/5 … … 58 58 random_seed(seed) 59 59 print "first sequential..." 60 solver = DifferentialEvolutionSolver2(ND,NP) #XXX: sequential60 solver = DifferentialEvolutionSolver2(ND,NP) 61 61 solver.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND) 62 62 solver.SetEvaluationLimits(generations=MAX_GENERATIONS) … … 67 67 print_solution( solver.bestSolution ) 68 68 69 #'''70 69 random_seed(seed) 71 70 print "\n and now parallel..." 72 solver2 = DifferentialEvolutionSolver2(ND,NP) #XXX: parallel73 solver2.SetMapper(Pool(NNODES).map) 71 solver2 = DifferentialEvolutionSolver2(ND,NP) 72 solver2.SetMapper(Pool(NNODES).map) # parallel 74 73 solver2.SetRandomInitialPoints(min=[-100.0]*ND, max=[100.0]*ND) 75 74 solver2.SetEvaluationLimits(generations=MAX_GENERATIONS) … … 79 78 print "" 80 79 print_solution( solver2.bestSolution ) 81 #'''82 80 83 81 # end of file -
mystic/examples/test_corana.py
r766 r772 22 22 from mystic.strategy import Best1Exp, Rand1Exp 23 23 24 import random 25 random .seed(123)24 from mystic.tools import random_seed 25 random_seed(123) 26 26 27 27 from mystic.models import corana -
mystic/examples/test_dejong3.py
r713 r772 25 25 from mystic.models.dejong import step as DeJong3 26 26 27 import random 28 random .seed(123)27 from mystic.tools import random_seed 28 random_seed(123) 29 29 30 30 ND = 5 -
mystic/examples/test_dejong4.py
r713 r772 25 25 from mystic.models.dejong import quartic as DeJong4 26 26 27 import random 28 random .seed(123)27 from mystic.tools import random_seed 28 random_seed(123) 29 29 30 30 ND = 30 -
mystic/examples/test_dejong5.py
r713 r772 23 23 from mystic.models.dejong import shekel as DeJong5 24 24 25 import random 26 random .seed(123)25 from mystic.tools import random_seed 26 random_seed(123) 27 27 28 28 ND = 2 -
mystic/examples/test_ffit.py
r726 r772 27 27 from mystic.tools import getch 28 28 29 import random 30 random .seed(123)29 from mystic.tools import random_seed 30 random_seed(123) 31 31 32 32 # get the target coefficients and cost function -
mystic/examples/test_ffit2.py
r713 r772 23 23 from mystic.models.poly import chebyshev16cost 24 24 25 import random 26 random .seed(123)25 from mystic.tools import random_seed 26 random_seed(123) 27 27 28 28 # get the target coefficients -
mystic/examples/test_ffitC.py
r723 r772 18 18 19 19 if __name__ == '__main__': 20 import random 20 21 from mystic.solvers import fmin 21 22 #from mystic._scipyoptimize import fmin 22 import random23 random .seed(123)23 from mystic.tools import random_seed 24 random_seed(123) 24 25 x = [random.uniform(-100,100) + Chebyshev8[i] for i in range(9)] 25 26 solution = fmin(ChebyshevCost, x) -
mystic/examples/test_ffitD.py
r713 r772 19 19 from test_ffit import plot_solution, print_solution, Chebyshev8, ChebyshevCost 20 20 21 import random 22 random .seed(123)21 from mystic.tools import random_seed 22 random_seed(123) 23 23 24 24 ND = 9 -
mystic/examples/test_fosc3d.py
r723 r772 16 16 from mystic.strategy import Best1Exp, Best1Bin, Rand1Exp 17 17 18 import random 19 random .seed(123)18 from mystic.tools import random_seed 19 random_seed(123) 20 20 21 21 from mystic.models import fosc3d as fOsc3D -
mystic/examples/test_griewangk.py
r713 r772 22 22 from mystic.strategy import Best1Exp, Rand1Exp 23 23 24 import random 25 random .seed(123)24 from mystic.tools import random_seed 25 random_seed(123) 26 26 27 27 # The costfunction for Griewangk's Function, Eq. (23) of [1]. -
mystic/examples/test_lorentzian2.py
r721 r772 19 19 from mystic.strategy import Best1Exp, Rand1Exp, Best2Exp, Best2Exp 20 20 from mystic.monitors import Monitor 21 from mystic.tools import getch , random_seed21 from mystic.tools import getch 22 22 23 random.seed(123) 23 from mystic.tools import random_seed 24 random_seed(123) 24 25 25 26 #matplotlib.interactive(True) -
mystic/examples/test_rosenbrock.py
r713 r772 23 23 from mystic.monitors import Monitor, VerboseMonitor 24 24 25 import random 26 random .seed(123)25 from mystic.tools import random_seed 26 random_seed(123) 27 27 28 28 ND = 3 … … 71 71 #ssow= VerboseMonitor(1) 72 72 73 # import random 73 74 # xinit = [random.random() for j in range(ND)] 74 75 xinit = [0.8,1.2,0.7] -
mystic/examples/test_wavy.py
r737 r772 19 19 #from mystic._scipyoptimize import fmin 20 20 21 import random 22 random .seed(123)21 from mystic.tools import random_seed 22 random_seed(123) 23 23 24 24 from mystic.models import wavy1, wavy2 -
mystic/examples/test_zimmermann.py
r713 r772 25 25 from mystic.strategy import Best1Exp, Rand1Exp 26 26 27 import random 28 random .seed(123)27 from mystic.tools import random_seed 28 random_seed(123) 29 29 30 30 # Eq. (24-26) of [2]. … … 60 60 from mystic.termination import CandidateRelativeTolerance as CRT 61 61 62 import random 62 63 simplex = Monitor() 63 64 esow = Monitor() -
mystic/examples_other/chebyshevinputs.py
r733 r772 20 20 from numpy import inf 21 21 import random 22 random.seed(123) 22 from mystic.tools import random_seed 23 random_seed(123) 23 24 24 25 x0 = [random.uniform(-5,5) + chebyshev8coeffs[i] for i in range(ND)] -
mystic/examples_other/roseninputs.py
r733 r772 19 19 20 20 from numpy import inf 21 import random 22 random .seed(123)21 from mystic.tools import random_seed 22 random_seed(123) 23 23 24 24 x0 = [0.8, 1.2, 0.5] -
mystic/examples_other/test_svr1.py
r713 r772 14 14 from mystic.svmtools import * 15 15 16 #random.seed(123) 16 from mystic.tools import random_seed 17 #random_seed(123) 17 18 18 19 x = arange(-5, 5.001) -
mystic/examples_other/testsolvers_pyre.py
r733 r772 66 66 termination = self.mod.termination 67 67 68 import random 69 random.seed(123)68 from mystic.tools import random_seed 69 random_seed(123) 70 70 71 71 # set initial points -
mystic/models/circle.py
r713 r772 16 16 17 17 from numpy import array, pi, arange 18 from numpy import random,sin, cos18 from numpy import sin, cos 19 19 from math import floor, sqrt 20 import random 20 21 21 random.seed(123)22 # random.seed(123) 22 23 23 24 class Circle(AbstractModel): -
mystic/models/lorentzian.py
r721 r772 16 16 from numpy import sum as numpysum 17 17 from numpy import array, pi, asarray, arange 18 from numpyimport random18 import random 19 19 20 20 class Lorentzian(AbstractModel): … … 53 53 F = lorentzian.ForwardFactory 54 54 def gensample(F, xmin, xmax): 55 from numpy import arange, random 55 from numpy import arange 56 import random 56 57 a = arange(xmin, xmax, (xmax-xmin)/200.) 57 58 ymin = 0 -
mystic/mystic/abstract_solver.py
r763 r772 406 406 matrix: -> the variance matrix. must be the right size! 407 407 """ 408 from numpy.random import multivariate_normal 408 from mystic.tools import random_state 409 prng = random_state(module='numpy.random') 409 410 assert(len(mean) == self.nDim) 410 411 if var is None: … … 418 419 var = var * numpy.eye(self.nDim) 419 420 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() 421 422 return 422 423 -
mystic/mystic/forward_model.py
r753 r772 65 65 from inspect import getargspec 66 66 from numpy import pi, sqrt, array, mgrid, random, real, conjugate, arange, sum 67 #from numpy.random import rand68 67 69 68 class CostFactory(object): -
mystic/mystic/metropolis.py
r713 r772 16 16 (not just a way to draw from the proposal) 17 17 """ 18 from numpyimport random18 import random 19 19 Yt = proposal(x) 20 20 r = min(target(Yt)/(target(x) + 1e-100), 1) -
mystic/mystic/scemtools.py
r733 r772 201 201 """ 202 202 import numpy 203 from mystic.tools import random_state 204 prng = random_state(module=numpy.random) 205 203 206 # function level constants 204 207 T = 100000. # predefined likelihood ratio. Paragraph 45 of [1.] … … 236 239 237 240 # 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) 239 242 cY = target(Yt) 240 243 … … 243 246 r = min( cY / (Sak[-1]+TINY), 1) 244 247 245 if numpy.random.random() <= r:248 if prng.rand() <= r: 246 249 Sk.append(Yt) 247 250 Sak.append(cY) -
mystic/mystic/tools.py
r760 r772 20 20 - getch: provides "press any key to quit" 21 21 - random_seed: sets the seed for calls to 'random()' 22 - random_state: build a localized random generator 22 23 - wrap_nested: nest a function call within a function object 23 24 - wrap_penalty: append a function call to a function object … … 115 116 return raw_input() 116 117 117 def random_seed(s ):118 def random_seed(s=None): 118 119 "sets the seed for calls to 'random()'" 119 120 import random … … 125 126 pass 126 127 return 128 129 def random_state(module='random', new=False, seed='!'): 130 """return a (optionally manually seeded) random generator 131 132 For a given module, return an object that has random number generation (RNG) 133 methods available. If new=False, use the global copy of the RNG object. 134 If seed='!', do not reseed the RNG (using seed=None 'removes' any seeding). 135 If seed='*', use a seed that depends on the process id (PID); this is useful 136 for 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 127 175 128 176 def wrap_nested(outer_function, inner_function):
Note: See TracChangeset
for help on using the changeset viewer.