当我清楚地使用 init 函数声明参数时,我遇到了这个问题。在此处链接我的代码:
class Node:
def _init_(self,pieza,turno,listaAsignados,parent,costeCamino,coste):#Initialize a Node
self.pieza = pieza
self.turno = turno
self.listaAsignados = listaAsignados
self.parent = parent
self.costeCamino = costeCamino
self.costeNodo = costeNodo
def calculateCost(C, i, j, listaPadre,M): #Calculate Cost of the next element
initCost = 0
cost = 0
listaDisponibles = [1]*M
for i in range(i,M-1):
minNum = 9999999999
minIndex = -1
for j in range(j,M-1):
if(not listaPadre[j] and listaDisponibles[j] and C[i][j]< minNum ):
minIndex = j
minNumber = costMatrix[i][j]
cost = cost+ minNumber
return cost
import heapq
def branch_bound(C):
"""
-C = Matrix of costs
"""
items=[]
priorityQueue = []
heapq.heapify(priorityQueue)
listaAsignados = [0]*M#Asigned list for the matrix
raiz = Node(-1,-1,listaAsignados,None,0,0)
heapq.heappush(listaAsignados,[raiz.cost,raiz])
while (priorityQueue):
examinateNode = heapq.heappop(priorityQueue)
examinateNode = examinateNode[1]
i = examinateNode.pieza+1
if (i == M):
return examinateNode.cost
for j in range(0,M-1):
if(examinateNode.listaAsignados[j] == 0):
costeCamino = examinateNode.pathCost+ C[i][j]
costeHijo = costeCamino+ calculateCost(C, i, j, examinateNode.listaAsignados,M)
nodoHijo = Node(i,j,examinateNode.listaAsignados,examinateNode,costeCamino,costeHijo)
heapq.heappush(listaAsignados,[nodoHijo.cost,nodoHijo])
return items
如果有人可以解释我为什么会发生此错误,我将不胜感激。我不知道为什么如果有构造函数,错误会继续:
<ipython-input-10-8d5dfd71f776> in branch_bound(C)
11 heapq.heapify(priorityQueue)
12 listaAsignados = [0]*M
13 raiz = Node(-1,-1,listaAsignados,None,0,0)<-------
14 heapq.heappush(listaAsignados,[raiz.cost,raiz])
15
TypeError:object()不带参数
答案 0 :(得分:1)
您需要写__init__
而不是_init_
。