forked from Farama-Foundation/ViZDoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_instances.lua
executable file
·103 lines (79 loc) · 2.59 KB
/
multiple_instances.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
#!/usr/bin/env th
local threads = require 'threads'
local noOfPlayers = 2
local episodes = 10
local pool = threads.Threads(
-- spawned threads
noOfPlayers,
-- Set-up the environment on each thread
function()
-- for some reason 'require' has an unexpected behavior when called
-- from 'threads' to execute a module outside this director.
require "vizdoom"
actions = {
[1] = torch.IntTensor({1,0,0}),
[2] = torch.IntTensor({0,1,0}),
[3] = torch.IntTensor({0,0,1})
}
end,
function()
function player1()
-- Create DoomGame instance.
local game = vizdoom.DoomGame()
-- Config
game:loadConfig("../../scenarios/multi_duel.cfg")
game:addGameArgs("-host 2 -deathmatch +timelimit 1.0 +sv_spawnfarthest 1")
game:addGameArgs("+name Player1 +colorset 0")
game:init()
for i = 1, episodes do
while not game:isEpisodeFinished() do
if game:isPlayerDead() then
game:respawnPlayer()
end
-- Make a random action
local action = actions[torch.random(#actions)]
local reward = game:makeAction(action)
end
print("Player1 frags:", game:getGameVariable(vizdoom.GameVariable.FRAGCOUNT))
game:newEpisode()
end
game:close()
end
function player2()
-- Create DoomGame instance.
local game = vizdoom.DoomGame()
-- Config
game:loadConfig("../../scenarios/multi_duel.cfg")
game:addGameArgs("-join 127.0.0.1")
game:addGameArgs("+name Player2 +colorset 3")
game:init()
for i = 1, episodes do
while not game:isEpisodeFinished() do
if game:isPlayerDead() then
game:respawnPlayer()
end
-- Make a random action
local action = actions[torch.random(#actions)]
local reward = game:makeAction(action)
end
print("Player2 frags:", game:getGameVariable(vizdoom.GameVariable.FRAGCOUNT))
game:newEpisode()
end
game:close()
end
end
)
pool:specific(true)
pool:addjob(1,
function()
player1()
end
)
pool:addjob(2,
function()
player2()
end
)
pool:synchronize()
pool:terminate()
print("Done")