Changeset 784
- Timestamp:
- 02/18/15 18:10:28 (15 months ago)
- Location:
- mystic/mystic
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/abstract_ensemble_solver.py
r783 r784 229 229 # check for termination messages 230 230 msg = termination(solver, info=True) 231 sig = "SolverInterrupt with %s" % {} 231 232 lim = "EvaluationLimits with %s" % {'evaluations':solver._maxfun, 232 233 'generations':solver._maxiter} … … 242 243 if disp: 243 244 print "Warning: Maximum number of iterations has been exceeded" 245 elif solver._EARLYEXIT: #XXX: self or solver ? 246 msg = sig 247 if disp: 248 print "Warning: Optimization terminated with signal interrupt." 244 249 elif msg and disp: 245 250 print "Optimization terminated successfully." -
mystic/mystic/abstract_solver.py
r783 r784 422 422 return 423 423 424 def enable_signal_handler(self): 424 def enable_signal_handler(self):#, callback='*'): 425 425 """enable workflow interrupt handler while solver is running""" 426 """ #XXX: disabled, as would add state to solver 427 input:: 428 - if a callback function is provided, generate a new handler with 429 the given callback. If callback is None, do not use a callback. 430 If callback is not provided, just turn on the existing handler. 431 """ 432 ## always _generate handler on first call 433 #if (self.signal_handler is None) and callback == '*': 434 # callback = None 435 ## when a new callback is given, generate a new handler 436 #if callback != '*': 437 # self._generateHandler(callback) 426 438 self._handle_sigint = True 427 439 … … 546 558 # check for termination messages 547 559 msg = termination(self, info=True) 560 sig = "SolverInterrupt with %s" % {} 548 561 lim = "EvaluationLimits with %s" % {'evaluations':self._maxfun, 549 562 'generations':self._maxiter} … … 559 572 if disp: 560 573 print "Warning: Maximum number of iterations has been exceeded" 574 elif self._EARLYEXIT: 575 msg = sig 576 if disp: 577 print "Warning: Optimization terminated with signal interrupt." 561 578 elif msg and disp: 562 579 print "Optimization terminated successfully." … … 671 688 return settings 672 689 673 def Solve(self, cost=None, termination=None, sigint_callback=None, 674 ExtraArgs=None, **kwds): 690 def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): 675 691 """Minimize a 'cost' function with given termination conditions. 676 692 … … 687 703 688 704 termination -- callable object providing termination conditions. 705 ExtraArgs -- extra arguments for cost. 706 707 Further Inputs: 708 689 709 sigint_callback -- callback function for signal handler. 690 ExtraArgs -- extra arguments for cost.691 692 Further Inputs:693 694 710 callback -- an optional user-supplied function to call after each 695 711 iteration. It is called as callback(xk), where xk is … … 700 716 cost = self._bootstrap_objective(cost, ExtraArgs) 701 717 # process and activate input settings 718 sigint_callback = kwds.pop('sigint_callback', None) 702 719 settings = self._process_inputs(kwds) 703 720 for key in settings: … … 705 722 706 723 # set up signal handler 724 self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton? 725 self._generateHandler(sigint_callback) 726 727 # activate signal handler 707 728 import signal 708 self._EARLYEXIT = False #XXX: why not use EARLYEXIT singleton?709 self._generateHandler(sigint_callback)710 729 if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 711 730 … … 721 740 722 741 # the main optimization loop 723 while not self.CheckTermination() and not self._EARLYEXIT:742 while not self.CheckTermination(): 724 743 self.Step(**settings) # includes settings['callback'] 725 744 else: self._exitMain() 726 745 727 # handlesignal interrupts746 # restore default handler for signal interrupts 728 747 signal.signal(signal.SIGINT,signal.default_int_handler) 729 748 -
mystic/mystic/differential_evolution.py
r783 r784 291 291 return settings 292 292 293 def Solve(self, cost=None, termination=None, sigint_callback=None, 294 ExtraArgs=None, **kwds): 293 def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): 295 294 """Minimize a function using differential evolution. 296 295 … … 307 306 308 307 termination -- callable object providing termination conditions. 309 sigint_callback -- callback function for signal handler.310 308 ExtraArgs -- extra arguments for cost. 311 309 … … 318 316 ScalingFactor -- multiplier for the impact of mutations on the 319 317 trial solution [default = 0.8] 318 sigint_callback -- callback function for signal handler. 320 319 callback -- an optional user-supplied function to call after each 321 320 iteration. It is called as callback(xk), where xk is … … 324 323 """ 325 324 super(DifferentialEvolutionSolver, self).Solve(cost, termination,\ 326 sigint_callback,ExtraArgs, **kwds)325 ExtraArgs, **kwds) 327 326 return 328 327 … … 461 460 return settings 462 461 463 def Solve(self, cost=None, termination=None, sigint_callback=None, 464 ExtraArgs=None, **kwds): 462 def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): 465 463 """Minimize a function using differential evolution. 466 464 … … 478 476 479 477 termination -- callable object providing termination conditions. 480 sigint_callback -- callback function for signal handler.481 478 ExtraArgs -- extra arguments for cost. 482 479 … … 489 486 ScalingFactor -- multiplier for the impact of mutations on the 490 487 trial solution [default = 0.8] 488 sigint_callback -- callback function for signal handler. 491 489 callback -- an optional user-supplied function to call after each 492 490 iteration. It is called as callback(xk), where xk is … … 495 493 """ 496 494 super(DifferentialEvolutionSolver2, self).Solve(cost, termination,\ 497 sigint_callback,ExtraArgs, **kwds)495 ExtraArgs, **kwds) 498 496 return 499 497 -
mystic/mystic/ensemble.py
r776 r784 57 57 58 58 #FIXME: should take cost=None, ExtraArgs=None... and utilize Step 59 def Solve(self, cost, termination=None, sigint_callback=None, 60 ExtraArgs=(), **kwds): 59 def Solve(self, cost, termination=None, ExtraArgs=(), **kwds): 61 60 """Minimize a function using batch grid optimization. 62 61 … … 73 72 74 73 termination -- callable object providing termination conditions. 74 ExtraArgs -- extra arguments for cost. 75 76 Further Inputs: 77 75 78 sigint_callback -- callback function for signal handler. 76 ExtraArgs -- extra arguments for cost.77 78 Further Inputs:79 80 79 callback -- an optional user-supplied function to call after each 81 80 iteration. It is called as callback(xk), where xk is the … … 84 83 """ 85 84 # process and activate input settings 85 sigint_callback = kwds.pop('sigint_callback', None) 86 86 settings = self._process_inputs(kwds) 87 87 disp = settings.get('disp', False) … … 93 93 #------------------------------------------------------------- 94 94 95 import signal96 #self._EARLYEXIT = False97 98 95 #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' 99 96 from python_map import python_map … … 103 100 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 104 101 105 #generate signal_handler 102 # set up signal handler 103 #self._EARLYEXIT = False 106 104 self._generateHandler(sigint_callback) 105 106 # activate signal_handler 107 import signal 107 108 if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 108 109 … … 222 223 #------------------------------------------------------------- 223 224 225 # restore default handler for signal interrupts 224 226 signal.signal(signal.SIGINT,signal.default_int_handler) 225 227 … … 248 250 self._termination = NormalizedChangeOverGeneration(convergence_tol) 249 251 250 def Solve(self, cost, termination=None, sigint_callback=None, 251 ExtraArgs=(), **kwds): 252 def Solve(self, cost, termination=None, ExtraArgs=(), **kwds): 252 253 """Minimize a function using buckshot optimization. 253 254 … … 264 265 265 266 termination -- callable object providing termination conditions. 267 ExtraArgs -- extra arguments for cost. 268 269 Further Inputs: 270 266 271 sigint_callback -- callback function for signal handler. 267 ExtraArgs -- extra arguments for cost.268 269 Further Inputs:270 271 272 callback -- an optional user-supplied function to call after each 272 273 iteration. It is called as callback(xk), where xk is the … … 275 276 """ 276 277 # process and activate input settings 278 sigint_callback = kwds.pop('sigint_callback', None) 277 279 settings = self._process_inputs(kwds) 278 280 disp = settings.get('disp', False) … … 284 286 #------------------------------------------------------------- 285 287 286 import signal287 #self._EARLYEXIT = False288 289 288 #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' 290 289 from python_map import python_map … … 294 293 self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 295 294 296 #generate signal_handler 295 # set up signal handler 296 #self._EARLYEXIT = False 297 297 self._generateHandler(sigint_callback) 298 299 # activate signal_handler 300 import signal 298 301 if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 299 302 … … 406 409 #------------------------------------------------------------- 407 410 411 # restore default handler for signal interrupts 408 412 signal.signal(signal.SIGINT,signal.default_int_handler) 409 413 -
mystic/mystic/scipy_optimize.py
r782 r784 270 270 return settings 271 271 272 def Solve(self, cost=None, termination=None, sigint_callback=None, 273 ExtraArgs=None, **kwds): 272 def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): 274 273 """Minimize a function using the downhill simplex algorithm. 275 274 … … 286 285 287 286 termination -- callable object providing termination conditions. 287 ExtraArgs -- extra arguments for cost. 288 289 Further Inputs: 290 288 291 sigint_callback -- callback function for signal handler. 289 ExtraArgs -- extra arguments for cost.290 291 Further Inputs:292 293 292 callback -- an optional user-supplied function to call after each 294 293 iteration. It is called as callback(xk), where xk is the … … 298 297 """ 299 298 super(NelderMeadSimplexSolver, self).Solve(cost, termination,\ 300 sigint_callback,ExtraArgs, **kwds)299 ExtraArgs, **kwds) 301 300 return 302 301 … … 593 592 return settings 594 593 595 def Solve(self, cost=None, termination=None, sigint_callback=None, 596 ExtraArgs=None, **kwds): 594 def Solve(self, cost=None, termination=None, ExtraArgs=None, **kwds): 597 595 """Minimize a function using modified Powell's method. 598 596 … … 609 607 610 608 termination -- callable object providing termination conditions. 609 ExtraArgs -- extra arguments for cost. 610 611 Further Inputs: 612 611 613 sigint_callback -- callback function for signal handler. 612 ExtraArgs -- extra arguments for cost.613 614 Further Inputs:615 616 614 callback -- an optional user-supplied function to call after each 617 615 iteration. It is called as callback(xk), where xk is the … … 622 620 """ 623 621 super(PowellDirectionalSolver, self).Solve(cost, termination,\ 624 sigint_callback,ExtraArgs, **kwds)622 ExtraArgs, **kwds) 625 623 return 626 624 -
mystic/mystic/termination.py
r776 r784 349 349 return _EvaluationLimits 350 350 351 def SolverInterrupt(): #XXX: enable = True ? 352 """handler is enabled and interrupt is given: 353 354 _EARLYEXIT == True""" 355 doc = "SolverInterrupt with %s" % {} 356 def _SolverInterrupt(inst, info=False): 357 if info: info = lambda x:x 358 else: info = bool 359 if inst._EARLYEXIT: return info(doc) 360 return info(null) 361 _SolverInterrupt.__doc__ = doc 362 return _SolverInterrupt 363 351 364 # end of file
Note: See TracChangeset
for help on using the changeset viewer.