diff --git a/simulation/ai-polygon/navigation/observed_goals.py b/simulation/ai-polygon/navigation/observed_goals.py index c890e64b..3d7f8a73 100644 --- a/simulation/ai-polygon/navigation/observed_goals.py +++ b/simulation/ai-polygon/navigation/observed_goals.py @@ -72,12 +72,16 @@ class ObservedGoals: if not len(obstacles): return distances offsets = obstacles-xy + squared = np.sum(offsets*offsets, axis=1) for first in range(0, len(candidates), 128): last = min(first+128, len(candidates)) - along = np.clip(direction[first:last] @ offsets.T, - start[first:last, None], length[first:last, None]) - difference = (offsets[None]-along[:, :, None]*direction[first:last, None]) - distances[first:last] = np.sqrt(np.min(np.sum(difference*difference, axis=2), axis=1)) + # Exact point-to-segment distance without an N*M*2 temporary or + # a threaded BLAS call for a two-coordinate product. + projection = (direction[first:last, 0, None]*offsets[None, :, 0] + + direction[first:last, 1, None]*offsets[None, :, 1]) + along = np.clip(projection, start[first:last, None], length[first:last, None]) + square_distance = squared[None] + along*(along-2*projection) + distances[first:last] = np.sqrt(np.maximum(0., np.min(square_distance, axis=1))) return distances def clearance_cost(self, candidates, pose, geometry, now): @@ -314,6 +318,15 @@ class ObservedGoals: and (candidate_mask is None or candidate_mask(before[None])[0]) and supported(before)): retain = before + retained_clearance = (self.approach_clearance(retain[None], pose, geometry, now)[0] + if retain is not None else None) + if retain is not None and retained_clearance >= geometry['width_m']: + # The selection rule below cannot replace this still supported + # intention. Avoid recomputing all alternative approaches each + # sensor tick: doing so used most of the original plan lease. + self.evidence['approach_clearance_m'] = ( + float(retained_clearance) if math.isfinite(retained_clearance) else None) + return retain.tolist() # Score the task direction at the existing 3.5 m observation horizon. # Distance to a remote endpoint mostly rewards longitudinal advance: # slightly farther side returns can beat a clear straight destination @@ -334,8 +347,6 @@ class ObservedGoals: + self.boundary_cost(candidates, geometry, now)) clearance_cost, clearances = self.clearance_cost(candidates, pose, geometry, now) score += clearance_cost - retained_clearance = (self.approach_clearance(retain[None], pose, geometry, now)[0] - if retain is not None else None) for index in np.argsort(score, kind="stable"): if supported(candidates[index]): selected = candidates[index] diff --git a/src/k1link/simulation/ai_polygon/observed_recovery.py b/src/k1link/simulation/ai_polygon/observed_recovery.py index 99ea1875..c3ebfe1b 100644 --- a/src/k1link/simulation/ai_polygon/observed_recovery.py +++ b/src/k1link/simulation/ai_polygon/observed_recovery.py @@ -142,6 +142,11 @@ class ObservedRecovery: yaw = None if self.phase == "surveying": yaw = next((v for v in self.sectors if v not in self.views), None) + elif self.phase == "executing" and self.selected and self.selected["reverse"]: + # BackUp moves along the rear body axis. Near its destination a + # centimetre of lateral error can flip the goal bearing sideways; + # that is not a change of travel direction or a new view request. + yaw = 180 elif self.phase == "executing" and self.selected: dx, dy = (self.selected["goal"][i] - pose[i] for i in range(2)) bearing = math.degrees(angle(math.atan2(dy, dx) - heading(pose))) diff --git a/tests/test_ai_polygon_observed_goals.py b/tests/test_ai_polygon_observed_goals.py index f9d4bf1f..5652a08c 100644 --- a/tests/test_ai_polygon_observed_goals.py +++ b/tests/test_ai_polygon_observed_goals.py @@ -316,3 +316,41 @@ def test_clearance_receipt_is_bound_and_does_not_invent_semantics(selector,misma elif mismatch=='pose':position=[1.,*position[1:]] selector.observe_navigation(receipt,position,seconds,run,1.) assert selector.clearance_observation is None and not selector.surface.cells + + +def test_clear_retained_intent_does_not_recompute_alternative_approaches(selector, monkeypatch): + points, position, target, _, _ = road() + selector.observe(np.c_[points, np.ones(len(points))], 1., position) + goal = selector.choose(points, position, target, None, (), GEOMETRY, 1.) + selector.observe_navigation(clearance_receipt(position, [[1.6, 1.5]]), position, 1, 'run', 1.) + evaluate = selector.approach_clearance + sizes = [] + def measured(candidates, *args): + sizes.append(len(candidates)) + return evaluate(candidates, *args) + monkeypatch.setattr(selector, 'approach_clearance', measured) + assert selector.choose(points, position, target, goal, (), GEOMETRY, 1.1) == goal + assert sizes == [1] + # New nearby geometry must still cause the alternatives to be evaluated. + selector.observe_navigation(clearance_receipt(position, [[1.6, 0.]]), position, 1, 'run', 1.1) + assert selector.choose(points, position, target, goal, (), GEOMETRY, 1.2) != goal + assert max(sizes) > 1 + + +def test_clearance_matches_exact_continuous_segments(selector): + rng = np.random.default_rng(122) + position = [17., -31., .37, 0., 0., 0., 1.] + candidates = np.c_[rng.uniform(-3., 3., (259, 2))+position[:2], np.zeros(259)] + obstacles = rng.uniform(-4., 4., (137, 2))+position[:2] + selector.observe_navigation(clearance_receipt(position, obstacles.tolist()), position, 1, 'run', 1.) + actual = selector.approach_clearance(candidates, position, GEOMETRY, 1.1) + expected = [] + for goal in candidates: + delta = goal[:2]-position[:2] + length = np.linalg.norm(delta) + direction = delta/length + a = np.asarray(position[:2])+min(length, GEOMETRY['width_m']/2)*direction + b = goal[:2] + projection = np.clip((obstacles-a) @ (b-a) / max(np.sum((b-a)**2), 1e-20), 0, 1) + expected.append(np.min(np.linalg.norm(obstacles-(a+projection[:, None]*(b-a)), axis=1))) + np.testing.assert_allclose(actual, expected, atol=1e-7, rtol=1e-7) diff --git a/tests/test_observed_recovery.py b/tests/test_observed_recovery.py index 79732b7d..c02455b6 100644 --- a/tests/test_observed_recovery.py +++ b/tests/test_observed_recovery.py @@ -365,3 +365,18 @@ def test_native_owner_does_not_bypass_unknown_view_or_progress_budget(): assert r.excluded == [selected['goal']] r.tick(pose(-.45), now+12.1, choose) assert r.native_action is None + + +def test_backup_keeps_rear_observation_when_lateral_error_dominates_near_goal(): + r = ObservedRecovery() + r.phase = 'executing' + r.sequence, r.option = 1, 1 + r.selected = dict(goal=[-.7, 0., 0.], reverse=True) + request = r.view_request(pose(), 1.) + assert request['yaw_degrees'] == 180 + near = pose() + near[0], near[1] = -.68, -.05 + assert r.view_request(near, 2.) == request + # Observation freshness still expires; stable direction grants no motion. + r.observe_view(request, pose(), 1.) + assert not r.view_current(near, 4.)