|
1 |
| -const N_CLONES = 100 |
| 1 | +const N_CLONES = 600 |
2 | 2 | const a1_prob = 0.5
|
3 | 3 | const a2_prob = 1 - a1_prob
|
4 |
| -const N_GEN = 10 |
5 |
| - |
| 4 | +const N_GEN = 100 |
| 5 | +const D = 5 |
6 | 6 | const gridx = Math.round(Math.sqrt(N_CLONES)) * Math.round(Math.sqrt(N_CLONES))==N_CLONES?Math.round(Math.sqrt(N_CLONES)) : Math.round(Math.sqrt(N_CLONES))+1
|
7 | 7 | const gridy = Math.round(Math.sqrt(N_CLONES)) * Math.round(Math.sqrt(N_CLONES))==N_CLONES?Math.round(Math.sqrt(N_CLONES)) : Math.round(Math.sqrt(N_CLONES))+1
|
8 | 8 |
|
9 | 9 | var grid = [...Array(gridx*gridy).keys()]
|
10 |
| -console.log(JSON.stringify(grid)); |
11 |
| - |
12 |
| - |
13 |
| - |
14 |
| - |
15 |
| - |
16 |
| - |
17 |
| - |
18 |
| - |
19 |
| - |
20 |
| - |
21 |
| - |
22 |
| - |
23 |
| - |
24 |
| - |
25 |
| - |
26 |
| - |
27 | 10 |
|
| 11 | +let count = 0 |
| 12 | +for (let i = 0; i < gridx; i++){ |
| 13 | + for (let j = 0; j < gridy; j++){ |
| 14 | + grid[count] = {x: i, y: j} |
| 15 | + count++ |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +for (let i = 0; i < N_CLONES; i++){ |
| 20 | + if (Math.random() < a1_prob){ |
| 21 | + grid[i].v1 = 1 |
| 22 | + if (Math.random() < a1_prob){ |
| 23 | + grid[i].v2 = 1 |
| 24 | + }else{ |
| 25 | + grid[i].v2 = 2 |
| 26 | + } |
| 27 | + }else{ |
| 28 | + grid[i].v1 = 2 |
| 29 | + if (Math.random() < a1_prob){ |
| 30 | + grid[i].v2 = 1 |
| 31 | + }else{ |
| 32 | + grid[i].v2 = 2 |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +//INIT Done |
| 38 | + |
| 39 | +function getRandomInt(min, max) { |
| 40 | + min = Math.ceil(min); |
| 41 | + max = Math.floor(max); |
| 42 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 43 | +} |
| 44 | + |
| 45 | +function move(x,y){ |
| 46 | + return [x + getRandomInt(-D,D), y + getRandomInt(-D,D)] |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +var a1a1_data = [] |
| 51 | +var a1a2_data = [] |
| 52 | +var a2a2_data = [] |
| 53 | +var g; |
| 54 | +for (let i = 0; i < N_GEN; i++){ |
| 55 | + g = grid.map((item)=>{ |
| 56 | + const [x,y]=move(item.x, item.y) |
| 57 | + item.x = x |
| 58 | + item.y = y |
| 59 | + return item |
| 60 | + }) |
| 61 | + var new_grid = [] |
| 62 | + for (let j = 0; j < N_CLONES; j++){ |
| 63 | + var {x,y,v1,v2}=g[j] |
| 64 | + var possible_mates = g.filter((el)=>{ |
| 65 | + return Math.abs(el.x - x)<=D && Math.abs(el.y - y)<=D |
| 66 | + }) |
| 67 | + var the_mate = possible_mates[Math.floor(Math.random() * possible_mates.length)]; |
| 68 | + var combinations = [[v1,the_mate.v1],[v1, the_mate.v2],[v2,the_mate.v1],[v2,the_mate.v2]][Math.floor(Math.random() * [[v1,the_mate.v1],[v1, the_mate.v2],[v2,the_mate.v1],[v2,the_mate.v2]].length)]; |
| 69 | + var offspring = {x:x,y:y,v1: combinations[0], v2: combinations[1]} |
| 70 | + new_grid.push(offspring) |
| 71 | + } |
| 72 | + g = new_grid |
| 73 | + var a1a1 = g.filter((el)=>{ |
| 74 | + return el.v1 == 1 && el.v2 == 1 |
| 75 | + }) |
| 76 | + var a1a2 = g.filter((el)=>{ |
| 77 | + return (el.v1 == 1 && el.v2 == 2) || (el.v1 == 2 && el.v2 == 1) |
| 78 | + }) |
| 79 | + var a2a2 = g.filter((el)=>{ |
| 80 | + return el.v1 == 2 && el.v2 == 2 |
| 81 | + }) |
| 82 | + a1a1_data.push(a1a1.length) |
| 83 | + a1a2_data.push(a1a2.length) |
| 84 | + a2a2_data.push(a2a2.length) |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +console.log(a1a1_data,a1a2_data,a2a2_data); |
| 89 | + |
| 90 | +// const ctx = document.getElementById('myChart').getContext('2d'); |
| 91 | + |
| 92 | +// const chart = new Chart(ctx, { |
| 93 | +// type: 'line', |
| 94 | +// data: { |
| 95 | +// labels: [...Array(N_GEN).keys()], |
| 96 | +// datasets: [{ |
| 97 | +// label: 'Number of a1a1', |
| 98 | +// data: a1a1_data, |
| 99 | +// borderWidth: 1, |
| 100 | +// fill: false, |
| 101 | +// borderColor: 'red' |
| 102 | +// }, |
| 103 | +// { |
| 104 | +// label: 'Number of a1a2', |
| 105 | +// data: a1a2_data, |
| 106 | +// borderWidth: 1, |
| 107 | +// fill: false, |
| 108 | +// borderColor: 'green' |
| 109 | +// }, |
| 110 | +// { |
| 111 | +// label: 'Number of a2a2', |
| 112 | +// data: a2a2_data, |
| 113 | +// borderWidth: 1, |
| 114 | +// fill: false, |
| 115 | +// borderColor: 'blue' |
| 116 | +// }, |
| 117 | + |
| 118 | +// ] |
| 119 | +// }, |
| 120 | +// options: { |
| 121 | +// scales: { |
| 122 | +// yAxes: [{ |
| 123 | +// ticks: { |
| 124 | +// beginAtZero: true |
| 125 | +// } |
| 126 | +// }], |
| 127 | +// y: { |
| 128 | +// display: true, |
| 129 | +// type: 'logarithmic', |
| 130 | +// } |
| 131 | +// } |
| 132 | +// } |
| 133 | +// }); |
28 | 134 |
|
29 | 135 |
|
30 | 136 |
|
|
0 commit comments