当我试图弄清楚植绒环境的重置条件(来自gym-flock)时,我想出了这个问题:'return False'可以以某种方式返回True吗?
核心代码是:
1:https://github.com/katetolstaya/multiagent_gnn_policies#available-algorithms 中的 test_model.py
def test(args, actor_path, render=True):
# initialize gym env
env_name = args.get('env')
env = gym.make(env_name)
if isinstance(env.env, gym_flock.envs.FlockingRelativeEnv):
env.env.params_from_cfg(args)
# use seed
seed = args.getint('seed')
env.seed(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
# initialize params tuple
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
learner = DAGGER(device, args)
n_test_episodes = args.getint('n_test_episodes')
learner.load_model(actor_path, device)
**for _ in range(n_test_episodes):
episode_reward = 0
state = MultiAgentStateWithDelay(device, args, env.reset(), prev_state=None)
done = False
while not done:
action = learner.select_action(state)
next_state, reward, done, _ = env.step(action.cpu().numpy())
next_state = MultiAgentStateWithDelay(device, args, next_state, prev_state=state)
episode_reward += reward
state = next_state
if render:
env.render()
print(episode_reward)
env.close()**
2:健身房环境代码:https://github.com/katetolstaya/gym-flock/tree/stable/gym_flock/envs/flocking中的 flocking_relative.py
def step(self, u):
#u = np.reshape(u, (-1, 2))
assert u.shape == (self.n_agents, self.nu)
#u = np.clip(u, a_min=-self.max_accel, a_max=self.max_accel)
self.u = u * self.action_scalar
# x position
self.x[:, 0] = self.x[:, 0] + self.x[:, 2] * self.dt + self.u[:, 0] * self.dt * self.dt * 0.5
# y position
self.x[:, 1] = self.x[:, 1] + self.x[:, 3] * self.dt + self.u[:, 1] * self.dt * self.dt * 0.5
# x velocity
self.x[:, 2] = self.x[:, 2] + self.u[:, 0] * self.dt
# y velocity
self.x[:, 3] = self.x[:, 3] + self.u[:, 1] * self.dt
self.compute_helpers()
return (self.state_values, self.state_network), self.instant_cost(), **False**, {}
对于 test_model.py 中的 while 循环来中断和重置 env,done 在某些时候应该是 True。但是,env.step(code part 2) 中的代码总是返回 False 来代替 done。
当 env.step 总是返回 False 时,这个循环如何中断?我已经测试并确认此代码工作正常,但很难理解如何。
请在RL和健身房有经验的人帮助我提前非常感谢