-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The main file is untitled0. Others are backups
- Loading branch information
0 parents
commit 7916769
Showing
9 changed files
with
1,201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import networkx as nx | ||
import operator | ||
import matplotlib.pyplot as plt | ||
import random as rnd | ||
import collections | ||
|
||
#ls = [1,2,3,4] | ||
ls = [] | ||
|
||
k=10 | ||
|
||
for i in range(1,k): | ||
ls.append(i) | ||
|
||
Gr = nx.Graph() | ||
Gr.add_nodes_from(ls) | ||
|
||
|
||
for i in range(0,int(k*2)): | ||
j = rnd.choice(ls) | ||
k = rnd.choice(ls) | ||
Gr.add_edge(j,k) | ||
|
||
#Gr.add_edge(2,3) | ||
|
||
''' | ||
G.add_edge(1,2) | ||
#G.add_edge(1,3) | ||
G.add_edge(2,3) | ||
G.add_edge(2,4) | ||
G.add_edge(2,10) | ||
G.add_edge(1,9) | ||
G.add_edge(4,8) | ||
G.add_edge(6,1) | ||
G.add_edge(6,10) | ||
G.add_edge(5,2) | ||
''' | ||
|
||
def degreeHistogram(G,clr): | ||
degree_sequence = sorted([d for n, d in G.degree()], reverse=True) | ||
degreeCount = collections.Counter(degree_sequence) | ||
deg, cnt = zip(*degreeCount.items()) | ||
plt.bar(deg, cnt, width=0.80, color=clr) | ||
plt.title("Degree Histogram") | ||
plt.ylabel("Count") | ||
plt.xlabel("Degree") | ||
plt.show() | ||
|
||
#probability of each node's degree | ||
#in resepect to WHOLE NETWORK | ||
def perMap(G): | ||
per = [] | ||
permap = {} | ||
temp = nx.degree(G) | ||
sum = 0 | ||
for i in temp: | ||
sum = sum + i[1] | ||
for i in temp: | ||
per.append(i[1]/sum) | ||
for i,j in zip(temp,per): | ||
permap[i[0]]=j | ||
return permap | ||
|
||
#ROULETTE METHOD | ||
def perMapRange(permap): | ||
#first mapping permap to [0,1] | ||
permap_mapped = {} | ||
_sum = 0.0 | ||
for keys in permap: | ||
_sum = _sum + permap[keys] | ||
for keys in permap: | ||
permap_mapped[keys] = permap[keys]/_sum | ||
#finding out ranges | ||
permaprange = {} | ||
prev = 0.0 | ||
sorted_permap = dict(sorted(permap_mapped.items(), key=operator.itemgetter(1))) | ||
for keys in sorted_permap: | ||
t = [] | ||
new = sorted_permap[keys] | ||
margin = 0.00001 | ||
a = prev | ||
b = new+prev-margin | ||
if a==0.0 and b==-margin: | ||
t.append(0.0) | ||
t.append(0.0) | ||
else: | ||
t.append(a) | ||
t.append(new+prev-margin) | ||
permaprange[keys] = tuple(t) | ||
prev = new+prev | ||
del t | ||
return permaprange | ||
|
||
def prefAttachment(G): | ||
permap = perMap(G) | ||
#print('Permap: {}'.format(permap)) | ||
permaprange = perMapRange(permap) | ||
#print('\nPermarange: {}'.format(permaprange)) | ||
G.add_node('NEW') | ||
#randInt to select a node from permarange | ||
select = rnd.randint(0,100)/100 | ||
print('\n\nselect probability: {}'.format(select)) | ||
for keys in permaprange: | ||
t = permaprange[keys] | ||
if select>=t[0] and select<=t[1]: | ||
print('Node Choosed: {}, Its degree: {}'.format(keys,G.degree(int(keys)))) | ||
#G.add_edge(rnd.randint(0,10),keys) | ||
G.add_edge('NEW',keys) | ||
|
||
for i in range(0,int(k*5)): | ||
prefAttachment(Gr) | ||
|
||
d = dict(nx.degree(Gr)) | ||
pos = nx.circular_layout(Gr) | ||
nx.draw(Gr,pos,node_size=[v*100 for v in d.values()],node_color='y', with_labels=True) | ||
plt.show() | ||
degreeHistogram(Gr,'r') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Feb 18 14:19:14 2019 | ||
@author: Om | ||
""" | ||
|
||
#Imported Packages | ||
#import numpy as np | ||
import random as rnd | ||
import matplotlib.pyplot as plt | ||
import networkx as nx | ||
|
||
###### GLOBAL VARIABLES ###### | ||
nodes = [] #nodes list | ||
D = 500 #Max size of WSN deployment area | ||
N = 250#Number of Nodes | ||
R = 50 #Radius of transmission | ||
############################## | ||
|
||
####### FUNCTIONS ######## | ||
|
||
#function to sort values in the nodes | ||
def sorti(n): | ||
return(sorted(nodes,key=lambda x:x[0])) #sorting via 'x' | ||
|
||
def distance(i,j): | ||
x = abs(i[0]-j[0]) | ||
y = abs(i[1]-j[1]) | ||
d = (x**2 + y**2)**(1/2) | ||
return d | ||
|
||
def neighbour(): | ||
nodes_nbr = {} #dict of nbrs | ||
temp_nodes = nodes.copy() | ||
for i in nodes: | ||
temp_nbr = [] | ||
for j in temp_nodes: | ||
if distance(i,j) < R and j!=i: | ||
temp_nbr.append(j) | ||
nodes_nbr[tuple(i)] = temp_nbr | ||
del temp_nbr | ||
return nodes_nbr | ||
|
||
#function to generate nodes | ||
def generateNode(): | ||
i=0 | ||
while i<N: | ||
tempNode1 = rnd.randint(0,D) | ||
tempNode2 = rnd.randint(0,D) | ||
tempNodes = [] | ||
tempNodes.append(tempNode1) | ||
tempNodes.append(tempNode2) | ||
if not nodes: | ||
nodes.append(tempNodes) | ||
i = i+1 | ||
else: | ||
if tempNodes not in nodes: | ||
nodes.append(tempNodes) | ||
i = i+1 | ||
|
||
#function to plot nodes | ||
def plotNodes(): | ||
x = [] | ||
y = [] | ||
for i in nodes: | ||
x.append(i[0]) | ||
y.append(i[1]) | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
plt.plot(x,y,'ro') | ||
for i,j in zip(x,y): | ||
ax.annotate('%s)' %j, xy=(i,j), xytext=(30,0), textcoords='offset points') | ||
ax.annotate('(%s,' %i, xy=(i,j)) | ||
plt.grid() | ||
plt.show() | ||
|
||
#function to print nodes and neighbour pair | ||
def printPairs(data): | ||
for key in data: | ||
print('{} -> {} \n'.format(key,data[tuple(key)])) | ||
|
||
#function to generate network from nodePair data | ||
def generateGraph(data): | ||
G = nx.Graph() | ||
for key in data: | ||
nbr = data[tuple(key)] | ||
for i in nbr: | ||
G.add_edge(tuple(key),tuple(i)) | ||
d = nx.degree(G) | ||
print(d) | ||
print(nx.clustering(G)) | ||
pos = nx.spring_layout(G) | ||
nx.draw(G,pos,node_size=6) | ||
plt.show() | ||
|
||
|
||
########################## | ||
|
||
#Calling function to generate nodes | ||
generateNode() | ||
|
||
#Calling function generate node & neighbour pairs | ||
nodePair = neighbour() | ||
|
||
#Calling function to print the pair | ||
#printPairs(nodePair) | ||
|
||
#Calling function to generate graph | ||
generateGraph(nodePair) | ||
|
||
#Plot the graph | ||
plotNodes() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Mon Feb 18 18:47:25 2019 | ||
@author: Om | ||
""" | ||
|
||
#Imported Packages | ||
#import numpy as np | ||
import random as rnd | ||
import matplotlib.pyplot as plt | ||
import networkx as nx | ||
import collections | ||
|
||
###### GLOBAL VARIABLES ###### | ||
nodes = [] #nodes list | ||
D = 500 #Max size of WSN deployment area | ||
N = 250#Number of Nodes | ||
R = 50 #Radius of transmission | ||
############################## | ||
|
||
####### FUNCTIONS ######## | ||
|
||
#function to sort values in the nodes | ||
def sorti(n): | ||
return(sorted(nodes,key=lambda x:x[0])) #sorting via 'x' | ||
|
||
def distance(i,j): | ||
x = abs(i[0]-j[0]) | ||
y = abs(i[1]-j[1]) | ||
d = (x**2 + y**2)**(1/2) | ||
return d | ||
|
||
def neighbour(): | ||
nodes_nbr = {} #dict of nbrs | ||
temp_nodes = nodes.copy() | ||
for i in nodes: | ||
temp_nbr = [] | ||
for j in temp_nodes: | ||
if distance(i,j) < R and j!=i: | ||
temp_nbr.append(j) | ||
nodes_nbr[tuple(i)] = temp_nbr | ||
del temp_nbr | ||
return nodes_nbr | ||
|
||
#function to generate nodes | ||
def generateNode(): | ||
i=0 | ||
while i<N: | ||
tempNode1 = rnd.randint(0,D) | ||
tempNode2 = rnd.randint(0,D) | ||
tempNodes = [] | ||
tempNodes.append(tempNode1) | ||
tempNodes.append(tempNode2) | ||
if not nodes: | ||
nodes.append(tempNodes) | ||
i = i+1 | ||
else: | ||
if tempNodes not in nodes: | ||
nodes.append(tempNodes) | ||
i = i+1 | ||
|
||
#function to plot nodes | ||
def plotNodes(): | ||
x = [] | ||
y = [] | ||
for i in nodes: | ||
x.append(i[0]) | ||
y.append(i[1]) | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
plt.plot(x,y,'ro') | ||
for i,j in zip(x,y): | ||
ax.annotate('%s)' %j, xy=(i,j), xytext=(30,0), textcoords='offset points') | ||
ax.annotate('(%s,' %i, xy=(i,j)) | ||
plt.grid() | ||
plt.show() | ||
|
||
#function to print nodes and neighbour pair | ||
def printPairs(data): | ||
for key in data: | ||
print('{} -> {} \n'.format(key,data[tuple(key)])) | ||
|
||
#function to generate network from nodePair data | ||
''' | ||
def generateGraph(data): | ||
G = nx.Graph() | ||
for key in data: | ||
nbr = data[tuple(key)] | ||
for i in nbr: | ||
G.add_edge(tuple(key),tuple(i)) | ||
d = nx.degree(G) | ||
print(d) | ||
print(nx.clustering(G)) | ||
pos = nx.spring_layout(G) | ||
nx.draw(G,pos,node_size=6) | ||
plt.show() | ||
''' | ||
|
||
def generateGraph(data): | ||
G = nx.Graph() | ||
for i in nodes: | ||
G.add_node(tuple(i),pos=i) | ||
for key in data: | ||
nbr = data[tuple(key)] | ||
for i in nbr: | ||
G.add_edge(tuple(i),tuple(key)) | ||
pos = nx.get_node_attributes(G,'pos') | ||
d = dict(nx.degree(G)) | ||
nx.draw(G,pos,node_size=[v*10 for v in d.values()],node_color='g') | ||
plt.title("WSN with edges to all neighbours: Area {} X {}".format(D,D)) | ||
plt.show() | ||
return G | ||
|
||
def degreeHistogram(G): | ||
degree_sequence = sorted([d for n, d in G.degree()], reverse=True) | ||
degreeCount = collections.Counter(degree_sequence) | ||
deg, cnt = zip(*degreeCount.items()) | ||
plt.bar(deg, cnt, width=0.80, color='b') | ||
plt.title("Degree Histogram") | ||
plt.ylabel("Count") | ||
plt.xlabel("Degree") | ||
plt.show() | ||
|
||
|
||
########################## | ||
|
||
#Calling function to generate nodes | ||
generateNode() | ||
|
||
#Calling function generate node & neighbour pairs | ||
nodePair = neighbour() | ||
|
||
#Calling function to print the pair | ||
#printPairs(nodePair) | ||
|
||
#Calling function to generate graph | ||
G = generateGraph(nodePair) | ||
|
||
#calling function to generate degree historgram | ||
degreeHistogram(G) | ||
|
||
#Plot the graph | ||
#plotNodes() |
Oops, something went wrong.