Exemple #1
0
def main():

    if version_info < REQ_VERSION:
        print("Python version too low! Please use", REQ_VERSION, "or later.")

    test_cases = read_test_cases()

    for test_case in test_cases:
        start = time.time()
        answer = "This puzzle is not solvable."
        queue = PriorityQueue()
        visited = set()

        if len(argv) < 2 or argv[2].strip().to_lower() != "no_check=true":
            if not has_answer(test_case):
                print(time.time() - start, answer)
                continue

        """
        The queue follows the order
            total cost, level, matrix, answer
        for all elements """
        queue.put((0, 0, test_case, ""))

        while not queue.empty():
            _, level, matrix, current_answer = queue.get()

            if level > 50:
                break

            if check_answer(matrix):
                answer = current_answer
                break

            permutations = calculate_permutations(matrix)

            for permutation, letter in permutations:
                # A tuple is necessary for storing in a set since it is immutable
                permutation_tuple = tuplize(permutation)
                if permutation_tuple not in visited:
                    heuristic_cost = calculate_heuristic(permutation)
                    visited.add(permutation_tuple)
                    queue.put((heuristic_cost+level+1,
                               level+1,
                               permutation,
                               current_answer + letter
                               ))

        print(time.time() - start, answer)
Exemple #2
0
def main():
    if version_info < REQ_VERSION:
        print("Python version too low! Please use", REQ_VERSION, "or later.")
        exit(1)

    test_cases = read_test_cases()

    for test_case in test_cases:
        start = time.time()
        answer = "This puzzle is not solvable."
        visited = set()
        queue = Queue()

        if len(argv) < 2 or argv[2].strip().to_lower() != "no_check=true":
            if not has_answer(test_case):
                print(time.time() - start, answer)
                continue

        queue.put((0, test_case, ""))

        while not queue.empty():
            level, matrix, current_answer = queue.get()

            # A tuple is necessary for storing in a set since it is immutable
            matrix_tuple = tuplize(matrix)

            if matrix_tuple not in visited:
                visited.add(matrix_tuple)
            else:
                continue

            if level > 50:
                break

            if check_answer(matrix):
                answer = current_answer
                break

            permutations = calculate_permutations(matrix)

            for permutation, letter in permutations:
                permutation_tuple = tuplize(permutation)
                if permutation_tuple not in visited:
                    queue.put((level + 1,
                               permutation,
                               current_answer + letter
                               ))

        print(time.time() - start, answer)
Exemple #3
0
def solve(grid, output, heuristic):

    if version_info < REQ_VERSION:
        print("Python version too low! Please use", REQ_VERSION, "or later.")

    test_case = grid

    start = time.time()
    answer = "This puzzle is not solvable."
    queue = PriorityQueue()
    visited = set()

    if not has_answer(test_case):
        print("TIME: " + str(time.time() - start),
              "   ---   ANSWER: " + str(answer))
        return
    """
    The queue follows the order
        total cost, level, matrix, answer
    for all elements """
    queue.put((0, 0, test_case, ""))
    while not queue.empty():
        _, level, matrix, current_answer = queue.get()

        if level > 50:
            break

        if check_answer(matrix, output):
            answer = current_answer
            break

        permutations = calculate_permutations(matrix)

        for permutation, letter in permutations:
            # A tuple is necessary for storing in a set since it is immutable
            permutation_tuple = tuplize(permutation)
            if permutation_tuple not in visited:
                heuristic_cost = calculate_heuristic(permutation, output,
                                                     heuristic)
                visited.add(permutation_tuple)
                queue.put((heuristic_cost + level + 1, level + 1, permutation,
                           (current_answer + letter)))

    print("TIME: " + str(time.time() - start),
          "   ---   ANSWER: " + str(answer))
    print("Emulating answer ...")
    return answer
Exemple #4
0
 def test_has_answer_even_row_inversions_even(self):
     # 0 is on a even row [2] and there are 4 inversions (the number 11 moving to the left)
     # Puzzle is not solvable
     matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 12], [13, 14, 15, 11]]
     assert utils.has_answer(matrix) is False
Exemple #5
0
 def test_has_answer_odd_row_inversions_odd(self):
     # 0 is on a odd row [1] and there are 3 inversions (the number 11 moving to the left)
     # Puzzle is not solvable
     matrix = [[1, 2, 3, 4], [5, 6, 7, 0], [8, 9, 10, 12], [13, 14, 11, 15]]
     assert utils.has_answer(matrix) is False
Exemple #6
0
 def test_has_answer_odd_row_inversions_even(self):
     # 0 is on a odd row [1] and there are 4 inversions (the number 11 moving to the left)
     matrix = [[1, 2, 3, 4], [5, 6, 7, 0], [8, 9, 10, 12], [13, 14, 15, 11]]
     assert utils.has_answer(matrix) is True
Exemple #7
0
 def test_has_answer_even_row_inversions_odd(self):
     # 0 is on a even row [2] and there are 3 inversions (the number 11 moving to the left)
     matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 0, 12], [13, 14, 11, 15]]
     assert utils.has_answer(matrix) is True
ce_score_file = sys.argv[2]
outfile = sys.argv[3]

neg_cnt = 4
ce_threshold_neg = 0.1
ce_threshold_pos = 0.9

q_text, p_text, p_title = utils.load_corpus(corpus='nq', q_type='train')
answers = utils.load_answers(q_type='train')
cand_qp_all, train_qids = utils.load_candidates(recall_cands_file, col=4)
ce_score = utils.load_ce_score(ce_score_file, train_qids, topk=100)

out = open(outfile, 'w')
for qid, pids in cand_qp_all.items():
    pos_pid = ''
    neg_pid = ''
    for index in range(100):
        _pid = pids[index]
        if utils.has_answer(p_text[_pid], answers[qid]) or utils.has_answer(p_title[_pid], answers[qid]):
            if not pos_pid:
                pos_pid = _pid
        else:
            if not neg_pid and ce_score[qid][index] < ce_threshold_neg:
                neg_pid = _pid
        if pos_pid and neg_pid:
            out.write('%s\t%s\t%s\t%s\t%s\t0\n' % (q_text[qid],
                    p_title.get(pos_pid, '-'), p_text[pos_pid],
                    p_title.get(neg_pid, '-'), p_text[neg_pid]))
            break
out.close()