gp-lite
Version:
Tiny, zero-dependency GA/GP engine for TypeScript/JS with first-class types, deterministic RNG, and budget-aware runs.
22 lines (18 loc) • 5.45 kB
JavaScript
;var U=(e,o,t)=>new Promise((s,i)=>{var l=n=>{try{u(t.next(n));}catch(a){i(a);}},c=n=>{try{u(t.throw(n));}catch(a){i(a);}},u=n=>n.done?s(n.value):Promise.resolve(n.value).then(l,c);u((t=t.apply(e,o)).next());});var N=e=>Math.max(1,Math.floor(.02*e));function O(e){var c,u,n,a,E,d,M,r,p,m,x,f,T,v;let o=(c=e.popSize)!=null?c:100,t=(u=e.elite)!=null?u:N(o),s=(a=(n=e.maxWallMs)!=null?n:e.timeLimitMs)!=null?a:1/0,i=(E=e.cxProb)!=null?E:.8,l=(d=e.mutProb)!=null?d:1-i;return {popSize:o,generations:(M=e.generations)!=null?M:1e3,elite:t,cxProb:i,mutProb:l,crossoverChildren:(r=e.crossoverChildren)!=null?r:1,immigration:(p=e.immigration)!=null?p:.02,tournament:(m=e.tournament)!=null?m:3,stall:(x=e.stall)!=null?x:50,targetFitness:(f=e.targetFitness)!=null?f:1/0,timeLimitMs:s,maxWallMs:s,maxEvaluations:(T=e.maxEvaluations)!=null?T:1/0,concurrency:(v=e.concurrency)!=null?v:1}}function R(e={},o={}){var P,G,F,b,y,C,L,_;let t=O(e),s=(P=o.expectedGenerations)!=null?P:t.generations,i=Math.floor(t.popSize*t.immigration),l=Math.max(0,t.popSize-t.elite-i),c=Math.ceil(l/2),u=c*2,n=t.popSize,a=Math.max(0,t.popSize-t.elite),E=n+t.generations*a,d=n+s*a,M=Math.min(d,t.maxEvaluations),r=(F=(G=o.units)==null?void 0:G.perEvaluationMs)!=null?F:0,p=(y=(b=o.units)==null?void 0:b.perGenerationOverheadMs)!=null?y:0,m=(L=(C=o.units)==null?void 0:C.perRunOverheadMs)!=null?L:0,x=n*r+m,f=a*r+p,T=x+t.generations*f,v=x+s*f,S=Math.min(v,t.maxWallMs),g=(_=o.units)==null?void 0:_.perEvaluationCost,D=g!==void 0?{perEval:g,plannedTotal:E*g,expectedTotal:d*g}:void 0;return {evaluations:{init:n,perGen:a,plannedTotal:E,expectedTotal:d,cappedByMaxEvaluations:Number.isFinite(t.maxEvaluations)?M:void 0},timeMs:r||p||m?{init:x,perGen:f,plannedTotal:T,expectedTotal:v,cappedByMaxWall:Number.isFinite(t.maxWallMs)?S:void 0}:void 0,monetary:D,operations:{immigrantsPerGen:i,childrenFromBreedingPerGen:l,pairsPerGen:c,selectionsPerGen:u,expectedCrossoversPerGen:c*t.cxProb,expectedMutationsPerGen:l*t.mutProb},notes:["Estimates assume no early stop (target/stall/time/evaluations).","Per-generation evaluations equal popSize - elite by construction.","Crossovers are counted per parent-pair; mutations per child genome."]}}function V(e){let o={};for(let t=0;t<e.length;t++){let s=e[t];if(s.startsWith("--")){let i=s.slice(2),l=e[t+1];l&&!l.startsWith("--")?(o[i]=l,t++):o[i]=true;}}return o}function A(e){if(e===void 0||e===true)return;let o=Number(e);return Number.isFinite(o)?o:void 0}function H(){console.log(`gp-lite-estimate
Usage:
gp-lite-estimate [--config path.json] [flags]
Config flags (override JSON when present):
--popSize N --generations N --elite N
--cxProb F --mutProb F --immigration F
--tournament N --stall N --targetFitness F
--maxWallMs N --maxEvaluations N --expectedGenerations N
Units (generic):
--perEvaluationMs F --perGenerationOverheadMs F
--perRunOverheadMs F --perEvaluationCost F
Output:
--json print machine-readable JSON
--help show this help
`);}function q(){return U(this,null,function*(){var E,d,M;let e=process.argv.slice(2),o=V(e);if(o.help)return H();let t={},s={},i;if(o.config&&typeof o.config=="string")try{let p=yield (yield import('fs/promises')).readFile(o.config,"utf8"),m=JSON.parse(p);t=(E=m.config)!=null?E:m,s=(d=m.units)!=null?d:{},i=(M=m.expectedGenerations)!=null?M:void 0;}catch(r){console.error(`Failed to read config: ${o.config}`,r),process.exit(2);}let l=r=>{let p=A(o[r]);p!==void 0&&(t[r]=p);};["popSize","generations","elite","cxProb","mutProb","immigration","tournament","stall","targetFitness","maxWallMs","maxEvaluations"].forEach(r=>l(r));let c=r=>{let p=A(o[r]);p!==void 0&&(s[r]=p);};["perEvaluationMs","perGenerationOverheadMs","perRunOverheadMs","perEvaluationCost"].forEach(r=>c(r));let u=A(o.expectedGenerations);u!==void 0&&(i=u);let n=R(t,{expectedGenerations:i,units:s});if(o.json){console.log(JSON.stringify(n,null,2));return}let a=[];a.push("gp-lite estimate"),a.push(""),a.push("Evaluations:"),a.push(` init=${n.evaluations.init} perGen=${n.evaluations.perGen} planned=${n.evaluations.plannedTotal} expected=${n.evaluations.expectedTotal}`),n.evaluations.cappedByMaxEvaluations!==void 0&&a.push(` cappedByMaxEvaluations=${n.evaluations.cappedByMaxEvaluations}`),n.timeMs&&(a.push("Time (ms):"),a.push(` init=${n.timeMs.init} perGen=${n.timeMs.perGen} planned=${n.timeMs.plannedTotal} expected=${n.timeMs.expectedTotal}`),n.timeMs.cappedByMaxWall!==void 0&&a.push(` cappedByMaxWall=${n.timeMs.cappedByMaxWall}`)),n.monetary&&(a.push("Monetary:"),a.push(` perEval=${n.monetary.perEval} planned=${n.monetary.plannedTotal} expected=${n.monetary.expectedTotal}`)),a.push("Operations (per gen):"),a.push(` selections=${n.operations.selectionsPerGen} pairs=${n.operations.pairsPerGen} crossovers~= ${n.operations.expectedCrossoversPerGen.toFixed(2)} mutations~= ${n.operations.expectedMutationsPerGen.toFixed(2)}`),n.notes.length&&(a.push("Notes:"),n.notes.forEach(r=>a.push(` - ${r}`))),console.log(a.join(`
`));})}q().catch(e=>{e.name==="ConfigError"||e.name==="ProblemError"||e.name==="EvolutionError"||e.name==="UserFunctionError"?(console.error(`${e.name}: ${e.message}`),e.code&&console.error(` code: "${e.code}"`)):console.error(e),process.exit(1);});//# sourceMappingURL=cli.cjs.map
//# sourceMappingURL=cli.cjs.map