程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'?

开发过程中遇到如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'的解决方法建议,希望对你解决如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'有所启发或帮助;

要修复 importError: DLL load Failed: The specifIEd procedure Could not be found. 错误,我 raed :https://stackoverflow.com/a/53111377/11747148
所以我输入:

pip install protobuf==3.6.0

(我也读过Encountering DLL error in OR Tools pywrapcp
但是我已经安装了 visual studio 2019 redistributables Version 14.28.29334.0 !我的版本比他们在 Encountering DLL error in OR Tools pywrapcp
中提到的版本更新 而那个解决方案对我不起作用!)

但是,当我再次执行源代码时,发生了新的错误:
AttributeError: module 'Google.protobuf.descriptor' has no attribute '_internal_create_key' 为了修复,我在:How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?
他们说:

pip install --upgrade protobuf

但它是一个毫无意义的Cicle!
对于第一个他们说降级protobuf 套餐。
对于第二个他们说升级protobuf 包。
我怎样才能摆脱这些错误?

源代码如果你想看:

import Json
import operator
from math import degrees,atan2,sqrt
import random
from ortools.consTraint_solver import pywrapcp
from ortools.consTraint_solver import routIng_enums_pb2

class position:

    def __init__(self,x,y):
        self.x = x
        self.y = y      

    def __str__(self):
        return "(" + str(self.X) + "," + str(self.y) + ") "

    def x_coor(self):
        return self.x

    def y_coor(self):
        return self.y

class Customer:

    pos = position(-1,-1)
    demand = 0

    def __init__(self,Name):
        self.name = name

    def setposition(self,y):
        self.pos = position(x,y)

    def setDemand(self,d):
        self.demand = d

    def setAngleWithDepot(self,a):
        self.angleWithDepot = a

    def __str__(self):
        # return str(self.Name) #+ " -> (" + str(self.pos.X) + "," + \
                #str(self.pos.y) + ") -> " + str(self.demand)
        return "(" + str(self.pos.X) + "," + \
                   str(self.pos.y) + " )"

def print_tuple(t):
    print ("["),for i in t:
        print (i),print ("]")

def copy(li):
    return [i for i in li]

def get_distance(cus1,cus2):
    # EuclIDeian
    dist = 0 
    dist = sqrt(((cus1.pos.x - cus2.pos.X) ** 2) + ((cus1.pos.y - cus2.pos.y) ** 2))
    return dist

def calculateDepotAngle(x,y,depot_x,depot_y):
    angle = degrees(atan2(y - depot_y,x - depot_X))
    bearing = (90 - anglE) % 360
    return bearing

def make_Dictionary(routE):
    global route_node
    route_node = {}
    counter = 0
    for r in route:
        route_node[counter] = r
        counter += 1
    # for k in route_node.keys():
    #     print k," ",route_node[k]

def get_route(routE):
    final = []
    for r in route:
        final.append(route_node[r])
    return final

def get_demand_route(routE):
    route_demand = 0
    for c in route:
        route_demand += c.demand
    return route_demand

def get_cost_route(routE):
    route_cost = 0
    for i in range(len(routE)- 1):
        route_cost += get_distance(route[i],route[i+1])
    return route_cost

def print_solution(final_routes):
    COST = 0
    for r in final_routes:
        print_tuple(r)
        print (get_demand_route(r))
        cost = get_cost_route(r)
        print (cost)
        COST += cost 
    print ("@R_172_10586@l Cost = ",COST)

def distance(i,j):
    I = route_node[i]
    J = route_node[j]
    # print "here ",I
    # print J
    return get_distance(I,J)

def TSP(sizE):
  # Create routIng model
    route_List = []
    if size > 0:
        # TSP of size args.tsp_size
        # Second argument = 1 to build a single tour (it's a TSp).
        # Nodes are indexed from 0 to parser_tsp_size - 1,by default the start of
        # the route is node 0.
        routIng = pywrapcp.RoutIngModel(size,1,0)

        search_parameters = pywrapcp.RoutIngModel.DefaultSearchParameters()
        # SetTing first solution heuristic (cheapest addition).
        search_parameters.first_solution_strategy = (
            routIng_enums_pb2.FirstSolutionStrategy.PATH_CHEApest_ARC)

        routIng.SetArcCostEvaluatorOfallVehicles(distancE)
        # ForbID node connections (randomly).
        rand = random.Random()
        rand.seed(0)

        assignment = routIng.solve()
        if assignment:
            # Solution cost.
            # print(assignment.objectiveValue())
            # Inspect solution.
            # Only one route here; otherwise iterate from 0 to routIng.vehicles() - 1
            route_number = 0
            node = routIng.Start(route_number)
            route = ''
            while not routIng.IsEnd(nodE):
                route += str(nodE) + ' -> '
                route_List.append(nodE)
                node = assignment.Value(routIng.Nextvar(nodE))
            route += '0'
            route_List.append(0)
            # print(routE)
        else:
            print('No solution found.')
            return -1
    return route_List


with open('data36.Json') as inputfile:
    data = Json.load(inputfilE)

noOfCustomers = len(data["nodes"])
vehicleCap = data["vehicleCapacity"][0]["1"]

DEPOT = Customer(0)
DEPOT.setposition(data["depot"]["x"],data["depot"]["y"])
DEPOT.setDemand(0)
DEPOT.setAngleWithDepot(0)

Customers = []
for i in range(0,noOfCustomers):
    c = Customer(i+1)
    c.setposition(data["nodes"][i]["x"],data["nodes"][i]["y"])
    c.setDemand(data["nodes"][i]["demand"])
    angle = calculateDepotAngle(c.pos.x,c.pos.y,DEPOT.pos.x,DEPOT.pos.y)
    c.setAngleWithDepot(anglE)
    Customers.append(C)

Customers.sort(key=lambda x: x.angleWithDepot,reverse=falsE)
route_node = {}
clusters = List()
final_routes = List()
tempCluster = List()
cap = 0
temp_Customers = copy(Customers)
while len(temp_Customers):
    currCust = temp_Customers.pop(0)
    if cap + currCust.demand <= vehicleCap:
        tempCluster.append(currCust)
        cap += currCust.demand
    else:
        clusters.append(tempCluster)
        tempCluster = List()
        cap = 0
        tempCluster.append(currCust)
        cap += currCust.demand
        
# print get_distance(DEPOT,Customers[0])
clusters.append(tempCluster)
for c in clusters:
    c.insert(0,DEPOT)
    # print_tuple(C)
    make_Dictionary(C)
    route = TSP(len(C))
    # print route
    route = get_route(routE)
    final_routes.append(routE)

print_solution(final_routes)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'全部内容,希望文章能够帮你解决如何在 python 中修复此错误:AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。