Changeset 176 for branches/alta/mystic-0.1a2/mystic/scipy_ncg.py
- Timestamp:
- 08/09/09 13:45:49 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.1a2/mystic/scipy_ncg.py
r169 r176 18 18 - StepMonitor = CustomSow() with 4 columns 19 19 - enable_signal_handler() 20 - termination = SolutionImprovement(tolerance) 20 21 21 22 Usage … … 130 131 131 132 132 def Solve(self, func, fprime,termination, sigint_callback=None,133 EvaluationMonitor=Null, StepMonitor=Null, GradMonitor=Null,134 HessianMonitor=Null,ExtraArgs=(), **kwds):133 def Solve(self, func, termination, sigint_callback=None, 134 EvaluationMonitor=Null, StepMonitor=Null, #GradientMonitor=Null, 135 ExtraArgs=(), **kwds): 135 136 """Minimize a function using NCG. 136 137 … … 143 144 144 145 func -- the Python function or method to be minimized. 145 fprime -- callable f'(x,*args)146 Gradient of f.147 146 termination -- callable object providing termination conditions. 148 147 … … 158 157 Further Inputs: 159 158 159 fprime -- callable f'(x,*args) 160 Gradient of f. 160 161 fhess_p : callable fhess_p(x,p,*args) 161 162 Function which computes the Hessian of f times an … … 186 187 x0 = self.population[0] 187 188 x0 = asarray(x0).flatten() 189 188 190 epsilon = _epsilon 189 disp = 1190 callback = None191 self.disp = 1 192 self.callback = None 191 193 fhess_p = None 192 194 fhess = None 193 195 194 196 if kwds.has_key('epsilon'): epsilon = kwds['epsilon'] 195 if kwds.has_key('callback'): callback = kwds['callback']196 if kwds.has_key('disp'): disp = kwds['disp']197 if kwds.has_key('callback'): self.callback = kwds['callback'] 198 if kwds.has_key('disp'): self.disp = kwds['disp'] 197 199 if kwds.has_key('fhess'): fhess = kwds['fhess'] 198 200 if kwds.has_key('fhess_p'): fhess_p = kwds['fhess_p'] 201 202 # fprime is actually required. Temporary fix?: 203 if kwds.has_key('fprime'): fprime = kwds['fprime'] 199 204 #------------------------------------------------------------- 200 205 … … 216 221 self._maxiter = len(x0)*200 217 222 218 # Wrap gradient function? 223 # Wrap gradient function? 224 # gcalls, fprime = wrap_function(fprime, args, GradientMonitor) 219 225 gcalls, fprime = wrap_function(fprime, args, Null) 220 226 … … 286 292 update = alphak * pk 287 293 294 # Put last solution in trialSolution for termination() 288 295 self.trialSolution = xk 289 296 290 297 xk = xk + update # upcast if necessary 291 if callback is not None:292 callback(xk)298 if self.callback is not None: 299 self.callback(xk) 293 300 k += 1 294 301 … … 304 311 self.generations = k 305 312 313 # Fix me? 306 314 self.hcalls = hcalls 315 self.gcalls = gcalls[0] 307 316 308 317 signal.signal(signal.SIGINT,signal.default_int_handler) 309 318 310 if disp:319 if self.disp: 311 320 fval = old_fval 312 321 if k >= self._maxiter: 313 if disp:322 if self.disp: 314 323 print "Warning: Maximum number of iterations has been exceeded" 315 324 print " Current function value: %f" % fval … … 319 328 print " Hessian evaluations: %d" % hcalls 320 329 else: 321 if disp:330 if self.disp: 322 331 print "Optimization terminated successfully." 323 332 print " Current function value: %f" % fval … … 402 411 403 412 from mystic.tools import Sow, CustomSow 404 from mystic.termination import XTermination413 from mystic.termination import SolutionImprovement 405 414 #stepmon = Sow() 406 415 stepmon = CustomSow('x','y','g','h', x='x', y='fval', \ … … 413 422 solver.SetEvaluationLimits(maxiter,None) 414 423 # Does requiring fprime break abstract_solver interface? 415 solver.Solve(func, fprime, termination=XTermination(xtol),\424 solver.Solve(func, SolutionImprovement(tolerance=xtol),\ 416 425 EvaluationMonitor=evalmon,StepMonitor=stepmon,\ 417 426 disp=disp, ExtraArgs=args, callback=callback,\ 418 427 epsilon=epsilon, fhess_p=fhess_p,\ 419 fhess=fhess )428 fhess=fhess, fprime=fprime) 420 429 solution = solver.Solution() 421 430 … … 427 436 fcalls = len(evalmon.x) 428 437 iterations = len(stepmon.x) 429 gcalls = iterations # Fix me? 438 439 # Fix me? 440 gcalls = solver.gcalls 430 441 hcalls = solver.hcalls 442 431 443 allvecs = [] 432 444 for i in range(iterations):
Note: See TracChangeset
for help on using the changeset viewer.