def calcCostList(self, positions, internal = False): # calculates cost given dictionary of edge positions ct = [] left = [] right = [] up = [] down = [] cType = self.cType revPos = dict([(v,k) for (k,v) in positions.items()]) #print positions #print revPos for (state,(y,x)) in positions.items(): if (y-1,x) not in revPos: up.append(state) #else: # edge = (revPos[(y-1,x)], state) # ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) if (y+1,x) not in revPos: down.append(state) else: edge = (state, revPos[(y+1,x)]) ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) if (y,x-1) not in revPos: left.append(state) #else: # edge = (revPos[(y,x-1)], state) # ct.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) if (y,x+1) not in revPos: right.append(state) else: edge = (state, revPos[(y,x+1)]) ct.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) #print "x edges", left, right #print "y edges", up, down if not internal: for e in left: ct.append(cost.indivPicker(cType, self.blankPos, e, "x", self, False)) for e in right: ct.append(cost.indivPicker(cType, e, self.blankPos, "x", self, False)) for e in up: ct.append(cost.indivPicker(cType, self.blankPos, e, "y", self, False)) for e in down: ct.append(cost.indivPicker(cType, e, self.blankPos, "y", self, False)) return ct
def calcCostMat(self, mat, internal = False): # calculated cost given matrix of edge positions ct = [] cType = self.cType for row in range(len(mat)): #print row, mat[row] if not internal: edge = (mat[row][len(mat[row]) - 1][1], self.blankPos) #self.output(edge[0]) #self.output(edge[1]) #print cost.indivPicker(cType, edge[0], edge[1], "x", self, False) ct.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) #print "x edge", mat[row][len(mat[row]) - 1][1] edge = (self.blankPos, mat[row][0][0]) ct.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) #print "x edge", mat[row][0][0] for col in range(len(mat[row])): if not internal: if row == 0: edge = (self.blankPos, mat[0][col][1]) #print "y edge", mat[0][col][1] ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) if col == 0: edge = (self.blankPos, mat[0][col][0]) #print "y edge", mat[0][col][0] ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) elif row == len(mat) - 1: edge = (mat[len(mat)-1][col][1], self.blankPos) ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) #print "y edge", mat[len(mat)-1][col][1] if col == 0: edge = (mat[len(mat)-1][col][0], self.blankPos) ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) #print "y edge", mat[len(mat)-1][col][0] if row != len(mat)-1 and col >= len(mat[row+1]): edge = (mat[row][col][1], self.blankPos) ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) #print "y edge", mat[row][col][1] if row != 0 and col >= len(mat[row-1]): edge = (self.blankPos, mat[row][col][1]) ct.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) #print "y edge", mat[row][col][1] edge = (mat[row][col][0], mat[row][col][1]) ct += [cost.indivPicker(cType, edge[0], edge[1], "x", self, False)] if row + 1 < len(mat) and col <= len(mat[row+1]): if col == len(mat[row+1]): edge = (mat[row][col][0],mat[row+1][col-1][1]) else: edge = (mat[row][col][0], mat[row+1][col][0]) ct += [cost.indivPicker(cType, edge[0], edge[1], "y", self, False)] col = len(mat[row]) - 1 if row + 1 < len(mat) and col < len(mat[row+1]): edge = (mat[row][col][1], mat[row+1][col][1]) ct += [cost.indivPicker(cType, edge[0], edge[1], "y", self, False)] #assert len(ct) == 2*self.sizeX*self.sizeY - self.sizeX - self.sizeY return ct
def setCost(self,costType, perX = None, perY = None, prior = None, dt = None, process = False): self.prx = perX self.pry = perY self.prior = prior self.costType = costType self.states[self.blankPos] = self.blank self.blankCount = self.sizeX * 2 + self.sizeY * 2 #print "blankCount", self.blankCount self.dataPieces[self.blankPos] = list(self.blank.getdata()) self.rotDataPieces[self.blankPos] = list(self.blank.getdata()) self.costX, self.costY = cost.picker(costType, self, perX, perY, prior, dt) # if process and False: # self.costX = cost.normalizeCost(self.costX) # self.costY = cost.normalizeCost(self.costY) if process: self.costX = cost.processCostX(self) self.costY = cost.processCostY(self) self.heapify() self.edgeScoreX = {} # Used to speed up extract Heap method for (e, c) in self.costX.items(): self.edgeScoreX[e] = c self.edgeScoreY = {} for (e, c) in self.costY.items(): self.edgeScoreY[e] = c #while len(self.heapX) > 0: # print hq.heappop(self.heapX) #print self.costY cType = self.cType # for evaluation purposes tc = [] for i in range(0,self.sizeY): for j in range(0,self.sizeX): if (i,j) not in self.corners: if j < self.sizeX-1 and (i,j+1) not in self.corners: edge = ((i,j),(i,j+1)) tc.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) if i < self.sizeY-1 and (i+1,j) not in self.corners: edge = ((i,j),(i+1,j)) tc.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) sx = self.sizeX sy = self.sizeY self.totalInternalCost = tc[:] for i in range(0,sx): edge = (self.blankPos,(0,i)) tc.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) edge = ((sy-1,i),self.blankPos) tc.append(cost.indivPicker(cType, edge[0], edge[1], "y", self, False)) for i in range(0,sy): edge = ((i,sx-1),self.blankPos) tc.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) edge = (self.blankPos,(i,0)) tc.append(cost.indivPicker(cType, edge[0], edge[1], "x", self, False)) self.totalCost = tc