Changeset 851


Ignore:
Timestamp:
12/29/15 18:57:21 (5 months ago)
Author:
mmckerns
Message:

constraints.solve now can use ensemble solvers

Location:
mystic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/mystic/constraints.py

    r823 r851  
    304304    and less accurate than writing the constraints solver from scratch. 
    305305 
     306NOTE: The ensemble solvers are available, using the default NestedSolver, 
     307    where the keyword 'guess' can be used to set the number of solvers. 
     308 
    306309NOTE: The default solver is 'diffev', with npop=min(40, ndim*5). The default 
    307310    termination is ChangeOverGeneration(), and the default guess is randomly 
    308311    selected points between the upper and lower bounds. 
    309312    """ 
     313    npts = 8 
     314    if type(guess) is int: npts, guess = guess, None 
     315 
    310316    ndim = 1 #XXX: better, increase in while loop catching IndexError ? 
    311317    if nvars is not None: ndim = nvars 
     
    316322    def cost(x): return 1. 
    317323 
    318     #XXX: don't allow solver string as a short-cut? 
     324    #XXX: don't allow solver string as a short-cut? #FIXME: add ensemble solvers 
     325    ensemble = False 
    319326    if solver is None or solver == 'diffev': 
    320327        from mystic.solvers import DifferentialEvolutionSolver as TheSolver 
     
    329336        from mystic.solvers import NelderMeadSimplexSolver as TheSolver 
    330337        solver = TheSolver(ndim) 
     338    elif solver == 'buckshot': 
     339        from mystic.solvers import BuckshotSolver as TheSolver 
     340        solver = TheSolver(ndim, max(8, npts)) #XXX: needs better default? 
     341        ensemble = True 
     342    elif solver == 'lattice': 
     343        from mystic.solvers import LatticeSolver as TheSolver 
     344        solver = TheSolver(ndim, max(8, npts)) #XXX: needs better default? 
     345        ensemble = True 
    331346     
    332347    if termination is None: 
    333348        from mystic.termination import ChangeOverGeneration as COG 
    334349        termination = COG() 
    335     if guess is not None: 
    336         solver.SetInitialPoints(guess) #XXX: nice if 'diffev' also had methods 
    337     else: 
    338         solver.SetRandomInitialPoints(lower_bounds, upper_bounds) 
     350    if not ensemble: 
     351        if guess is not None: 
     352            solver.SetInitialPoints(guess) #XXX: nice if 'diffev' had methods 
     353        else: 
     354            solver.SetRandomInitialPoints(lower_bounds, upper_bounds) 
    339355    if lower_bounds or upper_bounds: 
    340356        solver.SetStrictRanges(lower_bounds, upper_bounds) 
  • mystic/tests/test_constraints.py

    r776 r851  
    5555 
    5656  x = solve(penalty, guess=[2,3,1]) 
     57 #x = solve(penalty, solver='buckshot') 
    5758 
    5859  assert round(mean_constraint(x, 5.0)) == 0.0 
     
    7071 
    7172  x = solve(constraint, guess=[2,3,1]) 
     73 #x = solve(constraint, solver='buckshot') 
    7274 
    7375  assert almostEqual(mean(x), 1.0, tol=1e-15) 
Note: See TracChangeset for help on using the changeset viewer.