Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove_nonterminal_branches #208

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion ldp/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def add_transition(
"""Add a transition to the tree.

Args:
step_id: A unique identifier for the root node of the tree.
step_id: A unique identifier for this node in the tree.
The expected form of the step ID is "{parent step ID}:{step index}".
step: The transition to add.
weight: Weight of the transition. Defaults to 1.0.
Expand Down Expand Up @@ -336,6 +336,25 @@ def compute_advantages(self) -> None:
# See docstring for explanation.
# step.metadata["advantage"] = step.value - state_values[parent_id]

def remove_nonterminal_branches(self) -> TransitionTree:
"""Creates a new tree with only branches that end in terminal states (done=True)."""
new_tree = TransitionTree(self.root_id)
for trajectory in self.get_trajectories():
if not trajectory.done:
continue

traj_id_parts = cast(str, trajectory.traj_id).split(":")

for step in trajectory.steps:
step_id = ":".join(traj_id_parts[: step.timestep + 2])
new_tree.add_transition(
step_id=step_id,
step=step,
weight=self.get_weight(step_id),
)

return new_tree

def merge_identical_nodes(
self,
agent_state_hash_fn: Callable[[Any], Hashable],
Expand Down
Loading