def expand(self): if self.expanded: return self.score = 0 for direction in ['L','R','D','U']: newBoard = np.copy(self.board) if not move(newBoard,direction): self.scores[direction] = -np.inf continue unf = unfilledSpots(newBoard) self.unf[direction] = float(len(unf)) weight = float(1/self.unf[direction]) for spot in unf: newerBoard = np.copy(newBoard) addSpecificTile(newerBoard,2,spot[0],spot[1]) newNode = createFunction(newerBoard,self,direction) self.scores[direction] += 0.9*weight*newNode.score self.children2[direction].append(newNode) newerBoard = np.copy(newBoard) addSpecificTile(newerBoard,4,spot[0],spot[1]) newNode = createFunction(newerBoard,self,direction) self.scores[direction] += 0.1*weight*newNode.score self.children4[direction].append(newNode) if self.scores[direction] > self.score: self.score = self.scores[direction] self.bestDir = direction if self.parent: self.parent.update() self.expanded = True
def searchEM(board,nodesToExpand=1000): startNode = createFunction(board) heap = [] push(heap,(-startNode.score,startNode)) i = 0 while i < nodesToExpand: start = time.time() thisNode = pop(heap)[1] if not thisNode.expanded: thisNode.expand() for a in ['L', 'R', 'D', 'U']: for childNode in thisNode.children2[a]: push(heap,(-childNode.score,childNode)) for childNode in thisNode.children4[a]: push(heap,(-childNode.score,childNode)) i += 1 """ else: #print (i) for a in ['L', 'R', 'D', 'U']: for childNode in thisNode.children2[a]: push(heap,(-childNode.score,childNode)) for childNode in thisNode.children4[a]: push(heap,(-childNode.score,childNode)) """ #print "time to expand:" , time.time()-start Node.allNodes = {} #print (startNode.bestDir) #pdb.set_trace() return move(board,startNode.bestDir)