类方法作为模型函数和优化函数用于scipy.optimize
我正在用Python解决一个优化问题。我想定义一个类来完成这个任务。在这个类里,我想把一个模型函数作为类的方法,比如:
class MyClass(object):
def f(self,x,parameters):
但我还想在同一个类里定义另一个方法,用来对函数f在x上的优化,比如:
def Optim_Funtion(self):
scipy.optimize.minimize(f,x0,'method='Nelder-Mead')
我的问题是,我该怎么做?我需要把函数f作为self.f传递给Optim_Funtion方法吗?我找到一个相关的问题,但他们把优化问题放在了类的定义之外:类方法作为scipy.optimize.curve_fit的模型函数,这不是我想要的。
这是我正在使用的代码:
class LaserGating:
# Given laser pulse energy and min photon number to be received at a detector, calculate the max distance
def __init__(self, alpha, PhotonNumber, EnergyMin, EnergyMax, Wavelength,TargetReflection,d):
self.alpha = alpha
self.PhotonNumber = PhotonNumber # photon number @detector
self.EnergyMax = EnergyMax # laser pulse energy max
self.EnergyMin = EnergyMin # laser pulse energy Min
self.Wavelength = Wavelength # laser wavelengh
self.TargetReflection = TargetReflection # target reflection
self.d = d # detector size
self.PhotonEnergy = 1.054e-34*2*np.pi*3.e8/self.Wavelength # energy of a photon at wavelength "Wavelength"
self.PulseEnergy = self.EnergyMin
self.PulseEnergyRange = np.linspace(self.EnergyMin,self.EnergyMax,1000) # array of energy pulse values
return
def fMin(self,x,PulseEnergy):
# laser range model: x is the argument (distance) that the function is to be minimized on
f = self.PhotonNumber - PulseEnergy*self.TargetReflection * ((self.d/x)**2)*np.exp(-self.alpha*x)/self.PhotonEnergy
return f
def FindDistance(self):
#find maximale distance given energy and photon number@ detector
#print self.PulseEnergyRange
rangeEnergy = self.PulseEnergyRange
#print rangeEnergy
testrange = []
#for testeleements in rangeEnergy:
#print testeleements
for elements in rangeEnergy:
#initial guess. Fixed for the moment but should depend on elements
x0 = 10.
#print elements
# optimisation on x, using elements as arg
test = scp.optimize.newton(self.fMin,x0,args = (elements,),tol= 1e-3)
# append answer
testrange.append(test)
return testrange
当我运行它时,比如使用:
DistanceRange = LaserGating(0.001,1000,1.e-9,1.e-6,532.e-9,0.2,0.001)
DistanceRange.FindDistance()
我得到了以下错误信息:
enter ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-199-597c7ff1bb69> in <module>()
----> 1 DistanceRange.FindDistance()
<ipython-input-194-b1c115d544c0> in FindDistance(self)
32 x0 = 1000.
33
---> 34 test = scp.optimize.minimize(self.fMin,x0,args = (elements),method='Nelder-Mead',tol= 1e-2)
35 testrange.append(test)
36 print elements
C:\Users\spinchip\AppData\Local\Continuum\Anaconda\lib\site- packages\scipy\optimize\_minimize.pyc in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
411 callback=callback, **options)
412 elif meth == 'nelder-mead':
--> 413 return _minimize_neldermead(fun, x0, args, callback, **options)
414 elif meth == 'powell':
415 return _minimize_powell(fun, x0, args, callback, **options)
C:\Users\spinchip\AppData\Local\Continuum\Anaconda\lib\site- packages\scipy\optimize\optimize.pyc in _minimize_neldermead(func, x0, args, callback, xtol, ftol, maxiter, maxfev, disp, return_all, **unknown_options)
436 if retall:
437 allvecs = [sim[0]]
--> 438 fsim[0] = func(x0)
439 nonzdelt = 0.05
440 zdelt = 0.00025
C:\Users\spinchip\AppData\Local\Continuum\Anaconda\lib\site- packages\scipy\optimize\optimize.pyc in function_wrapper(*wrapper_args)
279 def function_wrapper(*wrapper_args):
280 ncalls[0] += 1
--> 281 return function(*(wrapper_args + args))
282
283 return ncalls, function_wrapper
TypeError: fMin() takes exactly 3 arguments (2 given)code here
所以问题出在调用方法时,额外的参数没有被识别。
提前感谢任何建议,
格雷戈里
1 个回答
3
把 args = (elements)
这样写,其实和 args = elements
是一样的,也就是说并没有创建一个元组。
如果你想传递一个只有一个元素的元组,可以用 args = (elements,)
这种写法,或者用 args = tuple([elements])
这样来做。