ethicalgardeners.actionhandler.ActionHandler¶
- class ethicalgardeners.actionhandler.ActionHandler(grid_world, action_enum)[source]¶
Bases:
objectHandles the execution of agent actions in the grid world environment.
The ActionHandler mediates between agents and the world grid, ensuring that actions are only executed when valid. It manages movement validation, flower planting and harvesting, and simple waiting actions.
- action_enum¶
An enumeration of possible actions (UP, DOWN, LEFT, RIGHT, HARVEST, WAIT, PLANT_TYPE_i). Created dynamically based on the number of flower types available.
- Type:
- __init__(grid_world, action_enum)[source]¶
Create the ActionHandler with a reference to the grid world.
- Parameters:
grid_world (
GridWorld) – The grid world environment where actions will be executed.action_enum (
_ActionEnum) – An enumeration of possible actions (UP, DOWN, LEFT, RIGHT, HARVEST, WAIT, PLANT_TYPE_i). Created dynamically based on the number of flower types available.
Methods
__init__(grid_world, action_enum)Create the ActionHandler with a reference to the grid world.
handle_action(agent, action)Process an agent's action and execute it in the grid world.
harvest_flower(agent)Harvest a fully grown flower at the agent's current position.
move_agent(agent, action)Move an agent in the specified direction if the move is valid.
plant_flower(agent, flower_type)Plant a flower of the specified type at the agent's current position.
update_action_mask(agent)Update the action mask for all agents based on the current state of the grid world.
wait(agent)Perform a wait action, which does not change the state of the world.
- _compute_new_position(position, action)[source]¶
Compute the new position based on the current position and action.
- Parameters:
position (tuple) – The current (x, y) coordinates of the agent.
action (
_ActionEnum) – The action to perform (UP, DOWN, LEFT, RIGHT).
- Returns:
The new (x, y) coordinates after applying the action.
- Return type:
- handle_action(agent: Agent, action)[source]¶
Process an agent’s action and execute it in the grid world.
This method delegates to specific handler methods based on the action type.
- Parameters:
agent (
Agent) – The agent performing the action.action (
_ActionEnum) – The action to perform (UP, DOWN, LEFT, RIGHT, HARVEST, WAIT or PLANT_TYPE_i). PLANT_TYPE_i plants a flower of type i at the agent’s current position.
- harvest_flower(agent: Agent)[source]¶
Harvest a fully grown flower at the agent’s current position.
The flower must be fully grown to be harvested. Upon harvesting, the agent receives seeds and money based on the flower type.
- Parameters:
agent (
Agent) – The agent harvesting the flower.
- move_agent(agent: Agent, action)[source]¶
Move an agent in the specified direction if the move is valid.
- Parameters:
agent (
Agent) – The agent to move.action (
_ActionEnum) – The direction to move (UP, DOWN, LEFT, RIGHT).
- plant_flower(agent: Agent, flower_type: int)[source]¶
Plant a flower of the specified type at the agent’s current position.
The agent must have available seeds of the specified flower type.
- update_action_mask(agent: Agent)[source]¶
Update the action mask for all agents based on the current state of the grid world.
This method checks the validity of each action for the agent and updates his action mask accordingly.
- Parameters:
agent (
Agent) – The agent for which to update the action mask.