Skip to content

Commit 759249d

Browse files
Courssumm02Courssumm02
authored andcommitted
Module 5 in the middle
1 parent d768b80 commit 759249d

File tree

3 files changed

+317
-21
lines changed

3 files changed

+317
-21
lines changed

Module 5/app.js

Lines changed: 126 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,136 @@
1-
const N_CLONES = 100
1+
const N_CLONES = 600
22
const a1_prob = 0.5
33
const a2_prob = 1 - a1_prob
4-
const N_GEN = 10
5-
4+
const N_GEN = 100
5+
const D = 5
66
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
77
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
88

99
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-
2710

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+
// });
28134

29135

30136

Module 5/main.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js'></script>
99
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
1010
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
11-
11+
<script src="https://d3js.org/d3.v7.min.js"></script>
1212
</head>
1313
<body class="container pt-5 pb-5">
1414
<main>
@@ -38,6 +38,7 @@ <h2>Tech used projects</h2>
3838
<input type="number" name="a1" id="a1" placeholder="0.5" value="0.3" step="0.0001">
3939
</form> -->
4040
<!-- <button type="button" onclick="app();">Run Simulation</button> -->
41+
<!-- <script src="util.js"></script> -->
4142
<canvas id="myChart"></canvas>
4243
<script src="app.js"></script>
4344
</body>

Module 5/util.js

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)