Published
Edited
Sep 19, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
array_initial_defectors = [0.0002, 0.002, 0.02, 0.2]
Insert cell
array_T_scores = [1.36, 1.56, 1.76, 1.96]
Insert cell
Insert cell
Insert cell
experiments = {
start_analysis;
//
var exper = array_initial_defectors
.map((x) => {
return array_T_scores.map((y) => ({
initial_defectors: x,
T_score: y,
grid: create_grid(x),
stats: []
}));
})
.flat();
return exper;
}
Insert cell
{
for (var exp of experiments) {
for (let i = 0; i < 200; ++i) {
make_grid_step(exp.grid, { R: 1, T: exp.T_score, S: 0, P: 0 });
}
get_stats(exp.grid, exp.stats);
}
}
Insert cell
Insert cell
defector_color = in_values[2][1]
Insert cell
T = in_values[0][0]
Insert cell
initial_defectors = in_values[0][1]
Insert cell
reset = in_values[1][0]
Insert cell
cooperator_color = in_values[2][0]
Insert cell
scores_main = {
return { R: 1, T: T, S: 0, P: 0 };
}
Insert cell
grid = {
reset;
return create_grid(initial_defectors);
}
Insert cell
function make_grid_step(grid, scores) {
var actor;
for (var i = 0; i < grid.length; ++i) {
grid[i].compute_score(grid, scores);
}
var newgrid = Array(grid);
for (var i = 0; i < grid.length; ++i) {
var new_actor = grid[i].next_generation(grid);
newgrid[i] = new_actor;
}
for (i in grid) {
grid[i] = newgrid[i];
}
}
Insert cell
grids_sample = Array([0.1, 0.2, 0.3])
Insert cell
function create_grid(fraction_of_defectors) {
return [...Array(N * M)].map((d, i) => {
var position = [Math.floor(i / N), i % M];
var rand = (Math.random() * 2) % 1;
return rand > fraction_of_defectors
? new Cooperator(position)
: new Defector(position);
});
}
Insert cell
N = 100
Insert cell
M = 100
Insert cell
grid_stats = []
Insert cell
function get_stats(matrix, stats_arr) {
stats_arr.push({
timestamp: stats_arr.length,
perc_coop:
matrix.reduce(
(acc, val) => acc + (val.type === 'cooperator' ? 1 : 0),
0
) / matrix.length
});

return stats_arr;
}
Insert cell
class Actor {
constructor(pos) {
this.x = pos[0];
this.y = pos[1];
this.color = 'grey';
this.score = 0;
}
choice() {
return '0';
}
compute_score(matrix, scores_obj) {
this.score = 0;
var nei;
for (nei of get_neighbours(this.x, this.y, matrix)) {
this.score += compute_score(this.choice(), nei.choice(), scores_obj);
}
}
next_generation(matrix, avg) {
var best_performer = get_neighbours(this.x, this.y, matrix).reduce(
(acc, val) => {
return val === undefined || val.score < acc.score ? acc : val;
},
{ score: -1 }
);

var new_born = Object.create(Object.getPrototypeOf(best_performer)); //https://www.nickang.com/2018-01-17-how-to-clone-class-instance-javascript/
new_born.color = best_performer.color;
new_born.type = best_performer.type;
new_born.x = this.x;
new_born.y = this.y;
new_born.score = 0;

return new_born;
}
}
Insert cell
function actorFactory(type, pos) {
if (type == 'Cooperator') return new Cooperator(pos);
if (type == 'Defector') return new Defector(pos);
}
Insert cell
class Cooperator extends Actor {
constructor(position) {
super(position);
this.color = cooperator_color;
this.type = 'cooperator';
}
choice() {
return 'C';
}
}
Insert cell
class Defector extends Actor {
constructor(position) {
super(position);
this.color = defector_color;
this.type = 'defector';
}
choice() {
return 'D';
}
}
Insert cell
function compute_score(a, b, scores_dict) {
if (a == 'C' && b == 'C') return scores_dict.R;
if (a == 'D' && b == 'D') return scores_dict.P;
if (a == 'D' && b == 'C') return scores_dict.T;
if (a == 'C' && b == 'D') return scores_dict.S;
console.log("wrong score");
}
Insert cell
height = 400
Insert cell
margin = ({ top: 20, right: 30, bottom: 30, left: 40 })
Insert cell
d3 = require("d3@6")
Insert cell
colorScale = d3.scaleSequential(d3.interpolateViridis).domain([0, 1])
Insert cell
function get_neighbours(x, y, matrix) {
var delta, xn, yn;
var neighbours = Array();
for (delta of [
[0, 0],
[0, 1],
[1, 0],
[-1, 1],
[-1, 0],
[-1, -1],
[0, -1],
[1, -1],
[1, 1]
]) {
xn = x + delta[0];
yn = y + delta[1];
if (xn > -1 && yn > -1 && xn < N && yn < M) {
neighbours.push(matrix[xn * M + yn]);
}
}
neighbours = shuffleArray(neighbours);
return neighbours;
}
Insert cell
// https://stackoverflow.com/a/12646864/5599687
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
var newarr = array;
return newarr
}
Insert cell
import { vl } from "@vega/vega-lite-api"
Insert cell
Inputs = require("@observablehq/inputs@0.7.21/dist/inputs.umd.min.js")
Insert cell
import { color } from "@jashkenas/inputs"
Insert cell
import { inputsGroup } from "@bumbeishvili/input-groups"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more