添加 PPO 算法实现及相关配置,更新训练入口以支持 SAC 和 PPO 模式

This commit is contained in:
2026-04-04 17:50:02 +08:00
parent f3d2d1a85f
commit a2ce5073c5
10 changed files with 675 additions and 73 deletions
+8 -3
View File
@@ -23,6 +23,11 @@ def evaluate_and_plot(model_path):
config = yaml.safe_load(f)
env = gym.make('Pendulum-v1')
if not isinstance(env.observation_space, gym.spaces.Box) or env.observation_space.shape is None:
raise TypeError("Pendulum-v1 observation_space must be a Box with a valid shape.")
if not isinstance(env.action_space, gym.spaces.Box) or env.action_space.shape is None:
raise TypeError("Pendulum-v1 action_space must be a Box with a valid shape.")
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
max_action = float(env.action_space.high[0])
@@ -47,9 +52,9 @@ def evaluate_and_plot(model_path):
actions = []
time_steps = []
episode_reward = 0
episode_reward: float = 0.0
for step in range(config['max_steps']):
# 【重点】:设置 evaluate=True,让网络输出确定的均值动作,关闭随机探索
# 设置 evaluate=True,让网络输出确定的均值动作,关闭随机探索
action = agent.select_action(state, evaluate=True)
# 记录当前步的数据
@@ -66,7 +71,7 @@ def evaluate_and_plot(model_path):
# 与环境交互
next_state, reward, terminated, truncated, _ = env.step(action)
state = next_state
episode_reward += reward
episode_reward += float(reward)
if terminated or truncated:
break