Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2010 01:10
What would you like to do?
""" Generate graphs from the spatial preferential attachment model,
and animate them"""
from pymc import runiform, rnormal
from networkx import DiGraph, draw
from pylab import clf, axis, savefig
from numpy import maximum, minimum
def perturb(x, e):
return maximum(0., minimum(rnormal(x, e**-2), 1.))
def distance(x, y):
return max(abs(x-y))
def make_graph(n, d, p, A1, A2, A3, position=None, affinity=None):
""" Create a directed graph according to the SPA model, using the
randomness provided in position and affinity, or generating it if
if position == None:
position = runiform(0, 1, size=(n, d))
if affinity == None:
affinity = runiform(0, 1, size=(n,n))
G = DiGraph()
for t in range(1, n):
for s in range(t):
if distance(position[s], position[t]) \
<= (A1 * G.in_degree(s) + A2) / (t + A3):
if affinity[t, s] <= p:
G.add_edge(t, s)
return G
class SPA:
def __init__(self, n=100, d=2, p=.3, A=[.1,5.,.1], delta=.01):
# set dimensions
self.n = n
self.d = d
# set model parameters
self.p = p
self.A1, self.A2, self.A3 = A
# set time stuff
self.t = 0 = delta
# initialize parameters
self.position = runiform(0, 1, size=(self.n, self.d))
self.affinity = runiform(0, 1, size=(self.n, self.n))
def update(self):
self.position = perturb(self.position,
self.affinity = perturb(self.affinity,
self.G = make_graph(self.n, self.d, self.p,
self.A1, self.A2, self.A3,
draw(self.G, pos=self.position, alpha=.8)
savefig('spa_%03d.png' % self.t)
self.t += 1
if __name__ == '__main__':
s = SPA()
for i in range(900):
import subprocess'mencoder mf://*.png -mf w=800:h=600 -ovc x264 -of avi -o spa.avi',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment