Evolutionary Strategy Simulator

Evolutionary Strategy Simulator :root{font-family:system-ui,-apple-system,sans-serif;color:#172334;background:#f5f7fb}*{box-sizing:border-box}body{margin:0}.wrap{max-width:1100px;margin:auto;padding:28px 18px 55px}h1{margin:0 0 8px;font-size:clamp(25px,4vw,38px)}.sub{color:#536273;margin:0 0 25px}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px}.card{background:white;border:1px solid #dce3ed;border-radius:14px;padding:20px;margin-bottom:18px;box-shadow:0 2px 9px #15243b08}h2{font-size:19px;margin:0 0 13px}.safe{border-top:5px solid #247e91}.risk{border-top:5px solid #d78332}table{border-collapse:collapse;width:100%;font-size:14px}th,td{text-align:left;padding:10px 6px;border-bottom:1px solid #e7ebf0}th{color:#536273}.hint{font-size:13px;color:#536273}.fields{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:15px}label{font-size:14px;font-weight:600;display:block}input{display:block;width:100%;padding:11px;margin-top:6px;border:1px solid #b7c4d4;border-radius:8px;font:inherit;background:white}button{padding:12px 17px;border-radius:9px;border:1px solid #a8b8ca;background:white;font-weight:650;cursor:pointer}button.primary{background:#163b63;color:white;border-color:#163b63}button:hover{filter:brightness(.94)}.actions{display:flex;flex-wrap:wrap;gap:10px;margin-top:19px}.stats{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px}.stat{background:#eff3f8;border-radius:10px;padding:15px}.stat small{display:block;color:#536273}.stat strong{display:block;font-size:clamp(19px,2.8vw,29px);margin-top:5px}.legend{display:flex;gap:20px;font-size:14px;margin:8px 0}.dot{display:inline-block;width:11px;height:11px;border-radius:50%;margin-right:6px}.chart{width:100%;height:auto;display:block}.error{color:#ad2929;min-height:20px}.note{font-size:13px;line-height:1.6;color:#526172}.dist{display:grid;grid-template-columns:1fr 1fr;gap:18px}.bars{display:flex;align-items:end;height:130px;gap:3px;border-bottom:1px solid #aebac8}.bar{flex:1;min-width:0;border-radius:3px 3px 0 0}.axis{display:flex;justify-content:space-between;font-size:12px;color:#667384;margin-top:5px}@media(max-width:650px){.grid,.dist{grid-template-columns:1fr}.fields{grid-template-columns:repeat(2,minmax(0,1fr))}.stats{grid-template-columns:repeat(2,minmax(0,1fr))}}@media(max-width:390px){.fields{grid-template-columns:1fr}}

Evolutionary Strategy Simulator

Explore when a high-risk aggressive strategy produces a greater reproductive payoff than playing it safe.

Strategy A · Play It Safe

Die rollOutcomePayoff
1Mating opportunity+1
2–6No mating opportunity0

No injury risk.

Strategy B · Take the Risk

Die rollOutcomePayoff
1–2Wins competition+3
3–4Loses competition0
5–6Seriously injuredSkip 1 round

Injured individuals miss subsequent opportunities.

Simulation settings

Individuals per strategyNumber of roundsSafe success rewardRisky success rewardRounds skipped after injuryAdditional injury penalty (offspring-equivalent)

Results

Safe: mean payoff—
Risky: mean net payoff—
Risky advantage—
Risky wins across simulations—

Cumulative mean payoff per individual

SafeRisky

Distribution of individual outcomes

Teaching notes & assumptions

Every active individual rolls an independent fair six-sided die each round. A safe player succeeds on 1; a risky player succeeds on 1–2, loses on 3–4, and is injured on 5–6. Injuries prevent participation in the specified number of subsequent rounds. Each individual retains the same strategy across all rounds. The model does not simulate death, population growth, inheritance, or competition between simulated individuals. A user-defined additional injury penalty is subtracted from net payoff; negative net payoffs are possible and are not literally negative numbers of offspring.

Theoretical immediate payoff per active round is (safe reward)/6 for Strategy A and (risky reward − injury penalty)/3 for Strategy B. Because injuries cause missed rounds, these immediate values are not the full multi-round expectations. The model illustrates a possible evolutionary trade-off; it does not establish that aggression is always adaptive.

(()=>{‘use strict’;const $=id=>document.getElementById(id),form=$(‘form’);const f=x=>Number.isFinite(x)?x.toFixed(2):’—’;function read(){let p={};for(let k of [‘n’,’rounds’,’safe’,’risk’,’skip’,’cost’]){let field=form.elements.namedItem(k);if(field.value.trim()===”)throw Error(‘Please fill in every setting.’);p[k]=Number(field.value);if(!Number.isFinite(p[k])||p[k]Number(field.max))throw Error(‘Please use values within the indicated ranges.’)}if(p.n<1||p.rounds<1||!Number.isInteger(p.n)||!Number.isInteger(p.rounds)||!Number.isInteger(p.skip))throw Error('Individuals, rounds and skipped rounds must be whole numbers.');return p}function sim(p,keep){let a=new Float64Array(p.n),b=new Float64Array(p.n),miss=new Uint16Array(p.n),sa=[0],sb=[0],ta=0,tb=0;for(let t=0;t<p.rounds;t++){for(let i=0;i<p.n;i++){if(Math.random()<1/6){a[i]+=p.safe;ta+=p.safe}if(miss[i]){miss[i]–;continue}let roll=Math.floor(Math.random()*6);if(roll=4){b[i]-=p.cost;tb-=p.cost;miss[i]=p.skip}}sa.push(ta/p.n);sb.push(tb/p.n)}return{sa,sb,ma:ta/p.n,mb:tb/p.n,a:keep?Array.from(a):null,b:keep?Array.from(b):null}}function chart(z,p){const svg=$(‘chart’),W=800,H=330,L=65,R=25,T=18,B=48;let low=Math.min(0,…z.sa,…z.sb),high=Math.max(0,…z.sa,…z.sb);if(low===high)high=low+1;let x=t=>L+(W-L-R)*t/p.rounds,y=v=>T+(H-T-B)*(high-v)/(high-low);let s=”;for(let j=0;j<=5;j++){let v=low+(high-low)*j/5,Y=y(v);s+=`${f(v)}`}for(let j=0;j<=5;j++){let t=Math.round(p.rounds*j/5);s+=`${t}`}function path(arr){return arr.map((v,i)=>(i?’L’:’M’)+x(i).toFixed(2)+’,’+y(v).toFixed(2)).join(‘ ‘)}s+=`Round`;svg.innerHTML=s}function histogram(arr,title,color){let min=Math.min(…arr),max=Math.max(…arr);if(min===max)max=min+1;let bins=Array(15).fill(0);for(let v of arr)bins[Math.min(14,Math.floor((v-min)/(max-min)*15))]++;let peak=Math.max(1,…bins);let section=document.createElement(‘div’);section.innerHTML=`

${title}

${bins.map(v=>`
`).join(”)}
${f(min)}${f(max)}
`;return section}function show(z,p,win,repeated){$(‘meanSafe’).textContent=f(z.ma);$(‘meanRisk’).textContent=f(z.mb);$(‘difference’).textContent=(z.mb-z.ma>=0?’+’:”)+f(z.mb-z.ma);$(‘winRate’).textContent=win===null?’—’:(win*100).toFixed(1)+’%’;chart(z,p);let dist=$(‘distributions’);dist.replaceChildren();if(z.a){dist.append(histogram(z.a,’Play It Safe’,’#247e91′),histogram(z.b,’Take the Risk’,’#d78332′))}else{dist.textContent=’Individual distributions are displayed for a single simulated population.’}let winner=z.mb>z.ma?’the risky strategy’:z.mb(reps===1?2000000:100000))throw Error(reps===1?’Limit individuals × rounds to 2 million.’:’For 1,000 simulations, limit individuals × rounds to 100,000.’);if(reps===1){show(sim(p,true),p,null,false);return}let sa=Array(p.rounds+1).fill(0),sb=Array(p.rounds+1).fill(0),ma=0,mb=0,wins=0;for(let i=0;iz.ma)wins++;for(let j=0;jx/1000),sb:sb.map(x=>x/1000),ma:ma/1000,mb:mb/1000},p,wins/1000,true)}catch(e){$(‘error’).textContent=e.message}}form.addEventListener(‘submit’,e=>{e.preventDefault();run(1)});$(‘repeat’).addEventListener(‘click’,()=>run(1000));$(‘reset’).addEventListener(‘click’,()=>{form.reset();run(1)});run(1)})();