File size: 8,302 Bytes
c483ec7
 
 
 
 
 
 
a8711ff
 
c483ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8711ff
c483ec7
a8711ff
 
c483ec7
a8711ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c483ec7
a8711ff
 
 
 
 
 
 
 
 
 
 
c483ec7
a8711ff
c483ec7
a8711ff
 
 
 
 
 
 
c483ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import torch
import torch.nn as nn
import torch.optim as optim
import chess
import os
import chess.engine as eng
import torch.multiprocessing as mp
import random
from pathlib import Path

# CONFIGURATION
CONFIG = {
    "stockfish_path": "/Users/aaronvattay/Downloads/stockfish/stockfish-macos-m1-apple-silicon",
    "model_path": "chessy_model.pth",
    "backup_model_path": "chessy_modelt-1.pth",
    "device": torch.device("mps"),
    "learning_rate": 1e-4,
    "num_games": 30,
    "num_epochs": 10,
    "stockfish_time_limit": 1.0,
    "search_depth": 1,
    "epsilon": 4
}

device = CONFIG["device"]

def board_to_tensor(board):
    piece_encoding = {
        'P': 1, 'N': 2, 'B': 3, 'R': 4, 'Q': 5, 'K': 6,
        'p': 7, 'n': 8, 'b': 9, 'r': 10, 'q': 11, 'k': 12
    }

    tensor = torch.zeros(64, dtype=torch.long)
    for square in chess.SQUARES:
        piece = board.piece_at(square)
        if piece:
            tensor[square] = piece_encoding[piece.symbol()]
        else:
            tensor[square] = 0

    return tensor.unsqueeze(0)

class NN1(nn.Module):
    def __init__(self):
        super().__init__()
        self.embedding = nn.Embedding(13, 64)
        self.attention = nn.MultiheadAttention(embed_dim=64, num_heads=16)
        self.neu = 512
        self.neurons = nn.Sequential(
            nn.Linear(4096, self.neu),
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, self.neu), 
            nn.ReLU(),
            nn.Linear(self.neu, 64), 
            nn.ReLU(),
            nn.Linear(64, 4)
        )

    def forward(self, x):
        x = self.embedding(x)
        x = x.permute(1, 0, 2)
        attn_output, _ = self.attention(x, x, x)
        x = attn_output.permute(1, 0, 2).contiguous()
        x = x.view(x.size(0), -1)
        x = self.neurons(x)
        return x

model = NN1().to(device)
optimizer = optim.Adam(model.parameters(), lr=CONFIG["learning_rate"])

try:
    model.load_state_dict(torch.load(CONFIG["model_path"], map_location=device))
    print(f"Loaded model from {CONFIG['model_path']}")
except FileNotFoundError:
    try:
        model.load_state_dict(torch.load(CONFIG["backup_model_path"], map_location=device))
        print(f"Loaded backup model from {CONFIG['backup_model_path']}")
    except FileNotFoundError:
        print("No model file found, starting from scratch.")

model.train()
criterion = nn.MSELoss()
engine = eng.SimpleEngine.popen_uci(CONFIG["stockfish_path"])
lim = eng.Limit(time=CONFIG["stockfish_time_limit"])

def get_evaluation(board):
    """
    Returns the evaluation of the board from the perspective of the current player.
    The model's output is from White's perspective.
    """
    tensor = board_to_tensor(board).to(device)
    with torch.no_grad():
        evaluation = model(tensor)[0][0].item()
    
    if board.turn == chess.WHITE:
        return evaluation
    else:
        return -evaluation

def search(board, depth, simulations=100):
    """
    Monte Carlo Tree Search with basic negamax evaluation.
    Reads moves from san_moves.txt and picks the best move.
    """
    # Load predefined moves from SAN file
    san_file_path = Path("ChessEnv/san_moves.txt")
    if san_file_path.exists():
        with open(san_file_path, "r") as f:
            san_moves = [line.strip() for line in f if line.strip()]
    else:
        san_moves = []

    def negamax(board, depth, alpha, beta):
        if depth == 0 or board.is_game_over():
            return get_evaluation(board)

        max_eval = float('-inf')
        for move in board.legal_moves:
            board.push(move)
            eval = -negamax(board, depth - 1, -beta, -alpha)
            board.pop()
            max_eval = max(max_eval, eval)
            alpha = max(alpha, eval)
            if alpha >= beta:
                break
        return max_eval

    move_scores = {}

    for _ in range(simulations):
        # Optionally start from a random legal move (Monte Carlo rollout)
        move = random.choice(list(board.legal_moves))
        
        # If san_moves.txt is not empty, prefer moves from it when possible
        move_san = board.san(move)
        if san_moves and move_san in san_moves:
            move = chess.Move.from_uci(board.parse_san(move_san).uci())
        
        board.push(move)
        score = -negamax(board, depth - 1, -float('inf'), float('inf'))
        board.pop()

        move_scores[move] = move_scores.get(move, 0) + score

    # Pick move with highest average score
    best_move = max(move_scores.items(), key=lambda x: x[1] / simulations)[0]
    return best_move



    

def game_gen(engine_side):
    data = []
    mc = 0
    board = chess.Board()
    while not board.is_game_over():
        is_bot_turn = board.turn != engine_side
        
        if is_bot_turn:
            evaling = {}
            for move in board.legal_moves:
                board.push(move)
                evaling[move] = -search(board, depth=CONFIG["search_depth"], alpha=float('-inf'), beta=float('inf'))
                board.pop()
            
            if not evaling:
                break
            
            keys = list(evaling.keys())
            logits = torch.tensor(list(evaling.values())).to(device)
            probs = torch.softmax(logits,dim=0)
            epsilon = min(CONFIG["epsilon"],len(keys))
            bests = torch.multinomial(probs,num_samples=epsilon,replacement=False)
            best_idx = bests[torch.argmax(logits[bests])]
            move = keys[best_idx.item()]
            
        else:
            result = engine.play(board, lim)
            move = result.move

        if is_bot_turn:
            data.append({
                'fen': board.fen(),
                'move_number': mc,
            })

        board.push(move)
        mc += 1

    result = board.result()
    c = 0
    if result == '1-0':
        c = 10.0
    elif result == '0-1':
        c = -10.0
    return data, c, mc
def train(data, c, mc):
    for entry in data:
            tensor = board_to_tensor(chess.Board(entry['fen'])).to(device)
            target = torch.tensor(c * entry['move_number'] / mc, dtype=torch.float32).to(device)
            output = model(tensor)[0][0]
            loss = criterion(output, target)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        
    print(f"Saving model to {CONFIG['model_path']}")
    torch.save(model.state_dict(), CONFIG["model_path"])
    return
def main():
    for i in range(CONFIG["num_epochs"]):
     mp.set_start_method('spawn', force=True)
     num_games = CONFIG['num_games']
     num_instances = mp.cpu_count()
     print(f"Saving backup model to {CONFIG['backup_model_path']}")
     torch.save(model.state_dict(), CONFIG["backup_model_path"])
     with mp.Pool(processes=num_instances) as pool:
        results_self = pool.starmap(game_gen, [(None,) for _ in range(num_games // 3)])
        results_white = pool.starmap(game_gen, [(chess.WHITE,) for _ in range(num_games // 3)])
        results_black = pool.starmap(game_gen, [(chess.BLACK,) for _ in range(num_games // 3)])
        results = []
        for s, w, b in zip(results_self, results_white, results_black):
         results.extend([s, w, b])
        for batch in results:
            data, c, mc = batch
            print(f"Saving backup model to {CONFIG['backup_model_path']}")
            torch.save(model.state_dict(), CONFIG["backup_model_path"])
            if data:
                train(data, c, mc)
    print("Training complete.")
    engine.quit()
if __name__ == "__main__":
 main()