Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/experiments/world-model/world-model.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name="description" content="让飞船从试飞中学会预测,再在脑中挑选路线。改变风力,亲手发现世界模型何时会出错。">
<meta name="color-scheme" content="light dark"><link rel="icon" href="data:,">
<link rel="stylesheet" href="./style.css"><link rel="stylesheet" href="./navigation.css?v=three-cat-brand-v1"><link rel="stylesheet" href="./world-model.css?v=planning-1&amp;art=heim-cockpit-v1">
<script type="module" src="./navigation.mjs?v=cats-in-scenes-v1"></script><script type="module" src="./world-model.mjs?v=simulation-beat-v1&amp;art=heim-cockpit-v1"></script>
<script type="module" src="./navigation.mjs?v=cats-in-scenes-v1"></script><script type="module" src="./world-model.mjs?v=frozen-panels-v1&amp;art=heim-cockpit-v1"></script>
<title>算法游乐场 · 脑内预演室</title>
<link rel="stylesheet" href="./space-theme.css">
<link rel="stylesheet" href="./experiment-showcase.css?v=home-aligned-v1">
Expand Down
13 changes: 11 additions & 2 deletions src/experiments/world-model/world-model.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {createLesson,nextLesson,recordLessonFlight,chooseLesson,CHOICES} from '.
const $=id=>document.getElementById(id),media=matchMedia('(prefers-reduced-motion: reduce)');
let session=createSession(),lesson=createLesson(),running=false,frame=null,token=0,lastTime=null,remainder=0,view='preview',comparisonEmphasis='after';
let inspectedPlan=null,inspectedRoute='best';
let renderedRecord=null,renderedPlanning=null;
const routeNames={original:'原路线',direction:'只换方向',best:'再调推力'};
const points=path=>path.map(p=>`${p.x.toFixed(3)},${p.y.toFixed(3)}`).join(' ');
const windText=wind=>`${wind>0?'↓ 下吹':'↑ 上吹'} ${Math.abs(wind).toFixed(1)}`;
Expand Down Expand Up @@ -104,6 +105,9 @@ function renderComparison(c){
$('comparison-hint').textContent=`正在突出${comparisonEmphasis==='before'?'学习前':'学习后'}预测。橙色实线始终是同一趟真实轨迹,坐标和原成绩不变。`;
}
const saved=session.comparison;$('learning-record').hidden=!saved;
// Session records are immutable; a new flight does not change the saved evidence.
if(saved===renderedRecord)return;
renderedRecord=saved;
if(!saved)return;
$('record-context').textContent=`${comparisonConditions(saved)} · M${saved.beforeVersion} → M${saved.afterVersion} · 起点、目标与整段推力相同`;
const axes=bounds([saved.before,saved.after,saved.actual],saved.target);
Expand All @@ -118,8 +122,14 @@ function renderComparison(c){
function renderPlanning(){
const plan=session.plan,active=Boolean(session.flight&&!session.flight.done);
$('planning-lab').hidden=!plan;$('planning-entry').hidden=!plan||active;
if(!plan){inspectedPlan=null;return;}
if(!plan){inspectedPlan=null;renderedPlanning=null;return;}
if(inspectedPlan!==plan){inspectedPlan=plan;inspectedRoute=plan.selectedKey;}
const f=session.flight,hasResult=view==='flight'&&f?.done&&f.modelVersion===plan.modelVersion;
const result=hasResult?f:null;
// Only simulation progress changes on a flight beat. Refresh this frozen panel
// when its route, interaction availability, or completed result actually changes.
if(renderedPlanning?.plan===plan&&renderedPlanning.route===inspectedRoute&&renderedPlanning.active===active&&renderedPlanning.result===result)return;
renderedPlanning={plan,route:inspectedRoute,active,result};
const selected=plan.alternatives.find(c=>c.key===inspectedRoute),best=plan.alternatives.at(-1);
const axes=bounds(plan.alternatives.map(c=>c.path),plan.target);
const reference=plan.alternatives[inspectedRoute==='original'?2:0],end=selected.path.at(-1);
Expand Down Expand Up @@ -148,7 +158,6 @@ function renderPlanning(){
const detail=`正在看「${routeNames[inspectedRoute]}」:预测${selected.distance<=plan.target.radius?'落在圈内':'没进圈'}。${why} 完整搜索选中的组合预测距靶心 ${errorText(best.distance)} 单位,但这不是实飞成绩。`;
if($('planning-detail').textContent!==detail)$('planning-detail').textContent=detail;
$('planning-launch').textContent=`用「${routeNames[inspectedRoute]}」实飞 ↗`;$('planning-launch').disabled=active;
const f=session.flight,hasResult=view==='flight'&&f?.done&&f.modelVersion===plan.modelVersion;
$('planning-result').hidden=!hasResult;
if(hasResult)$('planning-result').textContent=`刚才实飞「${routeNames[plan.selectedKey]}」:出发时预测距靶心 ${errorText(plan.distance)} → 实际 ${errorText(f.distance)} 单位。${f.distance<=f.target.radius?'这次实际进圈了。':'这次实际没进圈。'}${f.error>3?'模型的轨迹预测仍有偏差;选得再认真,也需要真实经历来校准。':'预测和实际较接近;能否进圈还取决于选的路线。'}`;
}
Expand Down
50 changes: 50 additions & 0 deletions tests/experiments/world-model/world-model-ui.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,56 @@ test('flight paints only when a simulation beat advances, even at 120Hz',async()
assert.equal(u.els.get('target-distance').textContent,'14.1 单位');
}finally{u.restore();}
});
function trackPanelWrites(els,prefix){
let writes=0;
for(const [id,el] of els){
if(!id.startsWith(prefix))continue;
let text=el.textContent;const set=el.setAttribute.bind(el);
Object.defineProperty(el,'textContent',{get:()=>text,set:value=>{text=value;writes++;},configurable:true});
el.setAttribute=(key,value)=>{writes++;set(key,value);};
}
return ()=>writes;
}
test('saved learning evidence is not rebuilt during the next real flight or pause',async()=>{
const u=await boot({source:true});try{
u.click('fly');u.finish();u.click('learn');u.click('plan');
const record=u.els.get('record-after-predicted').getAttribute('points');
const writes=trackPanelWrites(u.els,'record-');
u.click('fly');u.tick(0);for(let i=1;i<60;i++)u.tick(i*100);
u.click('pause');u.click('step');
assert.equal(writes(),0,'the saved flight has not changed during 60 new simulation beats');
assert.equal(u.els.get('record-after-predicted').getAttribute('points'),record);
assert.match(u.els.get('progress').textContent,/60 \/ 60/);
u.click('learn');
assert.ok(writes()>0,'learning a new flight must replace the saved evidence');
assert.match(u.els.get('record-context').textContent,/第 2 趟/);
u.click('reset');assert.equal(u.els.get('learning-record').hidden,true);
}finally{u.restore();}
});
test('frozen planning cards do not repaint on flight beats but completion and inspection refresh them',async()=>{
const u=await boot({source:true});try{
u.click('plan');u.click('planning-original');u.click('planning-launch');
const route=u.els.get('planning-map-route').getAttribute('points');
const writes=trackPanelWrites(u.els,'planning-');
u.tick(0);for(let i=1;i<59;i++)u.tick(i*100);
u.click('pause');u.click('step');
assert.equal(writes(),0,'only the actual flight is changing, not its frozen route candidates');
assert.equal(u.els.get('planning-map-route').getAttribute('points'),route);
assert.equal(u.els.get('planning-launch').disabled,true);
assert.equal(u.els.get('planning-result').hidden,true);
assert.match(u.els.get('progress').textContent,/59 \/ 60/);
u.click('step');
assert.ok(writes()>0,'completion must unlock controls and show the real result');
assert.equal(u.els.get('planning-launch').disabled,false);
assert.equal(u.els.get('planning-result').hidden,false);
assert.match(u.els.get('planning-result').textContent,/原路线.*实际/);
const completed=writes();u.click('planning-best');
assert.ok(writes()>completed,'inspecting another candidate must refresh the route comparison');
assert.equal(u.els.get('planning-best').getAttribute('aria-pressed'),'true');
assert.notEqual(u.els.get('planning-map-route').getAttribute('points'),route);
assert.match(u.els.get('planning-result').textContent,/原路线/,'inspection cannot relabel the actual flight');
}finally{u.restore();}
});
test('Heim reports only the displayed flight stage and waits for actual completion before a result',async()=>{
const u=await boot();try{
const e=id=>u.els.get(id);
Expand Down
Loading