Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPO-early-stop: finish the episode after 50 steps if avg reward is negative #51

Open
wants to merge 9 commits into
base: ppo-nm
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Early stop: prints, render, and fixup
xeviknal committed Apr 10, 2021
commit 1648b186fd0c18e852fa4aaad6ab133c097c4f04
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#for concurrent runs and logging
experiment='ppo-nm-scheduler'
experiment='ppo-nm-scheduler-early-stop'
if __name__ == "__main__":
hyperparams = {
'num_epochs': 25000, # Number of training episodes
4 changes: 4 additions & 0 deletions trainer.py
Original file line number Diff line number Diff line change
@@ -52,16 +52,19 @@ def select_action(self, state):
# It prevents the model from picking always the same action
m = torch.distributions.Categorical(probs)
action = m.sample()
print(f'Action: {action.item()}')
# We return the state in order to make sure that we operate with a valid tensor
return action, m.log_prob(action), vs_t, m.entropy(), state

def run_episode(self, epoch, current_steps):
state, ep_reward, steps = self.env.reset(), 0, 0
for t in range(self.env.spec().max_episode_steps): # Protecting from scenarios where you are mostly stopped
with torch.no_grad():
self.env.render()
state = self.prepare_state(state)
action_id, action_log_prob, vs_t, entropy, state = self.select_action(state)
next_state, reward, done, _ = self.env.step(self.action_set[action_id.item()])
print(f'Reward: {reward}')
# Store transition to memory
self.memory.push(state, action_id, action_log_prob, entropy, reward, vs_t, next_state)

@@ -99,6 +102,7 @@ def get_new_prob_of(self, action, state):
return m.log_prob(action)

def policy_update(self, transitions, v_targ, adv, iteration):
print(f'Updating iteration #{iteration}')
# Get transitions values
batch = Transition(*zip(*transitions))
state_batch = torch.cat(batch.state)
5 changes: 2 additions & 3 deletions wrappers/early_stop.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ def step(self, action):
avg = 1
if self.remaining_steps == 0:
avg = np.array(self.latest_rewards).mean()
if avg > 0:
self.remaining_steps = self.steps
self.latest_rewards = []
self.remaining_steps = self.steps
self.latest_rewards = []
return state, reward, avg < 0, info