Esempio n. 1
0
    def order_moves_by_warnsdorff_tuples(self, board, move_tuples):
        """ Order moves by Warnsdorff algorithm (minimum neighbors) one level
        :board: current board
        :move_tuples: sorted list of 3-tuples
                        (number-of follow-on moves,
                        move,
                        list of this move's follow-on
                        )
        :returns: list with moves of decreasing number of follow-on's folow-on moves
                    of 3-tuples (number-of follow-on folow-on's moves,
                                 move,
                                 list of this move's follow-on's follow-ons
                                 )
        """
        ffon_tuples = [
        ]  # list of (follow-on's follow-on cnt, move, ffon_list)
        for move_tuple in move_tuples:
            follow_ons = move_tuple[2]
            follow_board = ChessBoard(board)
            follow_board.set_piece('N', move_tuple[1])
            follow_on_tuples = self.order_moves_by_warnsdorff_1(
                follow_board, follow_ons)
            ffon_moves = []
            for follow_on_tuple in follow_on_tuples:
                ffon_moves.extend(follow_on_tuple[2])
            ffon_tuples.append((len(ffon_moves), move_tuple[1], ffon_moves))

        ffon_tuples_sorted = sorted(ffon_tuples, key=by_first)
        return ffon_tuples_sorted
Esempio n. 2
0
def build_chess(size, pieces, verbose):
    width, height = size
    # assert len(pieces) <= (width * height) / 2
    board = ChessBoard(width, height, pieces, verbose=verbose)
    t = datetime.now()
    board.find_combinations()
    print(board.get_combinations_number(), datetime.now() - t)
 def __init__(self,
              locs=None,
              ncol=None,
              nrow=None,
              piece=None,
              closed_tours=True):
     """ Set up validation constraints on which validation is made
     :locs: squares for which possible tour default ncolxnrow
     :ncol: number of columns default: nrow if nrow is not None else 8
     :nrow: number of rows default: ncol if ncol is not None else 8
     :piece: Chess piece letter e.g. 'K' for black king default: 'N' - black knight
     :closed_tours: Check for closed tour (ending square is one piece move from start)
                     default: True
     """
     if ncol is None:
         ncol = 8 if nrow is None else nrow
     self.ncol = ncol
     if nrow is None:
         nrow = 8 if ncol is None else ncol
     self.nrow = nrow
     if locs is None:
         locs = []
         for ic in range(ncol):
             for ir in range(nrow):
                 locs.append((ic, ir))
     self.locs = locs
     if piece is None:
         piece = 'N'
     self.piece = piece
     self.closed_tours = closed_tours
     self.cb = ChessBoard(ncol=self.ncol,
                          nrow=self.nrow)  # For acces to basic fns
Esempio n. 4
0
def can_an_passant(piece, move, board: ChessBoard):
    left_neighbor_pos = Vec2(move.old.x - 1, move.old.y)
    right_neighbor_pos = Vec2(move.old.x + 1, move.old.y)

    global left_neighbor
    global right_neighbor

    if move.old.x != 0:
        left_neighbor = board.get_piece(left_neighbor_pos)
    if move.old.x != 7:
        right_neighbor = board.get_piece(right_neighbor_pos)

    if piece.team == 1:
        if move.old.y == 3:
            if left_neighbor is not None:
                if left_neighbor.name == "Pawn" and left_neighbor.move_counter == 1 and left_neighbor.team != piece.team:
                    an_passant(move, "left", board)
                    return True
            elif right_neighbor is not None:
                if right_neighbor.name == "Pawn" and right_neighbor.move_counter == 1 and right_neighbor.team != piece.team:
                    an_passant(move, "right", board)
                    return True

    elif piece.team == 2:
        if move.old.y == 4:
            if left_neighbor is not None:
                if left_neighbor.name == "Pawn" and left_neighbor.move_counter == 1 and left_neighbor.team != piece.team:
                    an_passant(move, "left", board)
                    return True
            elif right_neighbor is not None:
                if right_neighbor.name == "Pawn" and right_neighbor.move_counter == 1 and right_neighbor.team != piece.team:
                    an_passant(move, "right", board)
                    return True
    return False
Esempio n. 5
0
 def test_check_board_for_b(self):
     cells = {
         (0, 0): 'K',
         (0, 1): None,
         (1, 0): None,
         (1, 1): None,
     }
     board = ChessBoard(3, 3, [])
     self.assertIsNone(board.check_board_for_b(2, 2, cells))
     self.assertEqual(board.check_board_for_b(2, 1, cells), {(1, 2)})
     self.assertEqual(board.check_board_for_b(0, 2, cells), {(2, 0)})
Esempio n. 6
0
 def display_path(cls, path,
                  desc=None, nrow=8, ncol=8,
                  width=400, height=400):
     """ Display board with path squares labeled
     :desc: text description
     :path: list of squares in order
     returns ChssBoardDisplay of displayed board
     """
     if not hasattr(cls, 'wm'):
         cls.wm = Tk()
     wm_base = cls.wm
     wm = Toplevel(wm_base)
     wm_base.withdraw() 
     wm.geometry("%dx%d" % (width, height))
     frame = Frame(wm, width=width, height=height, bg="", colormap="new")
     frame.pack(fill=BOTH, expand=True)
     wm.title(desc) 
     
     #creation of an instance
     cb = ChessBoard(nrow=nrow, ncol=ncol)
     cbd = ChessBoardDisplay(wm=wm, frame=frame, board=cb, path=path,
                             desc=desc,
                             width=width, height=height, nrow=nrow, ncol=ncol)
     if path is None:
         SlTrace.lg("No path")
         return
         
     for loc in path:
         loc = cb.loc2tuple(loc)
         cbd.label_square(loc)
     cbd.display()
     wd = 7
     if len(path) == 0:
         return
     
     loc_start = path[0]
     sq1 = cbd.get_square(loc_start)
     cbd.draw_outline(sq1, color="green", width=wd)
     loc_end = path[-1]
     sq2 = cbd.get_square(loc_end)
     cbd.draw_outline(sq2, color="red", width=wd)
     if cb.is_neighbor(loc_end, loc_start):
         p1 = sq1.get_center()
         p2 = sq2.get_center()
         cbd.draw_line(p1,p2, color="blue", width=wd)
     prev_loc = None
     for loc in path:
         loc = cb.loc2tuple(loc)
         if prev_loc is not None:
             cbd.display_connected_moves(loc, prev_loc)
         prev_loc = loc
     wm.lift()
     cbd.update_display()
     return cbd
Esempio n. 7
0
 def __init__(self):
     self.board = ChessBoard(self)
     self.pieces = [Pawn(self, "white", item) for item in [i + "2" for i in "abcdefgh"]]
     self.pieces.extend([Pawn(self, "black", item) for item in [i + "7" for i in "abcdefgh"]])
     self.pieces.extend([Rook(self, "white", item) for item in ("a1", "h1")])
     self.pieces.extend([Rook(self, "black", item) for item in ("a8", "h8")])
     self.pieces.extend([Bishop(self, "white", item) for item in ("c1", "f1")])
     self.pieces.extend([Bishop(self, "black", item) for item in ("c8", "f8")])
     self.pieces.extend([Knight(self, "white", item) for item in ("b1", "g1")])
     self.pieces.extend([Knight(self, "black", item) for item in ("b8", "g8")])
     self.pieces.extend([King(self, "white", "e1"), King(self, "black", "e8"), Queen(self, "white", "d1"), Queen(self, "black", "d8")])
     self.players = []
Esempio n. 8
0
 def __init__(self, setup_pieces=False):
     self.board = ChessBoard()
     self.highness = None
     self.p1_king = None
     self.p2_king = None
     self.player_turn = 1
     self.other_player_turn = 2
     self.turn_counter = 0
     self.move_history = []
     self.player_1_color = "yellow"
     self.player_2_color = "magenta"
     if setup_pieces:
         self.setup_pieces()
Esempio n. 9
0
 def test_check_board_general(self):
     cells = {
         (0, 0): 'K',
         (0, 1): None,
         (1, 0): None,
         (1, 1): None,
     }
     board = ChessBoard(3, 3, [])
     br1 = board.check_board(1, 1, cells, 'R')
     br2 = board.check_board_for_r(1, 1, cells)
     bk1 = board.check_board(1, 1, cells, 'K')
     bk2 = board.check_board_for_k(1, 1, cells)
     self.assertEqual(br1, br2)
     self.assertEqual(bk1, bk2)
     self.assertNotEqual(br2, bk2)
Esempio n. 10
0
def prepare_sim_ann():
    global solver, board_data
    basic_prepare()
    prepare_generator()
    board = ChessBoard.load_board(board_data)
    solver = SolverModelFactory.create_model("simulated_annealing", board,
                                             generator)
Esempio n. 11
0
def test_decide_next_step():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)
    y, x = gm.decide_next_step()

    assert y == 2 and x == 4
Esempio n. 12
0
    def __init__(self):
        QMainWindow.__init__(self)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.graphicsView.setMouseTracking(True)
        self.filter = Filter()
        #self.ui.graphicsView.installEventFilter(self.filter)
        self.installEventFilter(self.filter)
        #### Black and White

        self.setCursor(Qt.PointingHandCursor)
        self.mouse_point = LaBel(self)  # 将鼠标图片改为棋子
        self.mouse_point.setScaledContents(True)
        self.mouse_point.setPixmap(
            self.ui.graphicsView.black)  # Black chess piece, human always hold
        self.mouse_point.setGeometry(270, 270, PIECE, PIECE)

        # settings for the mouse
        self.mouse_point.raise_()
        self.setMouseTracking(True)
        self.AI_down = True

        self.chess_board = ChessBoard()
        self.pieces = [LaBel(self) for i in range(361)
                       ]  # All the chess pieces , 19*19 chess board

        for piece in self.pieces:
            piece.setVisible(True)  # Set Picture Visible
            piece.setScaledContents(True)  #
Esempio n. 13
0
 def __init__(self, path_starts=None, arrange=None,
              time_out=None,
              closed_tours=None,
              display_move=False,
              pW=None,
              move_time=.5,
              width=400,
              height=400,
              nrow = 8,
              ncol = 8,
              max_look_ahead=5):
     self.display_move = display_move
     self.pW = pW
     self.move_time = move_time
     self.width = width
     self.height = height
     self.nrow = nrow
     self.ncol = ncol
     self.board = ChessBoard(ncol=ncol, nrow=nrow)
     self.len_ckt = nrow*ncol
     if display_move:
         time_out = None             # Disable time_out if displaying moves
     self.time_out = time_out
     self.path_starts = path_starts
     self.closed_tours = closed_tours
     self.max_look_ahead = max_look_ahead
     self.arrange = arrange
     self.sqno = 0        # number within list
     self.displayed_paths = []   # Repository of displayed paths
     self.kpths = None           # Current path, if any
     self.ipstart = 0            # current start loc index
Esempio n. 14
0
def test_constructor():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tile = Tile(cb, 'black')
    assert tile.color == 'black'
    assert tile.CLEARANCE == 10
    assert tile.width == 90
    assert tile.height == 90
Esempio n. 15
0
def test_constructor():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    assert gm.black_turn == True
    assert gm.WIDTH == 800
    assert gm.HEIGHT == 800

    for y in range(ROW_NUM):
        for x in range(ROW_NUM):
            if y == 3 and x == 3:
                assert gm.tile_list[y][x].color == 'white'
            elif y == 4 and x == 4:
                assert gm.tile_list[y][x].color == 'white'
            elif y == 4 and x == 3:
                assert gm.tile_list[y][x].color == 'black'
            elif y == 3 and x == 4:
                assert gm.tile_list[y][x].color == 'black'
            else:
                assert gm.tile_list[y][x] == None

    assert gm.black_count == 2
    assert gm.white_count == 2
    assert gm.X_ADD == [0, 1, -1, 0, 1, -1, 1, -1]
    assert gm.Y_ADD == [1, 0, 0, -1, 1, -1, -1, 1]
    assert gm.has_input_name == False
    assert gm.TIME_DURATION == 1500
    assert gm.FILE_NAME == 'scores.txt'
Esempio n. 16
0
def test_get_legal_move_list():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    white_move_list = gm.get_legal_move_list('black')

    assert white_move_list == {(2, 3): 1, (3, 2): 1, (4, 5): 1, (5, 4): 1}
Esempio n. 17
0
def test_black_make_move():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    gm.black_make_move(324, 234)
    assert gm.tile_list[2][3].color == 'black'
    assert gm.black_count == 4
Esempio n. 18
0
def test_white_make_move():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    gm.white_make_move()

    assert gm.tile_list[2][4].color == 'white'
Esempio n. 19
0
 def __init__(self,
             frame=None, wm=None,
             desc="",
             x=None, y=None,
             width=400, height=400,     # REQUIRED, if window not expanded
             board=None,
             move_time = .5,
             path=None,
             nrow=8,
             ncol=8,
             ):
     """ Set up board display
     :frame: if one
     :wm: if one
     :desc: description if one
     :x: board x position    default: tkinter default
     :y: board y position
     :width: board width default: 400 pixels
     :height: board height default: 400 pixels
     :board: ChessBoard default: created
     :path: path to display
     :nrow: number of rows default: 8
     :ncol: number of columns default: 8
     """
     self.label_number = 0               # Number for default square labeling
     self.desc = desc
     if board is None:
         board = ChessBoard(nrow=nrow, ncol=ncol)
     self.path = path                    # Path, if associated, may be placed here
     self.board = board    
     self.width = width
     self.height = height
     if board is not None:
         nrow = board.nrow
         ncol = board.ncol
     self.nrow = nrow
     self.ncol = ncol
     if wm is None:
         wm = Tk()
     self.wm = wm
     width = int(width)
     height = int(height)
     geo = f"{width}x{height}"
     if x is not None or y is not None:
         geo += f"+{x}+{y}"
     wm.geometry(geo)
     if frame is None:
         frame = Frame(master=wm, width=width, height=height, bg="", colormap="new")
         frame.pack(fill=BOTH, expand=YES)
     self.frame = frame
     select_dots = self.create_board(frame, wm, width=width, height=height,
                       nrow=nrow, ncol=ncol)
     self.select_dots = select_dots
     self.prev_loc = None                # previous move location for path viewing
     self.move_time = move_time
     self.display_move_stack = []        # (loc, line)
     self.display_move_no = 0
Esempio n. 20
0
 def test_check_board_for_n(self):
     cells_1 = {
         (0, 0): 'K',
         (0, 1): None,
         (1, 0): None,
         (1, 1): None,
     }
     cells_2 = {
         (2, 2): 'K',
         (1, 1): None,
         (1, 2): None,
         (2, 1): None,
     }
     board = ChessBoard(3, 3, [])
     self.assertIsNone(board.check_board_for_n(2, 1, cells_1))
     self.assertIsNone(board.check_board_for_n(1, 2, cells_1))
     self.assertIsNone(board.check_board_for_n(0, 1, cells_2))
     self.assertEqual(board.check_board_for_n(2, 0, cells_1), {(1, 2)})
Esempio n. 21
0
    def next_move(self):
        """ Execute the next (currenly set) move, updating the path stack
        and displaying move if appropriate
        :returns: True iff at end of path search
        """
        if self.closed_tours:
            self.prune_not_closed()
        stke = self.path_stack[-1]
        next_move = stke.loc
        board = stke.board
        best_moves = stke.best_moves
        SlTrace.lg(f"best_moves = {best_moves}", "stack_build")
        if best_moves is None:
            SlTrace.lg(f"best_moves tested is None", "stack_build")
            best_moves = self.get_best_moves(board, next_move)
            SlTrace.lg(f"best_moves = {best_moves}", "stack_build")
            SlTrace.lg(f"len(best_moves)={len(best_moves)}", "stack_build")
        if len(best_moves) == 0:
            SlTrace.lg("tested as 0", "stack_build")
            self.ntry += 1
            if SlTrace.trace("no_more_moves"):
                SlTrace.lg("{:d}: No more moves at {} len_stk={:d}".format(
                    self.ntry, loc2desc(next_move), len_stk))
            if self.max_try is not None and self.ntry > self.max_try:
                SlTrace.lg("Giving up this search")
                return True

            if SlTrace.trace("no_more_moves"):
                self.display_stack_path("no_more_moves")
            if (self.last_complete_path is None
                    or len(self.path_stack) > len(self.last_complete_path)):
                self.last_complete_path = self.path_stack_path()
            if len(self.path_stack) <= self.track_level:
                self.track_level = len(self.path_stack)
                if SlTrace.trace("back_off_trace"):
                    if self.track_level > 0:
                        stke = self.path_stack[-1]
                        nxt_move, board, bst_moves = stke.loc, stke.board, stke.best_moves
                        self.board = board
                        SlTrace.lg(
                            "back_off_trace stk_len={:d} start={} at {} best_moves={}"
                            .format(self.track_level, loc2desc(self.loc_start),
                                    loc2desc(nxt_move), path_desc(bst_moves)))
                        self.display_stack_path("back_off_trace")
            self.widen_search()
            return False  # Backup
        else:
            SlTrace.lg(f"len(best_moves)={len(best_moves)} failed test for 0",
                       "stack_build")

        follow_move = best_moves.pop(0)
        stke = self.path_stack[-1]
        stke.best_moves = best_moves  # Update best moves
        new_board = ChessBoard(base_board=board)
        self.make_move(loc=follow_move, board=new_board)
        return False
Esempio n. 22
0
def test_in_bound():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    assert gm.in_bound(4, 4) == True
    assert gm.in_bound(1, 8) == False
    assert gm.in_bound(2, -1) == False
    assert gm.in_bound(-1, 7) == False
    assert gm.in_bound(9, 3) == False
Esempio n. 23
0
def test_flip_color():
    # please comment the first line in flip_color
    # function. millis() is not a python statement that can
    # casue error in py test. it is basically a timer recording
    # time and will not effect the test result.
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    gm.flip_color('white', 3, 2)
    assert gm.tile_list[3][3].color == 'black'
Esempio n. 24
0
def can_castle(move, board: ChessBoard):
    king = board.get_piece(move.old)

    king.is_castling_move(move, board)

    if king.is_castling:

        if not board.is_path_clear(move):
            return False

        if king.has_moved:
            return False

        if king.team == 1:
            if move.new == Vec2(6, 7):
                if board.get_piece(Vec2(7, 7)) is not None:
                    if board.get_piece(Vec2(7, 7)).has_moved is False:
                        return True

            elif move.new == Vec2(2, 7):
                if board.get_piece(Vec2(0, 7)) is not None:
                    if board.get_piece(Vec2(0, 7)).has_moved is False:
                        return True

        else:
            if move.new == Vec2(6, 0):
                if board.get_piece(Vec2(7, 0)) is not None:
                    if board.get_piece(Vec2(7, 0)).has_moved is False:
                        return True

            elif move.new == Vec2(2, 0):
                if board.get_piece(Vec2(0, 0)) is not None:
                    if board.get_piece(Vec2(0, 0)).has_moved is False:
                        return True

        # if board.is_space_safe(move.old, king.team):
        #     return False

        return False

    return False
Esempio n. 25
0
def is_castling(move, board: ChessBoard):
    king = board.get_piece(move.old)

    if king.team == 1:
        if move.new == Vec2(6, 7) or move.new == Vec2(2, 7):
            return True

    else:
        if move.new == Vec2(6, 0) or move.new == Vec2(2, 0):
            return True

    return False
Esempio n. 26
0
def solve_many_boards(seed_list, dim_each = 8, n_moves = 50,
    verbose = False, stop_each = None):
    '''Given a list of queen positioning seeds, a size for each board, and 
    a maximum number of moves to attempt before giving up, run your 
    columnwise CSP eight queens solver on each board configuration. 

    Don't hate the playa, hate the game'''
    bcount = len(seed_list)
    if bcount > 1:
        print(f"Working on {bcount} test cases...")
    for i, s in enumerate(seed_list):
        if bcount > 1:
            print(f"Case {i+1} of {bcount} (seed {s})")
        game = ChessBoard(dimension = dim_each, queen_seed = s)
        player = MinConflictColumnSolver(board_object = game, max_moves = n_moves)
        player.solve(verbose = verbose, stop_each = stop_each)
        print(player.solution_shortdoc())
        print("-" * bcount, "\n")
Esempio n. 27
0
def solve_puzzle():
    '''Accessing args with [] indicates a required param (we throw a 
    400-BAD REQUEST if it isn't included).

    TODO A: Should I made the Bad Request case more explicit?
    TODO B: CORS????

    example usage: "http://localhost:5000/solve?dimension=8&state_string=1525384358627583"
    '''
    board_dim = int(request.args["dimension"])
    board_state = request.args["state"]
    move_arg = request.args.get("max_moves")
    max_moves = 50 if move_arg is None else int(move_arg)
    cboard = ChessBoard(dimension = board_dim, state_string = board_state)
    solver = MinConflictColumnSolver(board_object = cboard, max_moves = max_moves)
    solver.solve()
    rsp_data = solver.get_solution()
    rsp_headers = {"Content-Type":"application/json"}
    return make_response(rsp_data, 200, rsp_headers)
Esempio n. 28
0
def test_constructor():
    row_num = 4
    cb = ChessBoard(WIDTH, HEIGHT, row_num)
    tiles = Tiles(cb)
    tile_list = []

    for x in range(row_num):
        tile_list.append([])
        for y in range(row_num):

            if tiles.tile_list[y][x] == None:
                tile_list[x].append(None)
            else:
                tile_list[x].append(tiles.tile_list[y][x].color)

    assert tile_list == [[None, None, None, None],
                         [None, 'white', 'black', None],
                         [None, 'black', 'white', None],
                         [None, None, None, None]]
    def CnnVsMonteCarlo(self, nb_iter):
        """player 0 => play game using CNN
           player 1 => play game using random method

        """
        moves=[]
        winner=[]
        print('The number of iteration is %s' %(str(nb_iter)))
        for i in range(nb_iter):
            print(str(i) + " iterations is done")
            player=0
            board=ChessBoard(8).getChessBoard()
            list_best_move=[]
            while len(Playout().getPossibleMove(board, player))>0:
                # make sure which player
                # CNN player
                if player==0:
                    X=np.concatenate((board, 1-board, np.zeros((8,8), dtype=int)+player), axis=0).reshape(1,3,8,8)
                    y=self.cnn.predict(X).reshape(64)
                    # y=y_predict
                    is_finished=False
                                        
                    max_value=-1
                    counter=0
                    while is_finished==False & counter<64:
                        counter+=1
                        for i in range(64):
                            if y[i]>max_value:
                                max_value=y[i]
                                move=i
                        if move in Playout().getPossibleMove(board, player):
                            list_best_move.append(move)
                            board=Playout().play(board, move, player)
                            player=1-player
                            is_finished=True
                        else:
                            y[move]=0
                            max_value=-1
                elif player==1:
                    possible_moves=Playout().getPossibleMove(board, player)
                    
                    list_mean_playout=[]
                    list_move=[]
                    
                    for i in range(len(possible_moves)):
                        copy_board=np.copy(board)
                        copy_board=Playout().play(copy_board, possible_moves[i], player)
                        list_mean_playout.append(Playout().getMeanOfPlayout(copy_board, 1-player, 10))
                        list_move.append(possible_moves[i])
                    
                    best_playout=-1
                    for i in range(len(list_mean_playout)):
                        if list_mean_playout[i]>best_playout:
                            best_playout=list_mean_playout[i]
                            best_move=list_move[i]
                    
                    list_best_move.append(best_move)
                    board=Playout().play(board, best_move, player)
                    player=1-player

            moves.append(list_best_move)
            winner.append(1-player)
            
        return moves, winner
Esempio n. 30
0
from chess_board import ChessBoard
from stochastic_hill_c import stochastic_hill_climbing as shc

b = ChessBoard()  # criação do tabuleiro
print("ORIGINAL: ", b.fitness)
print(b)
b.queens = shc(b)  # aplicação do hill climbing
b.update_board()
print("ALTERADO: ", b.fitness)
print(b)
def main():
    # Argument variable defaults
    mutation_chance_def = 0.02
    children_per_gen_def = 400
    pop_size_def = 1000
    thread_count_def = 1
    n_queens_def = 8
    reset_def = 250

    # Setup argument variable parser
    parser = argparse.ArgumentParser(
        description=
        'Runs the n-queen problem with given values, or defaults in case of '
        'missing values')
    parser.add_argument(
        '-m',
        '--mutation_chance',
        help='the mutation chance to use, which defaults to {}'.format(
            mutation_chance_def))
    parser.add_argument(
        '-c',
        '--children_per_gen',
        help=
        'the number of children to create each generation, which must be less than half of the '
        'given population. Defaults to {}'.format(children_per_gen_def))
    parser.add_argument(
        '-p',
        '--pop_size',
        help='the size of populations to create, defaulting to {}'.format(
            pop_size_def))
    parser.add_argument(
        '-t',
        '--thread_count',
        help='the number of threads to create, defaulting to {}'.format(
            thread_count_def))
    parser.add_argument(
        '-n',
        '--n_queens',
        help='the number of queens in the problem, defaulting to {}'.format(
            n_queens_def))
    parser.add_argument(
        '-r',
        '--reset',
        help=
        'the number of generations to run in a population before resetting to a '
        'new population, defaulting to {}'.format(reset_def))

    # Parse argument variables
    args = parser.parse_args()
    # Set mutation chance
    if args.mutation_chance:
        try:
            mutation_chance = float(args.mutation_chance)
            if mutation_chance <= 0 or mutation_chance > 1:
                raise ValueError
        except ValueError:
            print(
                'mutation chance must be a float such that 0 < mutation chance <= 1'
            )
            exit(-1)
    else:
        mutation_chance = mutation_chance_def
    # Set population size
    if args.pop_size:
        try:
            pop_size = int(args.pop_size)
            if pop_size <= 1:
                raise ValueError
        except ValueError:
            print('population size must be greater than 1')
            exit(-2)
    else:
        pop_size = pop_size_def
    # Set children per gen
    if args.children_per_gen:
        try:
            children_per_gen = int(args.children_per_gen)
            if children_per_gen * 2 > pop_size or children_per_gen <= 0:
                raise ValueError
        except ValueError:
            print(
                'the number of children to generate must be less than half the population size and greater than 0'
            )
            exit(-3)
    else:
        children_per_gen = children_per_gen_def
    # Set dimension of the problem
    if args.n_queens:
        try:
            n_queens = int(args.n_queens)
            if n_queens <= 0:
                raise ValueError
        except ValueError:
            print(
                'the dimension of the problem must be greater than 0, and must be passed as an int'
            )
            exit(-4)
    else:
        n_queens = n_queens_def
    # Set the number of threads to use
    if args.thread_count:
        try:
            thread_count = int(args.thread_count)
            if thread_count <= 0:
                raise ValueError
        except ValueError:
            print('the thread count must be greater than 0')
            exit(-5)
    else:
        thread_count = thread_count_def
    if args.reset:
        try:
            reset = int(args.reset)
            if reset <= 0:
                raise ValueError
        except ValueError:
            print(
                'the number of generations to reset after must be greater than 0'
            )
            exit(-6)
    else:
        reset = reset_def

    # Setup static variables for the ChessBoard
    ChessBoard.setup_statics(dimensions=n_queens,
                             mutation_chance=mutation_chance)

    # Genetic algorithm starts now, so the timer should start
    start_time = time()

    # Create threads for the different populations equal to the number requested
    threads = []
    for i in range(thread_count):
        new_thread = Thread(target=run_genetic_algorithm,
                            args=(pop_size, children_per_gen, reset))
        # Start the thread, and add it to the thread list
        new_thread.start()
        threads.append(new_thread)

    # This loop will be exited after enough time has passed without a solution being found, terminating the
    # program
    finding_solutions = True
    while finding_solutions:
        # Save the size of the list to use to check if the solution list size has changed, to exit the program
        num_solutions = len(solutions)
        # Sleep to allow new updates
        sleep(120)
        # If the number of solutions hasn't increased, then end the program
        if num_solutions == len(solutions):
            finding_solutions = False

    # The program has finished
    print('The program found {} solutions, with these being the solutions:'.
          format(len(solutions)))
    for solution in solutions:
        print(solution)
    print('The program finished in approximately {} seconds'.format(
        last_solution - start_time))
    exit(0)
def run_genetic_algorithm(population_size, number_of_children, reset):
    """Runs a genetic algorithm.

    Args:
        population_size (int): The size of the population to create.
        number_of_children (int): The number of children to breed each round.
        reset (int): The number of generations to reset after.
    """

    # Loop endlessly
    while True:
        # Create the initial population
        population = []
        for i in range(population_size):
            population.append(ChessBoard())

        generation_num = 1

        # Loop until the reset number is reached
        while generation_num <= reset:
            # Sort population by fitness, with most fit candidates first in the list
            population.sort(reverse=True)
            # Check the list to see if any of the population is a solution
            for board in population:
                # Checks to see if the board is a solution
                if bool(board):
                    add_solution(board.chromosome, generation_num)
                # Breaks if the current board was not a solution, as nothing after it in the sorted list will be
                else:
                    break
            # Pick the parents for the next generation
            parents = []
            for i in range(number_of_children * 2):
                # Select the portion of the population to grab a parent from
                portion = np.random.choice([0, 1, 2, 3, 4], p=breed_weights)
                # Select the parent from the designated portion
                parent_index = np.random.choice(
                    range(int(portion * len(population) / 5),
                          int((portion + 1) * len(population) / 5)))
                # Parents are removed so they cannot be selected again, and will be added back later
                parents.append(population.pop(parent_index))

            # Breed new children with the parents
            children = []
            for i in range(number_of_children):
                # Create the new child
                children.append(
                    ChessBoard(parent1=parents[i],
                               parent2=parents[0 - (i + 1)]))

            # Add the parents back into the population
            population += parents

            # Delete a number of boards from the population equal to the number of children to be added from the bottom
            # half
            for i in range(number_of_children):
                population.pop(np.random.randint(-int(len(population) / 2),
                                                 -1))

            # Add the children to the population to replace the killed population
            population += children

            # Increase generation number
            generation_num += 1
Esempio n. 33
0
 def test_unique_result(self):
     board = ChessBoard(3, 3, ['K', 'K', 'R'])
     board.find_combinations()
     self.assertEqual(board.get_combinations_number(), 4)
Esempio n. 34
0
    def test_validation(self):
        self.assertEqual(ChessBoard.validation(1), 1)
        self.assertEqual(ChessBoard.validation(1.0), 1)
        self.assertEqual(ChessBoard.validation(-1), 1)

        self.assertRaises(TypeError, ChessBoard.validation, '1', 0)
Esempio n. 35
0
 def generate_population(self, orders):
     for _ in range(self.population_count):
         chess_board = ChessBoard.load_board(orders)
         individual = Individual(chess_board)
         self.population.append(individual)