Пример #1
0
def test_sequences_from_database_loop(boards, logger):

    solver = triangular_puzzle_solver.PuzzleSolver(logger)
    logger.info("start Haley puzzle solving.")

    conn = solver_database.create_connection(
        r"D:\Temp\puzzle_haley\attempts_no_boards.db")
    sequences_count = 5

    total_sequences_tested = 0
    total_pieces_tested = 0
    while True:

        id_first_row, sequences = solver_database.get_untested_sequences(
            conn, sequences_count)
        logger.info(
            "*****get new sequences from database. (rows taken:{}) id first row:{}"
            .format(sequences_count, id_first_row))

        # sequences.append([7,2,3,11,8,1,10,6,4,9,5])  # test to see if winners are detected.
        sequences = [[7, 2, 3, 11, 8, 1, 10, 6, 4, 9, 5]] + sequences

        for s in sequences:
            print(s)
        st, pt = try_sequences_for_all_boards(boards, sequences)
        total_sequences_tested += st
        total_pieces_tested += pt
        logger.info(
            'For now: Total tested sequences: {}, total pieces tested: {}'.
            format(total_sequences_tested, total_pieces_tested))
        solver_database.set_sequences_as_tested(conn, sequences)
Пример #2
0
def prepare_puzzle_pieces(base_pieces_patterns):
    solver = triangular_puzzle_solver.PuzzleSolver()
    # prepare puzzle pieces
    pieces_with_orientations = solver.prepare_pieces(base_pieces_patterns)
    # for patterns in prepared_patterns:
    #     print(patterns)

    pieces_orientations_per_piece = [len(p) for p in pieces_with_orientations]

    return pieces_with_orientations, pieces_orientations_per_piece
Пример #3
0
def print_sequence_on_board(board, sequence):

    solver = triangular_puzzle_solver.PuzzleSolver(logger)
    if type(sequence) is str:
        elements = sequence.split(",")
        seq = []
        for p, o in zip(elements[0::2], elements[1::2]):
            seq.append((int(p), int(o)))
        sequence = seq

    # print(sequence)
    solver.build_up_state(board, pieces_with_orientations, sequence, True)
Пример #4
0
def show_a_solution():
    # winner found 2020-05-09
    '''
    2020-05-09 19:00:00,296 MainThread Start trying pieces sequence. (all orientations, top left to bottom right): [2, 5, 9, 3, 11, 10, 6, 1, 4, 7, 8]
    2020-05-09 19:00:00,369 MainThread Testing endend. Pieces tested:42. Found? False, state: []. longest state length: 2/11 ([(2, 3), (5, 1)])
    2020-05-09 19:00:00,369 MainThread Tested sequences: 58391
    2020-05-09 19:00:00,371 MainThread Start trying pieces sequence. (all orientations, top left to bottom right): [2, 3, 11, 6, 1, 4, 7, 5, 10, 9, 8]
    2020-05-09 19:00:00,561 MainThread We have a winner!
    2020-05-09 19:00:00,561 MainThread [(2, 3), (3, 5), (11, 3), (6, 0), (1, 1), (4, 4), (7, 0), (5, 0), (10, 2), (9, 5), (8, 10)]
    Traceback (most recent call last):
    File "puzzle_haley.py", line 542, in <module>
        pieces_tested, longest_state = solver.analyse_randomize_sequence_of_pieces(board, pieces_with_orientations, pieces_to_try_indeces)
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 74, in analyse_randomize_sequence_of_pieces
        pieces_tested, longest_state = self.analyse_sequence_of_pieces(board, pieces_with_orientations, pieces_indeces, show_longest_state=False)
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 85, in analyse_sequence_of_pieces
        success, state = self.try_sequence_recursive(try_board, sequence_of_pieces_indeces_to_try, pieces_with_orientations, state)
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 154, in try_sequence_recursive
        success, return_state = self.try_sequence_recursive(result_board, try_sequence_of_pieces_index, pieces_with_orientations, new_state , last_tried=None)
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 154, in try_sequence_recursive
        success, return_state = self.try_sequence_recursive(result_board, try_sequence_of_pieces_index, pieces_with_orientations, new_state , last_tried=None)
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 154, in try_sequence_recursive
        success, return_state = self.try_sequence_recursive(result_board, try_sequence_of_pieces_index, pieces_with_orientations, new_state , last_tried=None)
    [Previous line repeated 8 more times]
    File "C:\Data\GIT\triangular_puzzle_haley\triangular_puzzle_solver.py", line 115, in try_sequence_recursive
        raise
    RuntimeError: No active exception to reraise

    C:\Data\GIT\triangular_puzzle_haley>
    '''
    logger = logger_setup()
    solver = triangular_puzzle_solver.PuzzleSolver(logger)

    starting_puzzle_boards = prepare_base_boards_with_hexagon(
        pieces_with_orientations[0][0], base_board)
    board = starting_puzzle_boards[0]

    show_board = copy.deepcopy(board)
    winning_states = [
        [(2, 3), (3, 5), (11, 3), (6, 0), (1, 1), (4, 4), (7, 0), (5, 0),
         (10, 2), (9, 5),
         (8, 10)],  # 2020-05-09 after 67000 random sequences tries
        [(8, 6), (3, 5), (6, 0), (10, 11), (4, 5), (2, 4), (7, 9), (11, 3),
         (1, 0), (9, 3), (5, 0)],  # 2020-05-25 245000 attempts  
        [(2, 3), (10, 0), (11, 3), (6, 0), (8, 5), (9, 1), (5, 0), (1, 2),
         (7, 2), (4, 2), (3, 3)],  # 2020-05-25  322000 attempts 
        [(7, 6), (2, 0), (3, 5), (11, 1), (8, 4), (1, 0), (10, 7), (6, 3),
         (4, 0), (9, 3), (5, 0)],  # 2020-05-26 535000 attempts
        # [(,), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, )],
    ]

    solver.build_up_state(show_board, pieces_with_orientations,
                          winning_states[2], True)
Пример #5
0
    def __init__(self,
                 database_path,
                 board,
                 pieces_with_orientations,
                 index_per_piece_with_orientation,
                 logger=None):
        # we will not do any flipping or rotating. So provide all wanted symmetries.
        # index_per_piece_with_orientation --> used pieces index with orientation .(list of tuples)
        self.board = board
        self.pieces_with_orientations = pieces_with_orientations
        self.index_per_piece_with_orientation = index_per_piece_with_orientation
        self.logger = logger or logging.getLogger(__name__)

        self.solver = triangular_puzzle_solver.PuzzleSolver(self.logger)
        self.logger.info("Init environment for fitting pieces in board.")
        self.solver_db = solver_database_level_build_up.HaleyPuzzleBuildUpDatabase(
            database_path)
Пример #6
0
def prepare_base_boards_with_hexagon(hexagon, base_board):
    solver = triangular_puzzle_solver.PuzzleSolver()
    hexagon_translations_for_all_possible_positions_on_board = [
        [
            ("E", 3),
        ],
        [("E", 3), ("SE", 1)],
        [("E", 3), ("SE", 2)],
        [("E", 2), ("SE", 2)],
        [("E", 1), ("SE", 3)],
    ]

    # prepare base board
    puzzle_board = solver.create_empty_base_board(8, 13, base_board)

    # get all base boards.
    starting_puzzle_boards = solver.prepare_base_boards(
        puzzle_board, hexagon,
        hexagon_translations_for_all_possible_positions_on_board)

    # for board in starting_puzzle_boards:
    #     print(str(board))

    return starting_puzzle_boards
Пример #7
0
def test_manually(pieces_with_orientations):

    logger = logger_setup()
    solver = triangular_puzzle_solver.PuzzleSolver(logger)
    logger.info("start Haley puzzle solving.")

    # only do if not hardcoded.
    if GENERATE_PIECES:
        pieces_with_orientations, pieces_orientations_per_piece = prepare_puzzle_pieces(
            base_pieces_patterns)

    starting_puzzle_boards = prepare_base_boards_with_hexagon(
        pieces_with_orientations[0][0], base_board)

    board = starting_puzzle_boards[1]
    # seq = [(3,3)]

    success = solver.try_piece_on_board(board, pieces_with_orientations[3][3])
    success = solver.try_piece_on_board(board, pieces_with_orientations[8][11])

    if not success:
        print("Problemski!")

    print(board)
Пример #8
0
def try_sequences_for_all_boards(boards, sequences_to_try=None):
    # if no sequence to try provide (list of pieces to try in right sequence), a random one will be generated.
    solver = triangular_puzzle_solver.PuzzleSolver()
    tested_sequences = 0
    pieces_tested_count = 0
    for sequence_to_try in sequences_to_try:
        tested_sequences += 1
        try:
            pieces_tested, longest_state, board_index = solver.analyse_sequence_of_pieces_on_multiple_boards(
                boards, pieces_with_orientations, sequence_to_try)

            pieces_tested_count += pieces_tested

        except triangular_puzzle_solver.WinningSolutionFoundException as e:

            with open(r"D:\Temp\puzzle_haley\tmptmp.txt", "a") as f:
                f.write("{}\n".format(str(sequence_to_try)))

        # if len(longest_state) >= 11:
        #     logger.critical("winning sequence: {}".format(longest_state))

        logger.info('Tested sequences: {}, total pieces tested: {}'.format(
            tested_sequences, pieces_tested_count))
    return tested_sequences, pieces_tested_count