Package mystic :: Module abstract_nested_solver

Source Code for Module mystic.abstract_nested_solver

  1  #!/usr/bin/env python 
  2  # 
  3  ## Abstract Nested Solver Class 
  4  # by mmckerns@caltech.edu 
  5   
  6  """ 
  7  This module contains the base class for launching several mystic solvers 
  8  instances -- utilizing a parallel "map" function to enable parallel 
  9  computing.  This module describes the nested solver interface.  As with 
 10  the AbstractSolver, the "Solve" method must be overwritte with the derived 
 11  solver's optimization algorithm. Similar to AbstractMapSolver, a call to 
 12  self.map is required.  In many cases, a minimal function call interface for a 
 13  derived solver is provided along with the derived class.  See the following 
 14  for an example. 
 15   
 16  The default map API settings are provided within mystic, while 
 17  distributed and high-performance computing mappers and launchers 
 18  can be obtained within the "pathos" package, found here:: 
 19      - http://dev.danse.us/trac/pathos 
 20   
 21   
 22  Usage 
 23  ===== 
 24   
 25  A typical call to a 'nested' solver will roughly follow this example: 
 26   
 27      >>> # the function to be minimized and the initial values 
 28      >>> from mystic.models import rosen 
 29      >>> lb = [0.0, 0.0, 0.0] 
 30      >>> ub = [2.0, 2.0, 2.0] 
 31      >>>  
 32      >>> # get monitors and termination condition objects 
 33      >>> from mystic.tools import Sow 
 34      >>> stepmon = Sow() 
 35      >>> from mystic.termination import CandidateRelativeTolerance as CRT 
 36      >>>  
 37      >>> # select the parallel launch configuration 
 38      >>> from pyina.launchers import mpirun_launcher 
 39      >>> from pyina.ez_map import ez_map2 
 40      >>> NNODES = 4 
 41      >>> nbins = [4,4,4] 
 42      >>> 
 43      >>> # instantiate and configure the solver 
 44      >>> from mystic.scipy_optimize import NelderMeadSimplexSolver 
 45      >>> from mystic.nested import BatchGridSolver 
 46      >>> solver = BatchGridSolver(len(nbins), nbins) 
 47      >>> solver.SetNestedSolver(NelderMeadSimplexSolver) 
 48      >>> solver.SetStrictRanges(lb, ub) 
 49      >>> solver.SetMapper(ez_map2) 
 50      >>> solver.SetLauncher(mpirun_launcher, NNODES) 
 51      >>> solver.Solve(rosen, CRT(), StepMonitor=stepmon) 
 52      >>>  
 53      >>> # obtain the solution 
 54      >>> solution = solver.Solution() 
 55   
 56   
 57  Handler 
 58  ======= 
 59   
 60  All solvers packaged with mystic include a signal handler that 
 61  provides the following options:: 
 62      sol: Print current best solution. 
 63      cont: Continue calculation. 
 64      call: Executes sigint_callback, if provided. 
 65      exit: Exits with current best solution. 
 66   
 67  Handlers are enabled with the 'enable_signal_handler' method, 
 68  and are configured through the solver's 'Solve' method.  Handlers 
 69  trigger when a signal interrupt (usually, Ctrl-C) is given while 
 70  the solver is running.  ***NOTE: The handler currently is disabled 
 71  when the solver has been launched in parallel.***  
 72   
 73  """ 
 74  __all__ = ['AbstractNestedSolver'] 
 75   
 76   
 77  from mystic.tools import Null 
 78  from mystic.abstract_map_solver import AbstractMapSolver 
 79   
 80   
81 -class AbstractNestedSolver(AbstractMapSolver):
82 """ 83 AbstractNestedSolver base class for mystic optimizers that are nested within 84 a parallel map. This allows pseudo-global coverage of parameter space using 85 non-global optimizers. 86 """ 87
88 - def __init__(self, dim, **kwds):
89 """ 90 Takes one initial input: 91 dim -- dimensionality of the problem. 92 93 Additional inputs: 94 npop -- size of the trial solution population. [default = 1] 95 nbins -- tuple of number of bins in each dimension. [default = [1]*dim] 96 npts -- number of solver instances. [default = 1] 97 98 Important class members: 99 nDim, nPop = dim, npop 100 generations - an iteration counter. 101 bestEnergy - current best energy. 102 bestSolution - current best parameter set. [size = dim] 103 popEnergy - set of all trial energy solutions. [size = npop] 104 population - set of all trial parameter solutions. [size = dim*npop] 105 energy_history - history of bestEnergy status. [equivalent to StepMonitor] 106 signal_handler - catches the interrupt signal. [***disabled***] 107 """ 108 super(AbstractNestedSolver, self).__init__(dim, **kwds) 109 #self.signal_handler = None 110 #self._handle_sigint = False 111 112 # default settings for nested optimization 113 nbins = [1]*dim 114 if kwds.has_key('nbins'): nbins = kwds['nbins'] 115 self._nbins = nbins 116 npts = 1 117 if kwds.has_key('npts'): npts = kwds['npts'] 118 self._npts = npts 119 from mystic.scipy_optimize import NelderMeadSimplexSolver 120 self._solver = NelderMeadSimplexSolver 121 return
122
123 - def SetNestedSolver(self, solver):
124 """set the nested solver 125 126 input:: 127 - solver: a mystic solver class (e.g. NelderMeadSimplexSolver)""" 128 self._solver = solver 129 return
130
131 - def SetInitialPoints(self, x0, radius=0.05):
132 """Set Initial Points with Guess (x0) 133 134 input:: 135 - x0: must be a sequence of length self.nDim 136 - radius: generate random points within [-radius*x0, radius*x0] 137 for i!=0 when a simplex-type initial guess in required 138 139 *** this method must be overwritten ***""" 140 raise NotImplementedError, "must be overwritten..."
141
142 - def SetRandomInitialPoints(self, min=None, max=None):
143 """Generate Random Initial Points within given Bounds 144 145 input:: 146 - min, max: must be a sequence of length self.nDim 147 - each min[i] should be <= the corresponding max[i] 148 149 *** this method must be overwritten ***""" 150 raise NotImplementedError, "must be overwritten..."
151
152 - def SetMultinormalInitialPoints(self, mean, var = None):
153 """Generate Initial Points from Multivariate Normal. 154 155 input:: 156 - mean must be a sequence of length self.nDim 157 - var can be... 158 None: -> it becomes the identity 159 scalar: -> var becomes scalar * I 160 matrix: -> the variance matrix. must be the right size! 161 162 *** this method must be overwritten ***""" 163 raise NotImplementedError, "must be overwritten..."
164 165 166 if __name__=='__main__': 167 help(__name__) 168 169 # end of file 170