Changeset 102


Ignore:
Timestamp:
02/20/09 14:16:13 (7 years ago)
Author:
mmckerns
Message:

renamed "bounds" methods for more consistency;
added method to ensure initial guess is within bounds;
catch initial Simplex cost in energy_history;
rename termination "IRT" to "CRT";
add termination "NormalizedChangeOverGeneration?"

Location:
mystic
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/sam_corana2.py

    r97 r102  
    1212#from test_corana import * 
    1313from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    14 from mystic.termination import IterationRelativeTolerance as IRT 
     14from mystic.termination import CandidateRelativeTolerance as CRT 
    1515from mystic import getch, Sow 
    1616 
     
    4444    solver = fmin(2) 
    4545    solver.SetRandomInitialPoints([0,0],[2,2]) 
    46     solver.Solve(Corana2, termination=IRT(), StepMonitor = simplex) 
     46    solver.Solve(Corana2, termination=CRT(), StepMonitor = simplex) 
    4747    sol = solver.Solution() 
    4848     
  • mystic/examples/sam_mogi.py

    r97 r102  
    1010from test_mogi import * 
    1111from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    12 from mystic.termination import IterationRelativeTolerance as IRT 
     12from mystic.termination import CandidateRelativeTolerance as CRT 
    1313from mystic import getch, Sow 
    1414 
     
    6666    solver = fmin(len(xinit)) 
    6767    solver.SetInitialPoints(xinit) 
    68     solver.Solve(cost_function, termination=IRT(), StepMonitor = simplex) 
     68    solver.Solve(cost_function, termination=CRT(), StepMonitor = simplex) 
    6969    sol = solver.Solution() 
    7070    print sol 
     
    8484    solver = fmin(len(xinit)) 
    8585    solver.SetInitialPoints(xinit) 
    86     solver.Solve(cost_function, termination=IRT(), StepMonitor = simplex) 
     86    solver.Solve(cost_function, termination=CRT(), StepMonitor = simplex) 
    8787    sol = solver.Solution() 
    8888    print sol 
  • mystic/examples/sam_rosenbrock.py

    r97 r102  
    1313from test_rosenbrock import * 
    1414from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    15 from mystic.termination import IterationRelativeTolerance as IRT 
     15from mystic.termination import CandidateRelativeTolerance as CRT 
    1616from mystic import getch, Sow 
    1717 
     
    4545    solver = fmin(len(xinit)) 
    4646    solver.SetInitialPoints(xinit) 
    47     solver.Solve(rosen, termination=IRT(), StepMonitor = simplex) 
     47    solver.Solve(rosen, termination=CRT(), StepMonitor = simplex) 
    4848    sol = solver.Solution() 
    4949     
  • mystic/examples/sam_zimmermann.py

    r97 r102  
    1313from test_zimmermann import * 
    1414from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    15 from mystic.termination import IterationRelativeTolerance as IRT 
     15from mystic.termination import CandidateRelativeTolerance as CRT 
    1616from mystic import getch, Sow 
    1717 
     
    4242    solver = fmin(2) 
    4343    solver.SetRandomInitialPoints([0,0],[7,7]) 
    44     solver.Solve(CostFunction, termination=IRT(), StepMonitor = simplex) 
     44    solver.Solve(CostFunction, termination=CRT(), StepMonitor = simplex) 
    4545    sol = solver.Solution() 
    4646 
  • mystic/examples/test_mogi.py

    r97 r102  
    111111 
    112112    from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    113     from mystic.termination import IterationRelativeTolerance as IRT 
     113    from mystic.termination import CandidateRelativeTolerance as CRT 
    114114    from scipy.optimize import leastsq, fmin_cg 
    115115    # 
     
    128128    solver = fmin(len(point)) 
    129129    solver.SetInitialPoints(point) 
    130     solver.Solve(cost_function, IRT(), EvaluationMonitor = esow, StepMonitor = simplex) 
     130    solver.Solve(cost_function, CRT(), EvaluationMonitor = esow, StepMonitor = simplex) 
    131131    sol = solver.Solution() 
    132132 
  • mystic/examples/test_rosenbrock.py

    r97 r102  
    1616from mystic.models import rosen 
    1717from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    18 from mystic.termination import IterationRelativeTolerance as IRT 
     18from mystic.termination import CandidateRelativeTolerance as CRT 
    1919from mystic import Sow 
    2020 
     
    5252    solver = fmin(len(xinit)) 
    5353    solver.SetInitialPoints(xinit) 
    54     solver.Solve(rosen, IRT(), EvaluationMonitor = esow, StepMonitor = ssow) 
     54    solver.Solve(rosen, CRT(), EvaluationMonitor = esow, StepMonitor = ssow) 
    5555    sol = solver.Solution() 
    5656    print sol 
  • mystic/examples/test_rosenbrock2.py

    r97 r102  
    2626    from mystic.tools import VerboseSow 
    2727    stepmon = VerboseSow(10) 
    28     from mystic.termination import IterationRelativeTolerance as IRT 
     28    from mystic.termination import CandidateRelativeTolerance as CRT 
    2929 
    3030    from mystic.scipy_optimize import fmin, NelderMeadSimplexSolver 
     
    3434    solver.SetStrictRanges(min,max) 
    3535    solver.enable_signal_handler() 
    36     solver.Solve(rosen,termination=IRT(xtol=1e-5),StepMonitor=stepmon) 
     36    solver.Solve(rosen,termination=CRT(xtol=1e-5),StepMonitor=stepmon) 
    3737    print solver.Solution() 
    3838 
  • mystic/examples/test_zimmermann.py

    r97 r102  
    5252    from mystic import Sow 
    5353    from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 
    54     from mystic.termination import IterationRelativeTolerance as IRT 
     54    from mystic.termination import CandidateRelativeTolerance as CRT 
    5555 
    5656    simplex = Sow() 
     
    6060    solver = fmin(len(xinit)) 
    6161    solver.SetInitialPoints(xinit) 
    62     solver.Solve(CostFunction, IRT(), EvaluationMonitor = esow, StepMonitor = simplex) 
     62    solver.Solve(CostFunction, CRT(), EvaluationMonitor = esow, StepMonitor = simplex) 
    6363    sol = solver.Solution() 
    6464    print "fmin solution: ", sol 
  • mystic/mystic/differential_evolution.py

    r95 r102  
    1616##                      Birmingham, Alabama USA 
    1717##                      zunzun@zunzun.com 
     18## 
     19## bounds added by mmckerns@caltech.edu 
    1820 
    1921""" 
     
    8284        return 
    8385 
    84     def truncateSolutionAtRangeBoundary(self): 
    85         """truncate trialSolution at range boundary""" 
    86         if not self._useStrictRange: 
    87             return 
    88         min = self._strictMin 
    89         max = self._strictMax 
    90         for i in range(self.nDim): 
    91             if self.trialSolution[i] < min[i]: 
    92                 self.trialSolution[i] = min[i] 
    93             elif self.trialSolution[i] > max[i]: 
    94                 self.trialSolution[i] = max[i] 
    95         return 
     86#   def _truncateSolutionAtRangeBoundary(self): 
     87#       """truncate trialSolution at range boundary""" 
     88#       if not self._useStrictRange: 
     89#           return 
     90#       min = self._strictMin 
     91#       max = self._strictMax 
     92#       for i in range(self.nDim): 
     93#           if self.trialSolution[i] < min[i]: 
     94#               self.trialSolution[i] = min[i] 
     95#           elif self.trialSolution[i] > max[i]: 
     96#               self.trialSolution[i] = max[i] 
     97#       return 
    9698             
    97     def scaleSolutionWithRangeBoundary(self, base): 
     99    def _keepSolutionWithinRangeBoundary(self, base): #XXX: could be smarter? 
     100   #def scaleSolutionWithRangeBoundary(self, base): 
    98101        """scale trialSolution to be between base value and range boundary""" 
    99102        if not self._useStrictRange: 
  • mystic/mystic/scipy_optimize.py

    r101 r102  
    7070        return 
    7171 
    72     def setSimplexWithinRangeBoundary(self, x0, radius): 
     72    def _setGuessWithinRangeBoundary(self, x0): 
     73        """ensure that initial guess is set within bounds""" 
     74        if self._useStrictRange: 
     75            lo = asarray(self._strictMin) 
     76            hi = asarray(self._strictMax) 
     77            # crop x0 at bounds 
     78            x0[x0<lo] = lo[x0<lo] 
     79            x0[x0>hi] = hi[x0>hi] 
     80        return x0 
     81 
     82    def _setSimplexWithinRangeBoundary(self, x0, radius): 
    7383        """ensure that initial simplex is set within bounds""" 
    7484        #code modified from park-1.2/park/simplex.py (version 1257) 
     
    186196        fcalls, func = wrap_function(func, args, EvaluationMonitor) 
    187197        if self._useStrictRange: 
     198            x0 = self._setGuessWithinRangeBoundary(x0) 
    188199            func = wrap_bounds(func, self._strictMin, self._strictMax) 
    189200 
     
    248259 
    249260        #--- ensure initial simplex is within bounds --- 
    250         x0,val = self.setSimplexWithinRangeBoundary(x0,radius) 
     261        x0,val = self._setSimplexWithinRangeBoundary(x0,radius) 
    251262        #--- end bounds code --- 
    252263        for k in range(0,N): 
     
    265276        self.population = sim 
    266277        self.popEnergy = fsim 
     278        self.energy_history.append(self.bestEnergy) 
    267279 
    268280        iterations = 1 
     
    378390    stepmon = Sow() 
    379391    evalmon = Sow() 
    380     from mystic.termination import IterationRelativeTolerance as IRT 
     392    from mystic.termination import CandidateRelativeTolerance as CRT 
    381393 
    382394    solver = NelderMeadSimplexSolver(len(x0)) 
    383395    solver.SetInitialPoints(x0) 
    384396   #solver.enable_signal_handler() 
    385     solver.Solve(func,termination=IRT(xtol,ftol),\ 
     397    solver.Solve(func,termination=CRT(xtol,ftol),\ 
    386398                 maxiter=maxiter,maxfun=maxfun,\ 
    387399                 EvaluationMonitor=evalmon,StepMonitor=stepmon,\ 
  • mystic/mystic/strategy.py

    r96 r102  
    3333        i += 1 
    3434 
    35     inst.scaleSolutionWithRangeBoundary(inst.bestSolution) 
     35    inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 
    3636    return 
    3737 
     
    5656                                                  inst.population[r2][i]) 
    5757 
    58     inst.scaleSolutionWithRangeBoundary(inst.bestSolution) 
     58    inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 
    5959    return 
    6060 
     
    7575        i += 1 
    7676 
    77     inst.scaleSolutionWithRangeBoundary(inst.population[r1]) 
     77    inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 
    7878    return 
    7979 
     
    9696        i += 1 
    9797 
    98     inst.scaleSolutionWithRangeBoundary(inst.trialSolution) 
     98    inst._keepSolutionWithinRangeBoundary(inst.trialSolution) 
    9999    return 
    100100 
     
    116116        i += 1 
    117117 
    118     inst.scaleSolutionWithRangeBoundary(inst.bestSolution) 
     118    inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 
    119119    return 
    120120 
     
    136136        i += 1 
    137137 
    138     inst.scaleSolutionWithRangeBoundary(inst.population[r1]) 
     138    inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 
    139139    return 
    140140 
     
    154154        i += 1 
    155155 
    156     inst.scaleSolutionWithRangeBoundary(inst.population[r1]) 
     156    inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 
    157157    return 
    158158 
     
    173173        i += 1 
    174174 
    175     inst.scaleSolutionWithRangeBoundary(inst.trialSolution) 
     175    inst._keepSolutionWithinRangeBoundary(inst.trialSolution) 
    176176    return 
    177177 
     
    193193        i += 1 
    194194 
    195     inst.scaleSolutionWithRangeBoundary(inst.bestSolution) 
     195    inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 
    196196    return 
    197197 
     
    213213        i += 1 
    214214 
    215     inst.scaleSolutionWithRangeBoundary(inst.population[r1]) 
     215    inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 
    216216    return 
    217217     
  • mystic/mystic/termination.py

    r96 r102  
    1313 
    1414# Factories that give termination conditions 
     15def VTR(tolerance = 0.005): 
     16    """cost of last iteration is less than tolerance""" 
     17    def _(inst): 
     18         hist = inst.energy_history 
     19         return hist[-1] < tolerance 
     20    return _ 
     21 
    1522def ChangeOverGeneration(tolerance = 1e-6, generations = 30): 
    16     """change in cost is less than tolerance over a number of generations""" 
     23    """change in cost is less than tolerance over a number of generations 
     24cost[n-g] - cost[n] < tolerance, where g=generations""" 
    1725    def _(inst): 
    1826         hist = inst.energy_history 
     
    2129    return _ 
    2230 
    23 def VTR(tolerance = 0.005): 
    24     """change in cost from last iteration is less than tolerance""" 
     31def NormalizedChangeOverGeneration(tolerance = 1e-4, generations = 2): 
     32    """normalized change in cost is less than tolerance over a number of generations 
     33(cost[n-g] - cost[n]) /  0.5*(abs(cost[n-g]) + abs(cost[n])) <= tolerance""" 
     34    eta = 1e-20 
    2535    def _(inst): 
    2636         hist = inst.energy_history 
    27          return hist[-1] < tolerance 
     37         lg = len(hist) 
     38         diff = tolerance*(abs(hist[-generations])+abs(hist[-1])) + eta 
     39         return lg > generations and 2.0*(hist[-generations]-hist[-1]) <= diff 
    2840    return _ 
    29  
    30 def IterationRelativeTolerance(xtol=1e-4, ftol=1e-4): 
     41               
     42def CandidateRelativeTolerance(xtol=1e-4, ftol=1e-4): #FIXME: rename CandidateRelativeTolerance 
    3143    """absolute difference in candidates is less than tolerance 
    3244abs(xi-x0) <= xtol & abs(fi-f0) <= ftol, where x=params & f=cost""" 
     
    4254    return _ 
    4355 
    44                
    4556# end of file 
Note: See TracChangeset for help on using the changeset viewer.