Spaces:
Running
Running
Commit ·
08497aa
1
Parent(s): cb06b03
clean now
Browse files2987 unmodified lines2988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530862987 unmodified linesthis._aiLastObs = null;this.fixedStep = 0;this.aiStep = 0;this.interval = setInterval(() => this.tick(), 400);},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;}}// 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;}}// Error countingif (!fixedSd && !aiSd) {this._errCount++;if (this._errCount > 5) {log('Battle aborted — too many errors', 'err');this.stop(); return;}} 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();}finish() {if (!this.running) return; // guard against double-finish2987 unmodified lines29882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130722987 unmodified linesthis._aiLastObs = null;this.fixedStep = 0;this.aiStep = 0;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; }// Track consecutive errorsthis._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 manyif (!fixedSd && !aiSd) {this._errCount++;if (this._errCount > 5) {log('Battle aborted — too many consecutive errors', 'err');this.stop(); return;}} else {this._errCount = 0;}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(); }},finish() {if (!this.running) return; // guard against double-finish
app/viz/dashboard.py+71-85