forked from malizad/Spatial_Networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpRd.py
67 lines (54 loc) · 1.56 KB
/
SpRd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding: utf-8
# In[1]:
import matplotlib
import pylab as pl
import random as rd
import numpy as np
import networkx as nx
from scipy import stats
import scipy as sp
# In[2]:
def init(n,density):
global agents, pop
width = int(round((n/density)**0.5))
height = int(round((n/density)**0.5))
config = np.zeros([height, width])
pop = 0 #actual population size
for x in range(height):
for y in range(width):
if rd.random() < density:
config[x,y] = 1
pop += 1
# Populating the agents matrix with a random sequence of agents
seq = [x for x in range(pop)]
rd.shuffle(seq)
agents = np.zeros([height, width])
for r in range(height):
for t in range(width):
if agents[r,t] == 0:
agents[r,t] = -1
z = 0
for i in range(height):
for j in range(width):
if config[i,j] == 1:
agents[i,j]=seq[z]
z += 1
# In[3]:
def Pr_Con(a,c,node1, node2):
coord1 = np.where(agents == node1)
coord2 = np.where(agents == node2)
dist = sqrt (int(coord1[0] - coord2[0])**2 + (int(coord1[1] - coord2[1])**2))
pr = c * (dist**(-a))
return pr
# In[4]:
def SpRd(c,a):
network = nx.Graph()
for node in range(1,pop):
network.add_node(node)
for node1 in range(1,pop):
for node2 in range(node1 + 1,pop):
chance = rd.random()
Pr_Connection = Pr_Con(a,c,node1,node2)
if chance < Pr_Connection:
network.add_edge(node1,node2)
return network