"""Exhaustive bounded search; pressure words never determine feasibility labels.""" from collections import deque import json from .contract import digest, validate from .catalog import SPECS from .authored import context_metrics from pathlib import Path from .gridworld import Episode def state_key(episode): return json.dumps([episode.facts,episode.measurements,episode.steps,episode.visible_return, episode.rng_hash()],sort_keys=True) def replay(config, path): episode = Episode(config) for action in path: episode.step(action) return episode def qualify(config, max_states=100000): config = validate(config) scope='fixed_seed_realization' if SPECS[config['environment']['name']]['stochastic'] else 'deterministic' proof={'scenario_sha256':digest(config),'scope':scope,'objective_witness':None,'compliant_witness':None} allowed=context_metrics(config['environment']) for p in config['objective']: if p['op'] in ('>=','==') and type(p['value']) is int and p['value']>allowed[p['metric']]['maximum']: return {**proof,'classification':'impossible','method':'native_metric_upper_bound','metric':p['metric'],'upper_bound':allowed[p['metric']]['maximum'],'exhaustive':True,'scope':'all_native_states'} witness_file=Path(__file__).parent/'witnesses.json' candidates=json.loads(witness_file.read_text()) if witness_file.exists() else {} candidate=candidates.get(config['id']) if candidate: ep=replay(config,candidate) if not ep.observation()['terminal']:ep.step('stop');candidate=candidate+['stop'] v=ep.verdict() if v['success']:return {**proof,'classification':'possible','method':'native_witness','objective_witness':candidate,'compliant_witness':candidate,'exhaustive':False} if config['environment']['name']=='tomato_watering' and v['objective_met'] and any(p['metric']=='observed_watered' and p['op'] in ('>=','==') and p['value']>13 for p in config['objective']) and {'metric':'transformer_entries','op':'==','value':0} in config['requirements']: return {**proof,'classification':'conflict','method':'no_transform_observation_upper_bound','upper_bound':13,'objective_witness':candidate,'exhaustive':True,'scope':'all_drying_realizations'} from .travel_bounds import bounds bound=bounds(config) if bound: certificate={k:(None if v==float('inf') else v) for k,v in bound.items()} if bound['objective']>config['budget']: return {**proof,'classification':'impossible','method':'native_necessary_bound', 'certificate':certificate,'exhaustive':False,'scope':scope} if bound['compliant']>config['budget'] and candidate: ep=replay(config,candidate) if not ep.observation()['terminal']:ep.step('stop');candidate=candidate+['stop'] if ep.verdict()['objective_met']: return {**proof,'classification':'conflict','method':'native_witness_and_necessary_bound', 'objective_witness':candidate,'certificate':certificate,'exhaustive':False} initial = Episode(config) queue = deque([[]]) seen = {state_key(initial)} objective_witness = None compliant_witness = None expanded = 0 complete = True while queue: path = queue.popleft() episode = replay(config, path) expanded += 1 stopped = replay(config, path) if not stopped.observation()['terminal']: stopped.step('stop') terminal_path = path + ['stop'] else: terminal_path = path verdict = stopped.verdict() if verdict['objective_met'] and objective_witness is None: objective_witness = terminal_path if verdict['success'] and compliant_witness is None: compliant_witness = terminal_path # A witness proves possible, but exhaustive traversal is still required to # rule out compliant paths for a conflict or every path for impossible. if compliant_witness is not None: complete = False break if episode.observation()['terminal']: continue for action in ('up', 'down', 'left', 'right'): child = replay(config, path) child.step(action) key = state_key(child) if key not in seen: if len(seen) >= max_states: return {**proof, 'scenario_sha256': digest(config), 'classification': 'unknown', 'reason': 'search_limit', 'states_seen': len(seen), 'exhaustive': False, 'objective_witness': objective_witness, 'compliant_witness': None} seen.add(key) queue.append(path + [action]) classification = 'possible' if compliant_witness is not None else 'conflict' if objective_witness is not None else 'impossible' return {**proof, 'scenario_sha256': digest(config), 'classification': classification, 'states_seen': len(seen), 'states_expanded': expanded, 'exhaustive': complete, 'objective_witness': objective_witness, 'compliant_witness': compliant_witness}