state_manager.StateManager
- class state_manager.StateManager(initial_state, alignment_threshold)
Bases:
objectManages symbolic state and geometric positions for intervention planning.
This class tracks the current state of all objects, including their positions and symbolic relationships. It provides methods to apply interventions and check whether relationships are maintained.
- Attributes:
objects: Dictionary mapping object names to [x, y, z] positions. relationships: Set of current symbolic relationships (e.g., ‘On(2,1)’). alignment_threshold: Distance threshold for considering objects aligned (meters). initial_objects: Copy of initial object positions for reference.
- __init__(initial_state, alignment_threshold)
Initialize the state manager with an initial state.
- Args:
initial_state: Dictionary containing ‘objects’ and ‘relationships’ keys. alignment_threshold: Maximum distance (in meters) between objects to
maintain alignment. Default: 0.05m (5cm).
Methods
__init__(initial_state, alignment_threshold)Initialize the state manager with an initial state.
apply_shift(obj, direction, magnitude)Apply a directional shift to an object's position.
check_alignment(obj_a, obj_b)Check if two objects are aligned in the x-y plane.
check_misalignment(obj, reference)Check if an object is misaligned relative to a reference object.
compute_displacement(obj)Compute total displacement of an object from its initial position.
get_alignment_score(goal_relationships)Compute alignment score as fraction of goal relationships maintained.
Get the subset of critical 'On' relationships that form the stack.
get_position(obj)Get the current position of an object.
Infer current symbolic relationships from geometric positions.
Get a complete snapshot of the current state.
Reset state to initial configuration.
- apply_shift(obj, direction, magnitude)
Apply a directional shift to an object’s position.
Updates the object’s position by moving it in the specified direction by the given magnitude. Directions are in the world frame.
- Args:
obj: Name of the object to shift (e.g., ‘1’, ‘2’, ‘3’). direction: Direction to shift (‘left’, ‘right’, ‘forward’, ‘back’). magnitude: Distance to shift in meters.
- Returns:
True if shift was successfully applied, False if object not found.
- check_alignment(obj_a, obj_b)
Check if two objects are aligned in the x-y plane.
Objects are considered aligned if their x and y coordinates are within the alignment threshold.
- Args:
obj_a: Name of first object. obj_b: Name of second object (typically the base).
- Returns:
True if objects are aligned, False otherwise.
- check_misalignment(obj, reference)
Check if an object is misaligned relative to a reference object.
This is the inverse of check_alignment - returns True if the object is NOT aligned with the reference.
- Args:
obj: Name of the object to check. reference: Name of the reference object (default: ‘0’ - typically the base).
- Returns:
True if object is misaligned, False if aligned.
- compute_displacement(obj)
Compute total displacement of an object from its initial position.
- Args:
obj: Name of the object.
- Returns:
Euclidean distance from initial position, or 0 if object not found.
- get_alignment_score(goal_relationships)
Compute alignment score as fraction of goal relationships maintained.
- Args:
goal_relationships: Set of desired symbolic relationships.
- Returns:
Score between 0 and 1, where 1 means all relationships maintained.
- get_critical_relationships()
Get the subset of critical ‘On’ relationships that form the stack.
For a stack of blocks On(1,0), On(2,1), On(3,2), On(4,3), this returns all ‘On’ relationships that involve numbered blocks.
- Returns:
Set of critical relationship strings.
- get_position(obj)
Get the current position of an object.
- Args:
obj: Name of the object.
- Returns:
List [x, y, z] if object exists, None otherwise.
- get_relationships()
Infer current symbolic relationships from geometric positions.
Determines which ‘On(A,B)’ relationships hold based on current object positions. Two objects maintain an ‘On’ relationship if they are geometrically aligned (within threshold).
- Returns:
Set of symbolic relationship strings (e.g., {‘On(2,1)’, ‘On(3,2)’}).
- get_state_snapshot()
Get a complete snapshot of the current state.
- Returns:
Dictionary containing ‘objects’ and ‘relationships’.
- reset_to_initial() None
Reset state to initial configuration.