Kern-API (Core API)¶
custom_grid_env.interface.AgentInterface(render=True, render_mode=None, step_delay=100, slip_probability=0.2, slip_type='longitudinal', ghost_agent_class=None, use_particle_filter=True, pf_num_particles=200, pf_sensor_mode='both', show_particles=True, color_sensor_quality=0.8)
¶
Interface for AI agents to interact with the CustomGridEnv.
Usage
interface = AgentInterface(render=True, slip_probability=0.2) obs = interface.reset() while not interface.is_terminated(): action = your_agent.get_action(obs) obs, reward, done, info = interface.step(action) results = interface.get_episode_stats() interface.close()
Attributes:
| Name | Type | Description |
|---|---|---|
env |
CustomGridEnv
|
The gymnasium environment. |
render_enabled |
bool
|
Whether to render the environment. |
step_delay |
int
|
Delay between steps in milliseconds. |
total_reward |
float
|
Cumulative reward in the current episode. |
terminated |
bool
|
Whether the episode has terminated. |
truncated |
bool
|
Whether the episode was truncated. |
episode_steps |
int
|
Number of steps taken in the current episode. |
last_info |
dict
|
Information from the last step. |
Initializes the AgentInterface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
render
|
bool
|
Whether to render the graphical display. Defaults to True. |
True
|
render_mode
|
str
|
The mode to render with ("human" or "rgb_array"). Defaults to "rgb_array" if render is True and no mode is provided. |
None
|
step_delay
|
int
|
Milliseconds to wait between steps when rendering. Defaults to 100. |
100
|
slip_probability
|
float
|
Probability of slipping. Defaults to 0.2. |
0.2
|
ghost_agent_class
|
type
|
Class for ghost agent. Defaults to ChaseGhostAgent. |
None
|
color_sensor_quality
|
float
|
Probability of the color sensor measuring the correct color. Defaults to 0.8. |
0.8
|
Source code in src/custom_grid_env/interface.py
close()
¶
get_action_space()
¶
Gets the action space.
Returns:
| Type | Description |
|---|---|
Space
|
gym.spaces.Space: The action space. |
get_episode_stats()
¶
Gets statistics for the current/last episode.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Any]
|
Episode statistics. |
Source code in src/custom_grid_env/interface.py
get_observation_space()
¶
Gets the observation space.
Returns:
| Type | Description |
|---|---|
Space
|
gym.spaces.Space: The observation space. |
get_reward_structure()
¶
Gets the reward structure for the environment.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Any]
|
The reward structure. |
is_terminated()
¶
Checks if the current episode has ended.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if terminated or truncated, False otherwise. |
reset(seed=None)
¶
Resets the environment for a new episode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Random seed for reproducibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Any]
|
Initial observation for the agent. |
Source code in src/custom_grid_env/interface.py
set_ghost_agent(agent_class)
¶
Sets the ghost agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_class
|
Type[Agent]
|
The class of the new ghost agent. |
required |
step(action)
¶
Takes a step in the environment (agent moves, then ghost moves automatically).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
int
|
Agent action (0=left, 1=down, 2=right, 3=up). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[Dict[str, Any], float, bool, Dict[str, Any]]
|
(observation, reward, done, info) |
Source code in src/custom_grid_env/interface.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
custom_grid_env.env.CustomGridEnv(render_mode='human', slip_probability=0.2, slip_type='longitudinal', color_sensor_quality=0.8)
¶
Bases: Env
A custom grid environment with an agent and a ghost.
Attributes:
| Name | Type | Description |
|---|---|---|
render_mode |
str
|
Current render mode. |
slip_probability |
float
|
Probability of slipping to a perpendicular direction. |
rows |
int
|
Number of rows in the grid. |
cols |
int
|
Number of columns in the grid. |
grid |
ndarray
|
The grid containing cell information. |
agent_pos |
list
|
Current position of the agent [row, col]. |
start_pos |
list
|
Starting position of the agent. |
ghost_pos |
list
|
Current position of the ghost. |
ghost_start_pos |
list
|
Starting position of the ghost. |
step_count |
int
|
Current step count in the episode. |
current_turn |
int
|
Whose turn it is (0 for agent, 1 for ghost). |
Initializes the CustomGridEnv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
render_mode
|
str
|
The mode to render with. Defaults to "human". |
'human'
|
slip_probability
|
float
|
Chance to move perpendicular to intended direction. Defaults to 0.2. |
0.2
|
slip_type
|
str
|
Type of slipping ("perpendicular" or "longitudinal"). Defaults to "longitudinal". |
'longitudinal'
|
color_sensor_quality
|
float
|
Probability of the color sensor measuring the correct color. Defaults to 0.8. |
0.8
|
Source code in src/custom_grid_env/env.py
calculate_reward(caught_by_ghost=False)
¶
Calculates reward based on current game state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
caught_by_ghost
|
bool
|
Whether the ghost caught the agent. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[float, bool, Dict[str, Any]]
|
(reward, terminated, info_dict) |
Source code in src/custom_grid_env/env.py
close()
¶
get_current_turn()
¶
Returns whose turn it is.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
'agent' or 'ghost'. |
get_reward_structure()
¶
Gets the reward structure for this environment.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Dict[str, Any]
|
Dictionary describing all rewards and their values. |
Source code in src/custom_grid_env/env.py
move_ghost(ghost_action)
¶
Moves the ghost with an externally provided action.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ghost_action
|
int
|
Action for ghost (0=left, 1=down, 2=right, 3=up). |
required |
Source code in src/custom_grid_env/env.py
render()
¶
Renders the environment.
Returns:
| Type | Description |
|---|---|
Optional[ndarray]
|
np.ndarray, optional: RGB array if render_mode is "rgb_array". |
Source code in src/custom_grid_env/env.py
reset(seed=None, options=None)
¶
Resets the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Random seed. |
None
|
options
|
dict
|
Additional options. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[Dict[str, Any], Dict[str, Any]]
|
(observation, info) |
Source code in src/custom_grid_env/env.py
step(action)
¶
Executes one step in the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
int
|
Action for current entity. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[Dict[str, Any], float, bool, bool, Dict[str, Any]]
|
(observation, reward, terminated, truncated, info) |
Source code in src/custom_grid_env/env.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |