-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoevolution.html
More file actions
223 lines (197 loc) · 7.16 KB
/
coevolution.html
File metadata and controls
223 lines (197 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
</head>
<body>
<script>
function draw_line_chart(data,x_label,y_label,legend_values,x_max,y_max_flex) {
var margin = {top: 20, right: 20, bottom: 50, left: 50},
width = 700 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var version = d3.scale ? 3 : 4;
var color = (version == 3 ? d3.scale.category10() : d3.scaleOrdinal(d3.schemeCategory10));
if (!x_max) {
x_max = data[0].length > 0 ? data[0].length : data.length
}
var y_max = data[0].length > 0 ? d3.max(data, function(array) {
return d3.max(array);
}) : d3.max(data);
var x = (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.domain([0,x_max])
.range([0, width]);
var y = y_max_flex ? (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.domain([0, 1.1 * y_max])
.range([height, 0]) : (version == 3 ? d3.scale.linear() : d3.scaleLinear())
.range([height, 0]);
var xAxis = (version == 3 ? d3.svg.axis().scale(x).orient("bottom") :
d3.axisBottom().scale(x));
var yAxis = (version == 3 ? d3.svg.axis().scale(y).orient("left") :
d3.axisLeft().scale(y));
var line = (version == 3 ? d3.svg.line() : d3.line())
.x(function (d, i) {
var dat = (data[0].length > 0 ? data[0] : data);
return x((i/(dat.length-1)) * x_max);
})
.y(function (d) {
return y(d);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.style("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", 6)
.attr("dy", "3em")
.style("fill", "#000")
.text(x_label);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("dy", "-3.5em")
.style("text-anchor", "middle")
.style("fill", "#000")
.text(y_label);
if (legend_values.length > 0) {
var legend = svg.append("text")
.attr("text-anchor", "star")
.attr("y", 30)
.attr("x", width-100)
.append("tspan").attr("class", "legend_title")
.text(legend_values[0])
.append("tspan").attr("class", "legend_text")
.attr("x", width-100).attr("dy", 20).text(legend_values[1])
.append("tspan").attr("class", "legend_title")
.attr("x", width-100).attr("dy", 20).text(legend_values[2])
.append("tspan").attr("class", "legend_text")
.attr("x", width-100).attr("dy", 20).text(legend_values[3]);
}
else {
svg.selectAll("line.horizontalGridY")
.data(y.ticks(10)).enter()
.append("line")
.attr("x1", 1)
.attr("x2", width)
.attr("y1", function(d){ return y(d);})
.attr("y2", function(d){ return y(d);})
.style("fill", "none")
.style("shape-rendering", "crispEdges")
.style("stroke", "#f5f5f5")
.style("stroke-width", "1px");
svg.selectAll("line.horizontalGridX")
.data(x.ticks(10)).enter()
.append("line")
.attr("x1", function(d,i){ return x(d);})
.attr("x2", function(d,i){ return x(d);})
.attr("y1", 1)
.attr("y2", height)
.style("fill", "none")
.style("shape-rendering", "crispEdges")
.style("stroke", "#f5f5f5")
.style("stroke-width", "1px");
}
d3.select("body").style("font","10px sans-serif");
d3.selectAll(".axis line").style("stroke","#000");
d3.selectAll(".y.axis path").style("display","none");
d3.selectAll(".x.axis path").style("display","none");
d3.selectAll(".legend_title")
.style("font-size","12px").style("fill","#555").style("font-weight","400");
d3.selectAll(".legend_text")
.style("font-size","20px").style("fill","#bbb").style("font-weight","700");
if (data[0].length > 0) {
var simulation = svg.selectAll(".simulation")
.data(data)
.enter().append("g")
.attr("class", "simulation");
simulation.append("path")
.attr("class", "line")
.attr("fill", "none")
.attr("d", function(d) { return line(d); })
.style("stroke", function(d,i) { return color(i); });
}
else {
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("fill", "none")
.attr("d", line)
.style("stroke","steelblue");
}
d3.selectAll(".line").style("fill", "none").style("stroke-width","1.5px");
}
var data=[];
var host_frequencies=[0,0];
var parasite_frequencies=[0,0];
var generations=400;
var sh=0.2;
var sp=0.1;
function initialize_frequencies(){
host_frequencies[0]=Math.random();
data.push([host_frequencies[0]]);
host_frequencies[1]=1-host_frequencies[0];
data.push([host_frequencies[1]]);
parasite_frequencies[0]=Math.random();
data.push([parasite_frequencies[0]]);
parasite_frequencies[1]=1-parasite_frequencies[0];
data.push([parasite_frequencies[1]]);
}
initialize_frequencies();
function calc_host_w_p(i,ii){
return (i==ii?1-sh:1)*parasite_frequencies[ii]
}
function host_selection(){
var total_host_frequencies=0;
//alert(host_frequencies.length);
for(var i=0;i<host_frequencies.length;i++){
var host_fitness=0;
for(var ii=0;ii<parasite_frequencies.length;ii++){
host_fitness=host_fitness+calc_host_w_p(i,ii);
}
host_frequencies[i]=host_frequencies[i]*host_fitness;
total_host_frequencies=total_host_frequencies+host_frequencies[i];
}
for(var i=0;i<host_frequencies.length;i++){
host_frequencies[i]=host_frequencies[i]/total_host_frequencies;
}
}
function calc_parasite_w_p(i,ii){
return (i==ii?1:1-sp)*host_frequencies[ii]
}
function parasite_selection(){
var total_parasite_frequencies=0;
for(var i=0;i<parasite_frequencies.length;i++){
var parasite_fitness=0;
for(var ii=0;ii<host_frequencies.length;ii++){
parasite_fitness=parasite_fitness+calc_parasite_w_p(i,ii);
}
parasite_frequencies[i]=parasite_frequencies[i]*parasite_fitness;
total_parasite_frequencies=total_parasite_frequencies+parasite_frequencies[i];
}
for(var i=0;i<parasite_frequencies.length;i++){
parasite_frequencies[i]=parasite_frequencies[i]/total_parasite_frequencies;
}
}
for(var k=0;k<generations;k=k+1){
//alert(data);
host_selection();
parasite_selection();
data[0].push(host_frequencies[0]);
data[1].push(host_frequencies[1]);
data[2].push(parasite_frequencies[0]);
data[3].push(parasite_frequencies[1]);
}
//alert(data);
draw_line_chart(data,"Generations","p",[]);
</script>
</body>
</html>