def go_cmp(binary=False): b = board if not b.turn: b = b.mirror() legal_moves = list(b.legal_moves) best_move = legal_moves[0] for _ in range(5): for mv in legal_moves: if mv is best_move: continue b.push(best_move) best = util.state_to_cnn(util.board_to_state(b)) b.pop() b.push(mv) comp = util.state_to_cnn(util.board_to_state(b)) b.pop() with torch.no_grad(): model.train() t = torch.tensor(np.array([best, comp]), dtype=torch.float).to(device) x = model(t).detach().cpu() if not binary and x[0].argmax() < 10: best_move = mv elif binary and x[0] > 0: best_move = mv uci = best_move.uci() if not board.turn: uci = util.flip_uci(uci) print("bestmove " + uci)
def go_state(mode='buckets'): global board b = board if not board.turn: b = board.mirror() moves = list(b.legal_moves) cnn = [] for mv in moves: b.push(mv) state = util.board_to_state(b) cnn.append(util.state_to_cnn(state)) b.pop() model.train() with torch.no_grad(): y = model(torch.tensor(cnn, dtype=torch.float, device=device)).detach() idx = 0 if mode is 'buckets': idx = y.argmax(dim=1).argmax(dim=0).cpu().numpy() elif mode is 'value': idx = y.argmax().cpu().numpy() uci = moves[idx].uci() if not board.turn: uci = util.flip_uci(uci) print("bestmove " + uci)
def precompute(self): with open("data/depth18_gamma0.200000/moves_%s.csv" % (self.mode), "r") as csv_file: r = csv.reader(csv_file) self.n_items = 0 self.cnn = [] self.cp_loss = [] self.mask = [] self.legal_mask = [] self.num_of_splits = 0 print('Loading data...') for fen_without_count, dstr in progressbar.progressbar(r): cpdict = ast.literal_eval(dstr) fen = fen_without_count + " 0 1" board = chess.Board(fen=fen) state = util.board_to_state(board) legal_mask = util.movelist_to_actionmask(board.legal_moves) cnn = util.state_to_cnn(state) cp_loss, mask = util.cpdict_to_loss_mask(cpdict) self.cnn.append(cnn) self.cp_loss.append(cp_loss) self.mask.append(mask) self.legal_mask.append(legal_mask) self.n_items += 1 if self.n_items % 10000 == 0: self.save_precomputed() self.save_precomputed()
def random_move(boards, p_dict): cnn_t = torch.zeros(n_par_games, 17, 8, 8, dtype=torch.float, device=device) mask_t = torch.zeros(n_par_games, 64 * 64, dtype=torch.float, device=device) fen_without_count = [None for _ in boards] for i, b in enumerate(boards): turn = b.turn #p = None #fen_without_count[i] = ' '.join(board.fen().split()[:-2]) #if fen_without_count[i] in p_dict.keys(): # p = p_dict[fen_without_count[i]] if not turn: b = b.mirror() state = util.board_to_state(b) cnn = util.state_to_cnn(state) cnn_t[i] = torch.tensor(cnn).to(device) mask_t[i] = torch.tensor(util.movelist_to_actionmask(b.legal_moves), dtype=torch.float, device=device) y = model(cnn_t).detach() y_masked = torch.nn.functional.softmax(y, dim=1) * mask_t y_masked = torch.nn.functional.normalize(y_masked, p=1) p = y_masked.cpu().numpy() ucis = [] for i, b in enumerate(boards): #p_dict[fen_without_count[i]] = p[i] idx = np.random.choice(64 * 64, p=p[i]) uci = util.idx_to_uci(idx) if not b.turn: uci = util.flip_uci(uci) try: b.push_uci(uci) b.pop() except: uci += np.random.choice(['q', 'r', 'b', 'n']) ucis.append(uci) return ucis
def __init__(self): super(ChessMoveDataset_cp,self).__init__() with open("data/depth18_gamma0.200000/moves.csv", "r") as csv_file: r = csv.reader(csv_file) data = [] print('Loading data...') for fen_without_count,dstr in progressbar.progressbar(r): cpdict = ast.literal_eval(dstr) fen = fen_without_count + " 0 1" state = util.board_to_state(chess.Board(fen=fen)) cnn = util.state_to_cnn(state) cp_loss,mask = util.cpdict_to_loss_mask(cpdict) data.append((cnn,cp_loss,mask)) self.data = np.array(data)
def __iter__(self): it = None worker_info = torch.utils.data.get_worker_info() if worker_info is None: it = range(0,self.num_of_splits,1) else: worker_id = worker_info.id num_workers = worker_info.num_workers it = range(worker_id, self.num_of_splits, num_workers) for idx in it: b = np.load('data/pre_pov/boards_%s_%d.npy'%(self.mode,idx)) m = np.load('data/pre_pov/moves_%s_%d.npy'%(self.mode,idx)) for j in range(len(b)): yield torch.tensor(util.state_to_cnn(b[j]),dtype=torch.float) ,torch.tensor(m[j], dtype=torch.long)
def precompute(self): with open("data/depth18_gamma0.200000/moves_statevalue_%s.csv"%(self.mode), "r") as csv_file: r = csv.reader(csv_file) self.n_items = 0 self.cnn = [] self.values = [] self.num_of_splits = 0 print('Loading data...') for fen,value in progressbar.progressbar(r): board = chess.Board(fen=fen) state = util.board_to_state(board) cnn = util.state_to_cnn(state) self.cnn.append(cnn) self.values.append(int(value)) self.n_items += 1 if self.n_items % 10000 == 0: self.save_precomputed() self.save_precomputed()
def go(): global board b = board if not board.turn: b = board.mirror() state = util.board_to_state(b) cnn = util.state_to_cnn(state) model.train() with torch.no_grad(): d = {} for _ in range(100): y = model( torch.tensor(cnn, dtype=torch.float, device=device).unsqueeze(0)).detach() y = y - y.min() y_masked = y.cpu().numpy() * util.movelist_to_actionmask( b.legal_moves) uci = util.action_to_uci(y_masked) if not board.turn: uci = util.flip_uci(uci) try: board.push_uci(uci) board.pop() except: uci += 'q' if uci not in d.keys(): d[uci] = 1 else: d[uci] += 1 maxcount, bestuci = 0, None for uci, count in d.items(): if maxcount < count: bestuci = uci maxcount = count print("bestmove " + uci)