Changeset 110
- Timestamp:
- 03/11/09 00:40:03 (7 years ago)
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
DEV_NOTES
r109 r110 7 7 - use wrap_bounds instead of existing method (?) 8 8 - fix bug: DESolver throws error when given numpy.inf as a bound (!) 9 - add maxfun, disp, and scipy's callback to DESolver10 9 - enable signal_handler & sigint_callback (?) for all 'one-liner' interfaces 11 10 - DESolver needs documentation -
DEV_TODO
r109 r110 7 7 * Allow sigint_callback to take a list (i.e. provide call[i]) 8 8 * Add "constraints" to models (design similar to pyre.inventory and validators) 9 * Extend parallel launcher and communication to distributed services (gsl & idd) 9 10 10 11 INDIRECT: -
mystic/examples/test_ffitB.py
r109 r110 28 28 solver.Solve(ChebyshevCost, strategy, termination = VTR(0.01) , \ 29 29 maxiter= MAX_GENERATIONS, CrossProbability=1.0, ScalingFactor=0.9 , \ 30 sigint_callback = plot_solution \30 StepMonitor=VerboseSow(30), sigint_callback = plot_solution \ 31 31 ) 32 32 -
mystic/mystic/differential_evolution.py
r109 r110 45 45 'diffev'] 46 46 47 from mystic.tools import Null, wrap_function 47 from mystic.tools import Null, wrap_function, unpair 48 48 49 49 from abstract_solver import AbstractSolver … … 90 90 91 91 def Solve(self, costfunction, strategy, termination, 92 maxiter , CrossProbability = 0.5, ScalingFactor = 0.7,93 sigint_callback = None,94 EvaluationMonitor=Null, StepMonitor=Null, ExtraArgs=() ):92 maxiter=None, maxfun=None, sigint_callback=None, 93 CrossProbability = 0.5, ScalingFactor = 0.7, 94 EvaluationMonitor=Null, StepMonitor=Null, ExtraArgs=(), **kwds): 95 95 """Minimize a function using differential evolution. 96 96 … … 113 113 """ 114 114 #FIXME: Solve() interface does not conform to AbstractSolver interface 115 callback=None #user-supplied function, called after each step 116 if kwds.has_key('callback'): callback = kwds['callback'] 117 #------------------------------------------------------------- 118 115 119 import signal 116 120 import mystic.termination as detools … … 159 163 self.bestEnergy = 1.0E20 160 164 165 if maxiter is None: 166 maxiter = self.nDim * self.nPop * 10 #XXX: set better defaults? 167 if maxfun is None: 168 maxfun = self.nDim * self.nPop * 1000 #XXX: set better defaults? 169 self._maxiter = maxiter 170 self._maxfun = maxfun 171 161 172 generation = 0 162 173 for generation in range(maxiter): 163 174 StepMonitor(self.bestSolution[:], self.bestEnergy) 175 if fcalls[0] >= maxfun: break 164 176 for candidate in range(self.nPop): 165 177 # generate trialSolution (within valid range) … … 181 193 #print "Generation %d has best cost function : %f" % (generation, self.bestEnergy) 182 194 self.energy_history.append(self.bestEnergy) 195 196 if callback is not None: 197 callback(self.bestSolution) 183 198 184 199 if detools.EARLYEXIT or termination(self): … … 207 222 """ 208 223 def Solve(self, costfunction, strategy, termination, 209 maxiter , CrossProbability = 0.5, ScalingFactor = 0.7,210 sigint_callback = None,211 EvaluationMonitor=Null, StepMonitor=Null, ExtraArgs=() ):224 maxiter=None, maxfun=None, sigint_callback=None, 225 CrossProbability = 0.5, ScalingFactor = 0.7, 226 EvaluationMonitor=Null, StepMonitor=Null, ExtraArgs=(), **kwds): 212 227 """Minimize a function using differential evolution. 213 228 … … 230 245 """ 231 246 #FIXME: Solve() interface does not conform to AbstractSolver interface 247 callback=None #user-supplied function, called after each step 248 if kwds.has_key('callback'): callback = kwds['callback'] 249 #------------------------------------------------------------- 250 232 251 import signal 233 252 import mystic.termination as detools … … 274 293 self.bestEnergy = 1.0E20 275 294 295 if maxiter is None: 296 maxiter = self.nDim * self.nPop * 10 #XXX: set better defaults? 297 if maxfun is None: 298 maxfun = self.nDim * self.nPop * 1000 #XXX: set better defaults? 299 self._maxiter = maxiter 300 self._maxfun = maxfun 301 276 302 trialPop = [[0.0 for i in range(self.nDim)] for j in range(self.nPop)] 277 303 … … 279 305 for generation in range(maxiter): 280 306 StepMonitor(self.bestSolution[:], self.bestEnergy) 307 if fcalls[0] >= maxfun: break 281 308 for candidate in range(self.nPop): 282 309 # generate trialSolution (within valid range) … … 301 328 #print "Generation %d has best cost function : %f" % (generation, self.bestEnergy) 302 329 self.energy_history.append(self.bestEnergy) 330 331 if callback is not None: 332 callback(self.bestSolution) 303 333 304 334 if detools.EARLYEXIT or termination(self): … … 313 343 314 344 def diffev(func,x0,npop,args=(),bounds=None,ftol=5e-3,gtol=None, 315 maxiter=None, cross=1.0,scale=0.9, #maxfun=None,316 full_output=0,disp=1,retall=0 ): #,callback=None):345 maxiter=None,maxfun=None,cross=1.0,scale=0.9, 346 full_output=0,disp=1,retall=0,callback=None): 317 347 """interface for differential evolution that mimics scipy.optimize.fmin""" 318 348 … … 322 352 from mystic.strategy import Best1Exp #, Best1Bin, Rand1Exp 323 353 strategy = Best1Exp 324 if gtol: 354 if gtol: #if number of generations provided, use ChangeOverGeneration 325 355 from mystic.termination import ChangeOverGeneration 326 356 termination = ChangeOverGeneration(ftol,gtol) … … 329 359 termination = VTR(ftol) 330 360 331 def unroll(bounds): #TODO: move to tools?332 from numpy import asarray333 boundsT = asarray(bounds).transpose()334 return [i.tolist() for i in boundsT]335 336 361 ND = len(x0) 337 362 solver = DifferentialEvolutionSolver2(ND,npop) 338 363 if bounds: 339 minb,maxb = un roll(bounds)364 minb,maxb = unpair(bounds) 340 365 solver.SetStrictRanges(minb,maxb) 341 try: 342 minb,maxb = unroll(x0) 366 367 try: #x0 passed as 1D array of (min,max) pairs 368 minb,maxb = unpair(x0) 343 369 solver.SetRandomInitialPoints(minb,maxb) 344 except: 370 except: #x0 passed as 1D array of initial parameter values 345 371 solver.SetInitialPoints(x0) 346 372 347 373 #solver.enable_signal_handler() 348 #TODO: add maxfun & disp to DESolve 349 #TODO: add scipy's (end-of-iteration) callback to DESolve 374 #XXX: move maxiter & maxfun kwds from Solve() to solver.SetLimits() ? 350 375 #TODO: enable signal handlers & sigint_callbacks? for all minimal interfaces 351 376 #FIXME: DESolve can't handle bounds of numpy.inf 352 377 solver.Solve(func,strategy=strategy,termination=termination,\ 353 maxiter=maxiter,\ 354 #maxfun=maxfun,\ 378 maxiter=maxiter,maxfun=maxfun,\ 355 379 CrossProbability=cross,ScalingFactor=scale,\ 356 380 EvaluationMonitor=evalmon,StepMonitor=stepmon,\ 357 #sigint_callback= callback,\358 ExtraArgs=args )381 #sigint_callback=other_callback,\ 382 ExtraArgs=args,callback=callback) 359 383 solution = solver.Solution() 360 384 … … 368 392 allvecs = stepmon.x 369 393 370 from numpy import inf 371 maxfun=inf #XXX: maxfun not implemented for DESolvers 372 if fcalls >= maxfun: 394 if fcalls >= solver._maxfun: 373 395 warnflag = 1 374 396 if disp: 375 397 print "Warning: Maximum number of function evaluations has "\ 376 398 "been exceeded." 377 elif iterations >= maxiter:399 elif iterations >= solver._maxiter: 378 400 warnflag = 2 379 401 if disp: -
mystic/mystic/tools.py
r107 r110 33 33 34 34 -- wrap_bounds : impose bounds on a funciton object 35 36 -- unpair : convert a 1D array of N pairs to two 1D arrays of N values 35 37 36 38 """ … … 242 244 return _ 243 245 246 247 def unpair(pairs): 248 '''convert a 1D array of N pairs to two 1D arrays of N values 249 For example: 250 [a0,a1,a2],[b0,b1,b2] = unpair([(a0,b0),(a1,b1),(a2,b2)])''' 251 from numpy import asarray 252 pairsT = asarray(pairs).transpose() 253 return [i.tolist() for i in pairsT] 254 255 244 256 if __name__=='__main__': 245 257 import doctest
Note: See TracChangeset
for help on using the changeset viewer.