-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
199 lines (161 loc) · 5 KB
/
Agent.py
File metadata and controls
199 lines (161 loc) · 5 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import numpy as np; np.random.seed(0)
VerboseFlag = False
class Agent:
def __init__(self):
ActionNames, ActionMoves = DefineActions()
self.ActionNames = ActionNames
self.Actions = ActionMoves
self.SelectedAction = (0, 0)
self.SelectedActionName = None
self.ActionPolicy = None
self.SensorStates = None
def UpdateActionPolicy(self, NewPolicy):
if len(NewPolicy) == len(self.Actions):
self.ActionPolicy = NewPolicy
else:
print('Policy Not Updated ! Wrong Dimensions. \n'
' Policy must have the same shape as Actions')
def ActionSelection(self, ActionID=-1):
if ActionID >= 0 and ActionID < len(self.Actions): # Select the Given Action
SelectedActionID = ActionID
else: # Select Random Action (according to Policy)
SelectedActionID = np.random.choice(np.arange(len(self.Actions)), p=self.ActionPolicy)
if VerboseFlag: print('ActionID,SelectedActionID', ActionID, SelectedActionID)
self.SelectedAction = self.Actions[SelectedActionID]
self.SelectedActionName = self.ActionNames[SelectedActionID]
return SelectedActionID
# -----------------------------------------------------#
def DefineActions():
# Defining Actions - for all agents
# Each agent would be having actions which are a subset of all the actions
# ---------------------------------------------------------------
# Defining Individual Actions as Dictionaries
# ... so that the names and moves are easily verifiable
Action0 = {
'Name': 'Stay',
'Move': [(0, 0)]
}
Action1 = {
'Name': 'Up1',
'Move': [(-1, 0)]
}
Action2 = {
'Name': 'Down1',
'Move': [(1, 0)]
}
Action3 = {
'Name': 'Left1',
'Move': [(0, -1)]
}
Action4 = {
'Name': 'Right1',
'Move': [(0, 1)]
}
# ---------------------------#
Action5 = {
'Name': 'Up2',
'Move': [(-1, 0), (-1, 0)]
}
Action6 = {
'Name': 'Down2',
'Move': [(1, 0), (1, 0)]
}
Action7 = {
'Name': 'Left2',
'Move': [(0, -1), (0, -1)]
}
Action8 = {
'Name': 'Right2',
'Move': [(0, 1), (0, 1)]
}
# ---------------------------#
Action9 = {
'Name': 'Up3',
'Move': [(-1, 0), (-1, 0), (-1, 0)]
}
Action10 = {
'Name': 'Down3',
'Move': [(1, 0), (1, 0), (1, 0)]
}
Action11 = {
'Name': 'Left3',
'Move': [(0, -1), (0, -1), (0, -1)]
}
Action12 = {
'Name': 'Right3',
'Move': [(0, 1), (0, 1), (0, 1)]
}
# ---------------------------#
Action13 = {
'Name': 'Up4',
'Move': [(-1, 0), (-1, 0), (-1, 0), (-1, 0)]
}
Action14 = {
'Name': 'Down4',
'Move': [(1, 0), (1, 0), (1, 0), (1, 0)]
}
Action15 = {
'Name': 'Left4',
'Move': [(0, -1), (0, -1), (0, -1), (0, -1)]
}
Action16 = {
'Name': 'Right4',
'Move': [(0, 1), (0, 1), (0, 1), (0, 1)]
}
# ----------------------------------------------------------------
# ----------------------------------------------------------------
# Collecting all the actions into a dictionary of Actions
# Actions = {
# 0 : Action0,
# 1 : Action1,
# 2 : Action2,
# 3 : Action3,
# 4 : Action4
# }
Actions = {
0: Action0,
1: Action1,
2: Action2,
3: Action3,
4: Action4,
5: Action5,
6: Action6,
7: Action7,
8: Action8,
9: Action9,
10: Action10,
11: Action11,
12: Action12,
13: Action13,
14: Action14,
15: Action15,
16: Action16
}
# Separating out the Action Names and Moves
ActionNames = []
ActionMoves = []
for _, Action in Actions.items():
# print('Name :', Action['Name'], ', Move :', Action['Move'])
ActionNames.append(Action['Name'])
ActionMoves.append(Action['Move'])
# print('ActionNames : ', ActionNames)
# print('ActionMoves : ', ActionMoves)
# for Move in ActionMoves:
# for step in np.arange(2):
# if step < len(Move):
# print('Step', step, ':', Move, Move[step])
return ActionNames, ActionMoves
def GeneratePolicy(StepWeights = None, DirectionWeights=None):
if StepWeights is None:
StepWeights = [5, 4, 3, 2, 1]
if DirectionWeights is None:
DirectionWeights = [1, 1, 1, 1]
p = [StepWeights[0]*4] # Only one action has 0 steps - 'Stay'
# The factor of four is because other actions have 4 directions
for sw in StepWeights[1:]:
# Multiply stepweight with directionweight
p = p + [sw * x for x in DirectionWeights]
p = np.array(p) # Converting to numpy array
p = p / p.sum() # Normalising so that sum of probabilities is 1
if VerboseFlag: print('P : ', p, 'Sum: ', p.sum())
return p