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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""
多进程并行训练环境
"""
import numpy as np
import torch
from typing import List, Tuple, Optional
from concurrent.futures import ThreadPoolExecutor
import threading
from game import Game2048
from trainer import Transition, TrainingStats
import time


class ParallelGameEnv:
    """
    并行游戏环境
    使用多线程同时运行多个游戏实例
    """
    
    def __init__(self, num_envs: int = 4):
        """
        初始化并行环境
        
        Args:
            num_envs: 并行游戏数量
        """
        self.num_envs = num_envs
        self.envs = [Game2048() for _ in range(num_envs)]
        self.states = [env.get_state() for env in self.envs]
        self.scores = [env.accumulated_score for env in self.envs]
        self.situational_scores = [env.situational_score for env in self.envs]
        
        # 游戏结束回调
        self.on_game_end = None
        
        # 已完成游戏计数
        self.games_completed = 0
        
        # 使用线程池
        self.executor = ThreadPoolExecutor(max_workers=num_envs)
    
    def reset(self, indices: Optional[List[int]] = None) -> Tuple[np.ndarray, np.ndarray]:
        """
        重置指定环境
        
        Args:
            indices: 要重置的环境索引,None表示全部重置
            
        Returns:
            states: (num_envs, 4, 4) 状态数组
            score_features: (num_envs, 2) 分数特征数组
        """
        if indices is None:
            indices = range(self.num_envs)
        
        for i in indices:
            self.states[i] = self.envs[i].reset()
            self.scores[i] = self.envs[i].accumulated_score
            self.situational_scores[i] = self.envs[i].situational_score
        
        return self._get_batch_state()
    
    def reset_single(self, idx: int) -> Tuple[np.ndarray, np.ndarray]:
        """重置单个环境"""
        self.states[idx] = self.envs[idx].reset()
        self.scores[idx] = self.envs[idx].accumulated_score
        self.situational_scores[idx] = self.envs[idx].situational_score
        return self.states[idx], self._get_score_features(idx)
    
    def step(self, actions: List[int]) -> List[Transition]:
        """
        并行执行动作
        
        Args:
            actions: 每个环境要执行的动作列表
            
        Returns:
            transitions: 状态转移列表
        """
        transitions = []
        
        for i, action in enumerate(actions):
            old_state = self.states[i].copy()
            old_scores = self._get_score_features(i)
            old_situational = self.situational_scores[i]
            
            # 执行动作
            new_state, reward, moved, done = self.envs[i].move(action)
            
            # 更新状态
            self.states[i] = new_state
            self.scores[i] = self.envs[i].accumulated_score
            self.situational_scores[i] = self.envs[i].situational_score
            
            # 创建转移记录
            transition = Transition(
                state=old_state,
                scores=old_scores,
                action=action,
                reward=reward,
                next_state=new_state.copy(),
                next_scores=self._get_score_features(i),
                done=done,
                log_prob=0.0,  # 需要在外部填充
                value=0.0,     # 需要在外部填充
                valid_actions=self.envs[i].get_valid_actions()
            )
            transitions.append(transition)
            
            # 如果游戏结束,记录统计并重置
            if done:
                # 记录游戏统计
                game_stats = {
                    'score': self.scores[i],
                    'situational_score': self.situational_scores[i],
                    'max_tile': self.envs[i].get_max_tile(),
                    'moves': self.envs[i].moves_count
                }
                self.games_completed += 1
                
                # 调用回调
                if self.on_game_end:
                    self.on_game_end(game_stats)
                
                self.reset_single(i)
        
        return transitions
    
    def _get_batch_state(self) -> Tuple[np.ndarray, np.ndarray]:
        """获取批量状态"""
        states = np.array(self.states, dtype=np.float32)
        score_features = np.array([
            self._get_score_features(i) for i in range(self.num_envs)
        ], dtype=np.float32)
        return states, score_features
    
    def _get_score_features(self, idx: int) -> np.ndarray:
        """获取单个环境的分数特征"""
        max_accumulated = 50000
        max_situational = 200
        return np.array([
            min(self.scores[idx] / max_accumulated, 1.0),
            min(self.situational_scores[idx] / max_situational, 1.0)
        ], dtype=np.float32)
    
    def get_valid_actions(self) -> np.ndarray:
        """获取所有环境的有效动作"""
        return np.array([env.get_valid_actions() for env in self.envs])
    
    def get_game_stats(self) -> List[dict]:
        """获取所有游戏的统计信息"""
        return [
            {
                'score': env.accumulated_score,
                'situational_score': env.situational_score,
                'max_tile': env.get_max_tile(),
                'moves': env.moves_count,
                'game_over': env.game_over
            }
            for env in self.envs
        ]
    
    def close(self):
        """关闭环境"""
        self.executor.shutdown(wait=False)


class TrainingWorker:
    """
    训练工作器
    负责收集轨迹数据
    """
    
    def __init__(
        self,
        model,
        env: ParallelGameEnv,
        device: str = "cpu"
    ):
        self.model = model.to(device)
        self.model.eval()
        self.env = env
        self.device = device
        
        self.stats = TrainingStats()
    
    def collect_trajectories(
        self,
        num_steps: int = 256,
        deterministic: bool = False
    ) -> List[Transition]:
        """
        收集轨迹数据
        
        Args:
            num_steps: 每个环境收集的步数
            deterministic: 是否确定性选择动作
            
        Returns:
            transitions: 收集的转移数据
        """
        all_transitions = []
        
        for _ in range(num_steps):
            # 获取当前状态
            states = np.array(self.env.states, dtype=np.float32)
            score_features = np.array([
                self.env._get_score_features(i) 
                for i in range(self.env.num_envs)
            ], dtype=np.float32)
            valid_actions = self.env.get_valid_actions()
            
            # 转换为张量
            states_t = torch.FloatTensor(states).to(self.device)
            scores_t = torch.FloatTensor(score_features).to(self.device)
            valid_t = torch.BoolTensor(valid_actions).to(self.device)
            
            # 选择动作
            actions = []
            log_probs = []
            values = []
            
            with torch.no_grad():
                for i in range(self.env.num_envs):
                    action, log_prob, value = self.model.get_action(
                        states_t[i:i+1],
                        scores_t[i:i+1],
                        valid_t[i:i+1],
                        deterministic=deterministic
                    )
                    actions.append(action)
                    log_probs.append(log_prob)
                    values.append(value)
            
            # 执行动作
            transitions = self.env.step(actions)
            
            # 填充log_prob和value
            for i, t in enumerate(transitions):
                t.log_prob = log_probs[i]
                t.value = values[i]
                all_transitions.append(t)
        
        return all_transitions
    
    def run_episode(
        self,
        deterministic: bool = True,
        max_steps: int = 10000
    ) -> dict:
        """
        运行一局演示游戏
        
        Args:
            deterministic: 是否确定性选择
            max_steps: 最大步数
            
        Returns:
            游戏统计信息
        """
        # 重置环境
        env = Game2048()
        state = env.reset()
        
        total_reward = 0
        steps = 0
        
        while not env.game_over and steps < max_steps:
            # 获取分数特征
            max_accumulated = 50000
            max_situational = 200
            scores = np.array([
                min(env.accumulated_score / max_accumulated, 1.0),
                min(env.situational_score / max_situational, 1.0)
            ], dtype=np.float32)
            
            # 获取有效动作
            valid_actions = env.get_valid_actions()
            
            # 转换为张量
            state_t = torch.FloatTensor(state).unsqueeze(0).to(self.device)
            scores_t = torch.FloatTensor(scores).unsqueeze(0).to(self.device)
            valid_t = torch.BoolTensor(valid_actions).unsqueeze(0).to(self.device)
            
            # 选择动作
            with torch.no_grad():
                action, _, _ = self.model.get_action(
                    state_t, scores_t, valid_t, deterministic=deterministic
                )
            
            # 执行动作
            state, reward, moved, done = env.move(action)
            total_reward += reward
            steps += 1
        
        return {
            'score': env.accumulated_score,
            'situational_score': env.situational_score,
            'max_tile': env.get_max_tile(),
            'steps': steps,
            'total_reward': total_reward
        }


class TrainingLoop:
    """
    完整的训练循环
    """
    
    def __init__(
        self,
        model,
        trainer,
        num_envs: int = 4,
        device: str = "cpu",
        steps_per_update: int = 256,
        save_interval: int = 100,
        checkpoint_dir: str = "checkpoints"
    ):
        self.model = model
        self.trainer = trainer
        self.num_envs = num_envs
        self.device = device
        self.steps_per_update = steps_per_update
        self.save_interval = save_interval
        self.checkpoint_dir = checkpoint_dir
        
        # 初始化环境和工作器
        self.env = ParallelGameEnv(num_envs=num_envs)
        self.worker = TrainingWorker(model, self.env, device)
        
        # 训练状态
        self.training = False
        self.paused = False
        self.stats = TrainingStats()
        
        # 回调函数
        self.on_update_callback = None
        self.on_game_end_callback = None
    
    def train(
        self,
        total_games: int = 10000,
        stop_threshold: int = 100,
        min_improvement: float = 0.01
    ) -> None:
        """
        训练循环
        
        Args:
            total_games: 总游戏局数
            stop_threshold: 无提升停止阈值(局数)
            min_improvement: 最小提升比例
        """
        self.training = True
        
        games_since_improvement = 0
        best_avg_score = 0
        
        # 设置游戏结束回调
        def on_game_end(game_stats):
            self.stats.record_game(
                score=game_stats['score'],
                situational_score=game_stats['situational_score'],
                max_tile=game_stats['max_tile'],
                steps=game_stats['moves']
            )
            if self.on_game_end_callback:
                self.on_game_end_callback(game_stats)
        
        self.env.on_game_end = on_game_end
        
        try:
            while self.training and self.env.games_completed < total_games:
                if self.paused:
                    time.sleep(0.1)
                    continue
                
                # 收集轨迹
                transitions = self.worker.collect_trajectories(
                    num_steps=self.steps_per_update // self.num_envs,
                    deterministic=False
                )
                
                # 更新模型
                from trainer import RolloutBuffer
                buffer = RolloutBuffer(capacity=len(transitions))
                buffer.push_batch(transitions)
                
                update_stats = self.trainer.update(buffer)
                
                if self.on_update_callback:
                    self.on_update_callback(update_stats)
                
                # 检查停止条件(每10次更新检查一次)
                if self.env.games_completed % 10 == 0 and self.env.games_completed > 0:
                    current_avg = self.stats.get_avg_stats(window=100)['avg_score']
                    if current_avg > best_avg_score * (1 + min_improvement):
                        best_avg_score = current_avg
                        games_since_improvement = 0
                    else:
                        games_since_improvement = self.env.games_completed - int(best_avg_score / 100 * 100) if best_avg_score > 0 else 0
                    
                    if games_since_improvement >= stop_threshold:
                        print(f"No improvement for {stop_threshold} updates, stopping.")
                        break
                
        except KeyboardInterrupt:
            print("Training interrupted by user.")
        finally:
            self.training = False
            self.env.close()
    
    def stop(self) -> None:
        """停止训练"""
        self.training = False
    
    def pause(self) -> None:
        """暂停训练"""
        self.paused = True
    
    def resume(self) -> None:
        """恢复训练"""
        self.paused = False
    
    def get_stats(self) -> dict:
        """获取当前统计信息"""
        return self.stats.get_avg_stats()
    
    def save_checkpoint(self, path: str) -> None:
        """保存模型检查点"""
        torch.save({
            'model_state_dict': self.model.state_dict(),
            'optimizer_state_dict': self.trainer.optimizer.state_dict(),
            'stats': self.stats.get_avg_stats(),
            'games_played': self.stats.games_played
        }, path)
    
    def load_checkpoint(self, path: str) -> None:
        """加载模型检查点"""
        checkpoint = torch.load(path, map_location=self.device)
        self.model.load_state_dict(checkpoint['model_state_dict'])
        self.trainer.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])


if __name__ == "__main__":
    from model import Game2048Transformer
    from trainer import PPOTrainer
    
    # 测试并行环境
    env = ParallelGameEnv(num_envs=4)
    env.reset()
    
    print("Testing parallel environment...")
    for i in range(10):
        actions = [np.random.randint(0, 4) for _ in range(env.num_envs)]
        transitions = env.step(actions)
        print(f"Step {i}: collected {len(transitions)} transitions")
    
    env.close()
    print("Parallel environment test passed!")