缺少1个必需的位置参数:“dictVar”Python

2024-10-01 09:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python入门级程序员。我用一个odeint函数来解常微分方程组。我正在构建包含变量(a、b、c)、三角函数(sin、cos、tan)和算术运算符(+和-)的方程结构库(字典),然后尝试使用群智能算法对其进行优化。我只是提到代码中出错的部分。我已经查看了一些相同错误的已解决问题,但没有帮助我解决我的错误,或者可能我没有得到问题的精确解决方案。提前谢谢

dictVar = {'a':0, 'b': 1, 'c': 2}
ops = { "+": operator.add, "-": operator.sub }
t_range = arange(0.0,60.0,1.0)


def odeFunc(Y, t,z, x,dictVar):

    if x[-3] == 192:
        temp1 = 191
    else:
        temp1 = int(x[-3])
    if x[-2] == 192:
        temp2 = 191
    else:
        temp2 = int(x[-2])
    if x[-1] == 192:
        temp3 = 191
    else:
        temp3 = int(x[-1])
    modelOne = modelsOne[temp1]
    modelTwo = modelsTwo[temp2]
    modelThree = modelsThree[temp3]
    return GenModel(Y, x,z, modelOne,modelTwo,modelThree, dictVar)


def GenModel(Y,x,z,modelOne,modelTwo,modelThree, dictVar):
    dydt = zeros_like(Y)
    dydt[0] = ops[modelOne[0]](dydt[0],x[0]*z[0]*Y[0])
    dydt[0] = ops[modelOne[1]](dydt[0],x[1]*z[1]*Y[dictVar[modelOne[-3]]])
    dydt[0] = ops[modelOne[2]](dydt[0],x[2]*z[2]*Y[dictVar[modelOne[-2]]]*Y[dictVar[modelOne[-1]]])
    dydt[0] = ops[modelOne[3]](dydt[0],x[3]*z[3])

    dydt[1] = ops[modelTwo[0]](dydt[1],x[4]*z[0]*Y[1])
    dydt[1] = ops[modelTwo[1]](dydt[1],x[5]*z[1]*Y[dictVar[modelTwo[-3]]])
    dydt[1] = ops[modelTwo[2]](dydt[1],x[6]*z[2]*Y[dictVar[modelTwo[-2]]]*Y[dictVar[modelTwo[-1]]])
    dydt[1] = ops[modelTwo[3]](dydt[1],x[7]*z[3])

    dydt[2] = ops[modelThree[0]](dydt[2],x[8]*z[0]*Y[2])
    dydt[2] = ops[modelThree[1]](dydt[2],x[9]*z[1]*Y[dictVar[modelThree[-3]]])
    dydt[2] = ops[modelThree[2]](dydt[2],x[10]*z[2]*Y[dictVar[modelThree[-2]]]*Y[dictVar[modelThree[-1]]])
    dydt[2] = ops[modelThree[3]](dydt[2],x[11]*z[3])

    return dydt
#Equations
def pendulum_equations(w, t):
    T, I, V = w
    dT = 80 - 0.15*T*sin(T) - 0.00002*T*V
    dI = 0.00002*T*V - 0.55*I*cos(T)
    dV = 900*0.55*I - 5.5*V - 0.00002*T*V*tan(V)
    return  dT, dI, dV

result_init = odeint(pendulum_equations, initial_condi, t_range)

# Calculating Sum of Square Error here
def myfunc(xRand):
    result_new = odeint(odeFunc, initial_condi, t_range, args=(xRand,dictVar))
    result_sub = result_new - result_init
    return sum(result_sub*result_sub)

我犯了一个错误

TypeError: odeFunc() missing 1 required positional argument: 'dictVar'

Tags: returnifdef错误rangeresultopsodeint
1条回答
网友
1楼 · 发布于 2024-10-01 09:24:57

您需要将额外的参数作为一个元组处理,以便在odeFunc中使用

dictVar = {'a':0, 'b': 1, 'c': 2}
ops = { "+": operator.add, "-": operator.sub }
t_range = arange(0.0,60.0,1.0)


def odeFunc(Y, t,z, args):
    x,dictVar = args
    if x[-3] == 192:
        temp1 = 191
    else:
        temp1 = int(x[-3])
    if x[-2] == 192:
        temp2 = 191
    else:
        temp2 = int(x[-2])
    if x[-1] == 192:
        temp3 = 191
    else:
        temp3 = int(x[-1])
    modelOne = modelsOne[temp1]
    modelTwo = modelsTwo[temp2]
    modelThree = modelsThree[temp3]
    return GenModel(Y, x,z, modelOne,modelTwo,modelThree, dictVar)


def GenModel(Y,x,z,modelOne,modelTwo,modelThree, dictVar):
    dydt = zeros_like(Y)
    dydt[0] = ops[modelOne[0]](dydt[0],x[0]*z[0]*Y[0])
    dydt[0] = ops[modelOne[1]](dydt[0],x[1]*z[1]*Y[dictVar[modelOne[-3]]])
    dydt[0] = ops[modelOne[2]](dydt[0],x[2]*z[2]*Y[dictVar[modelOne[-2]]]*Y[dictVar[modelOne[-1]]])
    dydt[0] = ops[modelOne[3]](dydt[0],x[3]*z[3])

    dydt[1] = ops[modelTwo[0]](dydt[1],x[4]*z[0]*Y[1])
    dydt[1] = ops[modelTwo[1]](dydt[1],x[5]*z[1]*Y[dictVar[modelTwo[-3]]])
    dydt[1] = ops[modelTwo[2]](dydt[1],x[6]*z[2]*Y[dictVar[modelTwo[-2]]]*Y[dictVar[modelTwo[-1]]])
    dydt[1] = ops[modelTwo[3]](dydt[1],x[7]*z[3])

    dydt[2] = ops[modelThree[0]](dydt[2],x[8]*z[0]*Y[2])
    dydt[2] = ops[modelThree[1]](dydt[2],x[9]*z[1]*Y[dictVar[modelThree[-3]]])
    dydt[2] = ops[modelThree[2]](dydt[2],x[10]*z[2]*Y[dictVar[modelThree[-2]]]*Y[dictVar[modelThree[-1]]])
    dydt[2] = ops[modelThree[3]](dydt[2],x[11]*z[3])

    return dydt
#Equations
def pendulum_equations(w, t):
    T, I, V = w
    dT = 80 - 0.15*T*sin(T) - 0.00002*T*V
    dI = 0.00002*T*V - 0.55*I*cos(T)
    dV = 900*0.55*I - 5.5*V - 0.00002*T*V*tan(V)
    return  dT, dI, dV

result_init = odeint(pendulum_equations, initial_condi, t_range)

# Calculating Sum of Square Error here
def myfunc(xRand):
    result_new = odeint(odeFunc, initial_condi, t_range, args=(xRand,dictVar))
    result_sub = result_new - result_init
    return sum(result_sub*result_sub)

相关问题 更多 >