Example #1
0
 def test_clear(self):
     heap = Heap(lambda a, b: a <= b)
     sequence = [5, 9, 3, 4, 6, 2, 0, 8, 7, 1]
     heap.extend(sequence)
     self.assertFalse(heap.is_empty())
     heap.clear()
     self.assertTrue(heap.is_empty())
     self.assertRaises(IndexError, heap.peek)
     self.assertRaises(IndexError, heap.extract)
     heap.insert(9)
     self.assertEqual(1, len(heap))
     self.assertEqual(9, heap.peek())
     self.assertEqual(9, heap.extract())
     self.assertTrue(heap.is_empty())
Example #2
0
 def backwardA(self, m):
     counter = 0
     ZZ = np.random.choice([0, 1], size=[101, 101],
                           p=[1, 0])  # 0 = not traveled, 1=traveled
     CLOSED = list()
     OPEN = Heap()
     start = self.startNode()
     goal = self.node(self.gx, self.gy)
     self.lookAround(start, m)
     while self.x != goal.x or self.y != goal.y:
         counter += 1
         start.g = math.inf
         start.searchVal = counter
         goal.g = 0
         goal.searchVal = counter
         OPEN.clear()
         CLOSED.clear()
         OPEN.insert(goal)
         self.computePath(OPEN, CLOSED, goal, counter, start)
         if OPEN.isEmpty():  # open list is empty
             print("Cannot reach target")
             m.pprint(ZZ)
             sys.exit(0)
         path = self.reversePath(start, goal)
         zero = path[0]
         self.moveTo(zero)
         ZZ[zero.x][zero.y] = 1
         self.lookAround(zero, m)
         last = None
         for s in path[
                 1:]:  # moves agent, goes in proper order and checks for isBlocked
             if not s.isBlocked:
                 self.moveTo(s)
                 ZZ[s.x][s.y] = 1
                 m.pprint(ZZ, close=True)
                 self.lookAround(s, m)
                 last = s
             else:
                 break
         if last is not None and goal.x != last.x and goal.y != last.y:
             start = last
     print("target reached")
     m.pprint(ZZ)