Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
aleju committed May 25, 2016
1 parent 7069ae7 commit 8da4882
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 2,112 deletions.
3 changes: 3 additions & 0 deletions VAE.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- Old VAE stuff, ended up not being used.
-- Most of the code is adapted from https://github.com/y0ast/VAE-Torch .

require 'torch'
require 'nn'
require 'nngraph'
Expand Down
8 changes: 8 additions & 0 deletions action.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-- Object that represents an action.
-- Every action consists of two sub-actions:
-- arrowAction: The pressed arrow button (up, down, left, right).
-- buttonAction: The pressed "other" button (A, B, X, Y).
-- Both sub-actions are given by their action id (see actions.lua).
local Action = {}
Action.__index = Action

Expand All @@ -8,4 +13,7 @@ function Action.new(arrowAction, buttonAction)
return self
end

-- Objects here have no members functions, because those seemed to be gone
-- after torch.save() and then torch.load().

return Action
134 changes: 26 additions & 108 deletions actions.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local actions = {}

-- Action ids used by the emulator (?)
actions.ACTION_BUTTON_B = 0
actions.ACTION_BUTTON_Y = 1
actions.ACTION_BUTTON_SELECT = 2
Expand All @@ -12,6 +13,8 @@ actions.ACTION_BUTTON_A = 8
actions.ACTION_BUTTON_X = 9
actions.ACTION_BUTTON_L = 10
actions.ACTION_BUTTON_R = 11

-- List of all action ids.
actions.ACTIONS_ALL = {
actions.ACTION_BUTTON_B, actions.ACTION_BUTTON_Y,
actions.ACTION_BUTTON_SELECT, actions.ACTION_BUTTON_START,
Expand All @@ -20,27 +23,32 @@ actions.ACTIONS_ALL = {
actions.ACTION_BUTTON_A, actions.ACTION_BUTTON_X,
actions.ACTION_BUTTON_L, actions.ACTION_BUTTON_R
}
--[[actions.ACTIONS_NETWORK = {
actions.ACTION_BUTTON_B, --actions.ACTION_BUTTON_Y,
actions.ACTION_BUTTON_LEFT, actions.ACTION_BUTTON_RIGHT,
actions.ACTION_BUTTON_A, --actions.ACTION_BUTTON_X
}--]]

-- List of action ids that the network can use (i.e. for which it predicts rewards).
-- Note that the order is important, the first action id is the action that is
-- represented by the first output neuron of the network.
actions.ACTIONS_NETWORK = {
actions.ACTION_BUTTON_B, actions.ACTION_BUTTON_Y,
actions.ACTION_BUTTON_UP, actions.ACTION_BUTTON_DOWN,
actions.ACTION_BUTTON_LEFT, actions.ACTION_BUTTON_RIGHT,
actions.ACTION_BUTTON_A, actions.ACTION_BUTTON_X
}

-- List of arrow actions (up, down, left, right).
actions.ACTIONS_ARROWS = {
actions.ACTION_BUTTON_UP,actions.ACTION_BUTTON_DOWN,
actions.ACTION_BUTTON_LEFT, actions.ACTION_BUTTON_RIGHT
}

-- List of "other" button actions (A, B, X, Y).
actions.ACTIONS_BUTTONS = {
actions.ACTION_BUTTON_B, actions.ACTION_BUTTON_Y,
--actions.ACTION_BUTTON_SELECT, actions.ACTION_BUTTON_START,
actions.ACTION_BUTTON_A, actions.ACTION_BUTTON_X,
--actions.ACTION_BUTTON_L, actions.ACTION_BUTTON_R
}

-- Action names used by the emulator.
actions.ACTION_TO_BUTTON_NAME = {}
actions.ACTION_TO_BUTTON_NAME[0] = "gamepad-1-B"
actions.ACTION_TO_BUTTON_NAME[1] = "gamepad-1-Y"
Expand All @@ -55,6 +63,7 @@ actions.ACTION_TO_BUTTON_NAME[9] = "gamepad-1-X"
actions.ACTION_TO_BUTTON_NAME[10] = "gamepad-1-L"
actions.ACTION_TO_BUTTON_NAME[11] = "gamepad-1-R"

-- Short string names for each action, used for string conversions.
actions.ACTION_TO_SHORT_NAME = {}
actions.ACTION_TO_SHORT_NAME[0] = "B"
actions.ACTION_TO_SHORT_NAME[1] = "Y"
Expand All @@ -69,7 +78,7 @@ actions.ACTION_TO_SHORT_NAME[9] = "X"
actions.ACTION_TO_SHORT_NAME[10] = "L"
actions.ACTION_TO_SHORT_NAME[11] = "R"


-- Returns whether a certain action index represents an arrow action (up, down, left, right).
function actions.isArrowsActionIdx(actionIdx)
for i=1,#actions.ACTIONS_ARROWS do
if actionIdx == actions.ACTIONS_ARROWS[i] then
Expand All @@ -79,6 +88,7 @@ function actions.isArrowsActionIdx(actionIdx)
return false
end

-- Returns whether a certain action index represents a button action (A, B, X, Y).
function actions.isButtonsActionIdx(actionIdx)
for i=1,#actions.ACTIONS_BUTTONS do
if actionIdx == actions.ACTIONS_BUTTONS[i] then
Expand All @@ -88,6 +98,7 @@ function actions.isButtonsActionIdx(actionIdx)
return false
end

-- Transforms an action (arrow action index + button action index) to a short, readable string.
function actions.actionToString(action)
if action == nil then
return "nil"
Expand All @@ -96,49 +107,27 @@ function actions.actionToString(action)
end
end

-- Returns a new, random Action object.
function actions.createRandomAction()
local arrow = actions.ACTIONS_ARROWS[math.random(#actions.ACTIONS_ARROWS)]
local button = actions.ACTIONS_BUTTONS[math.random(#actions.ACTIONS_BUTTONS)]
return Action.new(arrow, button)
end

-- Resets all buttons (to "not pressed").
function actions.endAllActions()
--local lcid = 1
--local port, controller = input.lcid_to_pcid2(lcid)
--local controller = 0
--input.set2(port, controller, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for i=1,#actions.ACTIONS_ALL do
local newstate = 0 -- 1 = pressed, 0 = released
local mode = 3 -- 1 = autohold, 2 = framehold, others = press/release
input.do_button_action(actions.ACTION_TO_BUTTON_NAME[actions.ACTIONS_ALL[i]], newstate, mode)
end
end

--[[
function endAction(action)
local lcid = 1
local port, controller = input.lcid_to_pcid2(lcid)
--local controller = 0
local value = 0 -- 0 = release, 1 = press
input.set2(port, controller, action, value)
end
--]]

-- Starts an action.
-- @param action An Action object.
function actions.startAction(action)
assert(action ~= nil)
--for lcid=1,8 do
--print(port, controller)
--local controller = 0
--local value = 1 -- 0 = release, 1 = press
--input.set2(port, controller, action, value)
--end
--setJoypad2({action})
--print("Starting action!", action)
local newstate = 1 -- 1 = pressed, 0 = released
local mode = 3 -- 1 = autohold, 2 = framehold, others = press/release
--if action == ACTION_BUTTON_B or action == ACTION_BUTTON_A then
-- mode = 2
--end
local arrowAction = actions.ACTION_TO_BUTTON_NAME[action.arrow]
local buttonAction = actions.ACTION_TO_BUTTON_NAME[action.button]
assert(arrowAction ~= nil)
Expand All @@ -147,82 +136,11 @@ function actions.startAction(action)
input.do_button_action(buttonAction, newstate, mode)
end

function actions.setJoypad(actions)
print("set joypad")
local lcid = 1
local port, controller = input.lcid_to_pcid2(lcid)
local value = 1 -- 0 = release, 1 = press
--input.set2(port, controller, ACTION_BUTTON_A, value)
input.set2(port, controller, 0, 0)
--for i=0,32000 do
-- input.set2(port, controller, i, 1)
--end
end

function actions.setJoypad2(actions)
local lcid = 1
local port, controller = input.lcid_to_pcid2(lcid)
--[[
local table = {
B = false, Y = false, select = false, start = false,
up = false, down = false, left = false, right = false,
A = false, X = false,
L = false, R = false
}
for i=1,#actions do
local action = actions[i]
if action == ACTION_BUTTON_B then table.B = true end
if action == ACTION_BUTTON_Y then table.Y = true end
if action == ACTION_BUTTON_SELECT then table.select = true end
if action == ACTION_BUTTON_START then table.start = true end
if action == ACTION_BUTTON_UP then table.up = true end
if action == ACTION_BUTTON_DOWN then table.down = true end
if action == ACTION_BUTTON_LEFT then table.left = true end
if action == ACTION_BUTTON_RIGHT then table.right = true end
if action == ACTION_BUTTON_A then table.A = true end
if action == ACTION_BUTTON_X then table.X = true end
if action == ACTION_BUTTON_L then table.L = true end
if action == ACTION_BUTTON_R then table.R = true end
end
--]]
local table = {}
table["P1 B"] = true
table["P1 Y"] = true
table["P1 select"] = true
table["P1 start"] = true
table["P1 up"] = true
table["P1 down"] = true
table["P1 left"] = true
table["P1 right"] = true
table["P1 A"] = true
table["P1 X"] = true
table["P1 L"] = true
table["P1 R"] = true
local table2 = {}
table2["B"] = true
table2["Y"] = false
table2["select"] = false
table2["start"] = false
table2["up"] = false
table2["down"] = false
table2["left"] = false
table2["right"] = false
table2["A"] = true
table2["X"] = false
table2["L"] = false
table2["R"] = false
local table3 = {}
for i=1,12 do
table3[i] = false
if i==1 and math.random()<0.1 then table3[i] = true end
end
print("Sending to joyset...", table)
--for i=1,1 do
input.joyset(1, table3)
--end
end

-- Chooses an action based on a chain of states.
-- @param lastStates List of State objects.
-- @param perfect Boolean, sets exploration prob. to 0.0 (not really necessary anymore with pExplore).
-- @param bestAction Optionally an Action object for epsilon-greedy policy, otherwise the best action will be approximated.
-- @param pExplore Exploration probability for epsilon-greedy policy.
function chooseAction(lastStates, perfect, bestAction, pExplore)
perfect = perfect or false
pExplore = pExplore or STATS.P_EXPLORE_CURRENT
Expand Down
Loading

0 comments on commit 8da4882

Please sign in to comment.