def lookForPath(self): # this genertate the ques then asignes the first node to it holdStart = self.currentBoard.getStart() currentCanidate = None openlist = PriorityQueue() closedList = PriorityQueue() closedListTraker = 1 # deal with a generation problem where if the algorithym is called more then once it rembers previouse calls if (openlist.checkSize() > 1): openlist.clear() closedList.clear() openlist.push(holdStart, 1) #this is where the algorithym will atempt to find the correct path while openlist.checkSize() > 0: if currentCanidate != None: # tracls the closed list so it can determine if a object was used by it and for display purposes latter on closedList.push(currentCanidate, closedListTraker) closedListTraker = closedListTraker + 1 currentCanidate = openlist.pull() # reached goal returns list of positiosn to be converted into visual elements if (self.currentBoard.getGoal().getY() == currentCanidate.getY() ) and (self.currentBoard.getGoal().getX() == currentCanidate.getX()): finalPath = [] finalPath.append(self.currentBoard.getGoal()) holdParent = self.currentBoard.getPosition( self.currentBoard.getGoal()).getParents() while (holdParent != None): finalPath.append(holdParent) holdParent = self.currentBoard.getPosition( holdParent).getParents() finalPath.append(self.currentBoard.getStart()) return finalPath # adding new neighbour nodes to que else: hold = self.currentBoard.getNodesNeighbours(currentCanidate) for i in range(len(hold)): if (openlist.checkIfInQueue(hold[i]) == False and closedList.checkIfInQueue(hold[i]) == False): self.currentBoard.getPosition( hold[i]).setCarryOverWeight( self.currentBoard.getPosition( currentCanidate).getWeightAndCarryOver()) self.currentBoard.getPosition( hold[i]).setParent(currentCanidate) weightToBeUsed = self.currentBoard.getPosition( hold[i]).getDisPlusWeight() openlist.push(hold[i], weightToBeUsed)
def PriorityQueueUnitTest(): #test enqueue and dequeue UnitTest.resetErrorCount() q = PriorityQueue() UnitTest.assertTrue("q.isEmpty() -> " + str(q.isEmpty()), q.isEmpty()) q.enqueue(1, 2) q.enqueue(2, 3) UnitTest.assertFalse("q.isEmpty() -> " + str(q.isEmpty()), q.isEmpty()) v = q.dequeue() UnitTest.assertEquals("q.dequeue() -> " + str(v), v, 1) q.enqueue(3, 4) v = q.dequeue() UnitTest.assertEquals("q.dequeue() -> " + str(v), v, 2) v = q.dequeue() UnitTest.assertEquals("q.dequeue() -> " + str(v), v, 3) UnitTest.assertTrue("q.isEmpty() -> " + str(q.isEmpty()), q.isEmpty()) j = 0 for ch in "ABCDEFGHIJKLMNOPQRSTUVXYZ": q.enqueue(ch, j) j += 1 line = "" while not q.isEmpty(): line += q.dequeue() UnitTest.assertEquals("line -> \"" + line + "\"", line, "ABCDEFGHIJKLMNOPQRSTUVXYZ") #test clear q.clear() UnitTest.assertTrue("q.isEmpty() -> " + str(q.isEmpty()), q.isEmpty()) #test len j = 0 for ch in "ABCDEFGHIJKLMNOPQRSTUVXYZ": q.enqueue(ch, j) j += 1 length = q.len() UnitTest.assertEquals("length -> " + str(length), length, 26) q.clear() if (UnitTest.getErrorCount() == 0): print("QueueUnitTest succeeded") else: print("QueueUnitTest failed")
def search(self, initial_state_data, k, max_nodes=0): assert isinstance(initial_state_data, Search.NodeStateData) assert isinstance(k, int) current_node = self.__create_node(initial_state_data) explored = PriorityQueue() # the nodes we've already looked at frontier = PriorityQueue() # the nodes we're looking at now frontier_successors = PriorityQueue( ) # successors of the nodes we're looking at now frontier.push(current_node, current_node.search_data.hcost ) # add the first node to the frontier node_count = 0 while True: if frontier.empty(): # failure return None while not frontier.empty( ): # for every node in the frontier, expand it and pick the k best successors # print("moved ", str(current_node.state_data.last_move), " to " + str(current_node.state_data.parent), # str(current_node.search_data.hcost)) # get the next frontier node and mark it as explored current_node = frontier.pop() # print(current_node.state_data.get_tiles()) explored.push(current_node, current_node.search_data.hcost) if current_node.state_data.goal_test: # success return Search.build_solution(current_node.state_data) # expand unexplored neighbors for prioritized_neighbor_node in self.__prioritize_neighbors( current_node): neighbor_node = prioritized_neighbor_node[1] if not explored.contains(neighbor_node): frontier_successors.push(neighbor_node, prioritized_neighbor_node[0]) node_count += 1 if 0 < max_nodes <= node_count: # failure return None # get the k best moves and put them in explored, clearing frontier frontier = frontier_successors.truncate(k) frontier_successors.clear()
assert que.peek() is None assert que.poll() is None def add_to_heap(): que.add(3, 3) que.add(2, 2) que.add(5, 5) que.add(6, 6) que.add(7, 7) que.add(9, 9) que.add(4, 4) que.add(8, 8) que.add(1, 1) add_to_heap() for i in range(1, 10): assert que.poll().value == i que.clear() assert que.size() == 0 add_to_heap() assert que.contains(3) is True assert que.contains(4) is True assert que.contains(2) is True assert que.contains(11) is False assert que.contains(12) is False assert que.contains(9) is True
def PriorityQueueTest(): errorcount = 0 q = PriorityQueue() #UnitTest.assertTrue("q.isEmpty() -> " + str(q.isEmpty()), q.isEmpty()) if q.isEmpty() != True: errorcount += 1 if len(q) != 0: errorcount += 1 q.enqueue("Kokichi", 10) q.enqueue("Gonta", 9) q.enqueue("Kirumi", 4) q.enqueue("Kaede", 2) q.enqueue("Miu", 8) q.enqueue("Kaito", 11) q.enqueue("Ryoma", 3) q.enqueue("Korekiyo", 7) q.enqueue("Rantaro", 1) q.enqueue("Angie", 5) q.enqueue("Tenko", 6) ###TEST STUFF HERE### if q.isEmpty(): errorcount += 1 if len(q) != 11: errorcount += 1 line = [] while not q.isEmpty(): #print(q.printout()) #print(q.dequeue()) line.append(q.dequeue()) #print(q.printout()) #print(len(q)) #print(" ") for i in range(len(line)): if i + 1 != line[i][ 1]: #checks to see if the priority does indeed go from min(1) to max(11) values errorcount += 1 if q.isEmpty( ) != True: #makes sure that after dequeueing all of that, q is empty errorcount += 1 if len(q) != 0: errorcount += 1 #Testing out cases where the priorities are not numerically right after the other q.enqueue("Keebo", 11) q.enqueue("Tsumugi", 3) q.enqueue("Shuichi", 7) q.enqueue("Himiko", 1) q.enqueue("Maki", 5) line = [] while not q.isEmpty(): #print(q.printout()) #print(q.dequeue()) line.append(q.dequeue()) #print(q.printout()) #print(len(q)) #print(" ") #print(line) lastpriority = line[0][1] for i in range(1, len(line)): if lastpriority > line[i][ 1]: #checks to see if the priority of outputs still is from min to max errorcount += 1 lastpriority = line[i][1] q.enqueue("Monokid", 11) #Just adding values to make sure the .clear() function works... q.enqueue("Monodam", 3) q.enqueue("Monophanie", 7) q.enqueue("Monokuma", 1) q.enqueue("Monosuke", 6) q.enqueue("Monotaro", 5) q.enqueue("Nanokumas", 10) q.clear() #Test to see if .clear() works if len(q) != 0: errorcount += 1 if q.isEmpty() != True: errorcount += 1 if (errorcount == 0): print("PriorityQueueTest succeeded") else: print("PriorityQueueTest failed")