ganyishang
滚蛋吧代码君
采纳率70%
2021-04-18 11:06

Python代码运行出错,求大佬解答

50
import random
import numpy as np
import sympy

arry11=np.zeros((20,20))

def func1_V2(data_distance):  #计算V2
    V1=np.zeros(20)
    W=np.zeros(20)
    a=np.zeros((20,20))
    b=np.zeros((20,20))
    c=np.zeros(20)
    V2=np.zeros(20)
    for i in range(20):
        for j in range(20):
            a[i][j]=0.2061*sympy.exp((-10.229) * (data_distance[i][j])-1)
            V1[i]=V1[i]+a[i][j]
    for i in range(20):
        for j in range(20):
            b[i][j]=1.79*sympy.exp((-2*4.036) * (data_distance[i][j])-1)
            V2[i]=V2[i]+b[i][j]
    return V2


def func_distance(arry11):  #原子之间的距离公式
    data_distance = np.zeros((20, 20))
    for i in range(20):
        for j in range(20 - i):
            data_distance[i][j] = sympy.sqrt((arry11[i][0] - arry11[j][0]) ** 2 + (arry11[i][1] - arry11[j][1]) ** 2 + (arry11[i][2] - arry11[j][2]) ** 2)
    return data_distance

def func_gradient(data_distance):  #计算梯度
    V2=func1_V2(data_distance)
    r1 = np.zeros((20, 20))
    r2 = np.zeros((20, 20))
    r3 = np.zeros((20, 20))
    for i in range(20):
        for j in range(20):
            r1[i][j] = 0.2061 * (-10.229) * sympy.exp((-10.229) * (data_distance[i][j] - 1))
            r2[i][j] = 0.5 * V2[i] * 1.79 * (-2 * 4.036) * sympy.exp((-2 * 4.036) * (data_distance[i][j] - 1))
            r3[i][j] = r1[i][j] - r2[i][j]
    return r3


def func1_Gupta(data_distance):   #计算Gupta势能
    V1=np.zeros(20)
    a=np.zeros((20,20))
    b=np.zeros((20,20))
    c=np.zeros(20)
    V2=np.zeros(20)
    V3 = np.zeros(20)
    V_W=0
    for i in range(20):
        for j in range(20):
            a[i][j]=0.2061*sympy.exp((-10.229) * (data_distance[i][j])-1)
            V1[i]=V1[i]+a[i][j]
    for i in range(20):
        for j in range(20):
            b[i][j]=1.79*sympy.exp((-2*4.036) * (data_distance[i][j])-1)
            V2[i]=V2[i]+b[i][j]
    for i in range(20):
        V3[i]=sympy.sqrt(V2[i])
    for i in range(20):
        c[i]=V1[i]-V3[i]
    for i in range(20):
        V_W=V_W+c[i]
    return V_W



def DFP(arry11): #局部极小化
    a=sympy.symbols("a")
    r=np.zeros((20,20))
    r1=np.zeros((20, 20))
    H=np.eye(20)
    Nolmprove=0
    data_distance1=np.zeros((20,20))
    data_distance1=func_distance(arry11)
    while True:
        r1=func_gradient(data_distance1)
        p=-np.dot(H,r1)
        arry11 = arry11
        for i in range(20):
            for j in range(3):
                arry11[i][j]=arry11[i][j]+a
        alpha=func1_Gupta(arry11)
        difyL_a = sympy.diff(alpha, a)
        aa = sympy.solve([difyL_a], [a])
        a = aa.get(a)
        arry11_1=arry11
        arry11=arry11+np.dot(a,p)
        arry11_2=arry11
        data_distance2=func_distance(arry11)
        r2=func_gradient(data_distance2)
        for i in range(20):
            for j in range(20):
                r[i][j]=r2[i][j]-r1[i][j]
        arry11_3=arry11_2-arry11_1
        H1=np.dot(arry11_3,np.transpose(arry11_3))/np.dot(np.transpose(arry11_3),r)-np.dot(H,np.dot(r,np.transpose(np.dot(H,r))))/np.dot(transpose(r),np.dot(H,r))
        H=H+H1
        Nolmprove=Nolmprove+1
        if Nolmprove ==40:
            return arry11
            break
        else:
            continue

if __name__ == '__main__':
    print("开始运行!")
    arry11=np.zeros((20,20))
    for i in range(20):
        for j in range(20):
            arry11[i][j]=random.uniform(-2,2)
    data_distance_1 = np.zeros(20)
    arry11=DFP(arry11)
    arry11_1 = arry11
    data_distance = func_distance(arry11)
    Gupta1 = func1_Gupta(data_distance)
    max_1 = 0
    max_distance = 0
    x = 0
    y = 0
    z = 0
    x_list =np.zeros(20)
    y_list =np.zeros(20)
    z_list =np.zeros(20)
    while True:
        for i in range(20):
            for j in range(3):
                arry11[i][j] = arry11[i][j] + random.uniform(-0.5, 0.5);
        for i in range(20):
            x_list[i] = arry11[i][0]
            x = x + x_list[i]
            y_list[i] = arry11[i][1]
            y = y + y_list[i]
            z_list[i] = arry11[i][2]
            z = z + z_list[i]
        x = x / 20
        y = y / 20
        z = z / 20
        xyz = np.zeros(3)
        for i in range(20):
            data_distance[i] = sympy.sqrt((arry11[i][0] - x) ** 2 + (arry11[i][1] - y) ** 2 + (arry11[i][2] - z) ** 2);
            if data_distance >= max_distance:
                max_distance = data_distance
                xyz[0] = arry11[i][0]
                xyz[1] = arry11[i][1]
                xyz[2] = arry11[i][2]
                a = i
        arry11[a][0] = xyz[0] + random.uniform(-0.3, 0.3)
        arry11[a][1] = xyz[1] + random.uniform(-0.3, 0.3)
        arry11[a][2] = xyz[2] + random.uniform(-0.3, 0.3)
        arry11 = DFP(arry11)
        data_distance = func_distance(arry11)
        Gupta2 = func1_Gupta(data_distance)
        if Gupta2 <= Gupta1:
            Gupta1 = Gupta2
            arry11 = arry11
            arry11_1 = arry11
            max_1 = max_1 + 1
            continue
        else:
            arry11 = arry11_1  # 回到初始
        if max_1 == 30:
            print(arry11)
            break
        else:
            continue

子程序返回数组后,主程序调用该数据时显示类型错误,求大佬解答

  • 点赞
  • 收藏
  • 复制链接分享

5条回答

  • technologist_17 CSDN技术专家团-孙老师 19天前

    主要是这个,arry11[i][j]=arry11[i][j]+a

    arry11是一个float类型的数组,而a是一个symbol符号,arry11[i][j]+a是一个symbol表达式,无法直接赋值。

    建议将arry11[i][j]+a作为一个整体向后传递,可能到了函数func1_Gupta,仍然会遇到类似的问题,因为:

    a[i][j]=0.2061*sympy.exp((-10.229) * (data_distance[i][j])-1)

    看了你的代码,有三个建议:

    1、Numpy数组与标量运算,不要用循环,直接运算即可,运算会作用到数组中的每一个元素。例如:

    for i in range(20):
        for j in range(3):
            arry11[i][j]=arry11[i][j]+a

    可以写成:arry11[:,:3]+a,相当于给arry11的每一个元素加a。

    2、可以尝试声明a为symbol矩阵,例如a=zeros(20,20)。

    3、如果还是有问题,可以尝试将a不声明为symbol符号,用普通数值来计算。

    点赞 评论 复制链接分享
  • software7503 CSDN技术专家团-赖老师(软件之家) 19天前

    需要返回float类型的值,返回的值无法转换为float类型。

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 17天前

    非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

    速戳参与调研>>>https://t.csdnimg.cn/Kf0y

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 18天前

    您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论 复制链接分享
  • jumpe_17 jumpe17 19天前
            arry11 = arry11
            for i in range(20):
                for j in range(3):
                    arry11[i][j]=arry11[i][j]+a

    把这段换成

            arraySympy = np.array([])
            for i in range(20):
                for j in range(3):
                    arraySympy = np.append(arraySympy,arry11[i][j]+a)
            print(arraySympy)

    你看看是不是你想要的效果

    点赞 评论 复制链接分享

相关推荐