File tree Expand file tree Collapse file tree 5 files changed +227
-0
lines changed Expand file tree Collapse file tree 5 files changed +227
-0
lines changed Original file line number Diff line number Diff line change
1
+ function model ( v1 , v2 ) {
2
+ return {
3
+ v1v1 : v1 * v1 ,
4
+ v2v2 : v2 * v2 ,
5
+ v1v2 : v1 * v2 * 2
6
+ }
7
+ }
8
+
9
+ document . getElementById ( 'input' ) . onsubmit = function ( ) {
10
+ var v1 = parseFloat ( document . getElementById ( "v1" ) . value )
11
+ var v2 = parseFloat ( document . getElementById ( "v2" ) . value )
12
+ var tot = v1 + v2
13
+ if ( tot != 1 ) {
14
+ document . getElementById ( "error" ) . innerHTML = "v1 + v2 should be equal to 1"
15
+ document . getElementById ( "v1v1" ) . innerHTML = ""
16
+ document . getElementById ( "v1v2" ) . innerHTML = ""
17
+ document . getElementById ( "v2v2" ) . innerHTML = ""
18
+ document . getElementById ( "tot" ) . innerHTML = ""
19
+ return false
20
+ } else {
21
+ document . getElementById ( "error" ) . innerHTML = ""
22
+ document . getElementById ( "tot" ) . innerHTML = "Total = 1"
23
+ }
24
+ const output = model ( v1 , v2 )
25
+ console . log ( output ) ;
26
+ document . getElementById ( "v1v1" ) . innerHTML = "Ideal Prob: " + Math . round ( output . v1v1 * 100 ) / 100 ;
27
+ document . getElementById ( "v2v2" ) . innerHTML = "Ideal Prob: " + Math . round ( output . v2v2 * 100 ) / 100 ;
28
+ document . getElementById ( "v1v2" ) . innerHTML = "Ideal Prob: " + Math . round ( output . v1v2 * 100 ) / 100 ;
29
+ console . log ( "Running" ) ;
30
+ var _lpv1v2 = 1 - v1 - v2
31
+
32
+ var av1 = [ ]
33
+ var av2 = [ ]
34
+ var av1v2 = [ ]
35
+ var gen = [ ]
36
+
37
+ for ( let i = 0 ; i < 10 ; i ++ ) {
38
+
39
+ v1 = v1 * v1 + ( ( _lpv1v2 * _lpv1v2 ) / 4 )
40
+ v2 = v2 * v2
41
+ _lpv1v2 = 1 - v1 - v2
42
+
43
+ av1 . push ( v1 )
44
+ av2 . push ( v2 )
45
+ av1v2 . push ( _lpv1v2 )
46
+ gen . push ( i + 1 )
47
+
48
+ }
49
+ console . log ( av1 , av2 , av1v2 ) ;
50
+ const ctx = document . getElementById ( 'myChart' ) . getContext ( '2d' ) ;
51
+
52
+ const chart = new Chart ( ctx , {
53
+ type : 'line' ,
54
+ data : {
55
+ labels : gen ,
56
+ datasets : [ {
57
+ label : '# of v1' ,
58
+ data : av1 ,
59
+ borderWidth : 1 ,
60
+ fill : false ,
61
+ borderColor : 'red'
62
+ } ,
63
+ {
64
+ label : '# of v2' ,
65
+ data : av2 ,
66
+ borderWidth : 1 ,
67
+ fill : false ,
68
+ borderColor : 'green'
69
+ } ,
70
+ {
71
+ label : '# of v1v2' ,
72
+ data : av1v2 ,
73
+ borderWidth : 1 ,
74
+ fill : false ,
75
+ borderColor : 'blue'
76
+ } ,
77
+
78
+ ]
79
+ } ,
80
+ options : {
81
+ scales : {
82
+ yAxes : [ {
83
+ ticks : {
84
+ beginAtZero : true
85
+ }
86
+ } ] ,
87
+ y : {
88
+ display : true ,
89
+ type : 'logarithmic' ,
90
+ }
91
+ }
92
+ }
93
+ } ) ;
94
+ return false
95
+ } ;
96
+
Original file line number Diff line number Diff line change
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
+ </ head >
9
+ < body >
10
+
11
+ < form action ="" id ="input ">
12
+ < label for ="v1 "> V1 Probability</ label >
13
+ < input type ="number " id ="v1 " placeholder ="0.2 " min ="0 " max ="1 " step ="0.05 ">
14
+ < label for ="v2 "> V2 Probability</ label >
15
+ < input type ="number " id ="v2 " placeholder ="0.8 " min ="0 " max ="1 " step ="0.05 ">
16
+ < input type ="submit " value ="Submit ">
17
+ </ form >
18
+
19
+ < div class ="">
20
+ < div id ="v1v1 "> V1V1 Prob: 0.04</ div >
21
+ < div id ="v2v2 "> V2V2 Prob: 0.64</ div >
22
+ < div id ="v1v2 "> V1V2 Prob: 0.32</ div >
23
+ < div id ="tot "> Total = 1</ div >
24
+ < div id ="error "> </ div >
25
+ </ div >
26
+ < hr >
27
+
28
+ < div class ="">
29
+
30
+ < canvas id ="myChart " width ="1000 " height ="300 "> </ canvas >
31
+
32
+ </ div >
33
+ < script src ='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js '> </ script >
34
+ < script src ="app.js "> </ script >
35
+ <!-- <script src="long.js"></script> -->
36
+ </ body >
37
+ </ html >
Original file line number Diff line number Diff line change
1
+ function app ( ) {
2
+
3
+ const ANIMALS = parseInt ( document . getElementById ( "samples" ) . value )
4
+ const gen = parseInt ( document . getElementById ( "generations" ) . value )
5
+ const initial_a1_freq = 0.5
6
+ const initial_a2_freq = 1 - initial_a1_freq
7
+
8
+ var a1_freq = initial_a1_freq
9
+ var a2_freq = 1 - a1_freq
10
+ var arr_a1 = [ ]
11
+ var arr_a2 = [ ]
12
+ for ( var i = 0 ; i < gen ; i ++ ) {
13
+ let a1 = 0
14
+ let a2 = 0
15
+ for ( let j = 0 ; j < ANIMALS * 2 ; j ++ ) {
16
+ if ( Math . random ( ) < a1_freq ) {
17
+ a1 ++
18
+ } else {
19
+ a2 ++
20
+ }
21
+ }
22
+ a1_freq = a1 / ( a1 + a2 )
23
+ a2_freq = 1 - a1_freq
24
+ arr_a1 . push ( a1_freq )
25
+ arr_a2 . push ( a2_freq )
26
+ }
27
+
28
+
29
+ var ctx = document . getElementById ( 'myChart' ) . getContext ( '2d' ) ;
30
+
31
+
32
+ var a1_data = {
33
+ label : "A1" ,
34
+ data : arr_a1 ,
35
+ lineTension : 0.5 ,
36
+ fill : false ,
37
+ borderColor : 'red' ,
38
+ borderWidth : 1.5
39
+ }
40
+ var a2_data = {
41
+ label : "A2" ,
42
+ data : arr_a2 ,
43
+ lineTension : 0.5 ,
44
+ fill : false ,
45
+ borderColor : 'blue' ,
46
+ borderWidth : 1.5
47
+
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
+ }
70
+
71
+ document . getElementById ( 'input' ) . onsubmit = app ( )
Original file line number Diff line number Diff line change
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
+
10
+ </ head >
11
+ < body >
12
+
13
+ < form action ="" id ="input ">
14
+ < label for ="samples "> Number of samples</ label >
15
+ < input type ="number " name ="samples " id ="samples " placeholder ="100 " value ="100 ">
16
+ < label for ="generations "> Number of generations</ label >
17
+ < input type ="number " name ="generations " id ="generations " placeholder ="100 " value ="100 ">
18
+ </ form >
19
+ < button type ="button " onclick ="app(); "> Run Simulation</ button >
20
+ < canvas id ="myChart "> </ canvas >
21
+ < script src ="app.js "> </ script >
22
+ </ body >
23
+ </ html >
You can’t perform that action at this time.
0 commit comments