Skip to content

Instantly share code, notes, and snippets.

@xmichaelx
xmichaelx / nagel-schreckenberg.py
Created December 9, 2014 05:34
Simple python implementation of nagel-schreckenberg model for modelling traffic
from random import uniform, shuffle
import matplotlib.pyplot as plt
import numpy as np
L = 500 # number of cells in row
num_iters = 500 # number of iterations
density = 0.48 # how many positives
vmax = 2
p = 0.2
import snap
# requires python 2.7 and 64-bit machine
G1 = snap.TUNGraph.New()
with open("data/edges.csv") as f:
for line in f:
tokens = line.split()
n1, n2, weight = int(tokens[0]), int(tokens[1]), int(tokens[2])
if not G1.IsNode(n1):
G1.AddNode(n1)
@xmichaelx
xmichaelx / dla.js
Created December 6, 2014 16:22
Simple diffusion-limited aggregation simulation in JS
function createField(width, height) {
var field = new Uint8Array(width * height);
var fcenterX = Math.floor(width / 2);
var fcenterY = Math.floor(height / 2);
// center occupied
field[fcenterX + fcenterY * width] = 1;
return { field: field, width:width, height:height};
}
@xmichaelx
xmichaelx / cellular.py
Created November 26, 2014 16:01
Simple implementation of binary, 1D cellular automaton with sample 184 rule (http://en.wikipedia.org/wiki/Rule_184)
from random import uniform
import matplotlib.pyplot as plt
import numpy as np
from random import sample
n = 200 # number of cells in row
num_iters = 200 # number of iterations
density = 0.5 # how many positives
# rule 184
@xmichaelx
xmichaelx / vis.html
Created August 11, 2014 23:35
Simple network visualized using vis.js
<!DOCTYPE html>
<html>
<title>Network | Images</title>
<head>
<link href="http://visjs.org/dist/vis.css" rel="stylesheet" type="text/css" />
<script src="http://visjs.org/dist/vis.js"></script>
<script type="text/javascript">
function draw() {
var nodes = [
{id: 1, label: 'SLR1', group: 'source', value: 10},
@xmichaelx
xmichaelx / tle.js
Created August 11, 2014 14:33
Small snippet for converting strings in Two-Line Element into JSON format (with additional computed values such ass period, semi-major axis, JD and MJD).
var TLE =
(function () {
function isCorrupted(line) {
var checksum = parseInt(line[line.length-1]);
var computedChecksum = line.slice(0,line.length-1)
.replace(/[\-\-]/g,"1")
.replace(/[a-zA-Z\.+\s]/g,"")
.split("")
.map(function(digit) { return parseInt(digit) })
.reduce(function(prev, current) {return prev + current;}, 0) % 10;