-
Notifications
You must be signed in to change notification settings - Fork 5
/
torcs_test.lua
executable file
·132 lines (115 loc) · 2.96 KB
/
torcs_test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
local threads = require 'threads'
threads.Threads.serialization('threads.sharedserialize')
local tds = require 'tds'
local function threadedFormatter(thread)
local threadName = thread
return function(level, ...)
local msg = nil
if #{...} > 1 then
msg = string.format(({...})[1], table.unpack(fn.rest({...})))
else
msg = pprint.pretty_string(({...})[1])
end
return string.format("[%s: %s - %s] - %s\n", threadName, logroll.levels[level], os.date("%Y_%m_%d_%X"), msg)
end
end
local function setupLogging(thread)
local thread = thread or __threadid
require 'logroll'
if type(thread) == 'number' then
thread = ('%02d'):format(thread)
end
local file = paths.concat('log.'.. thread ..'.txt')
local flog = logroll.file_logger(file)
local formatterFunc = threadedFormatter(thread)
local plog = logroll.print_logger({formatter = formatterFunc})
log = logroll.combine(flog, plog)
end
local ctrlpool = threads.Threads(1)
local atomic = tds.AtomicCounter()
atomic:set(1)
num = 1
ctrlpool:addjob(setupLogging)
ctrlpool:addjob(
function ()
local unistd = require "posix.unistd"
__threadid = 0
local signal = require 'posix.signal'
signal.signal(signal.SIGINT, function(signum)
log.info("SIGINT received")
log.info('Ex(c)iting')
atomic:set(-1)
end)
end)
ctrlpool:addjob(
function ()
local temp = package.cpath
package.cpath = package.cpath .. ";TORCS/?.so"
ctrl = require 'TORCSctrl'
package.cpath = temp
while true do
ctrl.sleep(1)
if atomic:get() < 0 then break end
end
end
)
local function test()
log.info(env:getStateSpec())
log.info(env:getActionSpec())
totalstep = 200
nowstep = 0
log.info("env starting!")
local reward, terminal, state = 0, false, env:start()
log.info("is terminal: " .. tostring(terminal))
counting = 1
repeat
repeat
reward, observation, terminal = env:step(2)
nowstep = nowstep + 1
if term:get() < 0 then
env:cleanUp()
log.info("finish")
return
end
until terminal
if terminal then
reward, terminal, state = 0, false, env:start()
end
until nowstep >= totalstep
log.info("finish")
env:cleanUp()
end
game = threads.Threads(num,
function (id)
TORCS = require 'TORCS.TorcsDiscrete'
opt = {}
opt.mkey = id
opt.server = false
opt.game_config = 'quickrace_discrete_single_ushite-city.xml'
term = atomic
local image = require 'image'
end,
function ()
env = TORCS(opt)
end,
setupLogging,
function ( id )
log.info('Setting up Torch7')
-- Use enhanced garbage collector
torch.setheaptracking(true)
-- Set number of BLAS threads to 1 (per thread)
torch.setnumthreads(1)
-- Set default Tensor type (float is more efficient than double)
torch.setdefaulttensortype("torch.FloatTensor")
-- Set manual seed (different for each thread to have different experiences)
torch.manualSeed(id)
end
)
for _ = 1, num do
game:addjob(test)
end
game:synchronize()
game:terminate()
atomic:set(-1)
ctrlpool:synchronize()
ctrlpool:terminate()