JSFiddle - React, Tailwind, and code Playground
HTML
<title>Title</title>
<script type="text/javascript" src="jquery-3.0.0.js"></script>
<script type="text/javascript" src="flot/jquery.js"></script>
<script type="text/javascript" src="flot/jquery.flot.js"></script>
<body>
<div id="placeholder" style="width:600px; height:300px"></div>
<input type="button" id="run" name="run" value="Run Simulation" />
</body>
JavaScript
/**
* Created by nix on 7/1/16.
*/
// Simulation Parameters
$(function() {
$("#run").click(function() {
run_simulation({
test: true
});
});
});
var nrand = function() {
var x1, x2, rad;
do {
x1 = 2 * Math.random() - 1;
x2 = 2 * Math.random() - 1;
rad = x1 * x1 + x2 * x2;
} while (rad >= 1 || rad == 0);
var c = Math.sqrt(-2 * Math.log(rad) / rad);
return x1 * c;
};
var binomial = function(n, p) {
if (n * p > 15 && n * (1 - p) > 15) { // Normal approximation
var s = n * p * (p - 1);
return nrand() * s + (n * p);
}
var c = 0;
for (var i = 0; i < n; i++) {
if (Math.random() < p) {
c++;
}
}
return c;
};
var run_simulation = function(parameters) {
var defaults = {
num_generations: 1000,
simulate_individuals: true,
is_carrying_capacity_used: true,
carrying_capacity: 300,
num_sheep: 10,
sheep_reproduction_rate: 0.1,
fatality_rate: 0.001,
num_wolves: 10,
wolf_conversion_frequency: 0.4,
wolf_death_rate: 0.03
};
// Consolidate defaults and passed parameters.
parameters = $.extend({}, defaults, parameters);
var history_sheep = [
[0, parameters.num_sheep]
];
var history_wolves = [
[0, parameters.num_wolves]
];
var num_sheep = parameters.num_sheep;
var num_wolves = parameters.num_wolves;
for (var i = 1; i < parameters.num_generations; i++) {
var results = iterate(parameters, num_sheep, num_wolves);
num_sheep = results[0];
num_wolves = results[1];
history_sheep.push([i, num_sheep]);
history_wolves.push([i, num_wolves]);
if (num_sheep == 0 && num_wolves == 0) {
break;
}
}
var options = {
colors: ["blue", "red"]
};
$.plot($("#placeholder"), [history_sheep, history_wolves], options);
};
var...