def minimax(self, side, board, depth, max_depth): mov_gen = shogi.Rule().gen_moves ev = self.eval if depth == max_depth: result = self.lookup.get(board.__hash__(), None) if result is None: self.lookup[board.__hash__()] = ev(board) return self.lookup[board.__hash__()] else: if side == 1: best = -1000 moves = mov_gen(side, board) if len(moves) == 0: return -1000 for b in moves: now = self.minimax(-side, b, depth + 1, max_depth) if now > best: best = now if depth == 0: self.result = b return best else: best = 1000 moves = mov_gen(side, board) if len(moves) == 0: return 1000 for b in moves: now = self.minimax(-side, b, depth + 1, max_depth) if now < best: best = now if depth == 0: self.result = b return best
def make_move(self, side, board): mov_gen = shogi.Rule().gen_moves moves = mov_gen(side, board) if len(moves) == 0: return None q = [] for b in moves: q.append(model.predict(b.vectorize().reshape((1, 13 * 11)))) if side == 1: return moves[q.index(max(q))] if side == -1: return moves[q.index(min(q))]
hidden_size = 512 batch_size = 50 model = Sequential() model.add(Dense(hidden_size, input_shape=(13 * 11, ), activation='relu')) model.add(Dense(hidden_size, activation='relu')) model.add(Dense(hidden_size, activation='relu')) model.add(Dense(hidden_size, activation='relu')) model.add(Dense(1)) model.compile(Adam(lr=.2), "mse") # If you want to continue training from a previous model, just uncomment the line bellow # model.load_weights("manmodel.h5") # Define environment/game r = shogi.Rule() # Initialize experience replay object exp_replay = ExperienceReplay(max_memory=max_memory) # Train win_cnt = 0 for e in range(epoch): loss = 0. board = shogi.Board([[-2, -1, -3], [0, -4, 0], [0, 4, 0], [3, 1, 2]], []) game_over = False # get initial input input_t = board while not game_over:
def make_move(self, side, board): randint = random.randint mov_gen = shogi.Rule().gen_moves l = mov_gen(side, board) return None if len(l) == 0 else l[randint(0, len(l) - 1)]