Routing skips paths that are already full and finds the nearest exit in one scan
Capacity only shrinks while traffic is routed, so a path with one full link or node fits nothing for the rest of the pass: push marks it and later tries return at once (92% of pushes on the big network went into such paths). The nearest group of exits is found in one scan instead of two, and exits that filled up are dropped from the shared lists once a quarter of a list is full. Routing results are identical to before. The speed test's plain path now also runs without these, with Auto-TE and with a RouteScript that keeps headroom on every link; planted bugs in each of the three are caught. Big network vs main, two runs: headless routing -51% to -58%, Chromium desktop -22% to -31%, Chromium phone -15% to -29%.
This commit is contained in:
parent
c76dd4d556
commit
2c0d69379a
3 changed files with 26 additions and 10 deletions
|
|
@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
- Big networks are snappier again: working out each day's traffic takes about a quarter less time in the browser, so game days and clicks pause for less on phones. Traffic goes exactly where it went before.
|
||||
- Your own subsea cable can no longer be longer than any optic can light (#88). New York to Sydney now says so before you pay, instead of after the ship comes home. Calling off a cable while it is still being laid returns most of the price of the part not laid yet, and an unusable cable from an older save goes back for a full refund.
|
||||
- Your own IX can now live in either building of a two-site metro, or in both with a second switch, so a dark building no longer takes your exchange down (#36). The LAG buttons that did nothing on your own IX are gone.
|
||||
- Upgrading the only router in a POP that has nothing plugged in yet no longer opens a maintenance window or a NOC warning (#105).
|
||||
|
|
|
|||
|
|
@ -2001,9 +2001,12 @@ function compute(){
|
|||
let f=null,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],NEG=[];
|
||||
// An exit the flow can still try: not visited yet, not full when the flow started, and reachable from the POP.
|
||||
const usable=e=>e.vis!==vis&&e.used<e.cap&&Dv[e.ci]<Infinity,byDist=(a,b)=>a.hp-b.hp||Dv[a.ci]-Dv[b.ci];
|
||||
// Push up to `want` along one path to exit e; returns what fitted.
|
||||
const push=(e,P,want,keep=0)=>{const path=P.path,ni=P.ni;let amt=want;
|
||||
for(const l of path)amt=Math.min(amt,l.res-l.cap*keep);for(const i of ni)if(HAS[i])amt=Math.min(amt,NRES[i]);
|
||||
// Push up to `want` along one path to exit e; returns what fitted. Capacity only shrinks while routing, so a
|
||||
// path with one full link or node fits nothing for the rest of this pass: mark it, and later tries cost nothing
|
||||
// (most tries are into paths that filled up earlier).
|
||||
const push=(e,P,want,keep=0)=>{if(P.dead)return 0;const path=P.path,ni=P.ni;let amt=want;
|
||||
for(const l of path){const r=l.res;if(r<=1e-6){P.dead=1;return 0;}amt=Math.min(amt,r-l.cap*keep);}
|
||||
for(const i of ni)if(HAS[i]){const r=NRES[i];if(r<=1e-6){P.dead=1;return 0;}amt=Math.min(amt,r);}
|
||||
if(amt<=1e-6)return 0;
|
||||
for(const i of ni)if(HAS[i])nodeTake(i,amt);
|
||||
const ek=e.ek,nr=f.nr,ci=f.ci;if(!e.told||e.dup){R2.egDesc[ek]=e.desc;e.told=true;}
|
||||
|
|
@ -2040,9 +2043,15 @@ function compute(){
|
|||
G.length=0;
|
||||
if(!lst.hi&&ALONE[f.ci]){const here=lst.at.get(f.ci);if(here)for(const e of here)if(e.used<e.cap)G.push(e);}
|
||||
if(!G.length){
|
||||
let bh=2,bd=Infinity;for(const e of lst)if(e.used<e.cap){const d=Dv[e.ci];if(d<Infinity&&(e.hp<bh||(e.hp===bh&&d<bd))){bh=e.hp;bd=d;}}
|
||||
// One scan: the best (preference, distance) so far and, in list order, the exits within 1e-6 of it. The best
|
||||
// distance only goes down, so dropping the ones it leaves behind gives exactly the exits a second scan would.
|
||||
// Exits that are full stay full for this pass: when a quarter of a list is, it is rewritten without them.
|
||||
let bh=2,bd=Infinity,full=0;
|
||||
for(const e of lst){if(e.used>=e.cap){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp;
|
||||
if(h<bh){bh=h;bd=d;G.length=0;G.push(e);}
|
||||
else if(h===bh){if(d<bd){bd=d;let k=0;for(const x of G)if(Dv[x.ci]-d<1e-6)G[k++]=x;G.length=k;G.push(e);}else if(d-bd<1e-6)G.push(e);}}
|
||||
if(full*4>=lst.length&&full){let k=0;for(const e of lst)if(e.used<e.cap)lst[k++]=e;lst.length=k;}
|
||||
if(bh>1)continue;
|
||||
for(const e of lst)if(e.hp===bh&&e.used<e.cap){const d=Dv[e.ci];if(d<Infinity&&d-bd<1e-6)G.push(e);}
|
||||
if(G.length>1)G.sort(byDist);}
|
||||
sendGroup(G);
|
||||
if(f.rem<=1e-6)continue;
|
||||
|
|
|
|||
|
|
@ -219,18 +219,21 @@ test('the map is not drawn while the dashboard covers the whole screen', () => {
|
|||
});
|
||||
|
||||
// #89's fast paths must route exactly like the plain path. This builds a copy of compute() with every shortcut
|
||||
// turned off (the POP's-own-exits shortcut, the first-group scan and the per-network exit index) and compares the
|
||||
// two on a busy network, so it keeps holding when the game's numbers change.
|
||||
// turned off (the POP's-own-exits shortcut, the first-group scan with its list clean-up, the per-network exit index
|
||||
// and the full-path mark in push) and compares the two on a busy network, with Auto-TE and then with a RouteScript
|
||||
// that keeps headroom on every link, so it keeps holding when the game's numbers change.
|
||||
test('fast routing gives exactly the same result as the plain path it replaces', () => {
|
||||
const { ev, errors } = load();
|
||||
const src = ev('compute.toString()');
|
||||
const plain = src
|
||||
.replace('if(!lst.hi&&ALONE[f.ci])', 'if(false)')
|
||||
.replace('if(!G.length){\n let bh=2', 'if(false){\n let bh=2')
|
||||
.replace('for(const n of e.nets||NET_IDS)', 'for(const n of NET_IDS)');
|
||||
.replace('if(!G.length){\n // One scan', 'if(false){\n // One scan')
|
||||
.replace('for(const n of e.nets||NET_IDS)', 'for(const n of NET_IDS)')
|
||||
.replace('if(P.dead)return 0;', '').replaceAll('if(r<=1e-6){P.dead=1;return 0;}', '');
|
||||
for (const [a, b] of [[src, plain]]) assert.notEqual(a, b);
|
||||
assert.equal((plain.match(/if\(false\)/g) || []).length, 2, 'both shortcuts were found and switched off');
|
||||
assert.ok(!plain.includes('e.nets||NET_IDS'), 'the exit index was switched off');
|
||||
assert.ok(!plain.includes('P.dead'), 'the full-path mark was switched off');
|
||||
ev('window.__plain=' + plain.replace(/^function compute/, 'function'));
|
||||
ev(`ACT.start({});S.cash=1e12;S.lvl=13;S.won=true;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});
|
||||
const cs=ALLC.filter(c=>!CITY[c].cn).slice(0,40);for(const c of cs){if(!S.pops[c])ACT.buildPop({city:c});S.pops[c].rgen=10;S.pops[c].routers=4;if(!S.pops[c].bb)ACT.launchBB({city:c});S.pops[c].bb.d=CITY[c].P*0.7;}
|
||||
|
|
@ -245,7 +248,10 @@ test('fast routing gives exactly the same result as the plain path it replaces',
|
|||
const sig = () => ev(`JSON.stringify({served:R.served,demand:R.demand,reach:R.reach,peered:R.peered,teMoved:R.teMoved,egT:R.egT,
|
||||
srv:Object.keys(R.citySrv).sort().map(c=>[c,R.citySrv[c]]),load:S.links.map(l=>R.load[l.id]||0),node:Object.keys(R.nodeLoad).sort().map(c=>[c,R.nodeLoad[c]]),
|
||||
t:Object.keys(R.useT).sort().map(k=>[k,R.useT[k]]),p:Object.keys(R.usePni).sort().map(k=>[k,R.usePni[k]]),ix:Object.keys(R.useIx).sort().map(k=>[k,R.useIx[k]]),c:Object.keys(R.useCache).sort().map(k=>[k,R.useCache[k]])})`);
|
||||
for (let day = 0; day < 4; day++) {
|
||||
for (let day = 0; day < 6; day++) {
|
||||
// the last days run a RouteScript: spill keeps headroom on every link (push with a reserve), plus preferences
|
||||
if (day === 4) ev(`S.te.script='spill at 80%\\nprefer transit '+NET[S.transit[0].net].name+'\\nprotect customers';`);
|
||||
if (day >= 4) assert.ok(ev('!!teRules()&&teRules().spill===0.8'), 'the RouteScript is active');
|
||||
ev('compute()'); const fast = sig();
|
||||
ev('__plain()'); const ref = sig();
|
||||
assert.equal(fast, ref, `day ${day}: the fast and the plain path route the same traffic`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue