gpt4 book ai didi

python - 类方法作为模型函数和类方法作为 scipy.optimize 的优化函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:45 25 4
gpt4 key购买 nike

我正在使用 Python 来解决一个优化问题。我想定义一个类来完成这项工作。在类中,我想使用模型函数作为类的方法,例如:

class MyClass(object):
def f(self,x,parameters):

但我还想在同一个类中定义另一个方法,对 x 上的函数 f 进行优化,例如:

     def Optim_Funtion(self):
scipy.optimize.minimize(f,x0,'method='Nelder-Mead')

我的问题是如何做到这一点?我是否必须在 Optim_Funtion 方法中将函数 f 作为 self.f 传递?我发现了一个与此相关的问题,但他们将优化问题从类定义中剔除: class method as a model function for 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

所以问题出在调用方法时无法识别的附加参数上。

提前感谢您的任何建议,

格雷戈里

最佳答案

传递 args = (elements) 等同于 args = elements,即不创建元组。

要传递 1 元素元组,可以执行 args = (elements,)args = tuple([elements])

关于python - 类方法作为模型函数和类方法作为 scipy.optimize 的优化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com