Views
No views yet
Blackjack-v11from huggingface_hub import hf_hub_download
2import pickle
3import gymnasium as gym
4import numpy as np
5
6# 请将下面的占位符替换为你的实际仓库信息
7repo_id = "YOUR_USERNAME/YOUR_REPO_NAME" # 替换为你的仓库
8filename = "q-learning.pkl"
9
10# 加载模型
11model_path = hf_hub_download(repo_id=repo_id, filename=filename)
12
13with open(model_path, "rb") as f:
14 model = pickle.load(f)
15
16# 重建环境
17env = gym.make(
18 model["env_id"],
19 render_mode="rgb_array",
20 **model.get("env_config", {})
21)
22
23# 使用Q表进行推理
24qtable = model["qtable"]
25
26# 简单的推理示例
27state = env.reset()
28terminated = False
29while not terminated:
30# 状态转换为索引
31if isinstance(state, tuple):
32 state_idx = model.get("state_to_index", lambda s: s)(state)
33else:
34 state_idx = state
35
36action = np.argmax(qtable[state_idx])
37state, reward, terminated, truncated, _ = env.step(action)