Spaces:
Running
Running
Commit ·
08497aa
1
Parent(s): cb06b03
clean now
Browse files2987 unmodified lines298829892990299129912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044302130453023302430253026302730283029303030313032303330343035303630373038303930403041304230433046304730483049305030513052305330543045305530473048304930503051305230563057305830593060306130623063306430653054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230663067306830693070307130722987 unmodified lines this._aiLastObs = null; this.fixedStep = 0; this.aiStep = 0; this.interval = setInterval(() => this.tick(), 400); this.interval = setInterval(() => this.tick(), 160); },
async tick() { if (!this.running) return; var done1 = this.fixed.step >= this.horizon; var done2 = this.ai.step >= this.horizon; if (done1 && done2) { this.finish(); return; }
this._errCount = this._errCount || 0;
let fixedSd = null; let aiSd = null;
// Step 1: Fixed timer (always first) if (!done1) { this.fixedTimer++; var fixedAction = Math.floor(this.fixedTimer / this.FIXED_CYCLE) % 2 === 0 ? 0 : 1; try { const res = await fetch(BASE + '/step', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: this.fixedSessionId, action: fixedAction }) }); if (res.ok) fixedSd = await res.json(); else throw new Error('HTTP ' + res.status); } catch(e) { log('Fixed step error: ' + e.message, 'err'); fixedSd = null; async tick() { if (!this.running) return; var done1 = this.fixed.step >= this.horizon; var done2 = this.ai.step >= this.horizon; if (done1 && done2) { this.finish(); return; }
// Track consecutive errors this._errCount = this._errCount || 0;
var promises = [];
if (!done1) { this.fixedTimer++; var fixedAction = Math.floor(this.fixedTimer / this.FIXED_CYCLE) % 2 === 0 ? 0 : 1; promises.push( fetch(BASE+'/step',{method:'POST',headers:{'Content-Type':'application/json'}, body:JSON.stringify({session_id:this.fixedSessionId,action:fixedAction})}) .then(r=>{ if(!r.ok) throw new Error('HTTP '+r.status); return r.json(); }) .catch(e=>{log('Battle step err: '+e.message,'err');return null;}) ); } else { promises.push(Promise.resolve(null)); }
if (!done2) { var aiAction = 0; if (DQN.online) { var tmpState = DQN.extractState(this._aiLastObs || null); var qv = DQN.online.forward(tmpState); var rawA = 0; for(var i=1;i<5;i++) if(qv[i]>qv[rawA]) rawA=i; aiAction = rawA; } promises.push( fetch(BASE+'/step',{method:'POST',headers:{'Content-Type':'application/json'}, body:JSON.stringify({session_id:this.aiSessionId,action:aiAction})}) .then(r=>{ if(!r.ok) throw new Error('HTTP '+r.status); return r.json(); }) .catch(e=>{log('Battle step err: '+e.message,'err');return null;}) ); } else { promises.push(Promise.resolve(null)); }
var [fixedSd, aiSd] = await Promise.all(promises);
// If both returned null (errors), increment error count and stop if too many if (!fixedSd && !aiSd) { this._errCount++; if (this._errCount > 5) { log('Battle aborted — too many consecutive errors', 'err'); this.stop(); return; } } else { this._errCount = 0; } }
// Step 2: AI step (only if fixed step succeeded or we ignore) if (!done2) { let aiAction = 0; if (DQN.online) { var tmpState = DQN.extractState(this._aiLastObs || null); var qv = DQN.online.forward(tmpState); let bestA = 0; for (let i = 1; i < 5; i++) if (qv[i] > qv[bestA]) bestA = i; aiAction = bestA; } try { const res = await fetch(BASE + '/step', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: this.aiSessionId, action: aiAction }) }); if (res.ok) aiSd = await res.json(); else throw new Error('HTTP ' + res.status); } catch(e) { log('AI step error: ' + e.message, 'err'); aiSd = null; if (fixedSd) { this.fixed.step = fixedSd.step || 0; var fi = fixedSd.info || {}; this.fixed.cleared = fi.total_cleared || 0; if (fi.avg_delay > 0) { this.fixed.totalWait += fi.avg_delay; this.fixed.waitCount++; } if (fi.los) this.fixed.los = fi.los; this.fixedQHist.push((fi.ns_queue||0)+(fi.ew_queue||0)); if (this.fixedQHist.length > 120) this.fixedQHist.shift(); } }
// Error counting if (!fixedSd && !aiSd) { this._errCount++; if (this._errCount > 5) { log('Battle aborted — too many errors', 'err'); this.stop(); return; if (aiSd) { this._aiLastObs = aiSd.observation; this.ai.step = aiSd.step || 0; var ai = aiSd.info || {}; this.ai.cleared = ai.total_cleared || 0; if (ai.avg_delay > 0) { this.ai.totalWait += ai.avg_delay; this.ai.waitCount++; } if (ai.los) this.ai.los = ai.los; this.aiQHist.push((ai.ns_queue||0)+(ai.ew_queue||0)); if (this.aiQHist.length > 120) this.aiQHist.shift(); } } else { this._errCount = 0; }
// Process responses (same as before) if (fixedSd) { this.fixed.step = fixedSd.step || 0; var fi = fixedSd.info || {}; this.fixed.cleared = fi.total_cleared || 0; if (fi.avg_delay > 0) { this.fixed.totalWait += fi.avg_delay; this.fixed.waitCount++; } if (fi.los) this.fixed.los = fi.los; this.fixedQHist.push((fi.ns_queue||0)+(fi.ew_queue||0)); if (this.fixedQHist.length > 120) this.fixedQHist.shift(); }
if (aiSd) { this._aiLastObs = aiSd.observation; this.ai.step = aiSd.step || 0; var ai = aiSd.info || {}; this.ai.cleared = ai.total_cleared || 0; if (ai.avg_delay > 0) { this.ai.totalWait += ai.avg_delay; this.ai.waitCount++; } if (ai.los) this.ai.los = ai.los; this.aiQHist.push((ai.ns_queue||0)+(ai.ew_queue||0)); if (this.aiQHist.length > 120) this.aiQHist.shift(); }
this.updateUI(); if (this.fixed.step >= this.horizon && this.ai.step >= this.horizon) this.finish();}
this.updateUI(); if (this.fixed.step >= this.horizon && this.ai.step >= this.horizon) { this.finish(); } },
finish() { if (!this.running) return; // guard against double-finishapp/viz/dashboard.py+71-85