Changeset 858


Ignore:
Timestamp:
03/29/16 14:18:53 (7 weeks ago)
Author:
mmckerns
Message:

enable solver to use scipy.stats instance to generate initial population

Location:
mystic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/test_peaks.py

    r855 r858  
    1616nd = 2 
    1717npop = 20 
     18tol = 0.05 
    1819lb = [-3.]*nd 
    1920ub = [3.]*nd 
     
    2829solver.SetRandomInitialPoints(lb, ub) 
    2930solver.SetStrictRanges(lb, ub) 
    30 term = VTR()  
     31term = VTR(tol)  
    3132#term = COG() 
    3233solver.Solve(peaks, term, disp=True) 
     
    3435print 'solution = ', sol 
    3536print 'expected = [0.23, -1.63]' 
     37 
     38try: 
     39    from scipy.stats import uniform 
     40except ImportError: 
     41    exit() 
     42 
     43print '-'*60 
     44print 'using a uniform distribution...' 
     45solver = DifferentialEvolutionSolver(nd, npop) 
     46solver.SetDistributionInitialPoints(uniform(lb[0],ub[0])) 
     47solver.SetStrictRanges(lb, ub) 
     48term = VTR(tol)  
     49#term = COG() 
     50solver.Solve(peaks, term, disp=True) 
     51sol = solver.Solution() 
     52print 'solution = ', sol 
     53print 'expected = [0.23, -1.63]' 
     54 
     55 
     56 
  • mystic/mystic/abstract_solver.py

    r855 r858  
    400400                self.population[i][j] = random.uniform(min[j],max[j]) 
    401401 
    402     def SetMultinormalInitialPoints(self, mean, var = None): 
     402    def SetMultinormalInitialPoints(self, mean, var=None): 
    403403        """Generate Initial Points from Multivariate Normal. 
    404404 
     
    424424        for i in range(len(self.population)): 
    425425            self.population[i] = prng.multivariate_normal(mean, var).tolist() 
     426        return 
     427 
     428    def SetDistributionInitialPoints(self, dist): 
     429        """Generate Random Initial Points from Distribution (dist) 
     430 
     431input:: 
     432    - dist: a scipy.stats distribution instance""" 
     433        from mystic.tools import random_state 
     434        prng = random_state(module='numpy.random') 
     435        for i in range(self.nPop): 
     436            self.population[i] = dist.rvs(self.nDim, random_state=prng).tolist() 
    426437        return 
    427438 
     
    840851            continue 
    841852 
     853        # keep stepping if collapse 
     854#       while collapse and cc.collapse(self, verbose=False): 
     855#           while not self.Step(**settings): 
     856#               continue 
     857 
    842858        # restore default handler for signal interrupts 
    843859        if self._handle_sigint: 
Note: See TracChangeset for help on using the changeset viewer.