Spaces:
Running
Running
Download python/borderland_verify/certificates.py from FatUnc6969/borderland-rl: direct link, hf CLI and curl.
- Browser
- Download file 5 kB
-
https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_verify/certificates.py
- Command line
-
hf download hf://spaces/FatUnc6969/borderland-rl/python/borderland_verify/certificates.py
-
curl -L -o certificates.py https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_verify/certificates.py
5 kB
| from __future__ import annotations | |
| from copy import copy, deepcopy | |
| from dataclasses import asdict | |
| from borderland import Campaign, Action | |
| from borderland.domain import canonical | |
| from .reference import execute_reference | |
| def _branch(env): | |
| result=copy(env) | |
| result._state=deepcopy(env._state) | |
| result._events=[] | |
| return result | |
| def _optimal_suffix(env,memo,counts): | |
| t=env.observe() | |
| if t.done: | |
| return float(t.outcome=='escaped') | |
| key=canonical(t.to_dict()) | |
| if key in memo: | |
| return memo[key] | |
| counts[0]+=1 | |
| best=0.0 | |
| for action in t.legal_actions: | |
| branch=_branch(env) | |
| result=branch.step(action) | |
| if result.error: | |
| raise RuntimeError('Advertised legal action rejected') | |
| if result.observation.charge>=t.observation.charge: | |
| raise RuntimeError('Nondecreasing suffix charge') | |
| best=max(best,_optimal_suffix(branch,memo,counts)) | |
| memo[key]=best | |
| return best | |
| def _prefix(env): | |
| result=execute_reference(env,stop_before_valve=True) | |
| if result['failure'] or env.observe().observation.phase!=7: | |
| raise RuntimeError('Legal prefix failed: '+str(result['failure'])) | |
| t=env.observe() | |
| while t.observation.charge>5: | |
| t=env.step(Action('align_track',dict(position=t.observation.board['track']))) | |
| if t.error: raise RuntimeError('Budget equalization was not legal') | |
| t=env.replace_notebook('') | |
| if t.observation.charge!=5: | |
| raise RuntimeError('Insufficient charge at certificate decision') | |
| return [deepcopy(e) for e in env._events] | |
| def certify_twins(seed): | |
| seeds=[2*(seed//2),2*(seed//2)+1] | |
| envs=[Campaign.generated(s) for s in seeds] | |
| failures=[] | |
| prefixes=[] | |
| for env in envs: | |
| try: prefixes.append(_prefix(env)) | |
| except RuntimeError as error: failures.append(str(error)) | |
| if failures: | |
| return dict(scope='rule-chamber to final-valve only',**{'pass':False},failures=failures,prefixes=prefixes) | |
| views=[env.observe().to_dict() for env in envs] | |
| public_bytes=[canonical(v) for v in views] | |
| equality=dict(complete_transition=public_bytes[0]==public_bytes[1],observation=canonical(views[0]['observation'])==canonical(views[1]['observation']),legal_actions=canonical(views[0]['legal_actions'])==canonical(views[1]['legal_actions']),notebooks_empty=all(v['observation']['notebook']['text']=='' for v in views)) | |
| specs=[asdict(e._world) for e in envs] | |
| for spec in specs: | |
| spec.pop('polarity'); spec.pop('private_seed') | |
| intervention_equal=canonical(specs[0])==canonical(specs[1]) | |
| action_values=[]; suffixes=[]; node_counts=[] | |
| for env in envs: | |
| values={}; memo={}; counts=[0]; wins=[] | |
| for action in env.observe().legal_actions: | |
| branch=_branch(env); branch.step(action) | |
| q=_optimal_suffix(branch,memo,counts) | |
| values[canonical(action.to_dict())]=q | |
| if action.tag=='open_valve': | |
| end=_branch(env); first=end.step(action); final=end.step(Action('launch',{})) | |
| wins.append(dict(action=action.to_dict(),valve_transition=first.to_dict(),launch_transition=final.to_dict(),return_value=float(final.outcome=='escaped'))) | |
| action_values.append(values); suffixes.append(wins); node_counts.append(counts[0]) | |
| west=canonical(Action('open_valve',dict(side='west')).to_dict()) | |
| east=canonical(Action('open_valve',dict(side='east')).to_dict()) | |
| opposite=action_values[0].get(west)==1 and action_values[0].get(east)==0 and action_values[1].get(west)==0 and action_values[1].get(east)==1 | |
| alternatives=set(action_values[0])|set(action_values[1]) | |
| bound=max((action_values[0].get(a,0)+action_values[1].get(a,0))/2 for a in alternatives) | |
| all_delay_zero=all(v==0 for table in action_values for key,v in table.items() if key not in (west,east)) | |
| checks=list(equality.values())+[intervention_equal,opposite,all_delay_zero,bound==0.5] | |
| if not all(checks): failures.append('A matched-view, intervention or suffix-value premise failed.') | |
| return dict(scope='Rule chamber wiring intervention, mediated through Blackout, to final-valve decision; equal mixture of this pair only',seeds=seeds,prefixes=prefixes,decision_views=views,canonical_policy_inputs=public_bytes,equality=equality,intervention_equal=intervention_equal,action_values=action_values,suffixes=suffixes,enumerated_states=node_counts,oracle_ceiling=1.0,fixed_memoryless_distribution_bound=bound,fixed_memoryless_bound=bound,all_delay_actions_zero=all_delay_zero,**{'pass':all(checks)},failures=failures,limits=['No full-campaign memoryless impossibility claim.','Notebook carrying wiring facts resolves this ambiguity.','No proof of every Archive, Maze, Witness or Osmosis dependency.'],enumeration='Every legal gameplay continuation is traversed. Each spends positive charge, so depth is at most five. Notes cannot reveal new past facts or alter physical return; the certified policy input has an empty notebook.') | |