gameboy_worlds.emulation.hamtaro.base_metrics

 1from typing import Optional
 2
 3import numpy as np
 4
 5from gameboy_worlds.emulation.hamtaro.parsers import AgentState, HamtaroStateParser
 6from gameboy_worlds.emulation.tracker import MetricGroup
 7
 8
 9class HamtaroCoreMetrics(MetricGroup):
10    """Tracks the coarse Hamtaro game state for dev-play inspection."""
11
12    NAME = "hamtaro_core"
13    REQUIRED_PARSER = HamtaroStateParser
14
15    def start(self):
16        super().start()
17
18    def reset(self, first=False):
19        self.current_state = AgentState.FREE_ROAM
20        self.previous_state = self.current_state
21
22    def close(self):
23        self.reset()
24
25    def step(self, current_frame: np.ndarray, recent_frames: Optional[np.ndarray]):
26        self.previous_state = self.current_state
27        self.current_state = self.state_parser.get_agent_state(current_frame)
28
29    def report(self) -> dict:
30        return {
31            "agent_state": self.current_state,
32            "in_free_roam": self.current_state == AgentState.FREE_ROAM,
33            "in_dialogue": self.current_state == AgentState.IN_DIALOGUE,
34            "in_menu": self.current_state == AgentState.IN_MENU,
35        }
36
37    def report_final(self) -> dict:
38        return {}
class HamtaroCoreMetrics(gameboy_worlds.emulation.tracker.MetricGroup):
10class HamtaroCoreMetrics(MetricGroup):
11    """Tracks the coarse Hamtaro game state for dev-play inspection."""
12
13    NAME = "hamtaro_core"
14    REQUIRED_PARSER = HamtaroStateParser
15
16    def start(self):
17        super().start()
18
19    def reset(self, first=False):
20        self.current_state = AgentState.FREE_ROAM
21        self.previous_state = self.current_state
22
23    def close(self):
24        self.reset()
25
26    def step(self, current_frame: np.ndarray, recent_frames: Optional[np.ndarray]):
27        self.previous_state = self.current_state
28        self.current_state = self.state_parser.get_agent_state(current_frame)
29
30    def report(self) -> dict:
31        return {
32            "agent_state": self.current_state,
33            "in_free_roam": self.current_state == AgentState.FREE_ROAM,
34            "in_dialogue": self.current_state == AgentState.IN_DIALOGUE,
35            "in_menu": self.current_state == AgentState.IN_MENU,
36        }
37
38    def report_final(self) -> dict:
39        return {}

Tracks the coarse Hamtaro game state for dev-play inspection.

NAME = 'hamtaro_core'

Name of the MetricGroup.

The StateParser which implements the minimum required functionality for this MetricGroup to work.

def start(self):
16    def start(self):
17        super().start()

Called once when environment starts. All subclasses should call super() AFTER initializing their own variables. Only variables that will persist across episodes should be initialized here.

def reset(self, first=False):
19    def reset(self, first=False):
20        self.current_state = AgentState.FREE_ROAM
21        self.previous_state = self.current_state

Called when environment resets.

Arguments:
  • first (bool): Whether this is the first reset of the environment. If True, might need to aggregate metrics into running final totals.
def close(self):
23    def close(self):
24        self.reset()

Called when environment closes. Good for computing summary stats.

Step will not be called after this.

def step( self, current_frame: numpy.ndarray, recent_frames: Optional[numpy.ndarray]):
26    def step(self, current_frame: np.ndarray, recent_frames: Optional[np.ndarray]):
27        self.previous_state = self.current_state
28        self.current_state = self.state_parser.get_agent_state(current_frame)

Called each environment step to update metrics.

Arguments:
  • current_frame (np.ndarray): The current frame rendered by the emulator.
  • recent_frames (Optional[np.ndarray]): The stack of frames that were rendered during the last action. Shape is [n_frames, height, width, channels]. Can be None if rendering is disabled.
def report(self) -> dict:
30    def report(self) -> dict:
31        return {
32            "agent_state": self.current_state,
33            "in_free_roam": self.current_state == AgentState.FREE_ROAM,
34            "in_dialogue": self.current_state == AgentState.IN_DIALOGUE,
35            "in_menu": self.current_state == AgentState.IN_MENU,
36        }

Return metrics as dictionary for instantaneous variable tracking.

Returns

Dictionary of metrics

def report_final(self) -> dict:
38    def report_final(self) -> dict:
39        return {}

Return metrics as dictionary for logging. Called at end of environment (before close). Will never be called before self.close.

Returns

Dictionary of metrics