"""Evidence-shaped, no-Blender benchmark for living-room search ordering. The evidence bundle does not contain the original prepared candidate pools, so this benchmark uses measured room plans and conservative synthetic sofa/console/TV sizes. It exercises the same wall-segment and pair-ordering helpers used by the solver. """ import json import os from pathlib import Path import sys import time import pytest NATIVE = Path(__file__).resolve().parents[1] / 'source/scripts/internal/scene_generation/real_assets' sys.path[:0] = [str(NATIVE), str(NATIVE.parent)] import joint_layout as joint ROOT = Path(__file__).resolve().parents[3] DEFAULT_EVIDENCE = ROOT / 'evidence' / 'living_evidence.json' BUDGET_CASES = ('coll_5', 'samp_s06', 'samp_s07', 'samp_s12') SUCCESS_CASES = ('coll_4', 'rc1_r05', 'samp_s12') def _front_yaw(side): return {'N': 0., 'E': 90., 'S': 180., 'W': 270.}[side] def _opposite(side): return {'N': 'S', 'S': 'N', 'E': 'W', 'W': 'E'}[side] def _pose(room, side, tangent, dimensions, *, role, eid): x0, y0, x1, y1 = room['bounds_m'] wall = _opposite(side) if role == 'sofa' else side if wall == 'S': center = [tangent, y0 + dimensions[1] / 2. + .12] elif wall == 'N': center = [tangent, y1 - dimensions[1] / 2. - .12] elif wall == 'W': center = [x0 + dimensions[1] / 2. + .12, tangent] else: center = [x1 - dimensions[1] / 2. - .12, tangent] return dict(entity_id=eid, room_id=room['room_id'], role=role, center_xy_m=center, yaw_deg=_front_yaw(side), canonical_front_axis='+Y', dimensions_m=list(dimensions), wall_anchor=side, _cost=0.) def _evidence_path(): configured = os.environ.get('RHG_LIVING_EVIDENCE') return Path(configured).expanduser() if configured else DEFAULT_EVIDENCE def _case_inputs(case): evidence_path = _evidence_path() if not evidence_path.is_file(): pytest.skip( f'living benchmark evidence not found at {evidence_path}; ' 'set RHG_LIVING_EVIDENCE to the evidence JSON path' ) evidence = json.loads(evidence_path.read_text()) plan = evidence['plans'][case] room = next(row for row in plan['rooms'] if row['room_id'] == 'living') width = room['bounds_m'][2] - room['bounds_m'][0] depth = room['bounds_m'][3] - room['bounds_m'][1] sofa_dims = [min(1.8, width * .38), min(.85, depth * .22), .8] console_dims = [min(1.2, width * .28), .4, .7] tv_dims = [min(.8, width * .20), .2, .5] sofa_entity = next(e for e in evidence['attempts'] if e.get('case') == case and e.get('living'))['living_entities'][0] declared = sofa_entity.get('wall_anchor') or 'N' walls = joint._living_wall_segments(plan, room) sofas = [] consoles = [] for side in ('N', 'E', 'S', 'W'): tangent_lo, tangent_hi = ((room['bounds_m'][0], room['bounds_m'][2]) if side in {'N', 'S'} else (room['bounds_m'][1], room['bounds_m'][3])) samples = [tangent_lo + .25, (tangent_lo + tangent_hi) / 2., tangent_hi - .25] for tangent in samples: sofas.append(_pose(room, side, tangent, sofa_dims, role='sofa', eid='sofa')) consoles.append(_pose(room, side, tangent, console_dims, role='media_console', eid='media_console')) tv_candidates = [dict(dimensions_m=tv_dims, canonical_front_axis='+Y')] console_candidates = [dict(dimensions_m=console_dims, canonical_front_axis='+Y')] return plan, room, declared, walls, sofas, consoles, console_candidates, tv_candidates def _feasible_pair(sofa, console, room, plan): target = joint._living_front_side(sofa) if joint._living_pose_wall_side(console, room) != target: return False if joint._living_front_side(console) != _opposite(target): return False return joint._living_pair_score(sofa, room, plan, [dict(dimensions_m=console['dimensions_m'], canonical_front_axis='+Y')], [dict(dimensions_m=[.8, .2, .5], canonical_front_axis='+Y')])[0] == 0 def _run(case, optimized): started = time.perf_counter() plan, room, declared, walls, sofas, consoles, console_candidates, tv_candidates = _case_inputs(case) if optimized: sofas = sorted(sofas, key=lambda p: ( joint._living_pair_score(p, room, plan, console_candidates, tv_candidates), p['center_xy_m'])) else: sofas = sorted(sofas, key=lambda p: (0 if joint._living_front_side(p) == declared else 1, p['center_xy_m'])) nodes = 0 for sofa in sofas: nodes += 1 ordered = consoles if optimized: ordered = sorted(consoles, key=lambda p: joint._living_console_pose_key( p, sofa, room, plan, tv_candidates)) for console in ordered: nodes += 1 if _feasible_pair(sofa, console, room, plan): return dict(nodes=nodes, solved=True, sofa=sofa['center_xy_m'], console=console['center_xy_m'], layout_loss=round(abs(sofa['center_xy_m'][0] - console['center_xy_m'][0]) + abs(sofa['center_xy_m'][1] - console['center_xy_m'][1]), 6), elapsed_s=round(time.perf_counter() - started, 6)) return dict(nodes=nodes, solved=False, sofa=None, console=None, layout_loss=None, elapsed_s=round(time.perf_counter() - started, 6)) @pytest.mark.parametrize('case', BUDGET_CASES) def test_evidence_shaped_budget_cases_do_not_expand_more_nodes(case): baseline = _run(case, optimized=False) improved = _run(case, optimized=True) assert improved['nodes'] <= baseline['nodes'] @pytest.mark.parametrize('case', SUCCESS_CASES) def test_evidence_shaped_success_cases_keep_a_solution_and_loss(case): baseline = _run(case, optimized=False) improved = _run(case, optimized=True) assert improved['solved'] assert baseline['solved'] assert improved['layout_loss'] <= baseline['layout_loss'] + 1e-6