Changeset 784 for mystic


Ignore:
Timestamp:
02/18/15 18:10:28 (15 months ago)
Author:
mmckerns
Message:

added SolverInterrupt? as termination; pull _EARLYEXIT into CheckTermination?

Location:
mystic/mystic
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/abstract_ensemble_solver.py

    r783 r784  
    229229        # check for termination messages 
    230230        msg = termination(solver, info=True) 
     231        sig = "SolverInterrupt with %s" % {} 
    231232        lim = "EvaluationLimits with %s" % {'evaluations':solver._maxfun, 
    232233                                            'generations':solver._maxiter} 
     
    242243            if disp: 
    243244                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." 
    244249        elif msg and disp: 
    245250            print "Optimization terminated successfully." 
  • mystic/mystic/abstract_solver.py

    r783 r784  
    422422        return 
    423423 
    424     def enable_signal_handler(self): 
     424    def enable_signal_handler(self):#, callback='*'): 
    425425        """enable workflow interrupt handler while solver is running""" 
     426        """ #XXX: disabled, as would add state to solver 
     427input:: 
     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) 
    426438        self._handle_sigint = True 
    427439 
     
    546558        # check for termination messages 
    547559        msg = termination(self, info=True) 
     560        sig = "SolverInterrupt with %s" % {} 
    548561        lim = "EvaluationLimits with %s" % {'evaluations':self._maxfun, 
    549562                                            'generations':self._maxiter} 
     
    559572            if disp: 
    560573                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." 
    561578        elif msg and disp: 
    562579            print "Optimization terminated successfully." 
     
    671688        return settings 
    672689 
    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): 
    675691        """Minimize a 'cost' function with given termination conditions. 
    676692 
     
    687703 
    688704    termination -- callable object providing termination conditions. 
     705    ExtraArgs -- extra arguments for cost. 
     706 
     707Further Inputs: 
     708 
    689709    sigint_callback -- callback function for signal handler. 
    690     ExtraArgs -- extra arguments for cost. 
    691  
    692 Further Inputs: 
    693  
    694710    callback -- an optional user-supplied function to call after each 
    695711        iteration.  It is called as callback(xk), where xk is 
     
    700716        cost = self._bootstrap_objective(cost, ExtraArgs) 
    701717        # process and activate input settings 
     718        sigint_callback = kwds.pop('sigint_callback', None) 
    702719        settings = self._process_inputs(kwds) 
    703720        for key in settings: 
     
    705722 
    706723        # set up signal handler 
     724        self._EARLYEXIT = False  #XXX: why not use EARLYEXIT singleton? 
     725        self._generateHandler(sigint_callback) 
     726 
     727        # activate signal handler 
    707728        import signal 
    708         self._EARLYEXIT = False  #XXX: why not use EARLYEXIT singleton? 
    709         self._generateHandler(sigint_callback)  
    710729        if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 
    711730 
     
    721740 
    722741        # the main optimization loop 
    723         while not self.CheckTermination() and not self._EARLYEXIT: 
     742        while not self.CheckTermination(): 
    724743            self.Step(**settings)  # includes settings['callback'] 
    725744        else: self._exitMain() 
    726745 
    727         # handle signal interrupts 
     746        # restore default handler for signal interrupts 
    728747        signal.signal(signal.SIGINT,signal.default_int_handler) 
    729748 
  • mystic/mystic/differential_evolution.py

    r783 r784  
    291291        return settings 
    292292 
    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): 
    295294        """Minimize a function using differential evolution. 
    296295 
     
    307306 
    308307    termination -- callable object providing termination conditions. 
    309     sigint_callback -- callback function for signal handler. 
    310308    ExtraArgs -- extra arguments for cost. 
    311309 
     
    318316    ScalingFactor -- multiplier for the impact of mutations on the 
    319317        trial solution [default = 0.8] 
     318    sigint_callback -- callback function for signal handler. 
    320319    callback -- an optional user-supplied function to call after each 
    321320        iteration.  It is called as callback(xk), where xk is 
     
    324323        """ 
    325324        super(DifferentialEvolutionSolver, self).Solve(cost, termination,\ 
    326                                       sigint_callback, ExtraArgs, **kwds) 
     325                                                      ExtraArgs, **kwds) 
    327326        return 
    328327 
     
    461460        return settings 
    462461 
    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): 
    465463        """Minimize a function using differential evolution. 
    466464 
     
    478476 
    479477    termination -- callable object providing termination conditions. 
    480     sigint_callback -- callback function for signal handler. 
    481478    ExtraArgs -- extra arguments for cost. 
    482479 
     
    489486    ScalingFactor -- multiplier for the impact of mutations on the 
    490487        trial solution [default = 0.8] 
     488    sigint_callback -- callback function for signal handler. 
    491489    callback -- an optional user-supplied function to call after each 
    492490        iteration.  It is called as callback(xk), where xk is 
     
    495493        """ 
    496494        super(DifferentialEvolutionSolver2, self).Solve(cost, termination,\ 
    497                                       sigint_callback, ExtraArgs, **kwds) 
     495                                                        ExtraArgs, **kwds) 
    498496        return  
    499497 
  • mystic/mystic/ensemble.py

    r776 r784  
    5757 
    5858    #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): 
    6160        """Minimize a function using batch grid optimization. 
    6261 
     
    7372 
    7473    termination -- callable object providing termination conditions. 
     74    ExtraArgs -- extra arguments for cost. 
     75 
     76Further Inputs: 
     77 
    7578    sigint_callback -- callback function for signal handler. 
    76     ExtraArgs -- extra arguments for cost. 
    77  
    78 Further Inputs: 
    79  
    8079    callback -- an optional user-supplied function to call after each 
    8180        iteration.  It is called as callback(xk), where xk is the 
     
    8483        """ 
    8584        # process and activate input settings 
     85        sigint_callback = kwds.pop('sigint_callback', None) 
    8686        settings = self._process_inputs(kwds) 
    8787        disp = settings.get('disp', False) 
     
    9393        #------------------------------------------------------------- 
    9494 
    95         import signal 
    96        #self._EARLYEXIT = False 
    97  
    9895       #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' 
    9996        from python_map import python_map 
     
    103100            self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 
    104101 
    105         #generate signal_handler 
     102        # set up signal handler 
     103       #self._EARLYEXIT = False 
    106104        self._generateHandler(sigint_callback)  
     105 
     106        # activate signal_handler 
     107        import signal 
    107108        if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 
    108109 
     
    222223        #------------------------------------------------------------- 
    223224 
     225        # restore default handler for signal interrupts 
    224226        signal.signal(signal.SIGINT,signal.default_int_handler) 
    225227 
     
    248250        self._termination = NormalizedChangeOverGeneration(convergence_tol) 
    249251 
    250     def Solve(self, cost, termination=None, sigint_callback=None, 
    251                                             ExtraArgs=(), **kwds): 
     252    def Solve(self, cost, termination=None, ExtraArgs=(), **kwds): 
    252253        """Minimize a function using buckshot optimization. 
    253254 
     
    264265 
    265266    termination -- callable object providing termination conditions. 
     267    ExtraArgs -- extra arguments for cost. 
     268 
     269Further Inputs: 
     270 
    266271    sigint_callback -- callback function for signal handler. 
    267     ExtraArgs -- extra arguments for cost. 
    268  
    269 Further Inputs: 
    270  
    271272    callback -- an optional user-supplied function to call after each 
    272273        iteration.  It is called as callback(xk), where xk is the 
     
    275276        """ 
    276277        # process and activate input settings 
     278        sigint_callback = kwds.pop('sigint_callback', None) 
    277279        settings = self._process_inputs(kwds) 
    278280        disp = settings.get('disp', False) 
     
    284286        #------------------------------------------------------------- 
    285287 
    286         import signal 
    287        #self._EARLYEXIT = False 
    288  
    289288       #FIXME: EvaluationMonitor fails for MPI, throws error for 'pp' 
    290289        from python_map import python_map 
     
    294293            self._fcalls, cost = wrap_function(cost, ExtraArgs, self._evalmon) 
    295294 
    296         #generate signal_handler 
     295        # set up signal handler 
     296       #self._EARLYEXIT = False 
    297297        self._generateHandler(sigint_callback)  
     298 
     299        # activate signal_handler 
     300        import signal 
    298301        if self._handle_sigint: signal.signal(signal.SIGINT,self.signal_handler) 
    299302 
     
    406409        #------------------------------------------------------------- 
    407410 
     411        # restore default handler for signal interrupts 
    408412        signal.signal(signal.SIGINT,signal.default_int_handler) 
    409413 
  • mystic/mystic/scipy_optimize.py

    r782 r784  
    270270        return settings 
    271271 
    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): 
    274273        """Minimize a function using the downhill simplex algorithm. 
    275274 
     
    286285 
    287286    termination -- callable object providing termination conditions. 
     287    ExtraArgs -- extra arguments for cost. 
     288 
     289Further Inputs: 
     290 
    288291    sigint_callback -- callback function for signal handler. 
    289     ExtraArgs -- extra arguments for cost. 
    290  
    291 Further Inputs: 
    292  
    293292    callback -- an optional user-supplied function to call after each 
    294293        iteration.  It is called as callback(xk), where xk is the 
     
    298297""" 
    299298        super(NelderMeadSimplexSolver, self).Solve(cost, termination,\ 
    300                                   sigint_callback, ExtraArgs, **kwds) 
     299                                                  ExtraArgs, **kwds) 
    301300        return 
    302301 
     
    593592        return settings 
    594593 
    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): 
    597595        """Minimize a function using modified Powell's method. 
    598596 
     
    609607 
    610608    termination -- callable object providing termination conditions. 
     609    ExtraArgs -- extra arguments for cost. 
     610 
     611Further Inputs: 
     612 
    611613    sigint_callback -- callback function for signal handler. 
    612     ExtraArgs -- extra arguments for cost. 
    613  
    614 Further Inputs: 
    615  
    616614    callback -- an optional user-supplied function to call after each 
    617615        iteration.  It is called as callback(xk), where xk is the 
     
    622620""" 
    623621        super(PowellDirectionalSolver, self).Solve(cost, termination,\ 
    624                                   sigint_callback, ExtraArgs, **kwds) 
     622                                                  ExtraArgs, **kwds) 
    625623        return 
    626624 
  • mystic/mystic/termination.py

    r776 r784  
    349349    return _EvaluationLimits 
    350350 
     351def 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 
    351364# end of file 
Note: See TracChangeset for help on using the changeset viewer.