Skip to content

Commit

Permalink
Merge pull request #79 from RyanNavillus/puf07-nmmomg
Browse files Browse the repository at this point in the history
Add Syllabus Integration
  • Loading branch information
kywch authored Apr 17, 2024
2 parents 69194d5 + 1195589 commit b11b523
Show file tree
Hide file tree
Showing 4 changed files with 529 additions and 31 deletions.
27 changes: 21 additions & 6 deletions reinforcement_learning/environment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from argparse import Namespace

import nmmo
import nmmo.core.config as nc
import nmmo.core.game_api as ng
import pufferlib
import pufferlib.emulation

from pettingzoo.utils.wrappers.base_parallel import BaseParallelWrapper
from syllabus.core import PettingZooMultiProcessingSyncWrapper
from syllabus_task_wrapper import NMMOTaskWrapper

import nmmo
import nmmo.core.config as nc
import nmmo.core.game_api as ng

def alt_combat_damage_formula(offense, defense, multiplier, minimum_proportion):
return int(max(multiplier * offense - defense, offense * minimum_proportion))


class Config(
Expand All @@ -23,7 +27,6 @@ class Config(
nc.Exchange,
):
"""Configuration for Neural MMO."""

def __init__(self, env_args: Namespace):
super().__init__()

Expand All @@ -48,11 +51,23 @@ def __init__(self, env_args: Namespace):
self.set("CURRICULUM_FILE_PATH", env_args.curriculum_file_path)


def make_env_creator(reward_wrapper_cls: BaseParallelWrapper):
def make_env_creator(reward_wrapper_cls: BaseParallelWrapper, task_wrapper=False, curriculum=None):
def env_creator(*args, **kwargs):
"""Create an environment."""
env = nmmo.Env(Config(kwargs["env"])) # args.env is provided as kwargs
env = reward_wrapper_cls(env, **kwargs["reward_wrapper"])

# Add Syllabus task wrapper
if task_wrapper or curriculum is not None:
env = NMMOTaskWrapper(env)

# Use curriculum if provided
if curriculum is not None:
# Add Syllabus Sync Wrapper
env = PettingZooMultiProcessingSyncWrapper(
env, curriculum.get_components(), update_on_step=False, task_space=env.task_space,
)

env = pufferlib.emulation.PettingZooPufferEnv(env)
return env

Expand Down
3 changes: 3 additions & 0 deletions reinforcement_learning/stat_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def __init__(
self._reset_episode_stats()
self._stat_prefix = stat_prefix

def seed(self, seed):
self.env.seed(seed)

def observation(self, agent_id, agent_obs):
"""Called before observations are returned from the environment
Use this to define custom featurizers. Changing the space itself requires you to
Expand Down
Loading

0 comments on commit b11b523

Please sign in to comment.