-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethicalAgent.py
141 lines (124 loc) · 4.94 KB
/
ethicalAgent.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from abc import abstractclassmethod
import numpy as np
class ethicalAgent(object):
def __init__(self,type,attrs={}):
'''
default values
msgUtility = 10
msgCost = 0.1*msgUtility
burnoutDrop = 5
burnoutThreshold = 10*msgCost
'''
if('msgUtility' in attrs):
self.msgUtility = attrs['msgUtility']
else:
self.msgUtility = 10
if('costFactor' in attrs):
self.msgCost = attrs['costFactor']*self.msgUtility
else:
self.msgCost = 0.1*self.msgUtility
if('burnoutDrop' in attrs):
self.burnoutDrop = attrs['burnoutDrop']
else:
self.burnoutDrop = 5
if('burnoutThreshold' in attrs):
self.burnoutThreshold = attrs['burnoutThreshold']
else:
self.burnoutThreshold = 10*self.msgCost
self.type = type
self.isBurntout = False
self.burnoutState = 0
self.burnoutCount = 0
# self.nodeCost = {}
# self.nodeUtility = {}
self.msgSentSource = {}
self.msgSentInter = {}
self.msgRecvFrom = {}
self.msgForwardedBy = {}
self.msgForwardedOf = {}
def initNeig(self, neigs):
for nei in neigs:
self.msgSentSource[nei] = 0
self.msgSentInter[nei] = 0
self.msgRecvFrom[nei] = 0
self.msgForwardedBy[nei] = 0
self.msgForwardedOf[nei] = 0
self.isBurntout = False
self.burnoutState= 0
self.burnoutCount = 0
def sendMessage(self, inter):
# self.nodeCost[inter] += self.msgCost
self.msgSentSource[inter] += 1
pass
def recvMessage(self, source):
self.msgRecvFrom[source] += 1
# print("recv ",self.msgRecvFrom[source])
def getType(self):
return self.type
def getNodeCostInter(self):
totalMessagesSent = np.sum(list(self.msgSentInter.values()))
return totalMessagesSent*self.msgCost
def getNodeCost(self):
totalMessagesSent = np.sum(list(self.msgSentSource.values())) + np.sum(list(self.msgSentInter.values()))
return totalMessagesSent*self.msgCost
def getNodeUtility(self):
totalMessagesForwarded = np.sum(list(self.msgForwardedBy.values()))
totalMessagesDropped = np.sum(list(self.msgSentSource.values()))-np.sum(list(self.msgForwardedBy.values()))
return (totalMessagesForwarded-totalMessagesDropped)*self.msgUtility
def getBurnouts(self):
return self.burnoutCount
def getDropCount(self):
# totalMessagesDropped = np.sum(list(self.msgSentSource.values()))-np.sum(list(self.msgForwardedBy.values()))
totalMessagesDropped = np.sum(list(self.msgRecvFrom.values()))-np.sum(list(self.msgSentInter.values()))
a = np.sum(list(self.msgRecvFrom.values()))
b = np.sum(list(self.msgSentInter.values()))
# if b > a:
# print("here",b,a)
return totalMessagesDropped
def getForwardCount(self):
# totalMessagesForwarded = np.sum(list(self.msgForwardedBy.values()))
totalMessagesForwarded = np.sum(list(self.msgSentInter.values()))
return totalMessagesForwarded
def getProperty(self, prop):
if(prop == 'cost'):
return self.getNodeCost()
elif(prop == 'utility'):
return self.getNodeUtility()
elif(prop == 'burnout'):
return self.getBurnouts()
elif(prop == 'drops'):
return self.getDropCount()
elif(prop == 'forwards'):
return self.getForwardCount()
elif(prop == 'responsibility_score'):
# forwarded = np.sum(list(self.msgForwardedOf.values()))
# dropped = np.sum(list(self.msgRecvFrom.values())) - np.sum(list(self.msgForwardedOf.values()))
# print("fwd ", self.getForwardCount(), "drop ", self.getDropCount())
if self.getForwardCount() == 0 and self.getDropCount() == 0:
return 0
return (self.getForwardCount()-self.getDropCount())/(self.getForwardCount()+self.getDropCount())
return "NA"
def burnoutUpdate(self):
if(self.burnoutDrop != 0):
if(self.isBurntout):
self.burnoutState += 1
self.burnoutState %= self.burnoutDrop
if(self.burnoutState == 0):
self.isBurntout = False
else:
if(self.getNodeCost() >= (1+self.burnoutCount)*self.burnoutThreshold):
self.isBurntout = True
self.burnoutCount += 1
self.burnoutState = 1
return self.isBurntout
def sendOutcome(self, inter, sent):
if sent:
self.msgForwardedBy[inter] += 1
def epochUpdate(self):
return
@abstractclassmethod
def forwardMessage(self, source, dest):
pass
@abstractclassmethod
def isStable(self, previousState):
pass