Published
Edited
Nov 4, 2019
3 stars
Insert cell
Insert cell
draw = {
let agents = initAgents();
let scores = initScores();

function update() {

scores = gBuildScores(agents, payoffMatrix, neighborList, slotCount);
agents = gPickWinners(agents, scores, neighborList, slotCount);
gShow(agents, colorList, stateCount);
return gShow.getCanvas();
}

while (true) {
yield Promises.delay(2, update());
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
payoffMatrix = {
let payoffs = new Array(stateCount).fill(0).map(() => new Array(stateCount).fill(0));
for (let i = 0; i < stateCount; i++) {
for (let j = 0; j < stateCount; j++) {
let p = j - i;
if (p > halfStates) {
p -= stateCount;
} else if (p < -halfStates) {
p+= stateCount;
}
payoffs[i][j] = p;
}
}
return payoffs;
}
Insert cell
Insert cell
Insert cell
colorList = {
let colors = [];
for (let i = 0; i < stateCount; i++) {
let cVal = (i * 6) / stateCount;
let below = Math.floor(cVal);
let above = (below + 1) % 6;
let loFrac = cVal - below;
let hiFrac = 1 - loFrac;

let color = [
(rgbHex[below][0] * hiFrac) + (rgbHex[above][0] * loFrac),
(rgbHex[below][1] * hiFrac) + (rgbHex[above][1] * loFrac),
(rgbHex[below][2] * hiFrac) + (rgbHex[above][2] * loFrac),
];
colors.push(color);
}
return colors;
}
Insert cell
Insert cell
neighborList = {
let nList = [];
for (let i = -radius; i <= radius; i++) {
for (let j = -radius; j <= radius; j++) {
let dist = Math.sqrt(Math.pow(Math.abs(i),2) + Math.pow(Math.abs(j),2));
if (!(i == 0 && j == 0) && dist < radius+1) {
let coord = [i+(Math.random()*0.001),j+(Math.random()*0.001)];
nList.push(coord);
}
}
}
nList.sort((a,b)=>(Math.atan2(a[1],a[0]) - Math.atan2(b[1],b[0])));
let nList2 = nList.map((a)=>([Math.floor(a[0]),Math.floor(a[1])]));
return nList2;
}
Insert cell
slotCount = neighborList.length;
Insert cell
Insert cell
initAgents = function(){
let agents = new Array(640).fill(0).map(() => new Array(640).fill(0));
for (let i = 0; i < 640; i++) {
for (let j = 0; j < 640; j++) {
agents[i][j] = Math.floor(Math.random() * stateCount);
}
}
return agents;
}
Insert cell
initScores = function(){
let scores = new Array(640).fill(0).map(() => new Array(640).fill(0));
return scores;
}
Insert cell
gBuildScores = gpu.createKernel(function(agents, payoffs, nSlots, slotCount){
var currentState = agents[this.thread.y][this.thread.x];
var score = 0;
for (let i = 0; i < slotCount; i++) {
var p = (this.thread.y + nSlots[i][1] + 640) % 640;
var q = (this.thread.x + nSlots[i][0] + 640) % 640;
let otherState = agents[p][q];
score += payoffs[currentState][otherState];
}
return score;
}).setOutput([640, 640]);
Insert cell
gPickWinners = gpu.createKernel(function(agents, scores, nSlots, slotCount){
var maxScore = 0;
var topMove = agents[this.thread.y][this.thread.x];
let startIndex = Math.floor(Math.random() * slotCount);
for (let i = 0; i < slotCount; i++) {
let index = (startIndex + i) % slotCount;
var p = (this.thread.y + nSlots[index][1] + 640) % 640;
var q = (this.thread.x + nSlots[index][0] + 640) % 640;
if (scores[p][q] > maxScore) {
topMove = agents[p][q];
maxScore = scores[p][q];
}
}
return topMove;
}).setOutput([640, 640]);
Insert cell
gShow = gpu.createKernel(function(agents, colorList, stateCount){
let state = agents[this.thread.y][agents.thread.x];
this.color(
colorList[state][0],
colorList[state][1],
colorList[state][2],
1
);
}).setOutput([640, 640]).setGraphical(true);
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more