Changeset 81
- Timestamp:
- 01/23/09 13:03:25 (7 years ago)
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
python/nelder_mead.py
r67 r81 12 12 Takes two additional input args 13 13 -- EvaluationMonitor 14 -- S implexMonitor14 -- StepMonitor 15 15 16 16 """ … … 30 30 31 31 def fmin(func, x0, args=(), xtol=1e-4, ftol=1e-4, maxiter=None, maxfun=None, 32 full_output=0, disp=1, retall=0, EvaluationMonitor=Null, S implexMonitor=Null):32 full_output=0, disp=1, retall=0, EvaluationMonitor=Null, StepMonitor=Null): 33 33 """Minimize a function using the downhill simplex algorithm. 34 34 … … 70 70 EvaluationMonitor -- a callable object that will be passed x, fval 71 71 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. 72 74 73 75 """ … … 119 121 120 122 while (fcalls[0] < maxfun and iterations < maxiter): 121 S implexMonitor(sim, fsim)123 StepMonitor(sim, fsim) # get all values; "best" is sim[0] 122 124 if (max(numpy.ravel(abs(sim[1:]-sim[0]))) <= xtol \ 123 125 and max(abs(fsim[0]-fsim[1:])) <= ftol): … … 219 221 print "===================" 220 222 start = time.time() 221 x = fmin(rosen,x0) 223 from tools import VerboseSow 224 stepmon = VerboseSow(10) 225 x = fmin(rosen,x0,StepMonitor=stepmon) 222 226 print x 223 227 times.append(time.time() - start) … … 225 229 226 230 for k in range(len(algor)): 227 print algor[k], "\t -- ", times[k]231 print algor[k], "\t -- took", times[k] 228 232 229 233 if __name__ == "__main__": -
python/tools.py
r73 r81 148 148 return 149 149 def __call__(self, x, y): 150 from numpy import ndarray 150 151 Sow.__call__(self, x, y) 151 152 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] 152 157 if int(self._step % self._yinterval) == 0: 158 #print "Generation %d has best Chi-Squared: %s" % (self._step, y) 153 159 print "Generation %d has best Chi-Squared: %f" % (self._step, y) 154 160 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) 156 162 return 157 163 pass -
tests/sam_corana2.py
r62 r81 42 42 def run_once(): 43 43 simplex = Sow() 44 sol = fmin(Corana2, [random.uniform(0,2), random.uniform(0,2)], retall = True, S implexMonitor = simplex)44 sol = fmin(Corana2, [random.uniform(0,2), random.uniform(0,2)], retall = True, StepMonitor = simplex) 45 45 46 46 for x in simplex.x: -
tests/sam_mogi.py
r62 r81 4 4 See test_mogi.py 5 5 6 This uses Nelder-Mead and S implexMonitor6 This uses Nelder-Mead and StepMonitor 7 7 """ 8 8 … … 61 61 z1 = z0*random.uniform(0.5,1.5) 62 62 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, S implexMonitor = simplex)63 sol = fmin(cost_function, [random.uniform(x0-40,x0+40), random.uniform(y0-40,y0+40), z1, v1], retall = True, StepMonitor = simplex) 64 64 print sol[0] 65 65 … … 74 74 y1 = y0*random.uniform(0.5,1.5) 75 75 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, S implexMonitor = 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) 77 77 print sol[0] 78 78 -
tests/sam_rosenbrock.py
r62 r81 6 6 This one uses Nelder-Mead plus matlab viz. 7 7 8 It uses the S implexMonitor option to track all simplices generated during8 It uses the StepMonitor option to track all simplices generated during 9 9 the search. 10 10 """ … … 40 40 def run_once(x0,x1): 41 41 simplex = Sow() 42 sol = fmin(rosen, [x0, x1], retall = True, S implexMonitor = simplex)42 sol = fmin(rosen, [x0, x1], retall = True, StepMonitor = simplex) 43 43 44 44 for x in simplex.x: -
tests/sam_zimmermann.py
r9 r81 6 6 This one uses Nelder-Mead plus matlab viz. 7 7 8 It uses the S implexMonitor option to track all simplices generated during8 It uses the StepMonitor option to track all simplices generated during 9 9 the search. 10 10 """ … … 39 39 def run_once(): 40 40 simplex = Sow() 41 sol = fmin(CostFunction, [random.uniform(0,7), random.uniform(0,7)], retall = True, S implexMonitor = simplex)41 sol = fmin(CostFunction, [random.uniform(0,7), random.uniform(0,7)], retall = True, StepMonitor = simplex) 42 42 43 43 for x in simplex.x: -
tests/test_mogi.py
r80 r81 125 125 # 126 126 simplex, esow = Sow(), Sow() 127 sol = fmin(cost_function, point, EvaluationMonitor = esow, retall = True, S implexMonitor = simplex)127 sol = fmin(cost_function, point, EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 128 128 print "simplex solution: ", sol[1][-1] 129 129 # -
tests/test_zimmermann.py
r5 r81 67 67 simplex = Sow() 68 68 esow = Sow() 69 sol = fmin(CostFunction, [random.uniform(0,5) for j in range(ND)], EvaluationMonitor = esow, retall = True, S implexMonitor = simplex)69 sol = fmin(CostFunction, [random.uniform(0,5) for j in range(ND)], EvaluationMonitor = esow, retall = True, StepMonitor = simplex) 70 70 #print "solution: ", sol 71 71
Note: See TracChangeset
for help on using the changeset viewer.