Skip to content

Commit ed42d02

Browse files
Courssumm02Courssumm02
authored andcommitted
Made Module 4
1 parent 2bb0249 commit ed42d02

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed

Module 3/app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ function app(){
2626
return arr;
2727
}
2828

29+
Array.prototype.remove = function() {
30+
var what, a = arguments, L = a.length, ax;
31+
while (L && this.length) {
32+
what = a[--L];
33+
while ((ax = this.indexOf(what)) !== -1) {
34+
this.splice(ax, 1);
35+
}
36+
}
37+
return this;
38+
};
39+
2940
function num_Unique(array){
3041
var u = array.map(JSON.stringify)
3142
return u.unique().length
@@ -42,7 +53,8 @@ function app(){
4253
for (let j = 0; j < clones.length; j++){
4354
clones[j]=clones[j].map((base)=>{
4455
if (Math.random() < mutation_rate){
45-
return rand_base()
56+
let mybase = BASES.remove(base)
57+
return mybase[Math.floor(Math.random() * mybase.length)]
4658
}
4759
return base
4860
})

Module 4/app.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
function app(){
2+
3+
const ANIMALS = parseInt(document.getElementById("samples").value)
4+
const gen = parseInt(document.getElementById("generations").value)
5+
console.log(ANIMALS, gen);
6+
const initial_a1_freq = parseFloat(document.getElementById("a1").value)
7+
const initial_a2_freq = 1-initial_a1_freq
8+
9+
var a1_freq = initial_a1_freq
10+
var a2_freq = 1 - a1_freq
11+
var arr_a1 = []
12+
var arr_a2 = []
13+
for (var i = 0; i < gen; i++){
14+
let a1 = 0
15+
let a2 = 0
16+
for (let j = 0; j<ANIMALS*2;j++){
17+
if(Math.random()<a1_freq){
18+
a1++
19+
}else{
20+
a2++
21+
}
22+
}
23+
a1_freq=a1/(a1+a2)
24+
a2_freq = 1 - a1_freq
25+
arr_a1.push(a1_freq)
26+
arr_a2.push(a2_freq)
27+
}
28+
29+
30+
var ctx = document.getElementById('myChart').getContext('2d');
31+
32+
33+
var a1_data = {
34+
label: "A1",
35+
data: arr_a1,
36+
lineTension: 0.5,
37+
fill: false,
38+
borderColor: 'red',
39+
borderWidth: 1.5
40+
}
41+
var a2_data = {
42+
label: "A2",
43+
data: arr_a2,
44+
lineTension: 0.5,
45+
fill: false,
46+
borderColor: 'blue',
47+
borderWidth: 1.5
48+
}
49+
50+
const data = {
51+
labels: [...Array(gen).keys()],
52+
datasets: [a1_data, a2_data]
53+
};
54+
55+
56+
var myChart = new Chart(ctx, {
57+
type: 'line',
58+
data: data,
59+
options: {
60+
scales: {
61+
y: {
62+
beginAtZero: true
63+
}
64+
},
65+
bezierCurve: true
66+
}
67+
});
68+
myChart.update()
69+
}

Module 4/main.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js'></script>
9+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
10+
<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+
12+
</head>
13+
<body class="container pt-5 pb-5">
14+
<main>
15+
<h1>Module 4</h1>
16+
<p class="fs-5 col-md-8">Here I implement a simulation genetic drift with natural selection.</p>
17+
<div class="col-md-6">
18+
<h2>Tech used projects</h2>
19+
<p>Here are some libraries used in this module</p>
20+
<ul class="icon-list">
21+
<li><a href="https://getbootstrap.com/">Bootstrap</a></li>
22+
<li><a href="https://www.chartjs.org/">Chart Js</a></li>
23+
</ul>
24+
</div>
25+
<div class="mb-5">
26+
<a href="https://github.com/techboy-coder/nature-in-code" class="btn btn-primary btn-lg px-4">Other examples made by me</a>
27+
</div>
28+
29+
<hr>
30+
31+
</main>
32+
<form action="" id="input">
33+
<label for="samples">Number of samples</label>
34+
<input type="number" name="samples" id="samples" placeholder="100" value="100">
35+
<label for="generations">Number of generations</label>
36+
<input type="number" name="generations" id="generations" placeholder="100" value="100">
37+
<label for="a1">Frequency of a1 (between 1 and 0; --> a2 = 1-a1) </label>
38+
<input type="number" name="a1" id="a1" placeholder="0.5" value="0.3" step="0.0001">
39+
</form>
40+
<button type="button" onclick="app();">Run Simulation</button>
41+
<canvas id="myChart"></canvas>
42+
<script src="app.js"></script>
43+
</body>
44+
</html>

Module 4/notes.md

Whitespace-only changes.

0 commit comments

Comments
 (0)