Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Seminar-HFO
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
Seminar-HFO
Commits
edda8b73
Commit
edda8b73
authored
Oct 20, 2015
by
Matthew Hausknecht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added sleeps. Now works on my osx.
parent
21b0fe35
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
27 deletions
+35
-27
bin/Trainer.py
bin/Trainer.py
+35
-27
No files found.
bin/Trainer.py
View file @
edda8b73
...
@@ -56,6 +56,8 @@ class Trainer(object):
...
@@ -56,6 +56,8 @@ class Trainer(object):
Returns a Popen process object
Returns a Popen process object
"""
"""
print
'Launch npc
%
s-
%
d'
%
(
self
.
_offenseTeamName
if
play_offense
else
self
.
_defenseTeamName
,
player_num
)
if
play_offense
:
if
play_offense
:
team_name
=
self
.
_offenseTeamName
team_name
=
self
.
_offenseTeamName
else
:
else
:
...
@@ -84,8 +86,8 @@ class Trainer(object):
...
@@ -84,8 +86,8 @@ class Trainer(object):
Returns a Popen process object
Returns a Popen process object
"""
"""
print
'
[Trainer] Launching
%
s Agent
%
d'
%
\
print
'
Launch agent
%
s-
%
d'
%
(
self
.
_offenseTeamName
if
play_offense
(
'offense'
if
play_offense
else
'defense'
,
agent_num
)
else
self
.
_defenseTeamName
,
agent_num
)
if
play_offense
:
if
play_offense
:
assert
self
.
_numOffense
>
0
assert
self
.
_numOffense
>
0
team_name
=
self
.
_offenseTeamName
team_name
=
self
.
_offenseTeamName
...
@@ -223,7 +225,7 @@ class Trainer(object):
...
@@ -223,7 +225,7 @@ class Trainer(object):
self
.
_numOutOfTime
+=
1
self
.
_numOutOfTime
+=
1
if
event
in
{
'GOAL'
,
'OUT_OF_BOUNDS'
,
'CAPTURED_BY_DEFENSE'
,
'OUT_OF_TIME'
}:
if
event
in
{
'GOAL'
,
'OUT_OF_BOUNDS'
,
'CAPTURED_BY_DEFENSE'
,
'OUT_OF_TIME'
}:
self
.
_numTrials
+=
1
self
.
_numTrials
+=
1
print
'
[Trainer]
EndOfTrial:
%
d /
%
d
%
d
%
s'
%
\
print
'EndOfTrial:
%
d /
%
d
%
d
%
s'
%
\
(
self
.
_numGoals
,
self
.
_numTrials
,
self
.
_frame
,
event
)
(
self
.
_numGoals
,
self
.
_numTrials
,
self
.
_frame
,
event
)
self
.
_numFrames
+=
self
.
_frame
-
self
.
_lastTrialStart
self
.
_numFrames
+=
self
.
_frame
-
self
.
_lastTrialStart
self
.
_lastTrialStart
=
self
.
_frame
self
.
_lastTrialStart
=
self
.
_frame
...
@@ -242,16 +244,16 @@ class Trainer(object):
...
@@ -242,16 +244,16 @@ class Trainer(object):
msg
=
msg
[
1
:
length
+
1
]
msg
=
msg
[
1
:
length
+
1
]
if
msg
==
'START'
:
if
msg
==
'START'
:
if
self
.
_isPlaying
:
if
self
.
_isPlaying
:
print
'
[Trainer]
Already playing, ignoring message'
print
'Already playing, ignoring message'
else
:
else
:
self
.
startGame
()
self
.
startGame
()
elif
msg
==
'DONE'
:
elif
msg
==
'DONE'
:
raise
DoneError
raise
DoneError
elif
msg
==
'ready'
:
elif
msg
==
'ready'
:
print
'
[Trainer]
Agent Connected:'
,
team
,
player
print
'Agent Connected:'
,
team
,
player
self
.
_agentReady
.
add
((
team
,
player
))
self
.
_agentReady
.
add
((
team
,
player
))
else
:
else
:
print
'
[Trainer]
Unhandled message from agent:
%
s'
%
msg
print
'Unhandled message from agent:
%
s'
%
msg
def
initMsgHandlers
(
self
):
def
initMsgHandlers
(
self
):
""" Create handlers for different messages. """
""" Create handlers for different messages. """
...
@@ -278,7 +280,7 @@ class Trainer(object):
...
@@ -278,7 +280,7 @@ class Trainer(object):
""" Check that the next message is same as expected message. """
""" Check that the next message is same as expected message. """
msg
=
self
.
recv
(
retryCount
)
msg
=
self
.
recv
(
retryCount
)
if
msg
!=
expectedMsg
:
if
msg
!=
expectedMsg
:
print
>>
sys
.
stderr
,
'
[Trainer]
Error with message'
print
>>
sys
.
stderr
,
'Error with message'
print
>>
sys
.
stderr
,
' expected:
%
s'
%
expectedMsg
print
>>
sys
.
stderr
,
' expected:
%
s'
%
expectedMsg
print
>>
sys
.
stderr
,
' received:
%
s'
%
msg
print
>>
sys
.
stderr
,
' received:
%
s'
%
msg
# print >>sys.stderr,len(expectedMsg),len(msg)
# print >>sys.stderr,len(expectedMsg),len(msg)
...
@@ -306,7 +308,7 @@ class Trainer(object):
...
@@ -306,7 +308,7 @@ class Trainer(object):
self
.
_msgHandlers
.
append
([
args
,
handler
])
self
.
_msgHandlers
.
append
([
args
,
handler
])
else
:
else
:
if
(
'quiet'
not
in
kwargs
)
or
(
not
kwargs
[
'quiet'
]):
if
(
'quiet'
not
in
kwargs
)
or
(
not
kwargs
[
'quiet'
]):
print
'
[Trainer]
Updating handler for
%
s'
%
(
' '
.
join
(
args
))
print
'Updating handler for
%
s'
%
(
' '
.
join
(
args
))
self
.
_msgHandlers
[
i
]
=
[
args
,
handler
]
self
.
_msgHandlers
[
i
]
=
[
args
,
handler
]
def
unregisterMsgHandler
(
self
,
*
args
):
def
unregisterMsgHandler
(
self
,
*
args
):
...
@@ -328,7 +330,7 @@ class Trainer(object):
...
@@ -328,7 +330,7 @@ class Trainer(object):
""" Handle a message using the registered handlers. """
""" Handle a message using the registered handlers. """
i
,
prefixLength
,
handler
=
self
.
_findHandlerInd
(
msg
)
i
,
prefixLength
,
handler
=
self
.
_findHandlerInd
(
msg
)
if
i
<
0
:
if
i
<
0
:
print
'
[Trainer]
Unhandled message:'
,
msg
[
0
:
2
]
print
'Unhandled message:'
,
msg
[
0
:
2
]
else
:
else
:
handler
(
msg
[
prefixLength
:])
handler
(
msg
[
prefixLength
:])
...
@@ -343,16 +345,21 @@ class Trainer(object):
...
@@ -343,16 +345,21 @@ class Trainer(object):
msg
=
self
.
parseMsg
(
msg
)
msg
=
self
.
parseMsg
(
msg
)
self
.
handleMsg
(
msg
)
self
.
handleMsg
(
msg
)
def
waitForDisconnect
(
self
,
player_num
,
on_offense
):
def
disconnectPlayer
(
self
,
player
,
player_num
,
on_offense
):
"""Wait on a launched player to disconnect from the server. """
"""Wait on a launched player to disconnect from the server. """
print
'Disconnect
%
s-
%
d'
%
(
self
.
_offenseTeamName
if
on_offense
else
self
.
_defenseTeamName
,
player_num
)
player
.
terminate
()
time
.
sleep
(
0.1
)
self
.
send
(
'(look)'
)
self
.
send
(
'(look)'
)
partial
=
[
'ok'
,
'look'
]
partial
=
[
'ok'
,
'look'
]
self
.
_numPlayers
=
0
self
.
_numPlayers
=
0
def
f
(
body
):
def
f
(
body
):
del
self
.
_connectedPlayers
[:]
for
i
in
xrange
(
4
,
len
(
body
)):
for
i
in
xrange
(
4
,
len
(
body
)):
_
,
team
,
num
=
body
[
i
][
0
][:
3
]
_
,
team
,
num
=
body
[
i
][
0
][:
3
]
if
(
team
,
num
)
in
self
.
_connectedPlayers
:
if
(
team
,
num
)
not
in
self
.
_connectedPlayers
:
self
.
_connectedPlayers
.
remove
((
team
,
num
))
self
.
_connectedPlayers
.
append
((
team
,
num
))
self
.
registerMsgHandler
(
f
,
*
partial
,
quiet
=
True
)
self
.
registerMsgHandler
(
f
,
*
partial
,
quiet
=
True
)
team_name
=
self
.
_offenseTeamName
if
on_offense
else
self
.
_defenseTeamName
team_name
=
self
.
_offenseTeamName
if
on_offense
else
self
.
_defenseTeamName
while
(
team_name
,
str
(
player_num
))
in
self
.
_connectedPlayers
:
while
(
team_name
,
str
(
player_num
))
in
self
.
_connectedPlayers
:
...
@@ -365,11 +372,12 @@ class Trainer(object):
...
@@ -365,11 +372,12 @@ class Trainer(object):
server.
server.
"""
"""
#print 'Wait on player', player_num, on_offense
time
.
sleep
(
0.1
)
self
.
send
(
'(look)'
)
self
.
send
(
'(look)'
)
partial
=
[
'ok'
,
'look'
]
partial
=
[
'ok'
,
'look'
]
self
.
_numPlayers
=
0
self
.
_numPlayers
=
0
def
f
(
body
):
def
f
(
body
):
del
self
.
_connectedPlayers
[:]
for
i
in
xrange
(
4
,
len
(
body
)):
for
i
in
xrange
(
4
,
len
(
body
)):
_
,
team
,
num
=
body
[
i
][
0
][:
3
]
_
,
team
,
num
=
body
[
i
][
0
][:
3
]
if
(
team
,
num
)
not
in
self
.
_connectedPlayers
:
if
(
team
,
num
)
not
in
self
.
_connectedPlayers
:
...
@@ -383,6 +391,7 @@ class Trainer(object):
...
@@ -383,6 +391,7 @@ class Trainer(object):
def
checkIfAllPlayersConnected
(
self
):
def
checkIfAllPlayersConnected
(
self
):
""" Returns true if all players are connected. """
""" Returns true if all players are connected. """
print
'Checking all players are connected'
self
.
send
(
'(look)'
)
self
.
send
(
'(look)'
)
partial
=
[
'ok'
,
'look'
]
partial
=
[
'ok'
,
'look'
]
self
.
_numPlayers
=
0
self
.
_numPlayers
=
0
...
@@ -400,15 +409,15 @@ class Trainer(object):
...
@@ -400,15 +409,15 @@ class Trainer(object):
self
.
_isPlaying
=
True
self
.
_isPlaying
=
True
def
printStats
(
self
):
def
printStats
(
self
):
print
'
[Trainer]
TotalFrames =
%
i, AvgFramesPerTrial =
%.1
f, AvgFramesPerGoal =
%.1
f'
\
print
'TotalFrames =
%
i, AvgFramesPerTrial =
%.1
f, AvgFramesPerGoal =
%.1
f'
\
%
(
self
.
_numFrames
,
%
(
self
.
_numFrames
,
self
.
_numFrames
/
float
(
self
.
_numTrials
)
if
self
.
_numTrials
>
0
else
float
(
'nan'
),
self
.
_numFrames
/
float
(
self
.
_numTrials
)
if
self
.
_numTrials
>
0
else
float
(
'nan'
),
self
.
_numGoalFrames
/
float
(
self
.
_numGoals
)
if
self
.
_numGoals
>
0
else
float
(
'nan'
))
self
.
_numGoalFrames
/
float
(
self
.
_numGoals
)
if
self
.
_numGoals
>
0
else
float
(
'nan'
))
print
'
[Trainer]
Trials :
%
i'
%
self
.
_numTrials
print
'Trials :
%
i'
%
self
.
_numTrials
print
'
[Trainer]
Goals :
%
i'
%
self
.
_numGoals
print
'Goals :
%
i'
%
self
.
_numGoals
print
'
[Trainer]
Defense Captured :
%
i'
%
self
.
_numBallsCaptured
print
'Defense Captured :
%
i'
%
self
.
_numBallsCaptured
print
'
[Trainer]
Balls Out of Bounds:
%
i'
%
self
.
_numBallsOOB
print
'Balls Out of Bounds:
%
i'
%
self
.
_numBallsOOB
print
'
[Trainer]
Out of Time :
%
i'
%
self
.
_numOutOfTime
print
'Out of Time :
%
i'
%
self
.
_numOutOfTime
def
checkLive
(
self
,
necProcesses
):
def
checkLive
(
self
,
necProcesses
):
"""Returns true if each of the necessary processes is still alive and
"""Returns true if each of the necessary processes is still alive and
...
@@ -417,7 +426,7 @@ class Trainer(object):
...
@@ -417,7 +426,7 @@ class Trainer(object):
"""
"""
for
p
,
name
in
necProcesses
:
for
p
,
name
in
necProcesses
:
if
p
.
poll
()
is
not
None
:
if
p
.
poll
()
is
not
None
:
print
'
[Trainer]
Something necessary closed (
%
s), exiting'
%
name
print
'Something necessary closed (
%
s), exiting'
%
name
return
False
return
False
return
True
return
True
...
@@ -446,8 +455,7 @@ class Trainer(object):
...
@@ -446,8 +455,7 @@ class Trainer(object):
self
.
_npcPopen
.
append
(
player
)
self
.
_npcPopen
.
append
(
player
)
necProcesses
.
append
([
player
,
'offense_npc_'
+
str
(
player_num
)])
necProcesses
.
append
([
player
,
'offense_npc_'
+
str
(
player_num
)])
else
:
else
:
player
.
kill
()
self
.
disconnectPlayer
(
player
,
player_num
,
on_offense
=
True
)
self
.
waitForDisconnect
(
player_num
,
on_offense
=
True
)
# Launch defense
# Launch defense
agent_num
=
0
agent_num
=
0
...
@@ -465,13 +473,12 @@ class Trainer(object):
...
@@ -465,13 +473,12 @@ class Trainer(object):
self
.
_npcPopen
.
append
(
player
)
self
.
_npcPopen
.
append
(
player
)
necProcesses
.
append
([
player
,
'defense_npc_'
+
str
(
player_num
)])
necProcesses
.
append
([
player
,
'defense_npc_'
+
str
(
player_num
)])
else
:
else
:
player
.
kill
()
self
.
disconnectPlayer
(
player
,
player_num
,
on_offense
=
False
)
self
.
waitForDisconnect
(
player_num
,
on_offense
=
False
)
self
.
checkIfAllPlayersConnected
()
self
.
checkIfAllPlayersConnected
()
if
self
.
_numAgents
>
0
:
if
self
.
_numAgents
>
0
:
print
'
[Trainer]
Agents awaiting your connections'
print
'Agents awaiting your connections'
necOff
=
set
([(
self
.
_offenseTeamName
,
str
(
x
))
for
x
in
sorted_offense_agent_unums
])
necOff
=
set
([(
self
.
_offenseTeamName
,
str
(
x
))
for
x
in
sorted_offense_agent_unums
])
necDef
=
set
([(
self
.
_defenseTeamName
,
str
(
x
))
for
x
in
sorted_defense_agent_unums
])
necDef
=
set
([(
self
.
_defenseTeamName
,
str
(
x
))
for
x
in
sorted_defense_agent_unums
])
necAgents
=
necOff
.
union
(
necDef
)
necAgents
=
necOff
.
union
(
necDef
)
...
@@ -488,14 +495,15 @@ class Trainer(object):
...
@@ -488,14 +495,15 @@ class Trainer(object):
%
(
self
.
_offenseTeamName
,
self
.
_defenseTeamName
,
%
(
self
.
_offenseTeamName
,
self
.
_defenseTeamName
,
self
.
_numOffense
,
self
.
_numDefense
,
self
.
_numOffense
,
self
.
_numDefense
,
offense_nums
,
defense_nums
))
offense_nums
,
defense_nums
))
print
'Starting game'
self
.
startGame
()
self
.
startGame
()
while
self
.
checkLive
(
necProcesses
):
while
self
.
checkLive
(
necProcesses
):
prevFrame
=
self
.
_frame
prevFrame
=
self
.
_frame
self
.
listenAndProcess
()
self
.
listenAndProcess
()
except
TimeoutError
:
except
TimeoutError
:
print
'
[Trainer]
Haven
\'
t heard from the server for too long, Exiting'
print
'Haven
\'
t heard from the server for too long, Exiting'
except
(
KeyboardInterrupt
,
DoneError
):
except
(
KeyboardInterrupt
,
DoneError
):
print
'
[Trainer]
Finished'
print
'Finished'
finally
:
finally
:
try
:
try
:
self
.
_comm
.
sendMsg
(
'(bye)'
)
self
.
_comm
.
sendMsg
(
'(bye)'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment