Changeset 219


Ignore:
Timestamp:
04/29/10 14:23:44 (6 years ago)
Author:
mmckerns
Message:

bugfix for #102, DE uses initial population;
SOW now captures initial pop,E for each solver;
consistent maxiter termination per solver & w/ scipy

Location:
mystic
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/test_rosenbrock.py

    r118 r219  
    1616from mystic.termination import ChangeOverGeneration, VTR 
    1717from mystic.models import rosen 
    18 from mystic import Sow 
     18from mystic import Sow, VerboseSow 
    1919 
    2020import random 
     
    2323ND = 3 
    2424NP = 30 
     25#MAX_GENERATIONS = 29 
    2526MAX_GENERATIONS = 99999 
    2627 
     
    3233 
    3334    solver.Solve(rosen, termination = VTR(0.0001), \ 
    34                  CrossProbability=0.5, ScalingFactor=0.6) 
     35                 CrossProbability=0.5, ScalingFactor=0.6, disp=1) 
    3536 
    3637    solution = solver.Solution() 
     
    4445    print "without bounds..." 
    4546    from timeit import Timer 
     47    print "Differential Evolution" 
     48    print "======================" 
    4649    t = Timer("main()", "from __main__ import main") 
    4750    timetaken =  t.timeit(number=1) 
     
    5861    esow= Sow() 
    5962    ssow= Sow() 
     63   #ssow= VerboseSow(1) 
    6064 
    6165 #  xinit = [random.random() for j in range(ND)] 
     
    7579    solver.SetEvaluationLimits(maxiter=MAX_GENERATIONS) 
    7680    solver.Solve(rosen, VTR(0.0001), EvaluationMonitor=esow, StepMonitor=ssow,\ 
    77                  CrossProbability=0.5, ScalingFactor=0.6) 
     81                 CrossProbability=0.5, ScalingFactor=0.6, disp=1) 
    7882    sol = solver.Solution() 
    7983    print sol 
  • mystic/examples/test_rosenbrock2.py

    r103 r219  
    2424    print "===================" 
    2525    start = time.time() 
    26     from mystic.tools import Sow #VerboseSow 
     26    from mystic.tools import Sow, VerboseSow 
     27   #stepmon = VerboseSow(1) 
    2728    stepmon = Sow() #VerboseSow(10) 
    2829    from mystic.termination import CandidateRelativeTolerance as CRT 
    2930 
    30    #from scipy.optimize import fmin 
     31    from scipy.optimize import fmin 
    3132    from mystic.scipy_optimize import fmin, NelderMeadSimplexSolver 
    32    #print fmin(rosen,x0,retall=0,full_output=0) 
     33    print fmin(rosen,x0,retall=0,full_output=0,maxiter=121) 
    3334    solver = NelderMeadSimplexSolver(len(x0)) 
    3435    solver.SetInitialPoints(x0) 
    3536    solver.SetStrictRanges(min,max) 
     37    solver.SetEvaluationLimits(maxiter=146) 
    3638    solver.enable_signal_handler() 
    3739    solver.Solve(rosen,termination=CRT(xtol=4e-5),StepMonitor=stepmon,disp=1) 
  • mystic/examples/test_rosenbrock3.py

    r116 r219  
    2424    print "===========================" 
    2525    start = time.time() 
    26     from mystic.tools import Sow #VerboseSow 
    27     stepmon = Sow() #VerboseSow(1) 
     26    from mystic.tools import Sow, VerboseSow 
     27   #stepmon = VerboseSow(1) 
     28    stepmon = Sow() #VerboseSow(10) 
    2829    from mystic.termination import NormalizedChangeOverGeneration as NCOG 
    2930 
    30    #from scipy.optimize import fmin_powell 
     31    from scipy.optimize import fmin_powell 
    3132    from mystic.scipy_optimize import fmin_powell, PowellDirectionalSolver 
    32    #print fmin_powell(rosen,x0,retall=0,full_output=0) 
     33    print fmin_powell(rosen,x0,retall=0,full_output=0,maxiter=14) 
    3334    solver = PowellDirectionalSolver(len(x0)) 
    3435    solver.SetInitialPoints(x0) 
    3536    solver.SetStrictRanges(min,max) 
     37    solver.SetEvaluationLimits(maxiter=13) 
    3638    solver.enable_signal_handler() 
    3739    solver.Solve(rosen,termination=NCOG(tolerance=1e-4),StepMonitor=stepmon,disp=1) 
  • mystic/mystic/abstract_solver.py

    r140 r219  
    116116 
    117117        self._init_popEnergy  = 1.0E20 #XXX: or numpy.inf? 
    118         self.popEnergy        = [0.0] * NP 
     118        self.popEnergy        = [self._init_popEnergy] * NP 
    119119        self.population       = [[0.0 for i in range(dim)] for j in range(NP)] 
    120120        self.energy_history   = [] 
     
    211211            for j in range(self.nDim): 
    212212                self.population[i][j] = random.uniform(min[j],max[j]) 
    213             self.popEnergy[i] = self._init_popEnergy 
    214213 
    215214    def SetMultinormalInitialPoints(self, mean, var = None): 
     
    236235        for i in range(self.nPop): 
    237236            self.population[i] = multivariate_normal(mean, var).tolist() 
    238             self.popEnergy[i] = self._init_popEnergy 
    239237        return 
    240238 
  • mystic/mystic/differential_evolution.py

    r218 r219  
    249249        self.scale = ScalingFactor 
    250250 
    251         self.bestEnergy = self._init_popEnergy 
     251        #set initial solution and energy by running a single iteration 
     252        self.bestSolution = self.population[0][:] 
     253        self.bestEnergy = self.popEnergy[0] 
     254        for candidate in range(self.nPop): 
     255            # generate trialSolution (within valid range) 
     256            self.trialSolution[:] = self.population[candidate][:] 
     257            trialEnergy = costfunction(self.trialSolution) 
     258 
     259            if trialEnergy < self.popEnergy[candidate]: 
     260                # New low for this candidate 
     261                self.popEnergy[candidate] = trialEnergy 
     262                self.population[candidate][:] = self.trialSolution[:] 
     263                self.UpdateGenealogyRecords(candidate, self.trialSolution[:]) 
     264 
     265                # Check if all-time low 
     266                if trialEnergy < self.bestEnergy: 
     267                    self.bestEnergy = trialEnergy 
     268                    self.bestSolution[:] = self.trialSolution[:] 
     269                             
     270        self.energy_history.append(self.bestEnergy) 
     271        StepMonitor(self.bestSolution[:], self.bestEnergy) 
     272        self.generations = 0  #XXX: above currently *not* counted as an iteration 
     273        if callback is not None: 
     274            callback(self.bestSolution) 
    252275          
    253276        if self._maxiter is None: 
     
    256279            self._maxfun = self.nDim * self.nPop * 1000 #XXX: set better defaults? 
    257280 
    258         generation = 0 
    259         for generation in range(self._maxiter): 
    260             StepMonitor(self.bestSolution[:], self.bestEnergy) 
     281        #run for generations <= maxiter 
     282        for generation in range(self._maxiter - self.generations): 
    261283            if fcalls[0] >= self._maxfun: break 
    262284            for candidate in range(self.nPop): 
     
    277299                             
    278300            self.energy_history.append(self.bestEnergy) 
    279  
     301            StepMonitor(self.bestSolution[:], self.bestEnergy) 
     302            self.generations += 1 
    280303            if callback is not None: 
    281304                callback(self.bestSolution) 
     
    283306            if self._EARLYEXIT or termination(self): 
    284307                break 
    285  
    286         self.generations = generation + 1 
    287308 
    288309        signal.signal(signal.SIGINT,signal.default_int_handler) 
     
    394415        self.scale = ScalingFactor 
    395416 
    396         self.bestEnergy = self._init_popEnergy 
     417        #set initial solution and energy by running a single iteration 
     418        self.bestSolution = self.population[0][:] 
     419        self.bestEnergy = self.popEnergy[0] 
     420        trialPop = [[0.0 for i in range(self.nDim)] for j in range(self.nPop)] 
     421        for candidate in range(self.nPop): 
     422            # generate trialSolution (within valid range) 
     423            self.trialSolution[:] = self.population[candidate][:] 
     424            #XXX:[HACK] apply constraints 
     425            self.trialSolution[:] = constraints(self.trialSolution[:]) 
     426            #XXX:[HACK] -end- 
     427            trialPop[candidate][:] = self.trialSolution[:] 
     428 
     429        trialEnergy = map(costfunction, trialPop) 
     430 
     431        for candidate in range(self.nPop): 
     432            if trialEnergy[candidate] < self.popEnergy[candidate]: 
     433                # New low for this candidate 
     434                self.popEnergy[candidate] = trialEnergy[candidate] 
     435                self.population[candidate][:] = trialPop[candidate][:] 
     436                self.UpdateGenealogyRecords(candidate, trialPop[candidate][:]) 
     437               #XXX: was self.UpdateGenealogyRecords(candidate, self.trialSolution[:]) 
     438 
     439                # Check if all-time low 
     440                if trialEnergy[candidate] < self.bestEnergy: 
     441                    self.bestEnergy = trialEnergy[candidate] 
     442                    self.bestSolution[:] = trialPop[candidate][:] 
     443                         
     444        self.energy_history.append(self.bestEnergy) 
     445        StepMonitor(self.bestSolution[:], self.bestEnergy) 
     446        self.generations = 0  #XXX: above currently *not* counted as an iteration 
     447        if callback is not None: 
     448            callback(self.bestSolution) 
    397449          
    398450        if self._maxiter is None: 
     
    400452        if self._maxfun is None: 
    401453            self._maxfun = self.nDim * self.nPop * 1000 #XXX: set better defaults? 
    402         trialPop = [[0.0 for i in range(self.nDim)] for j in range(self.nPop)] 
    403  
    404         generation = 0 
    405         for generation in range(self._maxiter): 
    406             StepMonitor(self.bestSolution[:], self.bestEnergy) 
     454 
     455        #run for generations <= maxiter 
     456        for generation in range(self._maxiter - self.generations): 
    407457            if fcalls[0] >= self._maxfun: break 
    408458            for candidate in range(self.nPop): 
     
    435485                             
    436486            self.energy_history.append(self.bestEnergy) 
    437  
     487            StepMonitor(self.bestSolution[:], self.bestEnergy) 
     488            self.generations += 1 
    438489            if callback is not None: 
    439490                callback(self.bestSolution) 
     
    441492            if self._EARLYEXIT or termination(self): 
    442493                break 
    443  
    444         self.generations = generation + 1 
    445494 
    446495        signal.signal(signal.SIGINT,signal.default_int_handler) 
  • mystic/mystic/scipy_optimize.py

    r132 r219  
    207207            allvecs = [sim[0]] 
    208208        fsim[0] = func(x0) 
     209        StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
    209210 
    210211        #--- ensure initial simplex is within bounds --- 
     
    227228        self.popEnergy = fsim 
    228229        self.energy_history.append(self.bestEnergy) 
     230        StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
    229231 
    230232        iterations = 1 
    231233 
    232234        while (fcalls[0] < self._maxfun and iterations < self._maxiter): 
    233             StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
    234235            if self._EARLYEXIT or termination(self): 
    235236                break 
     
    295296            self.popEnergy = fsim 
    296297            self.energy_history.append(self.bestEnergy) 
     298            StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 
    297299 
    298300        self.generations = iterations 
  • mystic/mystic/tools.py

    r126 r219  
    161161        from numpy import ndarray 
    162162        Sow.__call__(self, x, y) 
    163         self._step += 1 
    164163        if isinstance(y,(list,ndarray)): 
    165164            y = y[0] #XXX: get the "best" fit... which should be in y[0] 
     
    171170        if int(self._step % self._xinterval) == 0: 
    172171            print "Generation %d has best fit parameters:\n %s" % (self._step, x) 
     172        self._step += 1 
    173173        return 
    174174    pass 
Note: See TracChangeset for help on using the changeset viewer.