Skip to content

Commit 37898bf

Browse files
authored
Hide excluded object types from the belief block and policy observations (#248)
Fan and Boil hide their switch objects from every state listing the agent reads (excluded_objects_in_state_str), so that no listing contradicts another. The observation's [belief] block and the frame a raw-control policy reads iterated the frame without that filter: fixes_r12 Fan's agents saw switch_0 to switch_3 in [belief] but not in [objects], and sim.state()['switch_0'] raised KeyError. Both now leave the excluded types out.
1 parent d74c4ae commit 37898bf

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

‎predicators/agent_sdk/primitive_policy.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from predicators.agent_sdk.sandbox_setup import pyguard_dir, pyguard_env
1919
from predicators.agent_sdk.tools.python_exec import _resolve_sandbox_file
2020
from predicators.agent_sdk.tools.sandbox_guard import _scrub_host_paths
21-
from predicators.structs import Action
21+
from predicators.structs import Action, excluded_object_type_names
2222

2323
if TYPE_CHECKING:
2424
from predicators.run.continual import ProtocolObservation, ProtocolSession
@@ -29,6 +29,7 @@ def primitive_observation(
2929
observation: Optional[ProtocolObservation] = None) -> Dict[str, Any]:
3030
"""A detached, JSON-compatible view of the current observed frame."""
3131
obs = observation if observation is not None else session.observe()
32+
hidden = excluded_object_type_names()
3233
objects = {
3334
obj.name: {
3435
"type": obj.type.name,
@@ -37,7 +38,7 @@ def primitive_observation(
3738
for name in obj.type.feature_names
3839
},
3940
}
40-
for obj in obs.frame
41+
for obj in obs.frame if obj.type.name not in hidden
4142
}
4243
sim_state = obs.frame.simulator_state
4344
joints = (sim_state.get("joint_positions")

‎predicators/agent_sdk/tools/continual_tools.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
ExecutionRequest, GiveUp, PrimitiveAction, RequestReset, SkillExecution
3838
from predicators.settings import CFG
3939
from predicators.structs import Action, GroundAtom, Predicate, State, Task, \
40-
_Option
40+
_Option, excluded_object_type_names
4141

4242
if TYPE_CHECKING: # pragma: no cover - import cycle through approaches
4343
from predicators.run.continual import InvocationResult, \
@@ -243,9 +243,7 @@ def format_observation(obs: "ProtocolObservation",
243243
and CFG.continual_uncertainty_decisions):
244244
lines.append("[belief] each object smoothed over the frames it "
245245
"rested through (value+-spread):")
246-
for obj in sorted(obs.frame, key=lambda o: o.name):
247-
if obj.name in obs.belief.frames_used:
248-
lines.append(" " + obs.belief.object_line(obj))
246+
lines.extend(belief_object_lines(obs.frame, obs.belief))
249247
if render_path:
250248
lines.append(f"[render] {render_path}")
251249
if with_budget:
@@ -254,6 +252,18 @@ def format_observation(obs: "ProtocolObservation",
254252
return "\n".join(lines)
255253

256254

255+
def belief_object_lines(frame: State, belief: Any) -> List[str]:
256+
"""The ``[belief]`` block's line for each object the belief smoothed,
257+
leaving out the types every state listing hides
258+
(``excluded_object_type_names``)."""
259+
hidden = excluded_object_type_names()
260+
return [
261+
" " + belief.object_line(obj)
262+
for obj in sorted(frame, key=lambda o: o.name)
263+
if obj.name in belief.frames_used and obj.type.name not in hidden
264+
]
265+
266+
257267
def _atoms_change_line(before: Set[GroundAtom], after: Set[GroundAtom]) -> str:
258268
added = sorted(str(a) for a in after - before)
259269
removed = sorted(str(a) for a in before - after)

‎tests/agent_sdk/test_state_listing_exclusion.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""Hidden object types (CFG.excluded_objects_in_state_str) stay out of every
2-
listing the agent reads: dict_str, pretty_str and the task digest."""
3-
from typing import Tuple
2+
listing the agent reads: dict_str, pretty_str, the task digest, the
3+
observation's belief block and a raw-control policy's observation."""
4+
from types import SimpleNamespace
5+
from typing import Any, Tuple
46

57
import numpy as np
68

79
from predicators import utils
10+
from predicators.agent_sdk.primitive_policy import primitive_observation
11+
from predicators.agent_sdk.tools.continual_tools import belief_object_lines
812
from predicators.agent_sdk.tools.digests import render_task_digest
913
from predicators.structs import Object, State, Task, Type, \
1014
excluded_object_type_names
@@ -34,6 +38,26 @@ def test_listings_hide_excluded_types() -> None:
3438
digest = render_task_digest(Task(state, set()), 0, [])
3539
assert "Objects: [fan_1:fan]" in digest
3640
assert "switch_1" not in digest
41+
# The belief block smooths every object but lists only the shown ones.
42+
belief = SimpleNamespace(frames_used={
43+
"fan_1": 3,
44+
"switch_1": 3
45+
},
46+
object_line=lambda obj: f"{obj.name}: smoothed")
47+
assert belief_object_lines(state, belief) == [" fan_1: smoothed"]
48+
# So does the observation a raw-control policy reads.
49+
obs = SimpleNamespace(
50+
frame=state,
51+
level=SimpleNamespace(task=SimpleNamespace(goal_nl="")),
52+
state=SimpleNamespace(value="NOT_FINISHED"),
53+
ledger=SimpleNamespace(episode_steps=0, run_steps=0,
54+
steps_remaining=9))
55+
session: Any = SimpleNamespace(observe=lambda: obs, action_spec={})
56+
assert set(primitive_observation(session)["objects"]) == {"fan_1"}
57+
utils.reset_config({})
58+
assert set(
59+
primitive_observation(session)["objects"]) == {"fan_1", "switch_1"}
60+
assert len(belief_object_lines(state, belief)) == 2
3761
del fan_1
3862

3963

0 commit comments

Comments
 (0)