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

Allow overwritting action by environment #56

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
8 changes: 6 additions & 2 deletions Master.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function Master:train()
-- Catch CTRL-C to save
self:catchSigInt()

local reward, state, terminal = 0, self.env:start(), false
local reward, state, terminal = 0, taken, self.env:start(), false

-- Set environment and agent to training mode
self.env:training()
Expand All @@ -97,7 +97,11 @@ function Master:train()
local action = self.agent:observe(reward, state, terminal) -- As results received, learn in training mode
if not terminal then
-- Act on environment (to cause transition)
reward, state, terminal = self.env:step(action)
reward, state, terminal, taken = self.env:step(action)
-- Update experience memory with actual action
if taken and taken ~= action then
self.agent.memory.actions[self.agent.memory.index] = taken
end
-- Track score
episodeScore = episodeScore + reward
else
Expand Down
4 changes: 2 additions & 2 deletions async/AsyncAgent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ end


function AsyncAgent:takeAction(action)
local reward, rawObservation, terminal = self.env:step(action - self.actionOffset)
local reward, rawObservation, terminal, taken = self.env:step(action - self.actionOffset)
if self.rewardClip > 0 then
reward = math.max(reward, -self.rewardClip)
reward = math.min(reward, self.rewardClip)
Expand All @@ -91,7 +91,7 @@ function AsyncAgent:takeAction(action)
self.stateBuffer:push(observation)
end

return reward, terminal, self.stateBuffer:readAll()
return reward, terminal, self.stateBuffer:readAll(), taken
end


Expand Down
7 changes: 6 additions & 1 deletion async/NStepQAgent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function NStepQAgent:learn(steps, from)
log.info('NStepQAgent starting | steps=%d | ε=%.2f -> %.2f', steps, self.epsilon, self.epsilonEnd)
local reward, terminal, state = self:start()

local taken

self.states:resize(self.batchSize, table.unpack(state:size():totable()))
self.tic = torch.tic()
repeat
Expand All @@ -44,7 +46,10 @@ function NStepQAgent:learn(steps, from)
local action = self:eGreedy(state, self.policyNet_)
self.actions[self.batchIdx] = action

reward, terminal, state = self:takeAction(action)
reward, terminal, state, taken = self:takeAction(action)
if taken and taken ~= action then
self.actions[self.batchIdx] = taken
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do it this way around, looking more like OneStepQAgent.

if taken and taken ~= action then
  action = taken
end
self.actions[self.batchIdx] = action

end
self.rewards[self.batchIdx] = reward

self:progress(steps)
Expand Down
7 changes: 5 additions & 2 deletions async/OneStepQAgent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ function OneStepQAgent:learn(steps, from)
log.info('%s starting | steps=%d | ε=%.2f -> %.2f', self.agentName, steps, self.epsilon, self.epsilonEnd)
local reward, terminal, state = self:start()

local action, state_
local action, state_, taken

self.tic = torch.tic()
for step1=1,steps do
if not terminal then
action = self:eGreedy(state, self.policyNet)
reward, terminal, state_ = self:takeAction(action)
reward, terminal, state_, taken = self:takeAction(action)
if taken and taken ~= action then
action = taken
end
else
reward, terminal, state_ = self:start()
end
Expand Down