# -*- coding: utf-8 -*- """ Created on Mon Oct 24 14:57:19 2011 @author: josef """ import numpy as np import numpy,scipy,scipy.optimize class Func(object): def __init__(self): self.counter = 0 self.counter2 = 0 def __call__(self, *args): print '\n iteration:', self.counter2, args if self.counter2 > 100: raise Exception self.counter2 = self.counter2+1 def func(self, x): print self.counter, repr(x), #x<-100 & x>-300, # if not x: print 'noooooooooo' if self.counter > 5000: raise Exception self.counter = self.counter+1 # if x[0]<-100 and x[0]>-300: # return np.nan #val = np.sum(-np.exp(-x[:1]/4.) -np.exp(-x[:1]**2))/1e-3 + np.sum((1-x[1:])**2) #+ np.nan * x<-100 val = -1/np.sum(-np.exp(-x[:1]/4.) -np.exp(-x[:1]**2))/1e3 + np.sum((1-x[1:])**2) #+ np.nan * x<-100 print val return val kill = Func() f=lambda x: -np.e**-x/2. +np.exp(-x**2) fp=lambda x: -f(x) x0=numpy.asarray([-10]*3) #x0=numpy.asarray([np.nan]) #x=scipy.optimize.fmin_bfgs(f,x0,fp,maxiter=10, callback=kill) #x=scipy.optimize.fmin_bfgs(kill.func,x0,fp,maxiter=10, callback=kill) x=scipy.optimize.fmin_bfgs(kill.func,x0,maxiter=50, callback=kill) print 'before: x=%s, after: x=%s' % (x0,x) scipy.optimize.fmin(kill.func,x0,maxiter=50, callback=kill)