gsfunction export_paper_bench(scenario_indices, method_indices) % Export actual reruns for the HTML viewer. Numerical code remains unchanged. here=fileparts(mfilename('fullpath')); out=fullfile(here,'paper_bench','data'); if nargin<1,scenario_indices=1:5;end if nargin<2,method_indices=1:12;end base=struct('c_ref',true,'c_gate',true); spec={ 'code','Code as is',0,struct('code',true); ... 'paper','Paper as written',0,struct('guard',0); ... 'reference','Reference only',1,struct('c_ref',true); ... 'ref-gate','Reference + gate',2,base; ... 'ref-gate-up','Reference + gate + upward gain',3,merge(base,struct('c_up',true)); ... 'ref-gate-guard','Reference + gate + code guard',3,merge(base,struct('c_guard',true)); ... 'ref-gate-activity','Reference + gate + activity',3,merge(base,struct('c_activity',true)); ... 'ref-gate-smooth','Reference + gate + smoothing',3,merge(base,struct('smooth',true)); ... 'old-rho','Earlier rho-recovery recommendation (withdrawn)',2,struct('recovery','drive','guard',0.02); ... 'slow-rho','Rho recovery + slow power average',2,struct('recovery','drive','eta_q',0.12); ... 'four','Reference + gate + activity + upward gain',4,merge(base,struct('c_activity',true,'c_up',true)); ... 'five','Reference + gate + activity + upward gain + smoothing',5,merge(base,struct('c_activity',true,'c_up',true,'smooth',true))}; for j=1:size(spec,1) methods(j)=struct('id',spec{j,1},'label',spec{j,2},'changes',spec{j,3},'options',spec{j,4}); %#ok end sc={ 'original','Original seeds',0,false; 'seed-7000','Unseen seeds +7000',7000,false; ... 'seed-8000','Unseen seeds +8000',8000,false; 'seed-9000','Unseen seeds +9000',9000,false; ... 'idle','Drive-idle-drive',0,true}; for j=1:size(sc,1) scenarios(j)=struct('id',sc{j,1},'label',sc{j,2},'seedOffset',sc{j,3},'idle',sc{j,4}); %#ok end hist={'Sudden','Gradual'}; bands={'Wideband','Narrowband'}; for k=1:8 cases(k)=struct('id',k,'history',hist{1+mod(k-1,2)},'band',bands{1+mod(floor((k-1)/2),2)},'adaptive',k>4); %#ok end manifest=struct('title','Paper benchmark','target',0.4,'unsafeThreshold',0.38,'scoreStart',5, ... 'methods',methods,'scenarios',scenarios,'cases',cases, ... 'sources',{{'data/study-summary.csv','data/seed-metrics.csv','data/findings.txt','data/equations.tex'}}, ... 'notes',{{'Each adaptive method has its own true margin because lambda changes the loop.', ... 'Unsafe means true margin below 0.38. Metrics use t >= 5 s.', ... 'The benchmark uses a chordal loop margin and a second-smallest estimator bin.', ... 'Idle means excitation off during 55--70 s. Sensor noise remains present.', ... 'Signal traces are saved at 256 Hz. Margin and lambda are saved at 8 Hz.'}}); writejson(fullfile(out,'manifest.json'),manifest); copyfile(fullfile(here,'paper_code_bridge_study.csv'),fullfile(out,'study-summary.csv')); copyfile(fullfile(here,'paper_code_bridge_seed_metrics.csv'),fullfile(out,'seed-metrics.csv')); copyfile(fullfile(here,'paper_code_bridge_findings.txt'),fullfile(out,'findings.txt')); copyfile(fullfile(here,'paper_code_bridge_equations.tex'),fullfile(out,'equations.tex')); memory=load(fullfile(here,'paper_code_bridge_idle_memory.mat')); writejson(fullfile(out,'idle-memory.json'),struct('methods',{memory.result.methods(:,1)'}, ... 'evidenceHighPct',100*memory.result.retained(:,1)', ... 'evidenceLowPct',100*memory.result.retained(:,2)')); for si=scenario_indices scenario=scenarios(si); for mi=method_indices method=methods(mi); runpath=fullfile(out,'runs',scenario.id,[method.id '.json']); if isfile(runpath),continue,end evalc('R=run_code_bridge_benchmark(0.4,method.options,scenario.seedOffset,scenario.idle);'); payload=struct('scenarioId',scenario.id,'methodId',method.id,'target',0.4,'unsafeThreshold',0.38,'scoreStart',5); for k=1:8 c=R.cases(k); rel=sprintf('data/signals/%s/%s/case-%d.json',scenario.id,method.id,k); file=fullfile(here,'paper_bench',strrep(rel,'/',filesep)); writejson(file,struct('t',c.t','dc',round(c.dc',6),'dm',round(c.dm',6))); gzip(file);delete(file); payload.cases(k)=struct('id',c.id,'history',c.history,'band',c.band,'adaptive',c.adaptive, ... 'metrics',struct('err',c.err,'rough',c.rough,'travel',c.travel,'unsafe',c.unsafe), ... 'tc',c.tc','delta',c.delta','b_est',c.b_est','b_true',c.b_true','lambda',c.lambda', ... 'signals',[rel '.gz']); end writejson(runpath,payload); fprintf('Exported %s / %s: 8 complete case histories\n',scenario.id,method.id) end end end function a=merge(a,b) for f=fieldnames(b)',a.(f{1})=b.(f{1});end end function writejson(file,value) folder=fileparts(file);if ~isfolder(folder),mkdir(folder);end fid=fopen(file,'w','n','UTF-8');fwrite(fid,jsonencode(value),'char');fclose(fid); end