Repository navigation
Expand file tree
/
Copy pathnetworkFunctions.py
More file actions
73 lines (68 loc) · 2.24 KB
/
Copy pathnetworkFunctions.py
File metadata and controls
73 lines (68 loc) · 2.24 KB
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
68
69
70
71
72
73
import random
import networkx as nx
from deonticAgent import deonticAgent
from virtuousAgent import virtuousAgent
from utilitarianAgent import utilitarianAgent
from transcendenceAgent import transcendenceAgent
#graph initialization
def initGraph(G, numNodes, common_attrs, type='Virtue'):
nodeAttr = {}
for i in range(numNodes):
temp = {}
type = type.split(" ")[0]
if(type=='Utilitarian'):
temp['agent'] = utilitarianAgent(common_attrs)
elif(type=='Virtue'):
temp['agent'] = virtuousAgent(common_attrs)
elif(type=='Deontology'):
temp['agent'] = deonticAgent(common_attrs)
elif(type=='Transcendence'):
temp['agent'] = transcendenceAgent(common_attrs)
nodeAttr[i] = temp
nx.set_node_attributes(G, nodeAttr)
for n in G.nodes():
neigList = list(G.neighbors(n))
G.nodes[n]['agent'].initNeig(neigList)
# def createMsgs(numMsg, numNodes, G, seed=32):
# msgList = []
# n = numNodes-1
# random.seed(seed)
# while(len(msgList) < numMsg):
# inter = random.randint(0, n)
# neigList = list(G.neighbors(inter))
# if len(neigList) <= 1:
# continue
# source = random.choice(neigList)
# neigList.remove(source)
# dest = random.choice(neigList)
# msg = [source, inter, dest]
# msgList.append(msg)
# return msgList
def transmitMsgs(msgList, G):
nf, nd = (0,0)
for n in G.nodes():
neigList = list(G.neighbors(n))
agent = G.nodes[n]['agent']
agent.initNeig(neigList)
for m in msgList:
# print(m)
s, i, d = m
source = G.nodes[s]['agent']
inter = G.nodes[i]['agent']
dest = G.nodes[d]['agent']
source.sendMessage(i)
inter_burntout = inter.burnoutUpdate()
if(not inter_burntout):
sent = inter.forwardMessage(s,d)
else:
sent = False
source.sendOutcome(i,sent)
# print(getNetworkProp(G, 'forwardProb'))
if sent:
nf += 1
else:
nd += 1
return nf, nd
def getNetworkProp(G, prop):
agents = [G.nodes[i]['agent'] for i in G.nodes()]
return [agent.getProperty(prop) for agent in agents]