Changeset 219
- Timestamp:
- 04/29/10 14:23:44 (6 years ago)
- Location:
- mystic
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_rosenbrock.py
r118 r219 16 16 from mystic.termination import ChangeOverGeneration, VTR 17 17 from mystic.models import rosen 18 from mystic import Sow 18 from mystic import Sow, VerboseSow 19 19 20 20 import random … … 23 23 ND = 3 24 24 NP = 30 25 #MAX_GENERATIONS = 29 25 26 MAX_GENERATIONS = 99999 26 27 … … 32 33 33 34 solver.Solve(rosen, termination = VTR(0.0001), \ 34 CrossProbability=0.5, ScalingFactor=0.6 )35 CrossProbability=0.5, ScalingFactor=0.6, disp=1) 35 36 36 37 solution = solver.Solution() … … 44 45 print "without bounds..." 45 46 from timeit import Timer 47 print "Differential Evolution" 48 print "======================" 46 49 t = Timer("main()", "from __main__ import main") 47 50 timetaken = t.timeit(number=1) … … 58 61 esow= Sow() 59 62 ssow= Sow() 63 #ssow= VerboseSow(1) 60 64 61 65 # xinit = [random.random() for j in range(ND)] … … 75 79 solver.SetEvaluationLimits(maxiter=MAX_GENERATIONS) 76 80 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) 78 82 sol = solver.Solution() 79 83 print sol -
mystic/examples/test_rosenbrock2.py
r103 r219 24 24 print "===================" 25 25 start = time.time() 26 from mystic.tools import Sow #VerboseSow 26 from mystic.tools import Sow, VerboseSow 27 #stepmon = VerboseSow(1) 27 28 stepmon = Sow() #VerboseSow(10) 28 29 from mystic.termination import CandidateRelativeTolerance as CRT 29 30 30 #from scipy.optimize import fmin31 from scipy.optimize import fmin 31 32 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) 33 34 solver = NelderMeadSimplexSolver(len(x0)) 34 35 solver.SetInitialPoints(x0) 35 36 solver.SetStrictRanges(min,max) 37 solver.SetEvaluationLimits(maxiter=146) 36 38 solver.enable_signal_handler() 37 39 solver.Solve(rosen,termination=CRT(xtol=4e-5),StepMonitor=stepmon,disp=1) -
mystic/examples/test_rosenbrock3.py
r116 r219 24 24 print "===========================" 25 25 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) 28 29 from mystic.termination import NormalizedChangeOverGeneration as NCOG 29 30 30 #from scipy.optimize import fmin_powell31 from scipy.optimize import fmin_powell 31 32 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) 33 34 solver = PowellDirectionalSolver(len(x0)) 34 35 solver.SetInitialPoints(x0) 35 36 solver.SetStrictRanges(min,max) 37 solver.SetEvaluationLimits(maxiter=13) 36 38 solver.enable_signal_handler() 37 39 solver.Solve(rosen,termination=NCOG(tolerance=1e-4),StepMonitor=stepmon,disp=1) -
mystic/mystic/abstract_solver.py
r140 r219 116 116 117 117 self._init_popEnergy = 1.0E20 #XXX: or numpy.inf? 118 self.popEnergy = [ 0.0] * NP118 self.popEnergy = [self._init_popEnergy] * NP 119 119 self.population = [[0.0 for i in range(dim)] for j in range(NP)] 120 120 self.energy_history = [] … … 211 211 for j in range(self.nDim): 212 212 self.population[i][j] = random.uniform(min[j],max[j]) 213 self.popEnergy[i] = self._init_popEnergy214 213 215 214 def SetMultinormalInitialPoints(self, mean, var = None): … … 236 235 for i in range(self.nPop): 237 236 self.population[i] = multivariate_normal(mean, var).tolist() 238 self.popEnergy[i] = self._init_popEnergy239 237 return 240 238 -
mystic/mystic/differential_evolution.py
r218 r219 249 249 self.scale = ScalingFactor 250 250 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) 252 275 253 276 if self._maxiter is None: … … 256 279 self._maxfun = self.nDim * self.nPop * 1000 #XXX: set better defaults? 257 280 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): 261 283 if fcalls[0] >= self._maxfun: break 262 284 for candidate in range(self.nPop): … … 277 299 278 300 self.energy_history.append(self.bestEnergy) 279 301 StepMonitor(self.bestSolution[:], self.bestEnergy) 302 self.generations += 1 280 303 if callback is not None: 281 304 callback(self.bestSolution) … … 283 306 if self._EARLYEXIT or termination(self): 284 307 break 285 286 self.generations = generation + 1287 308 288 309 signal.signal(signal.SIGINT,signal.default_int_handler) … … 394 415 self.scale = ScalingFactor 395 416 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) 397 449 398 450 if self._maxiter is None: … … 400 452 if self._maxfun is None: 401 453 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): 407 457 if fcalls[0] >= self._maxfun: break 408 458 for candidate in range(self.nPop): … … 435 485 436 486 self.energy_history.append(self.bestEnergy) 437 487 StepMonitor(self.bestSolution[:], self.bestEnergy) 488 self.generations += 1 438 489 if callback is not None: 439 490 callback(self.bestSolution) … … 441 492 if self._EARLYEXIT or termination(self): 442 493 break 443 444 self.generations = generation + 1445 494 446 495 signal.signal(signal.SIGINT,signal.default_int_handler) -
mystic/mystic/scipy_optimize.py
r132 r219 207 207 allvecs = [sim[0]] 208 208 fsim[0] = func(x0) 209 StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 209 210 210 211 #--- ensure initial simplex is within bounds --- … … 227 228 self.popEnergy = fsim 228 229 self.energy_history.append(self.bestEnergy) 230 StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 229 231 230 232 iterations = 1 231 233 232 234 while (fcalls[0] < self._maxfun and iterations < self._maxiter): 233 StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0]234 235 if self._EARLYEXIT or termination(self): 235 236 break … … 295 296 self.popEnergy = fsim 296 297 self.energy_history.append(self.bestEnergy) 298 StepMonitor(sim[0], fsim[0]) # sim = all values; "best" is sim[0] 297 299 298 300 self.generations = iterations -
mystic/mystic/tools.py
r126 r219 161 161 from numpy import ndarray 162 162 Sow.__call__(self, x, y) 163 self._step += 1164 163 if isinstance(y,(list,ndarray)): 165 164 y = y[0] #XXX: get the "best" fit... which should be in y[0] … … 171 170 if int(self._step % self._xinterval) == 0: 172 171 print "Generation %d has best fit parameters:\n %s" % (self._step, x) 172 self._step += 1 173 173 return 174 174 pass
Note: See TracChangeset
for help on using the changeset viewer.