-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvstime.html
More file actions
193 lines (164 loc) · 5.92 KB
/
pvstime.html
File metadata and controls
193 lines (164 loc) · 5.92 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
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
</head>
<body>
<script>
/*function round_number(value,decimals){
var shifter=Math.pow(10,decimals);
return Math.round(value*shifter)/shifter;
}*/
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 s=0.1; // This value will not impact the p value it finally reaches in
// in balancing selection , but it effects the speed at which it reaches that value
var h=1.5 // h value determines the kind of natural selection.
var initial_p=0.01;
var p_values = [];
var data = [];
var generations = 400;
var x_max=1;
while(initial_p<1){
p_values.push(initial_p);
initial_p=initial_p+0.01+(Math.random()*0.000001);
}
function run_simulation(p){
var results=[];
for(var i=0;i<=generations;i=i+1){
var q=1-p;
var delta_p=(p*q*s*(p*h+q*(1-h)))/(1-2*p*q*h*s-2*q*s);
var p=p+delta_p;
results.push(p)
}
data.push(results);
}
for(var i=0;i<p_values.length;i=i+1){
run_simulation(p_values[i]);
}
draw_line_chart(data,"Generation","p",[]);
</script>
</body>
</html>