-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent_1.py
200 lines (152 loc) · 6.57 KB
/
Agent_1.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
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
199
200
import random
from pprint import pprint
import config
import utils
from prey import Prey
from predator import Predator
class Agent_1:
def __init__(self, prey_loc, predator_loc):
"""
Initializing the position of the Agent at locations where prey and predator are not present
Parameters:
self
prey_loc (int): Location of the prey
predator_loc (int): Location of the predator
"""
# Handling condition where prey and predator are spawned on the same location
list_to_choose_from = list(range(50))
if prey_loc == predator_loc:
list_to_choose_from.remove(prey_loc)
else:
list_to_choose_from.remove(prey_loc)
list_to_choose_from.remove(predator_loc)
self.curr_pos = random.choice(list_to_choose_from)
def move(self, arena, prey_loc, predator_loc):
"""
Moves Agent 1 according to the given priority
Parameters:
self
arena (dictionary): Adjacency list representing the graph
prey_loc (int): Location of prey
predator_loc (int): Location of Predator
"""
"""
print("Initial pos",self.curr_pos)
# Neighbours of the current node are extracted here
self.neighbours = arena[self.curr_pos].copy()
# Distances from prey and predator will be stored in the following dicts
predator_dist = {}
prey_dist = {}
# Storing the distances of the agent location to the prey and predator
path, curr_pos_prey_dist = utils.get_shortest_path(self.curr_pos, prey_loc, arena)
path, curr_pos_predator_dist = utils.get_shortest_path(self.curr_pos, predator_loc, arena)
# Find distance from all neighbours to the prey and the predator
for i in self.neighbours:
path, prey_dist[i] = utils.get_shortest_path(i, prey_loc, arena)
path, predator_dist[i] = utils.get_shortest_path(i, predator_loc, arena)
# Defining subsets of nodes
closer_to_prey = {}
not_farther_from_prey = {}
farther_from_predator = {}
not_closer_to_predator = {}
# Adding nodes to the subsets
for k in prey_dist.keys():
if prey_dist[k] < curr_pos_prey_dist:
closer_to_prey[k]=prey_dist[k]
for k in prey_dist.keys():
if prey_dist[k] == curr_pos_prey_dist:
not_farther_from_prey[k]=prey_dist[k]
for k in predator_dist.keys():
if predator_dist[k] >= curr_pos_predator_dist:
farther_from_predator[k]=predator_dist[k]
for k in predator_dist.keys():
if predator_dist[k] == curr_pos_predator_dist:
farther_from_predator[k]=predator_dist[k]
# Assigning the position accorinding to the given priorrity
if len(set(closer_to_prey).intersection(set(farther_from_predator))) != 0:
self.curr_pos=min(closer_to_prey, key=closer_to_prey.get)
#print("priority 1")
elif len(set(closer_to_prey).intersection(set(not_closer_to_predator))) !=0:
self.curr_pos=min(closer_to_prey, key=closer_to_prey.get)
#print("priority 2")
elif len(set(not_farther_from_prey).intersection(set(farther_from_predator))) !=0:
self.curr_pos=min(not_farther_from_prey, key=not_farther_from_prey.get)
#print("priority 3")
elif len(set(closer_to_prey).intersection(set(not_closer_to_predator))) !=0:
self.curr_pos=min(closer_to_prey, key=closer_to_prey.get)
#print("priority 4")
elif len(farther_from_predator) !=0:
self.curr_pos=max(farther_from_predator, key=farther_from_predator.get)
#print("priority 5")
elif len(not_closer_to_predator) != 0:
self.curr_pos=min(not_closer_to_predator, key=not_closer_to_predator.get)
#print("priority 6")
else:
pass
#print("Sitting and Praying")
#print(curr_pos_prey_dist,curr_pos_predator_dist)
print("Current pos" , self.curr_pos)
"""
pos = utils.best_node(arena, self.curr_pos, prey_loc, predator_loc)
# Handling Sitting and praying case
if pos == 999:
pass
else:
self.curr_pos = pos
def begin(arena):
"""
Creates all the maze objects and plays number of games and collects data
Parameters:
arena (dict): Arena to use
Returns:
data_row (list): Results evaluated for the agent
"""
# Initiating game variables
game_count = 0
step_count = 0
# Initiating variables for analysis
win_count = 0
loss_count = 0
forced_termination = 0
# data = []
data_row = []
# Config variable (To be transferred to a parameter file)
number_of_games = config.NUMBER_OF_GAMES
forced_termination_threshold = config.FORCED_TERMINATION_THRESHOLD
while game_count < number_of_games:
# Creating objects
prey = Prey()
predator = Predator()
agent1 = Agent_1(prey.curr_pos, predator.curr_pos)
step_count = 0
while 1:
print("In game Agent_1 at game_count: ", game_count, " step_count: ", step_count)
print(agent1.curr_pos, prey.curr_pos, predator.curr_pos)
agent1.move(arena, prey.curr_pos, predator.curr_pos)
# Checking termination states
if agent1.curr_pos == prey.curr_pos:
win_count += 1
break
elif agent1.curr_pos == predator.curr_pos:
loss_count += 1
break
prey.move(arena)
# Checking termination states
if agent1.curr_pos == prey.curr_pos:
win_count += 1
break
predator.move(agent1.curr_pos, arena)
# Checking termination states
if agent1.curr_pos == predator.curr_pos:
loss_count += 1
break
step_count += 1
# Forcing termination
if step_count >= forced_termination_threshold:
forced_termination += 1
break
game_count += 1
data_row = ["Agent_1", win_count * 100 / number_of_games, loss_count * 100 / number_of_games,
forced_termination * 100 / number_of_games, 100.0, 100.0]
# data.append(data_row)
return data_row