如何最小化复杂的功能

时间:2014-02-09 18:15:03

标签: python complex-numbers minimization

当我尝试使用scipy.optimize fmin或scipy.optimize最小化来最小化我的复杂功能时,我遇到了问题。事实上,似乎最小化不想考虑到我有一个复杂功能的事实。

我将代码分为两部分,第一部分是Class,第二部分是调用类并解决最小化的主程序。

这是我的班级:

import sys,os
import numpy as np
import random, math
import matplotlib.pyplot as plt
import cmath

class real :
    def __init__(self):
        self.nmodes = 4
        self.L_ch = 1
        self.w = 2

    def minim(self,eps):
        self.T=np.array([[0.0,0.0],[0.00645-0.0012364j,0.0]])
        self.Id=np.array([[1,0],[0,1]])
        self.disp=np.linalg.det(self.T-self.Id)
        return self.disp

这是我的主程序:

import sys,os
import numpy as np
import random, math, cmath
from math import exp
import scipy
from scipy.optimize import fmin

from Carlo import *
#################################
# Call the Class and the function
#################################
A=real()
eps=0.003+0.0042j
C=A.minim(eps)
#################################
# Declare some parameters and list
#################################
Cp=0
track=2
variable=np.arange(track,6,1)
Save=[]
Tau=4.1
Freal=12
Fimag=14j
#################################
# Loop for minimize
#################################
for track in variable:
    Cp+=1
    print Cp
    if Cp==1:
        part_real=0.0
        part_imag=0.0
    else:
        part_real=Save[leng-1]/2*3.14*track
        part_imag=Save[leng]/2*3.14*track
    R_0 = fmin(C,[part_real,part_imag])
    print 'La valeur de R_0',R_0
    Save.append(Tau)
    Save.append(Freal+1)
    Save.append(Fimag+1)
    leng = len(Save) -1

但我收到了这个错误:

Traceback (most recent call last):
  File "Main.py", line 40, in <module>
    R_0 = fmin(C,[part_real,part_imag])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 360, in fmin
    res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 424, in _minimize_neldermead
    fsim[0] = func(x0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 268, in function_wrapper
    return function(x, *args)
TypeError: 'numpy.complex128' object is not callable

有人知道它究竟发生了什么? 谢谢。

1 个答案:

答案 0 :(得分:1)

在调用中:R_0 = fmin(C,[part_real,part_imag])你应该发送一个函数作为第一个参数,看起来C不是一个函数(这就是你得到错误“对象不可调用”的原因):
scipy.linalg.det返回矩阵的浮点数或复数行列式,请参阅:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.det.html