Changeset 81


Ignore:
Timestamp:
01/23/09 13:03:25 (7 years ago)
Author:
mmckerns
Message:

make nelder_mead's SimplexMonitor? fit StepMonitor? interface

Files:
8 edited

Legend:

Unmodified
Added
Removed
  • python/nelder_mead.py

    r67 r81  
    1212                     Takes two additional input args 
    1313                        -- EvaluationMonitor 
    14                         -- SimplexMonitor 
     14                        -- StepMonitor 
    1515                      
    1616""" 
     
    3030 
    3131def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None, 
    32          full_output=0, disp=1, retall=0, EvaluationMonitor=Null, SimplexMonitor=Null): 
     32         full_output=0, disp=1, retall=0, EvaluationMonitor=Null, StepMonitor=Null): 
    3333    """Minimize a function using the downhill simplex algorithm. 
    3434 
     
    7070      EvaluationMonitor -- a callable object that will be passed x, fval 
    7171           whenever the cost function is evaluated. 
     72      StepMonitor -- a callable object that will be passed x, fval 
     73           after the end of a simplex iteration. 
    7274 
    7375      """ 
     
    119121 
    120122    while (fcalls[0] < maxfun and iterations < maxiter): 
    121         SimplexMonitor(sim, fsim) 
     123        StepMonitor(sim, fsim) # get all values; "best" is sim[0] 
    122124        if (max(numpy.ravel(abs(sim[1:]-sim[0]))) <= xtol \ 
    123125            and max(abs(fsim[0]-fsim[1:])) <= ftol): 
     
    219221    print "===================" 
    220222    start = time.time() 
    221     x = fmin(rosen,x0) 
     223    from tools import VerboseSow 
     224    stepmon = VerboseSow(10) 
     225    x = fmin(rosen,x0,StepMonitor=stepmon) 
    222226    print x 
    223227    times.append(time.time() - start) 
     
    225229 
    226230    for k in range(len(algor)): 
    227         print algor[k], "\t -- ", times[k] 
     231        print algor[k], "\t -- took", times[k] 
    228232 
    229233if __name__ == "__main__": 
  • python/tools.py

    r73 r81  
    148148        return 
    149149    def __call__(self, x, y): 
     150        from numpy import ndarray 
    150151        Sow.__call__(self, x, y) 
    151152        self._step += 1 
     153        if isinstance(y,(list,ndarray)): 
     154            y = y[0] #XXX: get the "best" fit... which should be in y[0] 
     155        if isinstance(x[0],(list,ndarray)): #XXX: x should always be iterable 
     156            x = x[0] #XXX: get the "best" fit... which should be in x[0] 
    152157        if int(self._step % self._yinterval) == 0: 
     158           #print "Generation %d has best Chi-Squared: %s" % (self._step, y) 
    153159            print "Generation %d has best Chi-Squared: %f" % (self._step, y) 
    154160        if int(self._step % self._xinterval) == 0: 
    155             print "Generation %d has bet fit parameters: %r" % (self._step, x) 
     161            print "Generation %d has bet fit parameters: %s" % (self._step, x) 
    156162        return 
    157163    pass 
  • tests/sam_corana2.py

    r62 r81  
    4242def run_once(): 
    4343    simplex = Sow() 
    44     sol = fmin(Corana2, [random.uniform(0,2), random.uniform(0,2)], retall = True, SimplexMonitor = simplex) 
     44    sol = fmin(Corana2, [random.uniform(0,2), random.uniform(0,2)], retall = True, StepMonitor = simplex) 
    4545     
    4646    for x in simplex.x: 
  • tests/sam_mogi.py

    r62 r81  
    44See test_mogi.py 
    55 
    6 This uses Nelder-Mead and SimplexMonitor 
     6This uses Nelder-Mead and StepMonitor 
    77""" 
    88 
     
    6161    z1 = z0*random.uniform(0.5,1.5) 
    6262    v1 = v0*random.uniform(0.5,1.5) 
    63     sol = fmin(cost_function, [random.uniform(x0-40,x0+40), random.uniform(y0-40,y0+40), z1, v1], retall = True, SimplexMonitor = simplex) 
     63    sol = fmin(cost_function, [random.uniform(x0-40,x0+40), random.uniform(y0-40,y0+40), z1, v1], retall = True, StepMonitor = simplex) 
    6464    print sol[0] 
    6565     
     
    7474    y1 = y0*random.uniform(0.5,1.5) 
    7575    z1 = z0*random.uniform(0.5,1.5) 
    76     sol = fmin(cost_function, [random.uniform(x0-40,x0+40), y1, z1, random.uniform(v0-0.1,v0+0.1)], retall = True, SimplexMonitor = simplex) 
     76    sol = fmin(cost_function, [random.uniform(x0-40,x0+40), y1, z1, random.uniform(v0-0.1,v0+0.1)], retall = True, StepMonitor = simplex) 
    7777    print sol[0] 
    7878     
  • tests/sam_rosenbrock.py

    r62 r81  
    66This one uses Nelder-Mead plus matlab viz. 
    77 
    8 It uses the SimplexMonitor option to track all simplices generated during  
     8It uses the StepMonitor option to track all simplices generated during  
    99the search. 
    1010""" 
     
    4040def run_once(x0,x1): 
    4141    simplex = Sow() 
    42     sol = fmin(rosen, [x0, x1], retall = True, SimplexMonitor = simplex) 
     42    sol = fmin(rosen, [x0, x1], retall = True, StepMonitor = simplex) 
    4343     
    4444    for x in simplex.x: 
  • tests/sam_zimmermann.py

    r9 r81  
    66This one uses Nelder-Mead plus matlab viz. 
    77 
    8 It uses the SimplexMonitor option to track all simplices generated during  
     8It uses the StepMonitor option to track all simplices generated during  
    99the search. 
    1010""" 
     
    3939def run_once(): 
    4040    simplex = Sow() 
    41     sol = fmin(CostFunction, [random.uniform(0,7), random.uniform(0,7)], retall = True, SimplexMonitor = simplex) 
     41    sol = fmin(CostFunction, [random.uniform(0,7), random.uniform(0,7)], retall = True, StepMonitor = simplex) 
    4242     
    4343    for x in simplex.x: 
  • tests/test_mogi.py

    r80 r81  
    125125    # 
    126126    simplex, esow = Sow(), Sow() 
    127     sol = fmin(cost_function, point,  EvaluationMonitor = esow, retall = True, SimplexMonitor = simplex) 
     127    sol = fmin(cost_function, point,  EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 
    128128    print "simplex solution: ", sol[1][-1] 
    129129    # 
  • tests/test_zimmermann.py

    r5 r81  
    6767    simplex = Sow() 
    6868    esow = Sow() 
    69     sol = fmin(CostFunction, [random.uniform(0,5) for j in range(ND)], EvaluationMonitor = esow, retall = True, SimplexMonitor = simplex) 
     69    sol = fmin(CostFunction, [random.uniform(0,5) for j in range(ND)], EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 
    7070    #print "solution: ", sol 
    7171 
Note: See TracChangeset for help on using the changeset viewer.