-
Notifications
You must be signed in to change notification settings - Fork 30
/
vaccum-clearner-agent.py
198 lines (160 loc) · 5.38 KB
/
vaccum-clearner-agent.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
import random, math
class ContinuumWorld:
def __init__(self, grid_size=[2, 2], dirt=[[1, 1], [1, 1]]):
self.grid_size = grid_size
self.dirt = dirt
def print_dirt(self, agent, position):
# print(agent.position)
part = self.dirt[:agent.position[0]]
print()
for row in part:
print(*row, sep=", ")
current = self.dirt[agent.position[0]]
print(*current[:agent.position[1]], sep=", ", end=" ")
print("["+str(self.dirt[agent.position[0]][agent.position[1]])
+ "]", end=" ")
print(*current[agent.position[1]+1:], sep=", ")
part = self.dirt[agent.position[0]+1:]
for row in part:
print(*row, sep=", ")
print()
def noDirt(self):
for i in range(self.grid_size[0]):
for j in range(self.grid_size[1]):
if(self.dirt[i][j]==1):return False
return True
def fillDirt(self):
for i in range(self.grid_size[0]):
for j in range(self.grid_size[1]):
self.dirt[i][j]=1
print(self.dirt)
class Agent:
def __init__(self, position=[0, 0], max_moves=30, grid_size=0, dirt=0):
self.score = 0
self.m=0
self.position = position
self.max_moves = max_moves
self.world = ContinuumWorld(grid_size, dirt)
self.visited = set()
self.visited.add((self.position[0], self.position[1]))
def crosses_boundary(self, step):
# error = "Cannot go beyond! Change direction!\n"
if step == 'R':
if self.position[1]+1 > self.world.grid_size[1]-1:
# print(error)
return True
if step == 'L':
if self.position[1]-1 < 0:
# print(error)
return True
if step == 'U':
if self.position[0]-1 < 0:
# print(error)
return True
if step == 'D':
if self.position[0]+1 > self.world.grid_size[0]-1:
# print(error)
return True
return False
def perform_action(self, action):
self.m=self.m+1
if action == 'R':
self.position[1] += 1
if action == 'L':
self.position[1] -= 1
if action == 'U':
self.position[0] -= 1
if action == 'D':
self.position[0] += 1
if action == 'S':
self.world.dirt[self.position[0]][self.position[1]] = 0
self.score = self.m
def print_status(self, step, counter=0):
print(step, self.score)
self.world.print_dirt(self, self.position)
class SimpleReflexAgent(Agent):
def clean_grid(self):
cnt=1
while True:
i=1
while (self.world.noDirt()==False):
if self.world.dirt[self.position[0]][self.position[1]] > 0:
step = 'S'
else:
while(True):
step = random.choice(['R', 'L', 'U', 'D'])
if self.crosses_boundary(step):
continue
else:
break
self.perform_action(step)
# print("Step number:", i)
self.print_status(step, i)
i+=1
inp = input("Do you want to continue again ? y/n: ")
if(inp=='y' or inp=='Y'):
cnt+=1
self.world.fillDirt()
initial2 = []
x2=0
y2=0
while(True):
posStr2 = input("Enter initial position: ")
x2 = int(posStr2[0])
y2 = int(posStr2[-1])
initial2.append(x2-1)
initial2.append(y2-1)
self.position = initial2
if(x2<=self.world.grid_size[0] and x2>=1 and y2<=self.world.grid_size[1] and y2>=1):
break
else:
print("\nEnter proper row and column\n")
initial2=[]
continue
else: break
print(f"Average performance of Simplex Reflex Aagent is : {math.ceil(self.score/cnt)} moves")
# Driver Function
def main():
g = []
g_s = input("Enter grid size: ")
r = int(g_s[0])
c = int(g_s[-1])
g.append(r)
g.append(c)
print(g)
dirt = []
print("\nEnter grid row wise:\n")
for i in range(r):
line = []
l = []
line=(input().split())
for j in range(len(line)):
l.append(int(line[j]))
dirt.append(l)
print("\nGrid is: ")
for i in range(r):
for j in range(c):
print(dirt[i][j]," ",end="")
print()
print("\n")
initial = []
x=0
y=0
while(True):
posStr = input("Enter initial position: ")
x = int(posStr[0])
y = int(posStr[-1])
initial.append(x-1)
initial.append(y-1)
print(initial)
if(x<=r and x>=1 and y<=c and y>=1):
break
else:
print("\nEnter proper row and column\n")
initial = []
moves = 1
reflex_agent = SimpleReflexAgent(initial, moves, g, dirt)
print("**Simple Reflex Agent**\n")
reflex_agent.clean_grid()
if __name__ == "__main__":
main()