Changeset 82


Ignore:
Timestamp:
01/26/09 16:12:08 (7 years ago)
Author:
mmckerns
Message:

scipy.optimize.fmin fit to mystic.Solve interface;
mystic handlers, etc added to fmin solver;
VerboseSow? edited to handle simplex-type cloud of results per iteration

Files:
2 added
1 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • python/Make.mm

    r67 r82  
    3131    const.py \ 
    3232    differential_evolution.py \ 
    33     nelder_mead.py \ 
     33    scipy_optimize_fmin.py \ 
     34    nmtools.py \ 
    3435    detools.py \ 
    3536    tools.py \ 
  • python/tools.py

    r81 r82  
    110110class Sow(object): 
    111111    """ 
    112 # Instances of objects can be passed as parameters to fmin: 
     112# Instances of objects can be passed as parameters to solver.Solve(): 
    113113 
    114114sow = Sow() 
    115 fmin(rosen, x0, EvaulationMonitor=sow) 
     115solver.Solve(rosen, x0, EvaulationMonitor=sow) 
    116116 
    117117# Then parameters used to call the CostFunction can be retrieved via 
  • tests/sam_corana2.py

    r81 r82  
    1111import sam 
    1212from test_corana import * 
    13 from mystic.nelder_mead import fmin, Sow 
    14 from mystic import getch 
     13from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     14from mystic.nmtools import IterationRelativeError as IRE 
     15from mystic import getch, Sow 
    1516 
    1617def Corana2(x): 
     
    4243def run_once(): 
    4344    simplex = Sow() 
    44     sol = fmin(Corana2, [random.uniform(0,2), random.uniform(0,2)], retall = True, StepMonitor = simplex) 
     45    solver = fmin(2) 
     46    solver.SetRandomInitialPoints([0,0],[2,2]) 
     47    killme = IRE(1e-4,1e-4) 
     48    solver.Solve(Corana2, termination=killme, StepMonitor = simplex) 
     49    sol = solver.Solution() 
    4550     
    4651    for x in simplex.x: 
  • tests/sam_mogi.py

    r81 r82  
    99import sam 
    1010from test_mogi import * 
    11 from mystic.nelder_mead import fmin 
     11from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     12from mystic.nmtools import IterationRelativeError as IRE 
    1213from mystic import getch, Sow 
    1314 
     
    6162    z1 = z0*random.uniform(0.5,1.5) 
    6263    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, StepMonitor = simplex) 
    64     print sol[0] 
     64    xinit = [random.uniform(x0-40,x0+40), random.uniform(y0-40,y0+40), z1, v1] 
     65 
     66    solver = fmin(len(xinit)) 
     67    solver.SetInitialPoints(xinit) 
     68    killme = IRE(1e-4,1e-4) 
     69    solver.Solve(cost_function, termination=killme, StepMonitor = simplex) 
     70    sol = solver.Solution() 
     71    print sol 
    6572     
    6673    for x in simplex.x: 
    6774        sam.putarray('x',x) 
    6875        sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],4),'w-','LineWidth',2)") 
    69     return sol[0] 
     76    return sol 
    7077 
    7178 
     
    7481    y1 = y0*random.uniform(0.5,1.5) 
    7582    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, StepMonitor = simplex) 
    77     print sol[0] 
    78      
     83    xinit = [random.uniform(x0-40,x0+40), y1, z1, random.uniform(v0-0.1,v0+0.1)] 
     84 
     85    solver = fmin(len(xinit)) 
     86    solver.SetInitialPoints(xinit) 
     87    killme = IRE(1e-4,1e-4) 
     88    solver.Solve(cost_function, termination=killme, StepMonitor = simplex) 
     89    sol = solver.Solution() 
     90    print sol 
     91 
    7992    for x in simplex.x: 
    8093        sam.putarray('x',x) 
    8194        sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'w-','LineWidth',2)") 
    82     return sol[0] 
     95    return sol 
    8396 
    8497draw_contour_xv() 
  • tests/sam_rosenbrock.py

    r81 r82  
    1212import sam 
    1313from test_rosenbrock import * 
    14 from mystic.nelder_mead import fmin, Sow 
    15 from mystic import getch 
     14from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     15from mystic.nmtools import IterationRelativeError as IRE 
     16from mystic import getch, Sow 
    1617 
    1718def draw_contour(): 
     
    4041def run_once(x0,x1): 
    4142    simplex = Sow() 
    42     sol = fmin(rosen, [x0, x1], retall = True, StepMonitor = simplex) 
     43    xinit = [x0, x1] 
     44 
     45    solver = fmin(len(xinit)) 
     46    solver.SetInitialPoints(xinit) 
     47    killme = IRE(1e-4,1e-4) 
     48    solver.Solve(rosen, termination=killme, StepMonitor = simplex) 
     49    sol = solver.Solution() 
    4350     
    4451    for x in simplex.x: 
  • tests/sam_zimmermann.py

    r81 r82  
    1212import sam 
    1313from test_zimmermann import * 
    14 from mystic.nelder_mead import fmin 
     14from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     15from mystic.nmtools import IterationRelativeError as IRE 
    1516from mystic import getch, Sow 
    1617 
     
    3940def run_once(): 
    4041    simplex = Sow() 
    41     sol = fmin(CostFunction, [random.uniform(0,7), random.uniform(0,7)], retall = True, StepMonitor = simplex) 
    42      
     42    solver = fmin(2) 
     43    solver.SetRandomInitialPoints([0,0],[7,7]) 
     44    killme = IRE(1e-4,1e-4) 
     45    solver.Solve(CostFunction, termination=killme, StepMonitor = simplex) 
     46    sol = solver.Solution() 
     47 
    4348    for x in simplex.x: 
    4449        sam.putarray('x',x) 
  • tests/test_dejong3.py

    r62 r82  
    6363    print "CPU Time: %s" % timetaken 
    6464 
    65     from mystic.nelder_mead import fmin  
     65    from mystic.scipy_optimize_fmin import fmin 
    6666 
    6767    print fmin(DeJong3, [0 for i in range(5)]) 
  • tests/test_mogi.py

    r81 r82  
    110110if __name__ == '__main__': 
    111111 
    112     from mystic.nelder_mead import fmin 
     112    from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     113    from mystic.nmtools import IterationRelativeError as IRE 
    113114    from scipy.optimize import leastsq, fmin_cg 
    114115    # 
     
    125126    # 
    126127    simplex, esow = Sow(), Sow() 
    127     sol = fmin(cost_function, point,  EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 
    128     print "simplex solution: ", sol[1][-1] 
     128    solver = fmin(len(point)) 
     129    solver.SetInitialPoints(point) 
     130    killme = IRE(1e-4,1e-4) 
     131    solver.Solve(cost_function, killme, EvaluationMonitor = esow, StepMonitor = simplex) 
     132    sol = solver.Solution() 
     133 
     134    print "simplex solution: ", sol 
    129135    # 
    130136    solcg = fmin_cg(cost_function, point) 
     
    137143    plot_noisy_data() 
    138144    plot_sol(desol,'r-') 
    139     plot_sol(sol[1][-1],'k--') 
     145    plot_sol(sol,'k--') 
    140146    plot_sol(solcg,'b-.') 
    141147    pylab.legend(('Noisy data', 'Differential Evolution', 'Nelder Mead', 'Polak Ribiere'))  
  • tests/test_rosenbrock.py

    r5 r82  
    1313from mystic.differential_evolution import DifferentialEvolutionSolver 
    1414from mystic.detools import Best1Exp, Rand1Exp, Best2Bin, ChangeOverGeneration, VTR 
    15 from mystic.nelder_mead import fmin, rosen 
     15from mystic.scipy_optimize_fmin import __rosen as rosen 
     16from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     17from mystic.nmtools import IterationRelativeError as IRE 
    1618from mystic import Sow 
    1719 
     
    4547    esow= Sow() 
    4648    ssow= Sow() 
    47     sol = fmin(rosen, [random.random() for j in range(ND)], full_output=1, retall=1, EvaluationMonitor=esow, SimplexMonitor=ssow) 
    48     print sol[0] 
     49    xinit = [random.random() for j in range(ND)] 
     50 
     51    solver = fmin(len(xinit)) 
     52    solver.SetInitialPoints(xinit) 
     53    killme = IRE(1e-4,1e-4) 
     54    solver.Solve(rosen, killme, EvaluationMonitor = esow, StepMonitor = ssow) 
     55    sol = solver.Solution() 
     56    print sol 
    4957  
    5058    print len(esow.x) 
  • tests/test_zimmermann.py

    r81 r82  
    6363 
    6464    from mystic import Sow 
    65     from mystic.nelder_mead import fmin 
     65    from mystic.scipy_optimize_fmin import NelderMeadSimplexSolver as fmin 
     66    from mystic.nmtools import IterationRelativeError as IRE 
    6667 
    6768    simplex = Sow() 
    6869    esow = Sow() 
    69     sol = fmin(CostFunction, [random.uniform(0,5) for j in range(ND)], EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 
    70     #print "solution: ", sol 
     70    xinit = [random.uniform(0,5) for j in range(ND)] 
     71 
     72    solver = fmin(len(xinit)) 
     73    solver.SetInitialPoints(xinit) 
     74    killme = IRE(1e-4,1e-4) 
     75    solver.Solve(CostFunction, killme, EvaluationMonitor = esow, StepMonitor = simplex) 
     76    sol = solver.Solution() 
     77    print "solution: ", sol 
    7178 
    7279# end of file 
Note: See TracChangeset for help on using the changeset viewer.