Changeset 102
- Timestamp:
- 02/20/09 14:16:13 (7 years ago)
- Location:
- mystic
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/sam_corana2.py
r97 r102 12 12 #from test_corana import * 13 13 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 14 from mystic.termination import IterationRelativeTolerance as IRT14 from mystic.termination import CandidateRelativeTolerance as CRT 15 15 from mystic import getch, Sow 16 16 … … 44 44 solver = fmin(2) 45 45 solver.SetRandomInitialPoints([0,0],[2,2]) 46 solver.Solve(Corana2, termination= IRT(), StepMonitor = simplex)46 solver.Solve(Corana2, termination=CRT(), StepMonitor = simplex) 47 47 sol = solver.Solution() 48 48 -
mystic/examples/sam_mogi.py
r97 r102 10 10 from test_mogi import * 11 11 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 12 from mystic.termination import IterationRelativeTolerance as IRT12 from mystic.termination import CandidateRelativeTolerance as CRT 13 13 from mystic import getch, Sow 14 14 … … 66 66 solver = fmin(len(xinit)) 67 67 solver.SetInitialPoints(xinit) 68 solver.Solve(cost_function, termination= IRT(), StepMonitor = simplex)68 solver.Solve(cost_function, termination=CRT(), StepMonitor = simplex) 69 69 sol = solver.Solution() 70 70 print sol … … 84 84 solver = fmin(len(xinit)) 85 85 solver.SetInitialPoints(xinit) 86 solver.Solve(cost_function, termination= IRT(), StepMonitor = simplex)86 solver.Solve(cost_function, termination=CRT(), StepMonitor = simplex) 87 87 sol = solver.Solution() 88 88 print sol -
mystic/examples/sam_rosenbrock.py
r97 r102 13 13 from test_rosenbrock import * 14 14 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 15 from mystic.termination import IterationRelativeTolerance as IRT15 from mystic.termination import CandidateRelativeTolerance as CRT 16 16 from mystic import getch, Sow 17 17 … … 45 45 solver = fmin(len(xinit)) 46 46 solver.SetInitialPoints(xinit) 47 solver.Solve(rosen, termination= IRT(), StepMonitor = simplex)47 solver.Solve(rosen, termination=CRT(), StepMonitor = simplex) 48 48 sol = solver.Solution() 49 49 -
mystic/examples/sam_zimmermann.py
r97 r102 13 13 from test_zimmermann import * 14 14 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 15 from mystic.termination import IterationRelativeTolerance as IRT15 from mystic.termination import CandidateRelativeTolerance as CRT 16 16 from mystic import getch, Sow 17 17 … … 42 42 solver = fmin(2) 43 43 solver.SetRandomInitialPoints([0,0],[7,7]) 44 solver.Solve(CostFunction, termination= IRT(), StepMonitor = simplex)44 solver.Solve(CostFunction, termination=CRT(), StepMonitor = simplex) 45 45 sol = solver.Solution() 46 46 -
mystic/examples/test_mogi.py
r97 r102 111 111 112 112 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 113 from mystic.termination import IterationRelativeTolerance as IRT113 from mystic.termination import CandidateRelativeTolerance as CRT 114 114 from scipy.optimize import leastsq, fmin_cg 115 115 # … … 128 128 solver = fmin(len(point)) 129 129 solver.SetInitialPoints(point) 130 solver.Solve(cost_function, IRT(), EvaluationMonitor = esow, StepMonitor = simplex)130 solver.Solve(cost_function, CRT(), EvaluationMonitor = esow, StepMonitor = simplex) 131 131 sol = solver.Solution() 132 132 -
mystic/examples/test_rosenbrock.py
r97 r102 16 16 from mystic.models import rosen 17 17 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 18 from mystic.termination import IterationRelativeTolerance as IRT18 from mystic.termination import CandidateRelativeTolerance as CRT 19 19 from mystic import Sow 20 20 … … 52 52 solver = fmin(len(xinit)) 53 53 solver.SetInitialPoints(xinit) 54 solver.Solve(rosen, IRT(), EvaluationMonitor = esow, StepMonitor = ssow)54 solver.Solve(rosen, CRT(), EvaluationMonitor = esow, StepMonitor = ssow) 55 55 sol = solver.Solution() 56 56 print sol -
mystic/examples/test_rosenbrock2.py
r97 r102 26 26 from mystic.tools import VerboseSow 27 27 stepmon = VerboseSow(10) 28 from mystic.termination import IterationRelativeTolerance as IRT28 from mystic.termination import CandidateRelativeTolerance as CRT 29 29 30 30 from mystic.scipy_optimize import fmin, NelderMeadSimplexSolver … … 34 34 solver.SetStrictRanges(min,max) 35 35 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) 37 37 print solver.Solution() 38 38 -
mystic/examples/test_zimmermann.py
r97 r102 52 52 from mystic import Sow 53 53 from mystic.scipy_optimize import NelderMeadSimplexSolver as fmin 54 from mystic.termination import IterationRelativeTolerance as IRT54 from mystic.termination import CandidateRelativeTolerance as CRT 55 55 56 56 simplex = Sow() … … 60 60 solver = fmin(len(xinit)) 61 61 solver.SetInitialPoints(xinit) 62 solver.Solve(CostFunction, IRT(), EvaluationMonitor = esow, StepMonitor = simplex)62 solver.Solve(CostFunction, CRT(), EvaluationMonitor = esow, StepMonitor = simplex) 63 63 sol = solver.Solution() 64 64 print "fmin solution: ", sol -
mystic/mystic/differential_evolution.py
r95 r102 16 16 ## Birmingham, Alabama USA 17 17 ## zunzun@zunzun.com 18 ## 19 ## bounds added by mmckerns@caltech.edu 18 20 19 21 """ … … 82 84 return 83 85 84 deftruncateSolutionAtRangeBoundary(self):85 86 87 88 89 90 91 92 93 94 95 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 96 98 97 def scaleSolutionWithRangeBoundary(self, base): 99 def _keepSolutionWithinRangeBoundary(self, base): #XXX: could be smarter? 100 #def scaleSolutionWithRangeBoundary(self, base): 98 101 """scale trialSolution to be between base value and range boundary""" 99 102 if not self._useStrictRange: -
mystic/mystic/scipy_optimize.py
r101 r102 70 70 return 71 71 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): 73 83 """ensure that initial simplex is set within bounds""" 74 84 #code modified from park-1.2/park/simplex.py (version 1257) … … 186 196 fcalls, func = wrap_function(func, args, EvaluationMonitor) 187 197 if self._useStrictRange: 198 x0 = self._setGuessWithinRangeBoundary(x0) 188 199 func = wrap_bounds(func, self._strictMin, self._strictMax) 189 200 … … 248 259 249 260 #--- ensure initial simplex is within bounds --- 250 x0,val = self. setSimplexWithinRangeBoundary(x0,radius)261 x0,val = self._setSimplexWithinRangeBoundary(x0,radius) 251 262 #--- end bounds code --- 252 263 for k in range(0,N): … … 265 276 self.population = sim 266 277 self.popEnergy = fsim 278 self.energy_history.append(self.bestEnergy) 267 279 268 280 iterations = 1 … … 378 390 stepmon = Sow() 379 391 evalmon = Sow() 380 from mystic.termination import IterationRelativeTolerance as IRT392 from mystic.termination import CandidateRelativeTolerance as CRT 381 393 382 394 solver = NelderMeadSimplexSolver(len(x0)) 383 395 solver.SetInitialPoints(x0) 384 396 #solver.enable_signal_handler() 385 solver.Solve(func,termination= IRT(xtol,ftol),\397 solver.Solve(func,termination=CRT(xtol,ftol),\ 386 398 maxiter=maxiter,maxfun=maxfun,\ 387 399 EvaluationMonitor=evalmon,StepMonitor=stepmon,\ -
mystic/mystic/strategy.py
r96 r102 33 33 i += 1 34 34 35 inst. scaleSolutionWithRangeBoundary(inst.bestSolution)35 inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 36 36 return 37 37 … … 56 56 inst.population[r2][i]) 57 57 58 inst. scaleSolutionWithRangeBoundary(inst.bestSolution)58 inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 59 59 return 60 60 … … 75 75 i += 1 76 76 77 inst. scaleSolutionWithRangeBoundary(inst.population[r1])77 inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 78 78 return 79 79 … … 96 96 i += 1 97 97 98 inst. scaleSolutionWithRangeBoundary(inst.trialSolution)98 inst._keepSolutionWithinRangeBoundary(inst.trialSolution) 99 99 return 100 100 … … 116 116 i += 1 117 117 118 inst. scaleSolutionWithRangeBoundary(inst.bestSolution)118 inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 119 119 return 120 120 … … 136 136 i += 1 137 137 138 inst. scaleSolutionWithRangeBoundary(inst.population[r1])138 inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 139 139 return 140 140 … … 154 154 i += 1 155 155 156 inst. scaleSolutionWithRangeBoundary(inst.population[r1])156 inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 157 157 return 158 158 … … 173 173 i += 1 174 174 175 inst. scaleSolutionWithRangeBoundary(inst.trialSolution)175 inst._keepSolutionWithinRangeBoundary(inst.trialSolution) 176 176 return 177 177 … … 193 193 i += 1 194 194 195 inst. scaleSolutionWithRangeBoundary(inst.bestSolution)195 inst._keepSolutionWithinRangeBoundary(inst.bestSolution) 196 196 return 197 197 … … 213 213 i += 1 214 214 215 inst. scaleSolutionWithRangeBoundary(inst.population[r1])215 inst._keepSolutionWithinRangeBoundary(inst.population[r1]) 216 216 return 217 217 -
mystic/mystic/termination.py
r96 r102 13 13 14 14 # Factories that give termination conditions 15 def 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 15 22 def 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 24 cost[n-g] - cost[n] < tolerance, where g=generations""" 17 25 def _(inst): 18 26 hist = inst.energy_history … … 21 29 return _ 22 30 23 def VTR(tolerance = 0.005): 24 """change in cost from last iteration is less than tolerance""" 31 def 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 25 35 def _(inst): 26 36 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 28 40 return _ 29 30 def IterationRelativeTolerance(xtol=1e-4, ftol=1e-4):41 42 def CandidateRelativeTolerance(xtol=1e-4, ftol=1e-4): #FIXME: rename CandidateRelativeTolerance 31 43 """absolute difference in candidates is less than tolerance 32 44 abs(xi-x0) <= xtol & abs(fi-f0) <= ftol, where x=params & f=cost""" … … 42 54 return _ 43 55 44 45 56 # end of file
Note: See TracChangeset
for help on using the changeset viewer.