Пример #1
0
def bfs_no_tunneling(filename, env):
    # stats
    max_coverage = 0
    nodes_cnt = 0
    start_time = time.time()
    print_time = time.time() - 2

    # state
    queue = Queue()
    hashes = set()
    queue.append(env.get_init_state())  # push the initial state
    while not queue.empty():
        state = queue.pop()
        nodes_cnt = nodes_cnt + 1
        if time.time() > print_time + 1:
            if env.coverage(state) * 100 > max_coverage:
                max_coverage = env.coverage(state) * 100
            history = state.get_history()
            print_status(filename, -1, int(time.time() - start_time),
                         max_coverage, len(hashes), queue.items, nodes_cnt,
                         len(history), 0)
            print_time = time.time()
            nodes_cnt = 0
        possible_moves = env.nodes[state.pos].keys()
        for op in possible_moves:
            new_state = env.do_step(op=op, state=state)
            new_state_hash = env.state_hash(new_state)
            if new_state_hash in hashes:
                continue
            if env.goal_reached(new_state):
                clean_status()
                return new_state
            hashes.add(new_state_hash)
            queue.append(new_state)
    clean_status()
    return None
Пример #2
0
    def test_pop(self):
        q = Queue()
        self.assertTrue(q.empty())
        q.append("1")
        self.assertFalse(q.empty())

        self.assertEqual("1", q.pop())
        self.assertTrue(q.empty())
        q.append("2")
        self.assertFalse(q.empty())
        q.append("3")
        q.append("4")
        q.append("5")
        q.append("6")

        self.assertFalse(q.empty())
        self.assertEqual("2", q.pop())
        self.assertEqual("3", q.pop())
        self.assertEqual("4", q.pop())
        self.assertEqual("5", q.pop())
        self.assertFalse(q.empty())
        self.assertEqual("6", q.pop())
        self.assertTrue(q.empty())