Esempio n. 1
0
def run_baseline(board, fringe, explored_count, unexplored_cells,
                 undiscovered_mines, dim, score, knowledge_base):
    print "Running baseline"
    while (fringe and len(fringe) > 0):
        print "Baseline Fringe: ", fringe
        ((x_cord, y_cord)) = fringe.pop(0)
        if explored_count == dim * dim:
            return explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base

        # base line update
        print "Running baseline on: ", x_cord, y_cord
        mine_cells, safe_cells = run_clue_check(x_cord, y_cord, board)
        print "mine_cells", mine_cells
        print "safe_cells", safe_cells
        neighbors = []

        for cords in mine_cells:
            assert board[cords[0]][cords[1]].is_mine == True
            neighbors.extend(gb.get_neighbors(board, cords[0], cords[1]))
            undiscovered_mines = \
             mark_cell_as_mine(cords[0], cords[1], board, undiscovered_mines)
            explored_count += 1
            unexplored_cells.remove((cords[0], cords[1]))
            print "after marking mine cell knowledge:"
            gb.display_knowledge_base(knowledge_base)
            gb.visualize_agent_board(game_board)
            score = score + 1
        fringe.extend(mine_cells)

        if undiscovered_mines == 0:
            for unexplored_cell in unexplored_cells:
                if unexplored_cell not in safe_cells:
                    safe_cells.append(unexplored_cell)

        for cords in safe_cells:
            assert board[cords[0]][cords[1]].is_mine == False
            neighbors.extend(gb.get_neighbors(board, cords[0], cords[1]))
            undiscovered_mines = query_cell(cords[0], cords[1], board,
                                            undiscovered_mines)
            explored_count += 1
            unexplored_cells.remove((cords[0], cords[1]))
            print "after querying safe cell knowledge:"
            gb.display_knowledge_base(knowledge_base)
            gb.visualize_agent_board(game_board)
        fringe.extend(safe_cells)

        knowledge_base = update_knowledge_base(knowledge_base, board,
                                               mine_cells, safe_cells)
        # remove the duplicates from the fringe and keep the latest entry
        fringe = list(OrderedDict.fromkeys(fringe))

    return explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base
Esempio n. 2
0
def run_baseline(board, fringe, explored_count, unexplored_cells,
                 undiscovered_mines, dim, score):
    while (fringe and len(fringe) > 0):
        ((x_cord, y_cord)) = fringe.pop(0)
        if explored_count == dim * dim:
            return explored_count, unexplored_cells, undiscovered_mines, score

        # get the list of safe and mine cells
        mine_cells, safe_cells = run_clue_check(x_cord, y_cord, board)

        # mark all the cells identified as mines
        for cords in mine_cells:
            undiscovered_mines = mark_cell_as_mine(cords[0], cords[1], board,
                                                   undiscovered_mines)
            explored_count += 1
            unexplored_cells.remove((cords[0], cords[1]))
            gb.visualize_agent_board(game_board)
            score = score + 1
        fringe.extend(mine_cells)

        if undiscovered_mines == 0:
            for unexplored_cell in unexplored_cells:
                if unexplored_cell not in safe_cells:
                    safe_cells.append(unexplored_cell)

        # open all the cells identified as safe cells
        for cords in safe_cells:
            board, undiscovered_mines = query_cell(cords[0], cords[1], board,
                                                   undiscovered_mines)
            explored_count += 1
            unexplored_cells.remove((cords[0], cords[1]))
            gb.visualize_agent_board(game_board)
        fringe.extend(safe_cells)

        # remove duplicates from the fringe and keep the latest entry
        fringe = list(OrderedDict.fromkeys(fringe))

    return explored_count, unexplored_cells, undiscovered_mines, score
def start_baseline(board, total_mines, knowledge_base):
    explored_count = 0
    score = 0
    dim = len(board)
    unexplored_cells = get_unexplored_cells(dim, board)
    undiscovered_mines = total_mines
    knowledge_base[copy.deepcopy(frozenset(unexplored_cells))] = total_mines

    # For picking the first cell randomly
    # (row_index, col_index) = get_min_max_based_random_cell(knowledge_base, unexplored_cells)
    (row_index,
     col_index) = get_min_avg_based_random_cell(knowledge_base,
                                                unexplored_cells)
    print "Initial cell ", row_index, col_index
    fringe = [(row_index, col_index)]
    inference_fringe = []
    undiscovered_mines = \
     query_cell(row_index, col_index, board, undiscovered_mines)
    explored_count += 1
    unexplored_cells.remove((row_index, col_index))
    if board[row_index][col_index].value == 0:
        knowledge_base = update_knowledge_base(knowledge_base, board, [],
                                               [(row_index, col_index)])
    else:
        knowledge_base = update_knowledge_base(knowledge_base, board,
                                               [(row_index, col_index)], [])
    print "after querying random cell knowledge:"
    gb.display_knowledge_base(knowledge_base)
    gb.visualize_agent_board(game_board)

    # run baseline algorithm after opening one cell randomly
    explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base = \
     run_baseline(board, fringe, explored_count, unexplored_cells, undiscovered_mines, \
      dim, score, knowledge_base)
    if explored_count == dim * dim:
        return score

    while True:
        while True:
            print "Inferring..."
            mine_cells_2, safe_cells_2, knowledge_base = \
             infer_from_knowledge_base(knowledge_base, board)
            print "mine_cells_2 ", mine_cells_2
            print "safe_cells_2 ", safe_cells_2

            # no inference from basic inference
            if len(mine_cells_2) == 0 and len(safe_cells_2) == 0:
                print "Advanced Inference..."
                mine_cells_3, knowledge_base = \
                 advanced_inference(knowledge_base, board)
                print "mine_cells_3 ", mine_cells_3

                if len(mine_cells_3) == 0:
                    break
                print "!!.......EUREKA......!!"
                for cords in mine_cells_3:
                    assert board[cords[0]][cords[1]].is_mine == True
                    undiscovered_mines = \
                     mark_cell_as_mine(cords[0], cords[1], board, undiscovered_mines)
                    explored_count += 1
                    unexplored_cells.remove((cords[0], cords[1]))
                    print "after marking mine cell knowledge:"
                    gb.display_knowledge_base(knowledge_base)
                    gb.visualize_agent_board(game_board)
                    score = score + 1
                fringe.extend(mine_cells_3)

            # some inference from basic inference
            else:
                for cords in mine_cells_2:
                    assert board[cords[0]][cords[1]].is_mine == True
                    undiscovered_mines = \
                     mark_cell_as_mine(cords[0], cords[1], board, undiscovered_mines)
                    explored_count += 1
                    unexplored_cells.remove((cords[0], cords[1]))
                    print "after marking mine cell knowledge:"
                    gb.display_knowledge_base(knowledge_base)
                    gb.visualize_agent_board(game_board)
                    score = score + 1
                fringe.extend(mine_cells_2)

                for cords in safe_cells_2:
                    assert board[cords[0]][cords[1]].is_mine == False
                    undiscovered_mines = query_cell(cords[0], cords[1], board,
                                                    undiscovered_mines)
                    explored_count += 1
                    unexplored_cells.remove((cords[0], cords[1]))
                    print "after querying safe cell knowledge:"
                    gb.display_knowledge_base(knowledge_base)
                    gb.visualize_agent_board(game_board)
                fringe.extend(safe_cells_2)

            knowledge_base = update_knowledge_base(knowledge_base, board,
                                                   mine_cells_2, safe_cells_2)
            explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base = \
             run_baseline(board, fringe, explored_count, unexplored_cells, undiscovered_mines, \
              dim, score, knowledge_base)
            if explored_count == dim * dim:
                return score

        # (row_index, col_index) = get_min_max_based_random_cell(knowledge_base, unexplored_cells)
        (row_index,
         col_index) = get_min_avg_based_random_cell(knowledge_base,
                                                    unexplored_cells)
        # no_of_random_cell_calls += 1
        print "Random cell ", row_index, col_index
        fringe = [(row_index, col_index)]
        undiscovered_mines = query_cell(row_index, col_index, board,
                                        undiscovered_mines)
        explored_count += 1
        unexplored_cells.remove((row_index, col_index))
        if board[row_index][col_index].value == 0:
            knowledge_base = update_knowledge_base(knowledge_base, board, [],
                                                   [(row_index, col_index)])
        else:
            knowledge_base = update_knowledge_base(knowledge_base, board,
                                                   [(row_index, col_index)],
                                                   [])
        gb.display_knowledge_base(knowledge_base)
        gb.visualize_agent_board(game_board)

        explored_count, unexplored_cells, undiscovered_mines, score, knowledge_base = \
         run_baseline(board, fringe, explored_count, unexplored_cells, undiscovered_mines, \
          dim, score, knowledge_base)
        if explored_count == dim * dim:
            return score

    return score