Changeset 771 for mystic


Ignore:
Timestamp:
11/19/14 10:05:33 (18 months ago)
Author:
mmckerns
Message:

save allSolvers and monitors in ensemble solver; lattice solver default nbins=8

Location:
mystic
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/buckshot_example06.py

    r725 r771  
    7575    random.seed(123) 
    7676    ndim = 9 
    77     npts = 6 
     77    npts = 8 
    7878 
    7979    # draw frame and exact coefficients 
  • mystic/examples/lattice_example06.py

    r722 r771  
    7575    random.seed(123) 
    7676    ndim = 9 
    77    #nbins = [1]*ndim 
    78     nbins = [2,1,2,1,2,1,1,1,1] 
     77    nbins = [2,1,2,1,2,1,2,1,1] 
    7978 
    8079    # draw frame and exact coefficients 
  • mystic/mystic/abstract_ensemble_solver.py

    r769 r771  
    129129        self._bestSolver      = None # 'best' solver (after Solve) 
    130130        self._total_evals     = 0 # total function calls (after Solve) 
     131        if kwds.has_key('nbins'): NP = reduce(lambda x,y:x*y, nbins) 
     132        else: NP = npts #XXX: merge npts/nbins kwds? 
     133        self._allSolvers      = [None for j in range(NP)] 
    131134        return 
    132135 
  • mystic/mystic/ensemble.py

    r769 r771  
    135135 
    136136        # run optimizer for each grid point 
    137         from copy import deepcopy as copy 
    138         op = [copy(solver) for i in range(len(initial_values))] 
     137        from copy import deepcopy as _copy 
     138        op = [_copy(solver) for i in range(len(initial_values))] 
    139139       #cf = [cost for i in range(len(initial_values))] 
    140140        vb = [verbose for i in range(len(initial_values))] 
     
    144144        # generate the local_optimize function 
    145145        def local_optimize(solver, x0, rank=None, disp=False, callback=None): 
     146            from copy import deepcopy as _copy 
     147            from mystic.tools import isNull 
    146148            solver.id = rank 
    147149            solver.SetInitialPoints(x0) 
     
    150152                                       max=solver._strictMax) # or lower,upper ? 
    151153            solver.Solve(cost, disp=disp, callback=callback) 
    152             return solver 
     154            sm = solver._stepmon 
     155            em = solver._evalmon 
     156            if isNull(sm): sm = ([],[],[],[]) 
     157            else: sm = (_copy(sm._x),_copy(sm._y),_copy(sm._id),_copy(sm._info)) 
     158            if isNull(em): em = ([],[],[],[]) 
     159            else: em = (_copy(em._x),_copy(em._y),_copy(em._id),_copy(em._info)) 
     160            return solver, sm, em 
    153161 
    154162        # map:: solver = local_optimize(solver, x0, id, verbose) 
     
    158166        # save initial state 
    159167        self._AbstractSolver__save_state() 
     168        #XXX: HACK TO GET CONTENT OF ALL MONITORS 
     169        # reconnect monitors; save all solvers 
     170        from mystic.monitors import Monitor 
     171        while results: #XXX: option to not save allSolvers? skip this and _copy 
     172            _solver, _stepmon, _evalmon = results.pop() 
     173            sm = Monitor() 
     174            sm._x,sm._y,sm._id,sm._info = _stepmon 
     175            _solver._stepmon.extend(sm) 
     176            del sm 
     177            em = Monitor() 
     178            em._x,em._y,em._id,em._info = _evalmon 
     179            _solver._evalmon.extend(em) 
     180            del em 
     181            self._allSolvers[len(results)] = _solver 
     182        del results, _solver, _stepmon, _evalmon 
     183        #XXX: END HACK 
     184 
    160185        # get the results with the lowest energy 
    161         self._bestSolver = results[0] 
     186        self._bestSolver = self._allSolvers[0] 
    162187        bestpath = self._bestSolver._stepmon 
    163188        besteval = self._bestSolver._evalmon 
    164189        self._total_evals = self._bestSolver.evaluations 
    165         for solver in results[1:]: 
     190        for solver in self._allSolvers[1:]: 
    166191            self._total_evals += solver.evaluations # add func evals 
    167192            if solver.bestEnergy < self._bestSolver.bestEnergy: 
     
    294319 
    295320        # run optimizer for each grid point 
    296         from copy import deepcopy as copy 
    297         op = [copy(solver) for i in range(len(initial_values))] 
     321        from copy import deepcopy as _copy 
     322        op = [_copy(solver) for i in range(len(initial_values))] 
    298323       #cf = [cost for i in range(len(initial_values))] 
    299324        vb = [verbose for i in range(len(initial_values))] 
     
    303328        # generate the local_optimize function 
    304329        def local_optimize(solver, x0, rank=None, disp=False, callback=None): 
     330            from copy import deepcopy as _copy 
     331            from mystic.tools import isNull 
    305332            solver.id = rank 
    306333            solver.SetInitialPoints(x0) 
     
    309336                                       max=solver._strictMax) # or lower,upper ? 
    310337            solver.Solve(cost, disp=disp, callback=callback) 
    311             return solver 
     338            sm = solver._stepmon 
     339            em = solver._evalmon 
     340            if isNull(sm): sm = ([],[],[],[]) 
     341            else: sm = (_copy(sm._x),_copy(sm._y),_copy(sm._id),_copy(sm._info)) 
     342            if isNull(em): em = ([],[],[],[]) 
     343            else: em = (_copy(em._x),_copy(em._y),_copy(em._id),_copy(em._info)) 
     344            return solver, sm, em 
    312345 
    313346        # map:: solver = local_optimize(solver, x0, id, verbose) 
     
    317350        # save initial state 
    318351        self._AbstractSolver__save_state() 
     352        #XXX: HACK TO GET CONTENT OF ALL MONITORS 
     353        # reconnect monitors; save all solvers 
     354        from mystic.monitors import Monitor 
     355        while results: #XXX: option to not store allSolvers? skip this & _copy 
     356            _solver, _stepmon, _evalmon = results.pop() 
     357            sm = Monitor() 
     358            sm._x,sm._y,sm._id,sm._info = _stepmon 
     359            _solver._stepmon.extend(sm) 
     360            del sm 
     361            em = Monitor() 
     362            em._x,em._y,em._id,em._info = _evalmon 
     363            _solver._evalmon.extend(em) 
     364            del em 
     365            self._allSolvers[len(results)] = _solver 
     366        del results, _solver, _stepmon, _evalmon 
     367        #XXX: END HACK 
     368 
    319369        # get the results with the lowest energy 
    320         self._bestSolver = results[0] 
     370        self._bestSolver = self._allSolvers[0] 
    321371        bestpath = self._bestSolver._stepmon 
    322372        besteval = self._bestSolver._evalmon 
    323373        self._total_evals = self._bestSolver.evaluations 
    324         for solver in results[1:]: 
     374        for solver in self._allSolvers[1:]: 
    325375            self._total_evals += solver.evaluations # add func evals 
    326376            if solver.bestEnergy < self._bestSolver.bestEnergy: 
     
    366416 
    367417 
    368 def lattice(cost,ndim,nbins=None,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
     418def lattice(cost,ndim,nbins=8,args=(),bounds=None,ftol=1e-4,maxiter=None, \ 
    369419            maxfun=None,full_output=0,disp=1,retall=0,callback=None,**kwds): 
    370420    """Minimize a function using the lattice ensemble solver. 
     
    381431    cost -- the Python function or method to be minimized. 
    382432    ndim -- dimensionality of the problem. 
    383     nbins -- tuple of number of bins in each dimension. [default = (2,)*ndim] 
     433    nbins -- tuple of number of bins in each dimension. [default = 8 bins total] 
    384434 
    385435Additional Inputs: 
     
    448498        termination = VTRChangeOverGeneration(ftol) 
    449499 
    450     if nbins is None: nbins = 2**ndim 
    451  
    452500    solver = LatticeSolver(ndim,nbins) 
    453501    solver.SetNestedSolver(_solver) #XXX: skip settings for configured solver? 
Note: See TracChangeset for help on using the changeset viewer.