Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Neural MMO
neural-mmo-starter-kit
Commits
9b812f28
Commit
9b812f28
authored
Jun 25, 2021
by
Siddhartha Laghuvarapu
Browse files
Changes for submission-test-rllib-v3
parent
2be9cd06
Changes
6
Hide whitespace changes
Inline
Side-by-side
neural-mmo
0 → 120000
View file @
9b812f28
../../neuralmmo_master/neural-mmo
\ No newline at end of file
players.yaml
View file @
9b812f28
# Define agents that will be used for evaluation.
# Agents need to implement abstract class NeuralMMOAgent.
# Agents will be located in agents/
#
Max n
umber of opponent agents is 127
#
N
umber of opponent agents is
exactly
127
player_agent
:
file
:
neural
_baseline_agent
agent_class
:
Neural
BaselineAgent
agent_type
:
neural
file
:
scripted
_baseline_agent
agent_class
:
Baseline
Combat
Agent
agent_type
:
scripted
opponent_agents
:
agent_1
:
...
...
resource
View file @
9b812f28
neural-mmo/docs/source/resource/
\ No newline at end of file
../../neuralmmo_master/neural-mmo/resource
\ No newline at end of file
rollout.py
View file @
9b812f28
from
tqdm
import
trange
import
sys
sys
.
path
.
append
(
'neural-mmo/'
)
import
gym
import
gym_neuralmmo
from
forge.ethyr.torch
import
utils
from
forge.trinity.env
import
Env
from
forge.blade.io.action
import
static
as
Action
import
projekt
from
utils.helpers
import
load_agents
import
random
import
copy
def
assign_agents
(
player_agent
,
opponent_agents
):
player_index
=
0
if
len
(
opponent_agents
)
!=
127
:
raise
Exception
(
"Number of opponent agents should add up to exactly 127"
)
random
.
shuffle
(
opponent_agents
)
player_index
=
random
.
randint
(
0
,
127
)
agents
=
copy
.
deepcopy
(
opponent_agents
)
agents
.
insert
(
player_index
,
player_agent
)
return
agents
,
player_index
def
main
():
env
=
gym
.
make
(
"neuralmmo-v0"
)
def
run_episode
(
player_index
,
agents
,
N_TIME_STEPS
):
config
=
projekt
.
config
.
CompetitionRound1
()
env
=
Env
(
config
)
n_steps
=
0
neural_agents
=
set
()
dead_agents
=
[]
obs
=
env
.
reset
()
entids
=
list
(
obs
.
keys
())
agent_entid_map
=
dict
(
zip
(
range
(
len
(
agents
)),
entids
))
entid_agent_map
=
{
x
[
1
]:
x
[
0
]
for
x
in
agent_entid_map
.
items
()}
for
idx
,
agent
in
enumerate
(
agents
):
if
agent
.
type
==
'neural'
:
neural_agents
.
add
(
agent_entid_map
[
idx
])
actions
=
{}
for
entid
in
entids
:
actions
[
entid
]
=
agents
[
entid_agent_map
[
entid
]].
register_reset
(
obs
[
entid
])
alive_agents
=
list
(
obs
.
keys
())
while
len
(
obs
.
keys
())
>
0
and
n_steps
<
N_TIME_STEPS
:
for
entid
in
actions
:
if
entid
not
in
neural_agents
:
realm
=
env
.
realm
if
Action
.
Attack
in
actions
[
entid
]:
targID
=
actions
[
entid
][
Action
.
Attack
][
Action
.
Target
]
actions
[
entid
][
Action
.
Attack
][
Action
.
Target
]
=
realm
.
entity
(
targID
)
obs
,
dones
,
rewards
,
_
=
env
.
step
(
actions
,
omitDead
=
True
,
preprocess
=
neural_agents
)
for
entid
in
alive_agents
:
if
entid
not
in
list
(
obs
.
keys
()):
dead_agents
.
append
(
entid
)
alive_agents
=
list
(
obs
.
keys
())
actions
=
{}
for
entid
in
alive_agents
:
if
entid
in
dead_agents
:
continue
actions
[
entid
]
=
agents
[
entid_agent_map
[
entid
]].
compute_action
(
obs
[
entid
])
n_steps
+=
1
for
entid
in
obs
:
if
entid
not
in
dead_agents
:
dead_agents
.
append
(
entid
)
logs
=
env
.
terminal
()
player_entid
=
entid_agent_map
[
player_index
]
player_log
=
{}
player_log
[
"Achievement"
]
=
logs
[
'Stats'
][
'Achievement'
][
player_entid
]
player_log
[
"Equipment"
]
=
logs
[
'Stats'
][
'Equipment'
][
player_entid
]
player_log
[
"Equipment"
]
=
logs
[
'Stats'
][
'Exploration'
][
player_entid
]
player_log
[
"PlayerKills"
]
=
logs
[
'Stats'
][
'PlayerKills'
][
player_entid
]
player_log
[
"Foraging"
]
=
logs
[
'Stats'
][
'Foraging'
][
player_entid
]
return
player_log
def
print_statistics
(
player_statistics
,
episode
):
print
(
"======= Episode {} ========"
.
format
(
episode
+
1
))
print
(
"Achievement "
,
player_statistics
[
'Achievement'
])
print
(
"Equipment "
,
player_statistics
[
'Equipment'
])
print
(
"Equipment "
,
player_statistics
[
'Equipment'
])
print
(
"PlayerKills "
,
player_statistics
[
'PlayerKills'
])
print
(
"Foraging "
,
player_statistics
[
'Foraging'
])
print
(
"========================="
)
if
__name__
==
"__main__"
:
player_agent
,
opponent_agents
=
load_agents
(
"players.yaml"
)
env
.
set_player_agent
(
player_agent
)
env
.
set_eval_agents
(
opponent_agents
)
n_episodes
=
100
total_rewards
=
0
for
_
in
trange
(
n_episodes
):
obs
=
env
.
reset
()
action
=
player_agent
.
register_reset
(
obs
)
done
=
False
while
done
==
False
:
obs
,
dones
,
rewards
,
_
=
env
.
step
(
action
)
action
=
player_agent
.
compute_action
(
obs
[
"player"
])
total_rewards
+=
rewards
[
"player"
]
done
=
dones
[
"player"
]
print
(
"The total reward is "
,
total_rewards
)
if
__name__
==
"__main__"
:
main
()
N_EPISODES
=
10
N_TIME_STEPS
=
1024
for
episode
in
range
(
N_EPISODES
):
agents
,
player_index
=
assign_agents
(
player_agent
,
opponent_agents
)
statistics
=
run_episode
(
player_index
,
agents
,
N_TIME_STEPS
)
print_statistics
(
statistics
,
episode
)
rollout_update.py
0 → 100644
View file @
9b812f28
import
sys
sys
.
path
.
append
(
'neural-mmo/'
)
from
forge.ethyr.torch
import
utils
from
forge.trinity.env
import
Env
import
projekt
from
utils.helpers
import
load_agents
import
random
import
copy
def
assign_agents
(
player_agent
,
opponent_agents
):
player_index
=
0
if
len
(
opponent_agents
)
!=
127
:
raise
Exception
(
"Number of opponent agents should add up to exactly 127"
)
random
.
shuffle
(
opponent_agents
)
player_index
=
random
.
randint
(
0
,
127
)
agents
=
copy
.
deepcopy
(
opponent_agents
)
agents
.
insert
(
player_index
,
player_agent
)
return
agents
,
player_index
def
run_episode
(
player_index
,
agents
,
N_TIME_STEPS
):
config
=
projekt
.
config
.
CompetitionRound1
()
env
=
Env
(
config
)
n_steps
=
0
neural_agents
=
set
()
dead_agents
=
[]
obs
=
env
.
reset
()
entids
=
list
(
obs
.
keys
())
agent_entid_map
=
dict
(
zip
(
range
(
len
(
agents
)),
entids
))
entid_agent_map
=
{
x
[
1
]:
x
[
0
]
for
x
in
agent_entid_map
.
items
()}
for
idx
,
agent
in
enumerate
(
agents
):
if
agent
.
type
==
'neural'
:
neural_agents
.
add
(
agent_entid_map
[
idx
])
actions
=
{}
for
entid
in
entids
:
actions
[
entid
]
=
agents
[
entid_agent_map
[
entid
]].
register_reset
(
obs
[
entid
])
while
len
(
obs
.
keys
())
>
0
and
n_steps
<
N_TIME_STEPS
:
obs
,
dones
,
rewards
,
_
=
env
.
step
(
actions
,
omitDead
=
False
)
alive_agents
=
list
(
obs
.
keys
())
for
entid
in
dones
:
if
dones
[
entid
]:
dead_agents
.
append
(
entid
)
actions
=
{}
for
entid
in
alive_agents
:
if
entid
not
in
entid_agent_map
:
continue
actions
[
entid
]
=
agents
[
entid_agent_map
[
entid
]].
compute_action
(
obs
[
entid
])
n_steps
+=
1
for
entid
in
obs
:
if
entid
not
in
dead_agents
:
dead_agents
.
append
(
entid
)
logs
=
env
.
terminal
()
player_entid
=
entid_agent_map
[
player_index
]
player_log
=
{}
if
player_entid
in
dead_agents
:
player_log
[
"Achievement"
]
=
logs
[
'Stats'
][
'Achievement'
][
player_entid
]
player_log
[
"Equipment"
]
=
logs
[
'Stats'
][
'Equipment'
][
player_entid
]
player_log
[
"Exploration"
]
=
logs
[
'Stats'
][
'Exploration'
][
player_entid
]
player_log
[
"PlayerKills"
]
=
logs
[
'Stats'
][
'PlayerKills'
][
player_entid
]
player_log
[
"Foraging"
]
=
logs
[
'Stats'
][
'Foraging'
][
player_entid
]
return
player_log
def
print_statistics
(
player_statistics
,
episode
):
print
(
player_statistics
,
episode
)
if
__name__
==
"__main__"
:
player_agent
,
opponent_agents
=
load_agents
(
"players.yaml"
)
N_EPISODES
=
10
N_TIME_STEPS
=
10
for
episode
in
range
(
N_EPISODES
):
agents
,
player_index
=
assign_agents
(
player_agent
,
opponent_agents
)
statistics
=
run_episode
(
player_index
,
agents
,
N_TIME_STEPS
)
print_statistics
(
statistics
,
episode
)
utils/helpers.py
View file @
9b812f28
...
...
@@ -7,6 +7,7 @@ def get_agent(agent_dict):
sys
.
path
.
append
(
"agents/"
)
module
=
importlib
.
import_module
(
agent_dict
[
"file"
])
agent
=
getattr
(
module
,
agent_dict
[
"agent_class"
])()
agent
.
type
=
agent_dict
[
"agent_type"
]
return
agent
...
...
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