Changeset 748


Ignore:
Timestamp:
08/12/14 11:13:17 (22 months ago)
Author:
mmckerns
Message:

add solver one-liners for ensemble solvers; fix: reducer in ensemble solvers

Location:
mystic/mystic
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/abstract_ensemble_solver.py

    r727 r748  
    166166        solver.SetConstraints(self._constraints) 
    167167        solver.SetPenalty(self._penalty) 
    168         if self._reducer[0]: #XXX: always, settable, or sync'd ? 
    169             solver.SetReducer(*self._reducer) 
     168        if self._reducer: #XXX: always, settable, or sync'd ? 
     169            solver.SetReducer(self._reducer, arraylike=True) 
    170170        return solver 
    171171 
     
    243243            print "         Iterations: %d" % solver.generations 
    244244            print "         Function evaluations: %d" % solver._fcalls[0] 
    245             print "         Total Function evaluations: %d" % self._total_evals 
     245            print "         Total function evaluations: %d" % self._total_evals 
    246246 
    247247        if info: 
  • mystic/mystic/differential_evolution.py

    r737 r748  
    284284Description: 
    285285 
    286     Uses a differential evolution algorith to find the minimum of 
     286    Uses a differential evolution algorithm to find the minimum of 
    287287    a function of one or more variables. 
    288288 
     
    439439Description: 
    440440 
    441     Uses a differential evolution algorith to find the minimum of 
     441    Uses a differential evolution algorithm to find the minimum of 
    442442    a function of one or more variables. This implementation holds 
    443443    the current generation invariant until the end of each iteration. 
     
    478478Description: 
    479479 
    480     Uses Storn & Prices's differential evolution algorith to find the minimum 
     480    Uses Storn & Prices's differential evolution algorithm to find the minimum 
    481481    of a function of one or more variables. Mimics a scipy.optimize style 
    482482    interface. 
     
    506506        iteration.  It is called as callback(xk), where xk is the 
    507507        current parameter vector. 
    508     handler -- boolean - enable/disable handling of interrupt signal 
    509     strategy -- strategy - override the default mutation strategy 
    510     itermon -- monitor - override the default GenerationMonitor 
    511     evalmon -- monitor - override the default EvaluationMonitor 
     508    handler -- boolean - enable/disable handling of interrupt signal. 
     509    strategy -- strategy - override the default mutation strategy. 
     510    itermon -- monitor - override the default GenerationMonitor. 
     511    evalmon -- monitor - override the default EvaluationMonitor. 
    512512    constraints -- an optional user-supplied function.  It is called as 
    513513        constraints(xk), where xk is the current parameter vector. 
     
    548548Description: 
    549549 
    550     Uses a differential evolution algorith to find the minimum of 
     550    Uses a differential evolution algorithm to find the minimum of 
    551551    a function of one or more variables. Mimics a scipy.optimize style 
    552552    interface. 
     
    576576        iteration.  It is called as callback(xk), where xk is the 
    577577        current parameter vector. 
    578     handler -- boolean - enable/disable handling of interrupt signal 
    579     strategy -- strategy - override the default mutation strategy 
    580     itermon -- monitor - override the default GenerationMonitor 
    581     evalmon -- monitor - override the default EvaluationMonitor 
     578    handler -- boolean - enable/disable handling of interrupt signal. 
     579    strategy -- strategy - override the default mutation strategy. 
     580    itermon -- monitor - override the default GenerationMonitor. 
     581    evalmon -- monitor - override the default EvaluationMonitor. 
    582582    constraints -- an optional user-supplied function.  It is called as 
    583583        constraints(xk), where xk is the current parameter vector. 
  • mystic/mystic/ensemble.py

    r715 r748  
    361361        return  
    362362 
     363 
     364def lattice(cost,ndim,nbins=2,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
     365            maxfun=None,full_output=0,disp=1,retall=0,callback=None,**kwds): 
     366    """Minimize a function using the lattice ensemble solver. 
     367     
     368Description: 
     369 
     370    Uses a lattice ensemble algorithm to find the minimum of 
     371    a function of one or more variables. Mimics the scipy.optimize.fmin 
     372    interface. Starts N solver instances at regular intervals in parameter 
     373    space, determined by 'nbins' (N = numpy.prod(nbins); len(nbins) == ndim).  
     374 
     375Inputs: 
     376 
     377    cost -- the Python function or method to be minimized. 
     378    ndim -- dimensionality of the problem. 
     379    nbins -- tuple of number of bins in each dimension. 
     380 
     381Additional Inputs: 
     382 
     383    args -- extra arguments for cost. 
     384    bounds -- list - n pairs of bounds (min,max), one pair for each parameter. 
     385    ftol -- number - acceptable relative error in cost(xopt) for convergence. 
     386    gtol -- number - maximum number of iterations to run without improvement. 
     387    maxiter -- number - the maximum number of iterations to perform. 
     388    maxfun -- number - the maximum number of function evaluations. 
     389    full_output -- number - non-zero if fval and warnflag outputs are desired. 
     390    disp -- number - non-zero to print convergence messages. 
     391    retall -- number - non-zero to return list of solutions at each iteration. 
     392    callback -- an optional user-supplied function to call after each 
     393        iteration.  It is called as callback(xk), where xk is the 
     394        current parameter vector. 
     395    solver -- solver - override the default nested Solver instance. 
     396    handler -- boolean - enable/disable handling of interrupt signal. 
     397    itermon -- monitor - override the default GenerationMonitor. 
     398    evalmon -- monitor - override the default EvaluationMonitor. 
     399    constraints -- an optional user-supplied function.  It is called as 
     400        constraints(xk), where xk is the current parameter vector. 
     401        This function must return xk', a parameter vector that satisfies 
     402        the encoded constraints. 
     403    penalty -- an optional user-supplied function.  It is called as 
     404        penalty(xk), where xk is the current parameter vector. 
     405        This function should return y', with y' == 0 when the encoded 
     406        constraints are satisfied, and y' > 0 otherwise. 
     407 
     408Returns: (xopt, {fopt, iter, funcalls, warnflag, allfuncalls}, {allvecs}) 
     409 
     410    xopt -- ndarray - minimizer of function 
     411    fopt -- number - value of function at minimum: fopt = cost(xopt) 
     412    iter -- number - number of iterations 
     413    funcalls -- number - number of function calls 
     414    warnflag -- number - Integer warning flag: 
     415        1 : 'Maximum number of function evaluations.' 
     416        2 : 'Maximum number of iterations.' 
     417    allfuncalls -- number - total function calls (for all solver instances) 
     418    allvecs -- list - a list of solutions at each iteration 
     419 
     420    """ 
     421    handler = False 
     422    if kwds.has_key('handler'): 
     423        handler = kwds['handler'] 
     424    from mystic.solvers import NelderMeadSimplexSolver as _solver 
     425    if kwds.has_key('solver'): 
     426        _solver = kwds['solver'] 
     427 
     428    from mystic.monitors import Monitor 
     429    stepmon = Monitor() 
     430    evalmon = Monitor() 
     431    if kwds.has_key('itermon'): 
     432        stepmon = kwds['itermon'] 
     433    if kwds.has_key('evalmon'): 
     434        evalmon = kwds['evalmon'] 
     435 
     436    gtol = 10 # termination generations (scipy: 2, default: 10) 
     437    if kwds.has_key('gtol'): 
     438        gtol = kwds['gtol'] 
     439    if gtol: #if number of generations is provided, use NCOG 
     440        from mystic.termination import NormalizedChangeOverGeneration 
     441        termination = NormalizedChangeOverGeneration(ftol,gtol) 
     442    else: 
     443        from mystic.termination import VTRChangeOverGeneration 
     444        termination = VTRChangeOverGeneration(ftol) 
     445 
     446    if isinstance(nbins, int): nbins = [nbins]*ndim 
     447 
     448    solver = LatticeSolver(ndim,nbins) 
     449    solver.SetNestedSolver(_solver) #XXX: skip settings for configured solver? 
     450    solver.SetEvaluationLimits(maxiter,maxfun) 
     451    solver.SetEvaluationMonitor(evalmon) 
     452    solver.SetGenerationMonitor(stepmon) 
     453    if kwds.has_key('penalty'): 
     454        penalty = kwds['penalty'] 
     455        solver.SetPenalty(penalty) 
     456    if kwds.has_key('constraints'): 
     457        constraints = kwds['constraints'] 
     458        solver.SetConstraints(constraints) 
     459    if bounds is not None: 
     460        minb,maxb = unpair(bounds) 
     461        solver.SetStrictRanges(minb,maxb) 
     462 
     463    if handler: solver.enable_signal_handler() 
     464    solver.Solve(cost,termination=termination,disp=disp, \ 
     465                 ExtraArgs=args,callback=callback) 
     466    solution = solver.Solution() 
     467 
     468    # code below here pushes output to scipy.optimize.fmin interface 
     469    msg = solver.CheckTermination(disp=disp, info=True) 
     470 
     471    x = solver.bestSolution 
     472    fval = solver.bestEnergy 
     473    warnflag = 0 
     474    fcalls = solver.evaluations 
     475    all_fcalls = solver._total_evals 
     476    iterations = max(0,len(solver._stepmon.y)-1) #FIXME: solver.generations 
     477    allvecs = solver._stepmon.x 
     478 
     479    if fcalls >= solver._maxfun: #XXX: check against total or individual? 
     480        warnflag = 1 
     481    elif iterations >= solver._maxiter: #XXX: check against total or individual? 
     482        warnflag = 2 
     483    else: pass 
     484 
     485    if full_output: 
     486        retlist = x, fval, iterations, fcalls, warnflag, all_fcalls 
     487        if retall: 
     488            retlist += (allvecs,) 
     489    else: 
     490        retlist = x 
     491        if retall: 
     492            retlist = (x, allvecs) 
     493 
     494    return retlist 
     495 
     496 
     497def buckshot(cost,ndim,npts=8,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
     498             maxfun=None,full_output=0,disp=1,retall=0,callback=None,**kwds): 
     499    """Minimize a function using the buckshot ensemble solver. 
     500     
     501Description: 
     502 
     503    Uses a buckshot ensemble algorithm to find the minimum of 
     504    a function of one or more variables. Mimics the scipy.optimize.fmin 
     505    interface. Starts 'npts' solver instances at random points in parameter 
     506    space.  
     507 
     508Inputs: 
     509 
     510    cost -- the Python function or method to be minimized. 
     511    ndim -- dimensionality of the problem. 
     512    npts -- number of solver instances. 
     513 
     514Additional Inputs: 
     515 
     516    args -- extra arguments for cost. 
     517    bounds -- list - n pairs of bounds (min,max), one pair for each parameter. 
     518    ftol -- number - acceptable relative error in cost(xopt) for convergence. 
     519    gtol -- number - maximum number of iterations to run without improvement. 
     520    maxiter -- number - the maximum number of iterations to perform. 
     521    maxfun -- number - the maximum number of function evaluations. 
     522    full_output -- number - non-zero if fval and warnflag outputs are desired. 
     523    disp -- number - non-zero to print convergence messages. 
     524    retall -- number - non-zero to return list of solutions at each iteration. 
     525    callback -- an optional user-supplied function to call after each 
     526        iteration.  It is called as callback(xk), where xk is the 
     527        current parameter vector. 
     528    solver -- solver - override the default nested Solver instance. 
     529    handler -- boolean - enable/disable handling of interrupt signal. 
     530    itermon -- monitor - override the default GenerationMonitor. 
     531    evalmon -- monitor - override the default EvaluationMonitor. 
     532    constraints -- an optional user-supplied function.  It is called as 
     533        constraints(xk), where xk is the current parameter vector. 
     534        This function must return xk', a parameter vector that satisfies 
     535        the encoded constraints. 
     536    penalty -- an optional user-supplied function.  It is called as 
     537        penalty(xk), where xk is the current parameter vector. 
     538        This function should return y', with y' == 0 when the encoded 
     539        constraints are satisfied, and y' > 0 otherwise. 
     540 
     541Returns: (xopt, {fopt, iter, funcalls, warnflag, allfuncalls}, {allvecs}) 
     542 
     543    xopt -- ndarray - minimizer of function 
     544    fopt -- number - value of function at minimum: fopt = cost(xopt) 
     545    iter -- number - number of iterations 
     546    funcalls -- number - number of function calls 
     547    warnflag -- number - Integer warning flag: 
     548        1 : 'Maximum number of function evaluations.' 
     549        2 : 'Maximum number of iterations.' 
     550    allfuncalls -- number - total function calls (for all solver instances) 
     551    allvecs -- list - a list of solutions at each iteration 
     552 
     553    """ 
     554    handler = False 
     555    if kwds.has_key('handler'): 
     556        handler = kwds['handler'] 
     557    from mystic.solvers import NelderMeadSimplexSolver as _solver 
     558    if kwds.has_key('solver'): 
     559        _solver = kwds['solver'] 
     560 
     561    from mystic.monitors import Monitor 
     562    stepmon = Monitor() 
     563    evalmon = Monitor() 
     564    if kwds.has_key('itermon'): 
     565        stepmon = kwds['itermon'] 
     566    if kwds.has_key('evalmon'): 
     567        evalmon = kwds['evalmon'] 
     568 
     569    gtol = 10 # termination generations (scipy: 2, default: 10) 
     570    if kwds.has_key('gtol'): 
     571        gtol = kwds['gtol'] 
     572    if gtol: #if number of generations is provided, use NCOG 
     573        from mystic.termination import NormalizedChangeOverGeneration 
     574        termination = NormalizedChangeOverGeneration(ftol,gtol) 
     575    else: 
     576        from mystic.termination import VTRChangeOverGeneration 
     577        termination = VTRChangeOverGeneration(ftol) 
     578 
     579    solver = BuckshotSolver(ndim,npts) 
     580    solver.SetNestedSolver(_solver) #XXX: skip settings for configured solver? 
     581    solver.SetEvaluationLimits(maxiter,maxfun) 
     582    solver.SetEvaluationMonitor(evalmon) 
     583    solver.SetGenerationMonitor(stepmon) 
     584    if kwds.has_key('penalty'): 
     585        penalty = kwds['penalty'] 
     586        solver.SetPenalty(penalty) 
     587    if kwds.has_key('constraints'): 
     588        constraints = kwds['constraints'] 
     589        solver.SetConstraints(constraints) 
     590    if bounds is not None: 
     591        minb,maxb = unpair(bounds) 
     592        solver.SetStrictRanges(minb,maxb) 
     593 
     594    if handler: solver.enable_signal_handler() 
     595    solver.Solve(cost,termination=termination,disp=disp, \ 
     596                 ExtraArgs=args,callback=callback) 
     597    solution = solver.Solution() 
     598 
     599    # code below here pushes output to scipy.optimize.fmin interface 
     600    msg = solver.CheckTermination(disp=disp, info=True) 
     601 
     602    x = solver.bestSolution 
     603    fval = solver.bestEnergy 
     604    warnflag = 0 
     605    fcalls = solver.evaluations 
     606    all_fcalls = solver._total_evals 
     607    iterations = max(0,len(solver._stepmon.y)-1) #FIXME: solver.generations 
     608    allvecs = solver._stepmon.x 
     609 
     610    if fcalls >= solver._maxfun: #XXX: check against total or individual? 
     611        warnflag = 1 
     612    elif iterations >= solver._maxiter: #XXX: check against total or individual? 
     613        warnflag = 2 
     614    else: pass 
     615 
     616    if full_output: 
     617        retlist = x, fval, iterations, fcalls, warnflag, all_fcalls 
     618        if retall: 
     619            retlist += (allvecs,) 
     620    else: 
     621        retlist = x 
     622        if retall: 
     623            retlist = (x, allvecs) 
     624 
     625    return retlist 
     626 
     627 
     628 
    363629# backward compatibility 
    364630ScattershotSolver = BuckshotSolver 
  • mystic/mystic/scipy_optimize.py

    r724 r748  
    326326        iteration.  It is called as callback(xk), where xk is the 
    327327        current parameter vector. 
    328     handler -- boolean - enable/disable handling of interrupt signal 
    329     itermon -- monitor - override the default GenerationMonitor 
    330     evalmon -- monitor - override the default EvaluationMonitor 
     328    handler -- boolean - enable/disable handling of interrupt signal. 
     329    itermon -- monitor - override the default GenerationMonitor. 
     330    evalmon -- monitor - override the default EvaluationMonitor. 
    331331    constraints -- an optional user-supplied function.  It is called as 
    332332        constraints(xk), where xk is the current parameter vector. 
     
    645645        iteration.  It is called as callback(xk), where xk is the 
    646646        current parameter vector. 
    647     direc -- initial direction set 
    648     handler -- boolean - enable/disable handling of interrupt signal 
    649     itermon -- monitor - override the default GenerationMonitor 
    650     evalmon -- monitor - override the default EvaluationMonitor 
     647    direc -- initial direction set. 
     648    handler -- boolean - enable/disable handling of interrupt signal. 
     649    itermon -- monitor - override the default GenerationMonitor. 
     650    evalmon -- monitor - override the default EvaluationMonitor. 
    651651    constraints -- an optional user-supplied function.  It is called as 
    652652        constraints(xk), where xk is the current parameter vector. 
  • mystic/mystic/solvers.py

    r715 r748  
    3838    diffev      -- DifferentialEvolutionSolver 
    3939    diffev2     -- DifferentialEvolutionSolver2 
     40    == Pseudo-Global Optimizers == 
     41    buckshot    -- BuckshotSolver 
     42    lattice     -- LatticeSolver 
    4043    == Local-Search Optimizers == 
    4144    fmin        -- NelderMeadSimplexSolver 
     
    6669from ensemble import BuckshotSolver 
    6770from ensemble import LatticeSolver 
     71from ensemble import buckshot, lattice 
    6872 
    6973# local-search optimizers 
Note: See TracChangeset for help on using the changeset viewer.